Skip to content

Commit b3ce55f

Browse files
Add psql PROMPT variable for search_path.
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 28a1121. On older versions that don't report search_path, %S is replaced with a question mark. Suggested-by: Lauri Siltanen <[email protected]> Author: Florents Tselai <[email protected]> Reviewed-by: Jelte Fennema-Nio <[email protected]> Reviewed-by: Jim Jones <[email protected]> Reviewed-by: Chao Li <[email protected]> Discussion: https://postgr.es/m/CANsM767JhTKCRagTaq5Lz52fVwLPVkhSpyD1C%2BOrridGv0SO0A%40mail.gmail.com
1 parent 03fbb08 commit b3ce55f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4974,6 +4974,17 @@ testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
49744974
</listitem>
49754975
</varlistentry>
49764976

4977+
<varlistentry id="app-psql-prompting-S">
4978+
<term><literal>%S</literal></term>
4979+
<listitem>
4980+
<para>
4981+
The current value of <xref linkend="guc-search-path"/>, or
4982+
<literal>?</literal> if connected to a server running
4983+
<productname>PostgreSQL</productname> 17 or older.
4984+
</para>
4985+
</listitem>
4986+
</varlistentry>
4987+
49774988
<varlistentry id="app-psql-prompting-s">
49784989
<term><literal>%s</literal></term>
49794990
<listitem><para>The name of the service.</para></listitem>

src/bin/psql/prompt.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* %P - pipeline status: on, off or abort
3535
* %> - database server port number
3636
* %n - database user name
37+
* %S - search_path
3738
* %s - service
3839
* %/ - current database
3940
* %~ - like %/ but "~" when database name equals user name
@@ -167,6 +168,16 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
167168
if (pset.db)
168169
strlcpy(buf, session_username(), sizeof(buf));
169170
break;
171+
/* search_path */
172+
case 'S':
173+
if (pset.db)
174+
{
175+
const char *sp = PQparameterStatus(pset.db, "search_path");
176+
177+
/* Use ? for versions that don't report search_path. */
178+
strlcpy(buf, sp ? sp : "?", sizeof(buf));
179+
}
180+
break;
170181
/* service name */
171182
case 's':
172183
{

0 commit comments

Comments
 (0)