Waiting for 8.4 – psql vs. tabs + wrapped output

Today I will describe 2 new patches which modify psql:

First patch was written and committed by Bruce Momjian.

Commit message describes the thing pretty simply:

Have psql output tab as the proper number of spaces, rather than \x09.

What it means? In 8.3 and earlier when Your string contained tab characters it was printed as “\x09", thus rendering output pretty nasty:

# SELECT 'a     b';
 ?COLUMN?
----------
 a\x09b
(1 ROW)

What's more, it also damaged functions which were created with tab-based indentation (arguably bad idea anyway):

# \df+ getgid
List of functions
-[ RECORD 1 ]-------+----------------------------------------------------------------------------------------------------------------------------------------------
Schema              | public
Name                | getgid
Result data type    | text
Argument data types | in_username text, in_domain text
Owner               | mails
Language            | plpgsql
Source code         |
                    : DECLARE
                    : \x09reply TEXT;
                    : BEGIN
                    : \x09SELECT d.gid INTO reply FROM accounts a join domains d on a.domain_id = d.id WHERE a.username = in_username AND d.fullname = in_domain;
                    : \x09RETURN reply;
                    : END;
                    :
Description         |

This is even easy to understand – only 1 level of indentation, but just imagine (or check in Your database) how it looks for functions that have many levels of indentation!

After compiling PostgreSQL with this new patch, situation looks differently:

# SELECT 'a     b';
 ?COLUMN?
-----------
 a       b
(1 ROW)

And how about \df+ ?

# \df+ getgid
List OF functions
-[ RECORD 1 ]-------+---------------------------------------------------------------------------------------------------------------------------------------------------
Schema              | public
Name                | getgid
RESULT DATA TYPE    | text
Argument DATA types | in_username text, in_domain text
Volatility          | volatile
Owner               | mails
LANGUAGE            | plpgsql
SOURCE code         |
                    : DECLARE
                    :         reply TEXT;
                    : BEGIN
                    :         SELECT d.gid INTO reply FROM accounts a JOIN domains d ON a.domain_id = d.id WHERE a.username = in_username AND d.fullname = in_domain;
                    :         RETURN reply;
                    : END;
                    :
Description         |

Sweet, much more readable.

Now for the second patch.

It was written by Bryce Nesbitt, and committted also by Bruce Momjian.

Commit message was:

Add psql '\pset format wrapped' mode to wrap output to screen width, or
file/pipe output too if \pset columns' is set.
Bryce Nesbitt

What it does can be best shown by example.

Unfortunately I can't find a good way to show it with text-only example, so instead please check this screenshot (click for larger version):

wrapper-screenshot

As You can see it makes the output more readable. On the other hand – if You use psql a lot, then I strongly suggest to check this post for ultimate solution for long-lines in psql.