]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
psql: Add %i prompt escape to indicate hot standby status.
authorFujii Masao <fujii@postgresql.org>
Tue, 3 Feb 2026 01:03:19 +0000 (10:03 +0900)
committerFujii Masao <fujii@postgresql.org>
Tue, 3 Feb 2026 01:03:19 +0000 (10:03 +0900)
This commit 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 <jim.jones@uni-muenster.de>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Greg Sabino Mullane <htamfids@gmail.com>
Reviewed-by: Srinath Reddy Sadipiralla <srinath2133@gmail.com>
Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Andreas Karlsson <andreas@proxel.se>
Discussion: https://www.postgresql.org/message-id/flat/016f6738-f9a9-4e98-bb5a-e1e4b9591d46@uni-muenster.de

doc/src/sgml/ref/psql-ref.sgml
src/bin/psql/prompt.c

index e464e3b13de5e5637aaaa81607603f7da132b5c0..8b1d948ba0566bdd6229a533b6f5b0c0cbb453e7 100644 (file)
@@ -5075,6 +5075,23 @@ 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 connected server is running in hot standby mode.
+        The value is shown as <literal>standby</literal>, if the server is
+        currently in hot standby and reports
+        <xref linkend="guc-in-hot-standby"/> as <literal>on</literal>,
+        and <literal>primary</literal> otherwise. This is useful when
+        connecting to multiple servers to quickly determine the role of
+        each connection. A value of <literal>?</literal> is shown
+        when connected to a server running
+        <productname>PostgreSQL</productname> 13 or older.
+        </para>
+        </listitem>
+      </varlistentry>
+
       <varlistentry id="app-psql-prompting-x">
         <term><literal>%x</literal></term>
         <listitem>
index 891cd6374f0942a70843a6dfed2bfff3aa9c7588..9725d53dfe7844187e8708f78cf81b8787f42394 100644 (file)
@@ -44,6 +44,8 @@
  *                     or a ! if session is not connected to a database;
  *             in prompt2 -, *, ', or ";
  *             in prompt3 nothing
+ * %i - "standby" or "primary" depending on the server's in_hot_standby
+ *      status, or "?" if unavailable (empty if unknown)
  * %x - transaction status: empty, *, !, ? (unknown or no connection)
  * %l - The line number inside the current statement, starting from 1.
  * %? - the error code of the last query (not yet implemented)
@@ -258,7 +260,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));
+                                               }
+                                               /* Use ? for versions that don't report in_hot_standby */
+                                               else
+                                                       buf[0] = '?';
+                                       }
+                                       break;
                                case 'x':
                                        if (!pset.db)
                                                buf[0] = '?';