Thread: display hot standby state in psql prompt
Hi, Some weeks ago we briefly discussed in the discord channel the possibility of introducing a psql prompt display option to identify if the connected database is in hot standby mode, which can be useful when using multiple hosts in the connection string. Right now, it's using the in_hot_standby value in prompt.c to determine the database state: case 'i': if (pset.db && PQparameterStatus(pset.db, "in_hot_standby")) { const char *hs = PQparameterStatus(pset.db, "in_hot_standby"); if (hs && strcmp(hs, "on") == 0) strlcpy(buf, "standby", sizeof(buf)); else strlcpy(buf, "primary", sizeof(buf)); .. which could be used like this: psql (18beta1) Type "help" for help. postgres=# \set PROMPT1 '[%i] # ' [standby] # SELECT pg_promote(); pg_promote ------------ t (1 row) [primary] # The hardcoded "standby" and "primary" strings are not very flexible, but I am not sure how to make these strings customisable just yet. Any thoughts on this feature? Best regards, Jim
On Wed, Jun 25, 2025 at 4:02 AM Jim Jones <jim.jones@uni-muenster.de> wrote:
if (pset.db && PQparameterStatus(pset.db, "in_hot_standby"))
Seems transaction_read_only might be a more useful thing to examine? That's the side-effect, if you will, that people really care about when in hot standby mode (and of course, we can get into TRO other ways).
Cheers,
Greg
--
Crunchy Data - https://www.crunchydata.com
Enterprise Postgres Software Products & Tech Support
Hi Greg On 25.06.25 17:17, Greg Sabino Mullane wrote: > Seems transaction_read_only might be a more useful thing to examine? > That's the side-effect, if you will, that people really care about > when in hot standby mode (and of course, we can get into TRO other ways). Good point. But wouldn't it mean that I would need to execute a query every time the prompt is refreshed? Since I cannot get the value of transaction_read_only via PQparameterStatus. I like the idea, but I'm concerned about the overhead. Best, Jim
On Wed, Jun 25, 2025 at 11:50 AM Jim Jones <jim.jones@uni-muenster.de> wrote:
Since I cannot get the value of transaction_read_only via PQparameterStatus.
Hmmm... we can at least get default_transaction_read_only. As fe-connect.c points out:
/*
* "transaction_read_only = on" proves that at least one
* of default_transaction_read_only and in_hot_standby is
* on, but we don't actually know which. We don't care
* though for the purpose of identifying a read-only
* session, so satisfy the CONNECTION_CHECK_TARGET code by
* claiming they are both on. On the other hand, if it's
* a read-write session, they are certainly both off.
*/
* of default_transaction_read_only and in_hot_standby is
* on, but we don't actually know which. We don't care
* though for the purpose of identifying a read-only
* session, so satisfy the CONNECTION_CHECK_TARGET code by
* claiming they are both on. On the other hand, if it's
* a read-write session, they are certainly both off.
*/
Maybe that's good enough? It won't detect people starting a new transaction and declaring it read-only, but it should be sufficient to warn people when a connection is starting out in a read-only state. And it will still toggle auto-magically on promotion.
Cheers,
Greg
--
Crunchy Data - https://www.crunchydata.com
Enterprise Postgres Software Products & Tech Support