Re: proposal \gcsv

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "Alvaro Herrera" <alvherre(at)2ndquadrant(dot)com>
Cc: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>,"Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>,"Vik Fearing" <vik(at)postgresfriends(dot)org>,"Erik Rijkers" <er(at)xs4all(dot)nl>,"PostgreSQL Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: proposal \gcsv
Date: 2020-04-02 09:23:49
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alvaro Herrera wrote:

> Can we fix that by adding some syntax to allow command aliases?
> So you could add to your .psqlrc something like
> \alias \gcsv \pset push all \; \cbuf; \; \pset pop
> where the \cbuf is a hypothetical "function" that expands to the current
> query buffer. This needs some refining I guess, but it'd allow you to
> create your own shortcuts for the most common features you want without
> excessive typing effort.

Since variables can contain metacommands, they can be abused
as macros. For instance I think a declaration like this would work:

\set gcsv '\\pset push all \\pset format csv \\g \\pset pop'

or with another pset with embedded single quotes:

\set gcsv '\\pset push all \\pset format csv \\pset csv_fieldsep ''\\t'' \\g
\\pset pop'

This kind of usage is not mentioned explicitly in the doc, so it might be
hard to discover, but without the push/pop feature that doesn't exist,
we can already do that:

test=> \set gcsv '\\pset format csv \\pset csv_fieldsep ''\\t'' \\g'

test=> select 1,2 :gcsv | (echo "START OF OUTPUT"; cat)
Output format is csv.
Field separator for CSV is " ".
?column? ?column?
1 2

Best regards,
Daniel Vérité
PostgreSQL-powered mailer:
Twitter: @DanielVerite

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexey Bashtanov 2020-04-02 09:35:03 Re: control max length of parameter values logged
Previous Message John Naylor 2020-04-02 09:22:31 Re: truncating timestamps on arbitrary intervals