psql: Add more information about service name
authorMichael Paquier <michael@paquier.xyz>
Wed, 18 Dec 2024 06:16:12 +0000 (15:16 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 18 Dec 2024 06:16:12 +0000 (15:16 +0900)
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/6723c612.050a0220.1567f4.b94a@mx.google.com

doc/src/sgml/ref/psql-ref.sgml
src/bin/psql/command.c
src/bin/psql/prompt.c

index e42073ed7483aa32b23acf420a86d12cead86538..72f3347e53d6eccf207772742851a739575ec509 100644 (file)
@@ -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>
index 1f3cbb11f7c895db931cda707f7ba5b11c236b7b..cd16f27947aa8b68949bbf4437be6e16af97d459 100644 (file)
@@ -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);
index 0d99d00ac922ca20d7512b68a18108a132dd6c06..ea880bcacbed9ea9caf8795f8e31dce55bec567d 100644 (file)
@@ -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)