May 9th, 2008 by depesz | Tags: , , , , | No comments »
Did it help? If yes - maybe you can help me? Donate BTC to 19zPa5diT2LZqGtTi8f8bfApLn8rw9zBHx

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.

Leave a comment