Re: proposal \gcsv

Daniel Verite <daniel@manitou-mail.org>

From: "Daniel Verite" <daniel@manitou-mail.org>
To: "Alvaro Herrera" <alvherre@2ndquadrant.com>
Cc: "Pavel Stehule" <pavel.stehule@gmail.com>,"Tom Lane" <tgl@sss.pgh.pa.us>,"Vik Fearing" <vik@postgresfriends.org>,"Erik Rijkers" <er@xs4all.nl>,"PostgreSQL Hackers" <pgsql-hackers@postgresql.org>
Date: 2020-04-02T09:23:49Z
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 "	".
START OF OUTPUT
?column?	?column?
1	2


Best regards,
-- 
Daniel Vérité
PostgreSQL-powered mailer: http://www.manitou-mail.org
Twitter: @DanielVerite



Commits

  1. Allow psql's \g and \gx commands to transiently change \pset options.