From 7ca481afc46d1c49e221fc2aefd89bc2187b2200 Mon Sep 17 00:00:00 2001 From: Florents Tselai Date: Mon, 9 Jun 2025 18:51:26 +0300 Subject: [PATCH] Make %S available in psql PROMPTs to indicate the current search_path --- 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 | 5 +++++ 4 files changed, 30 insertions(+) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 8f7d8758ca0..16c05d6f926 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -4920,6 +4920,11 @@ testdb=> INSERT INTO my_table VALUES (:'content'); The name of the service. + + %S + The current search path. + + %/ The name of the current database. diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 47352b7faed..35b9066738a 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -2658,3 +2658,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 7f1a23de1e8..d308e85de0d 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -43,6 +43,8 @@ extern const char *session_username(void); 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 3aa7d2d06c8..74f24d59004 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -172,6 +172,11 @@ get_prompt(promptStatus_t status, ConditionalStack cstack) if (pset.db && PQservice(pset.db)) strlcpy(buf, PQservice(pset.db), sizeof(buf)); break; + /* search_path */ + case 'S': + if (pset.db && PQparameterStatus(pset.db, "search_path")) + strlcpy(buf, session_search_path(), sizeof(buf)); + break; /* backend pid */ case 'p': if (pset.db) -- 2.49.0