Waiting for PostgreSQL 10 – psql: Add \gx command

On 7th of March, Stephen Frost committed patch:

psql: Add \gx command
 
It can often be useful to use expanded mode output (\x) for just a
single query.  Introduce a \gx which acts exactly like \g except that it
will force expanded output mode for that one \gx call.  This is simpler
than having to use \x as a toggle and also means that the user doesn't
have to worry about the current state of the expanded variable, or
resetting it later, to ensure a given query is always returned in
expanded mode.
 
Primairly Christoph's patch, though I did tweak the documentation and help
text a bit, and re-indented the tab completion section.
 
Author: Christoph Berg
Reviewed By: Daniel Verite
Discussion: <a href="https://postgr.es/m/20170127132737.6skslelaf4txs6iw%40msg.credativ.de">https://www.postgresql.org/message-id/20170127132737.6skslelaf4txs6iw%40msg.credativ.de

Description seems to be pretty simple. But just in case, simple example.

As you know you end query in psql by adding semicolon at the end, like:

$ SELECT * FROM pg_database LIMIT 2;
 datname  | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace | datacl 
----------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+------------+---------------+--------
 postgres |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12297 |          544 |          1 |          1663 | 
 depesz   |  16386 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12297 |          544 |          1 |          1663 | 
(2 ROWS)

You could have also used \g, like here:

$ SELECT * FROM pg_database LIMIT 2\g
 datname  | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace | datacl 
----------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+------------+---------------+--------
 postgres |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12297 |          544 |          1 |          1663 | 
 depesz   |  16386 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12297 |          544 |          1 |          1663 | 
(2 ROWS)

This was mostly helpful in cases where you wanted to output single query results into file:

$ SELECT * FROM pg_database LIMIT 2 \g /tmp/zzz
$ \! cat /tmp/zzz
 datname  | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace | datacl 
----------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+------------+---------------+--------
 postgres |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12297 |          544 |          1 |          1663 | 
 depesz   |  16386 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12297 |          544 |          1 |          1663 | 
(2 ROWS)

or to pipe:

$ SELECT * FROM pg_database LIMIT 2 \g |nl
     1	 datname  | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace | datacl 
     2	----------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+------------+---------------+--------
     3	 postgres |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12297 |          544 |          1 |          1663 | 
     4	 depesz   |  16386 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12297 |          544 |          1 |          1663 | 
     5	(2 ROWS)

Now, you can also, just for this query, use expanded mode, which previously you had to enable and then disable manually by calling \x. Now you can simply:

$ SELECT * FROM pg_database LIMIT 2 \gx
-[ RECORD 1 ]-+------------
datname       | postgres
datdba        | 10
encoding      | 6
datcollate    | en_US.UTF-8
datctype      | en_US.UTF-8
datistemplate | f
datallowconn  | t
datconnlimit  | -1
datlastsysoid | 12297
datfrozenxid  | 544
datminmxid    | 1
dattablespace | 1663
datacl        | 
-[ RECORD 2 ]-+------------
datname       | depesz
datdba        | 16386
encoding      | 6
datcollate    | en_US.UTF-8
datctype      | en_US.UTF-8
datistemplate | f
datallowconn  | t
datconnlimit  | -1
datlastsysoid | 12297
datfrozenxid  | 544
datminmxid    | 1
dattablespace | 1663
datacl        |

It's not huge change, but it's definitely helpful. Thanks guys.