On 27/01/2026 04:46, Fujii Masao wrote:
> Yeah, I'm fine with switching back to the original proposal.

PFA v8 containing the original proposal.

It now only checks in_hot_standby to determine if the current state of
the connected server is "primary" or "standby".

psql (19devel)
Type "help" for help.

postgres=# \set PROMPT1 '[%i] # '
[standby] # SHOW in_hot_standby;
 in_hot_standby
----------------
 on
(1 row)

[standby] # SELECT pg_promote();
 pg_promote
------------
 t
(1 row)

[primary] # SHOW in_hot_standby;
 in_hot_standby
----------------
 off
(1 row)

[primary] #

The documentation was updated accordingly.

Thanks!

Best, Jim
From dc142e7f86c10e82621c3a086cad97701967b537 Mon Sep 17 00:00:00 2001
From: Jim Jones <[email protected]>
Date: Tue, 27 Jan 2026 09:21:22 +0100
Subject: [PATCH v8] Add %i prompt escape to indicate hot standby status

This patch introduces a new prompt escape %i for psql, which shows
whether the connected server is operating in hot standby mode. It
expands to standby if the server reports in_hot_standby = on, and
primary otherwise.

This is useful for distinguishing standby servers from primary ones
at a glance, especially when working with multiple connections in
replicated environments where libpq's multi-host connection strings
are used.

Author: Jim Jones <[email protected]>
Reviewed-by: Fujii Masao <[email protected]>
Reviewed-by: Greg Sabino Mullane <[email protected]>
Reviewed-by: Srinath Reddy Sadipiralla <[email protected]>
Reviewed-by: Nathan Bossart <[email protected]>
Reviewed-by: Chao Li <[email protected]>
Reviewed-by: Andreas Karlsson <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/[email protected]
---
 doc/src/sgml/ref/psql-ref.sgml | 14 ++++++++++++++
 src/bin/psql/prompt.c          | 18 +++++++++++++++++-
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index e464e3b13d..2c6bf785fa 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -5075,6 +5075,20 @@ testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
         </listitem>
       </varlistentry>
 
+      <varlistentry id="app-psql-prompting-i">
+        <term><literal>%i</literal></term>
+        <listitem>
+          <para>
+            Indicates whether the server is in hot standby mode.
+            The value is shown as <literal>standby</literal> if the server reports
+            <literal>in_hot_standby</literal> as <literal>on</literal>,
+            and <literal>primary</literal> otherwise.
+            This can be useful when connecting to multiple hosts to quickly identify
+            if the connected server is currently in recovery mode.
+          </para>
+        </listitem>
+      </varlistentry>
+
       <varlistentry id="app-psql-prompting-x">
         <term><literal>%x</literal></term>
         <listitem>
diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index 44eae59641..8996b2fb94 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -258,7 +258,23 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
 							break;
 					}
 					break;
-
+				case 'i':
+					if (pset.db)
+					{
+						const char *hs = PQparameterStatus(pset.db, "in_hot_standby");
+						if (hs)
+						{
+							if (strcmp(hs, "on") == 0)
+								strlcpy(buf, "standby", sizeof(buf));
+							else
+								strlcpy(buf, "primary", sizeof(buf));
+						}
+						else
+							buf[0] = '\0';
+					}
+					else
+						buf[0] = '\0';
+					break;
 				case 'x':
 					if (!pset.db)
 						buf[0] = '?';
-- 
2.43.0

Reply via email to