psql: Add variable SERVICEFILE
authorMichael Paquier <[email protected]>
Mon, 14 Jul 2025 00:08:46 +0000 (09:08 +0900)
committerMichael Paquier <[email protected]>
Mon, 14 Jul 2025 00:08:46 +0000 (09:08 +0900)
This new psql variable can be used to check which service file has been
used for a connection.  Like other variables, this can be set in a
PROMPT or reported by an \echo, like these commands:
\echo :SERVICEFILE
\set PROMPT1 '=(%:SERVICEFILE:)%# '

This relies on commits 092f3c63efc6 and fef6da9e9c87 to retrieve this
information from the connection's PQconninfoOption.

Author: Ryo Kanbayashi <[email protected]>
Discussion: https://postgr.es/m/CAKkG4_nCjx3a_F3gyXHSPWxD8Sd8URaM89wey7fG_9g7KBkOCQ@mail.gmail.com

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

index 95f4cac2467e315654a0ab2edf990b257b00888d..4f7b11175c671b17520c19fa49437356a68d0d63 100644 (file)
@@ -4623,6 +4623,15 @@ bar
         </listitem>
       </varlistentry>
 
+      <varlistentry id="app-psql-variables-servicefile">
+        <term><varname>SERVICEFILE</varname></term>
+        <listitem>
+        <para>
+        The service file name, if applicable.
+        </para>
+        </listitem>
+      </varlistentry>
+
       <varlistentry id="app-psql-variables-shell-error">
        <term><varname>SHELL_ERROR</varname></term>
        <listitem>
index 0a55901b14e1e6a6fc37b3d1553715e11d61a67e..0e00d73487c33617734d4f4645b507bf0209858e 100644 (file)
@@ -4481,6 +4481,7 @@ SyncVariables(void)
    char        vbuf[32];
    const char *server_version;
    char       *service_name;
+   char       *service_file;
 
    /* get stuff from connection */
    pset.encoding = PQclientEncoding(pset.db);
@@ -4500,6 +4501,11 @@ SyncVariables(void)
    if (service_name)
        pg_free(service_name);
 
+   service_file = get_conninfo_value("servicefile");
+   SetVariable(pset.vars, "SERVICEFILE", service_file);
+   if (service_file)
+       pg_free(service_file);
+
    /* this bit should match connection_warnings(): */
    /* Try to get full text form of version, might include "devel" etc */
    server_version = PQparameterStatus(pset.db, "server_version");
@@ -4529,6 +4535,7 @@ UnsyncVariables(void)
 {
    SetVariable(pset.vars, "DBNAME", NULL);
    SetVariable(pset.vars, "SERVICE", NULL);
+   SetVariable(pset.vars, "SERVICEFILE", NULL);
    SetVariable(pset.vars, "USER", NULL);
    SetVariable(pset.vars, "HOST", NULL);
    SetVariable(pset.vars, "PORT", NULL);