Add psql PROMPT variable for search_path.
authorNathan Bossart <nathan@postgresql.org>
Tue, 28 Oct 2025 19:08:38 +0000 (14:08 -0500)
committerNathan Bossart <nathan@postgresql.org>
Tue, 28 Oct 2025 19:08:38 +0000 (14:08 -0500)
The new %S substitution shows the current value of search_path.
Note that this only works when connected to Postgres v18 or newer,
since search_path was first marked as GUC_REPORT in commit
28a1121fd9.  On older versions that don't report search_path, %S is
replaced with a question mark.

Suggested-by: Lauri Siltanen <lauri.siltanen@gmail.com>
Author: Florents Tselai <florents.tselai@gmail.com>
Reviewed-by: Jelte Fennema-Nio <postgres@jeltef.nl>
Reviewed-by: Jim Jones <jim.jones@uni-muenster.de>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/CANsM767JhTKCRagTaq5Lz52fVwLPVkhSpyD1C%2BOrridGv0SO0A%40mail.gmail.com

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

index 1a339600bc48fedde95c706afbb1a8a193a22652..84683f62b1c86ba816fc166605b9bc44b8dcbb32 100644 (file)
@@ -4974,6 +4974,17 @@ testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
         </listitem>
       </varlistentry>
 
+      <varlistentry id="app-psql-prompting-S">
+       <term><literal>%S</literal></term>
+       <listitem>
+        <para>
+         The current value of <xref linkend="guc-search-path"/>, or
+         <literal>?</literal> if connected to a server running
+         <productname>PostgreSQL</productname> 17 or older.
+        </para>
+       </listitem>
+      </varlistentry>
+
       <varlistentry id="app-psql-prompting-s">
         <term><literal>%s</literal></term>
         <listitem><para>The name of the service.</para></listitem>
index b08d7328fbfe7846ffc6194507e9cde82fa1e387..59a2ceee07a66134ddb31acf09833037f65f056a 100644 (file)
@@ -34,6 +34,7 @@
  * %P - pipeline status: on, off or abort
  * %> - database server port number
  * %n - database user name
+ * %S - search_path
  * %s - service
  * %/ - current database
  * %~ - like %/ but "~" when database name equals user name
@@ -167,6 +168,16 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
                                        if (pset.db)
                                                strlcpy(buf, session_username(), sizeof(buf));
                                        break;
+                                       /* search_path */
+                               case 'S':
+                                       if (pset.db)
+                                       {
+                                               const char *sp = PQparameterStatus(pset.db, "search_path");
+
+                                               /* Use ? for versions that don't report search_path. */
+                                               strlcpy(buf, sp ? sp : "?", sizeof(buf));
+                                       }
+                                       break;
                                        /* service name */
                                case 's':
                                        {