On Tue, Oct 28, 2025 at 09:08:36AM +0800, Chao Li wrote:
>> On Oct 28, 2025, at 04:29, Nathan Bossart <[email protected]> wrote:
>> Here is what I have staged for commit. I ended up simplifying the patch a
>> bit. In particular, I thought better of the question mark business. It
>> looks like we ordinarily just skip values that can't be found, and an empty
>> search_path will appear as "" (two double-quotes), so you can still
>> distinguish empty versus not-available.
>
> +1, I like this idea.
Actually, I take it back. The following command to empty the search_path
will cause %S to be replaced with nothing, in which case you can't
distinguish empty versus not-reported.
SELECT pg_catalog.set_config('search_path', '', false);
So, I've added the question mark back.
--
nathan
>From 7f332b7f3e667e7c69a24b556c2bc5405ba6be4c Mon Sep 17 00:00:00 2001
From: Nathan Bossart <[email protected]>
Date: Tue, 28 Oct 2025 12:05:17 -0500
Subject: [PATCH v5 1/1] Add psql PROMPT variable for the current search_path.
The new %S substitution shows the current 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 <[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
---
doc/src/sgml/ref/psql-ref.sgml | 10 ++++++++++
src/bin/psql/prompt.c | 11 +++++++++++
2 files changed, 21 insertions(+)
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 1a339600bc4..f6760b81370 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -4974,6 +4974,16 @@ testdb=> <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 <xref linkend="guc-search-path"/>, or <literal>?</literal>
+ if connected to a server running PostgreSQL 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>
diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index b08d7328fbf..59a2ceee07a 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -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':
{
--
2.39.5 (Apple Git-154)