psql: make %P prompt option consistent when not connected

Started by Chao Li3 months ago2 messageshackers
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi Hackers,

While reviewing and testing patch [1]/messages/by-id/016f6738-f9a9-4e98-bb5a-e1e4b9591d46@uni-muenster.de, I noticed that psql’s %P prompt option does not check pset.db, which makes it inconsistent with other prompt options. As a result, when there is no database connection, other prompt elements are suppressed, but the pipeline status is still shown. For example:

```
evantest=# \set PROMPT1 '[%P] %R '
[off] = \c
connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
Previous connection kept
[off] = ;
FATAL: terminating connection due to administrator command
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
The connection to the server was lost. Attempting reset: Failed.
[off] !
[off] !
```

To confirm whether this behavior is intentional, I went through the original discussion for adding %P [2]/messages/by-id/CAO6_XqroE7JuMEm1sWz55rp9fAYX2JwmcP_3m_v51vnOFdsLiQ@mail.gmail.com, but didn’t find this case mentioned, so it seems this detail was simply not considered at the time.

This patch adds a pset.db check to align %P with the other prompt options, keeping the prompt display consistent when not connected. With the patch applied, all psql TAP tests pass:
```
# +++ tap check in src/bin/psql +++
t/001_basic.pl ........... ok
t/010_tab_completion.pl .. ok
t/020_cancel.pl .......... ok
t/030_pager.pl ........... ok
All tests successful.
Files=4, Tests=221, 5 wallclock secs ( 0.01 usr 0.01 sys + 1.05 cusr 1.38 csys = 2.45 CPU)
Result: PASS
```
[1]: /messages/by-id/016f6738-f9a9-4e98-bb5a-e1e4b9591d46@uni-muenster.de
[2]: /messages/by-id/CAO6_XqroE7JuMEm1sWz55rp9fAYX2JwmcP_3m_v51vnOFdsLiQ@mail.gmail.com

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v1-0001-psql-make-P-prompt-escape-consistent-when-not-con.patchapplication/octet-stream; name=v1-0001-psql-make-P-prompt-escape-consistent-when-not-con.patch; x-unix-mode=0644Download+2-3
#2Michael Paquier
michael@paquier.xyz
In reply to: Chao Li (#1)
Re: psql: make %P prompt option consistent when not connected

On Wed, Jan 28, 2026 at 04:38:08PM +0800, Chao Li wrote:

While reviewing and testing patch [1], I noticed that psql’s %P
prompt option does not check pset.db, which makes it inconsistent
with other prompt options. As a result, when there is no database
connection, other prompt elements are suppressed, but the pipeline
status is still shown.

Point taken. It's true that it is confusing to show an "off" status
in this case if we don't have an active connection. Will adjust and
backpatch.
--
Michael