Waiting for PostgreSQL 13 – Allow invisible PROMPT2 in psql.

On 19th of November 2019, Thomas Munro committed patch:

Allow invisible PROMPT2 in psql. 
 
Keep track of the visible width of PROMPT1, and provide %w as a way
for PROMPT2 to generate the same number of spaces.
 
Author: Thomas Munro, with ideas from others
 
Discussion: https://postgr.es/m/CA%2BhUKG%2BzGd7RigjWbxwhzGW59gUpf76ydQECeGdEdodH6nd__A%40mail.gmail.com

So, I guess you know that psql has prompting variables:

  • PROMPT1 – used when psql is waiting for you to type in command
  • PROMPT2 – used when you entered already part of the command, but then you pressed enter without ; at the end, so psql expects you to continue with the query
  • PROMPT3 – used when you're in process of COPY FROM STDIN

For example, with default psql:

  1. depesz=# SELECT 1,
  2. depesz-# 'a',
  3. depesz-# 'b';
  4.  ?COLUMN? | ?COLUMN? | ?COLUMN? 
  5. ----------+----------+----------
  6.         1 | a        | b
  7. (1 ROW)

Please note that prompts in lines 2 and 3 are different from line 1 (depesz= vs. depesz).

This difference can be a bit too minimal to notice properly.

You can always, for example:

  1. depesz=# \SET PROMPT2 '>> '
  2. depesz=# SELECT 1,
  3. >> 'a',
  4. >> 'b';
  5.  ?COLUMN? | ?COLUMN? | ?COLUMN? 
  6. ----------+----------+----------
  7.         1 | a        | b
  8. (1 ROW)

But the width of prompt in subsequent lines is different making reading of the query harder.

Now, with this new change, we can:

  1. depesz=# \SET PROMPT2 '%w'
  2. depesz=# SELECT 1,
  3.          'a',
  4.          'b';
  5.  ?COLUMN? | ?COLUMN? | ?COLUMN? 
  6. ----------+----------+----------
  7.         1 | a        | b
  8. (1 ROW)

Which is great because not only are subsequent query lines easier to read, the whole thing is now copy-paste-able without any edition.

Great stuff, thanks to all involved.

4 thoughts on “Waiting for PostgreSQL 13 – Allow invisible PROMPT2 in psql.”

  1. Thanks for this cool article.

    Can you show the values of PROMPT1, PROMPT2 & PROMPT3 used, please?

    Do you need to provide a “static” number of space to get the `PROMPT3` configuration OK?

  2. @Thomas B:

    My PROMPT1/2/3 in these examples are basic, just like in “psql -X”. So you can start psql, and then do \set – and you’ll see them all.

    Not sure what you mean by PROIMPT3 configuration OK. I generally never modify PROMPT3, i just keep it as default ‘>> ‘

  3. this should incorperate the current length of the prompt2, since i use %R quite a lot, which tells me if there are for example open parenticies.

    this is my prompt setup:

    🐘 15:53:47 » @:5432/ # select (
    🐘 1 ( » true, false )
    🐘 2 – » ;

    and the existing 8 chars on PROMPT2 should be incorporated to get the offset for %w

Comments are closed.