summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Paquier2024-12-18 06:16:12 +0000
committerMichael Paquier2024-12-18 06:16:12 +0000
commit477728b5d6fa16461b81cd22b0568fec1eab97ac (patch)
tree5f675621947600a0d6e74be65e20a02380a4769f
parent4b99fed7541e330b669fe488a274c0c69490391c (diff)
psql: Add more information about service name
This commit adds support for the following items in psql, able to show a service name, when available: - Variable SERVICE. - Substitution %s in PROMPT{1,2,3}. This relies on 4b99fed7541e, that has made the service name available in PGconn for libpq. Author: Michael Banck Reviewed-by: Greg Sabino Mullane Discussion: https://postgr.es/m/[email protected]
-rw-r--r--doc/src/sgml/ref/psql-ref.sgml14
-rw-r--r--src/bin/psql/command.c2
-rw-r--r--src/bin/psql/prompt.c6
3 files changed, 22 insertions, 0 deletions
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index e42073ed748..72f3347e53d 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -4380,6 +4380,15 @@ bar
</listitem>
</varlistentry>
+ <varlistentry id="app-psql-variables-service">
+ <term><varname>SERVICE</varname></term>
+ <listitem>
+ <para>
+ The service name, if applicable.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry id="app-psql-variables-shell-error">
<term><varname>SHELL_ERROR</varname></term>
<listitem>
@@ -4674,6 +4683,11 @@ 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 name of the service.</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/command.c b/src/bin/psql/command.c
index 1f3cbb11f7c..cd16f27947a 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -4082,6 +4082,7 @@ SyncVariables(void)
pset.sversion = PQserverVersion(pset.db);
SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
+ SetVariable(pset.vars, "SERVICE", PQservice(pset.db));
SetVariable(pset.vars, "USER", PQuser(pset.db));
SetVariable(pset.vars, "HOST", PQhost(pset.db));
SetVariable(pset.vars, "PORT", PQport(pset.db));
@@ -4115,6 +4116,7 @@ void
UnsyncVariables(void)
{
SetVariable(pset.vars, "DBNAME", NULL);
+ SetVariable(pset.vars, "SERVICE", NULL);
SetVariable(pset.vars, "USER", NULL);
SetVariable(pset.vars, "HOST", NULL);
SetVariable(pset.vars, "PORT", NULL);
diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index 0d99d00ac92..ea880bcacbe 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -33,6 +33,7 @@
* %p - backend pid
* %> - database server port number
* %n - database user name
+ * %s - service
* %/ - current database
* %~ - like %/ but "~" when database name equals user name
* %w - whitespace of the same width as the most recent output of PROMPT1
@@ -165,6 +166,11 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
if (pset.db)
strlcpy(buf, session_username(), sizeof(buf));
break;
+ /* service name */
+ case 's':
+ if (pset.db && PQservice(pset.db))
+ strlcpy(buf, PQservice(pset.db), sizeof(buf));
+ break;
/* backend pid */
case 'p':
if (pset.db)