v2-0001-psql-Add-S-prompt-escape-to-display-current-searc.patch
application/octet-stream
Filename: v2-0001-psql-Add-S-prompt-escape-to-display-current-searc.patch
Type: application/octet-stream
Part: 0
From c61f41191c64cb771fc54ac33af7b0da2bbdf7c9 Mon Sep 17 00:00:00 2001
From: Florents Tselai <florents.tselai@gmail.com>
Date: Fri, 24 Oct 2025 16:59:48 +0300
Subject: [PATCH v2] psql: Add %S prompt escape to display current search_path
This adds a new prompt escape sequence, %S, which expands to the
current value of the session's search_path as reported by libpq via
PQparameterStatus().
If the connected server does not report a search_path parameter
(e.g., servers older than v18), or if the client is not connected,
%S displays a question mark (?). An empty but valid search_path ("")
is shown as empty.
---
doc/src/sgml/ref/psql-ref.sgml | 5 +++++
src/bin/psql/common.c | 18 ++++++++++++++++++
src/bin/psql/common.h | 2 ++
src/bin/psql/prompt.c | 12 ++++++++++++
4 files changed, 37 insertions(+)
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 1a339600bc4..808751768f1 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -4979,6 +4979,11 @@ testdb=> <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
<listitem><para>The name of the service.</para></listitem>
</varlistentry>
+ <varlistentry id="app-psql-prompting-S">
+ <term><literal>%S</literal></term>
+ <listitem><para>The current search path.</para></listitem>
+ </varlistentry>
+
<varlistentry id="app-psql-prompting-slash">
<term><literal>%/</literal></term>
<listitem><para>The name of the current database.</para></listitem>
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index cd329ade12b..7eb25af9afb 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -2705,3 +2705,21 @@ recognized_connection_string(const char *connstr)
{
return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
}
+
+/*
+ * Return the session search_path.
+ */
+const char *
+session_search_path(void)
+{
+ const char *val;
+
+ if (!pset.db)
+ return NULL;
+
+ val = PQparameterStatus(pset.db, "search_path");
+ if (val)
+ return val;
+ else
+ return PQuser(pset.db);
+}
diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h
index 64762ab9817..f50e4a1e8f7 100644
--- a/src/bin/psql/common.h
+++ b/src/bin/psql/common.h
@@ -44,6 +44,8 @@ extern char *get_conninfo_value(const char *keyword);
extern void expand_tilde(char **filename);
extern void clean_extended_state(void);
+extern const char *session_search_path(void);
+
extern bool recognized_connection_string(const char *connstr);
#endif /* COMMON_H */
diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index b08d7328fbf..7dbadcdb079 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -176,6 +176,18 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
strlcpy(buf, service_name, sizeof(buf));
}
break;
+ /* %S: current search_path, or "?" if not reported by the server */
+ case 'S':
+ /*
+ * Distinguish unknown (NULL) from an empty but valid search_path ("").
+ * If not connected or older server doesn't report it via ParameterStatus,
+ * show "?".
+ */
+ if (!pset.db || PQparameterStatus(pset.db, "search_path") == NULL)
+ strlcpy(buf, "?", sizeof(buf));
+ else
+ strlcpy(buf, PQparameterStatus(pset.db, "search_path"), sizeof(buf));
+ break;
/* backend pid */
case 'p':
if (pset.db)
--
2.49.0