psql: Add more information about service name
authorMichael Paquier
Wed, 18 Dec 2024 06:16:12 +0000 (15:16 +0900)
committerMichael Paquier
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[email protected]

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
         
       
 
+      
+        SERVICE
+        
+        
+        The service name, if applicable.
+        
+        
+      
+
       
        SHELL_ERROR
        
@@ -4674,6 +4683,11 @@ testdb=> INSERT INTO my_table VALUES (:'content');
         
       
 
+      
+        %s
+        The name of the service.
+      
+
       
         %/
         The name of the current database.
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)