libpq: Remove PQservice()
authorMichael Paquier <[email protected]>
Wed, 9 Jul 2025 03:46:13 +0000 (12:46 +0900)
committerMichael Paquier <[email protected]>
Wed, 9 Jul 2025 03:46:13 +0000 (12:46 +0900)
This routine has been introduced as a shortcut to be able to retrieve a
service name from an active connection, for psql.  Per discussion, and
as it is only used by psql, let's remove it to not clutter the libpq API
more than necessary.

The logic in psql is replaced by lookups of PQconninfoOption for the
active connection, instead, updated each time the variables are synced
by psql, the prompt shortcut relying on the variable synced.

Reported-by: Noah Misch <[email protected]>
Discussion: https://postgr.es/m/20250706161319[email protected]
Backpatch-through: 18

doc/src/sgml/libpq.sgml
src/bin/psql/command.c
src/bin/psql/common.c
src/bin/psql/common.h
src/bin/psql/prompt.c
src/interfaces/libpq/exports.txt
src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/libpq-fe.h

index 298c4b38ef90a7f430d06a3f57875f9c2ee88689..b2c2cf9eac831e0297ec9db6a8cb72799db35ca1 100644 (file)
@@ -2740,26 +2740,6 @@ char *PQport(const PGconn *conn);
      </listitem>
     </varlistentry>
 
-    <varlistentry id="libpq-PQservice">
-     <term><function>PQservice</function><indexterm><primary>PQservice</primary></indexterm></term>
-
-     <listitem>
-      <para>
-       Returns the service of the active connection.
-
-<synopsis>
-char *PQservice(const PGconn *conn);
-</synopsis>
-      </para>
-
-      <para>
-       <xref linkend="libpq-PQservice"/> returns <symbol>NULL</symbol> if the
-       <parameter>conn</parameter> argument is <symbol>NULL</symbol>.
-       Otherwise, if there was no service provided, it returns an empty string.
-      </para>
-     </listitem>
-    </varlistentry>
-
     <varlistentry id="libpq-PQtty">
      <term><function>PQtty</function><indexterm><primary>PQtty</primary></indexterm></term>
 
index 9fcd2db832656c10ea41e60b874d74e382af456f..0a55901b14e1e6a6fc37b3d1553715e11d61a67e 100644 (file)
@@ -4480,6 +4480,7 @@ SyncVariables(void)
 {
    char        vbuf[32];
    const char *server_version;
+   char       *service_name;
 
    /* get stuff from connection */
    pset.encoding = PQclientEncoding(pset.db);
@@ -4489,12 +4490,16 @@ SyncVariables(void)
    setFmtEncoding(pset.encoding);
 
    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));
    SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
 
+   service_name = get_conninfo_value("service");
+   SetVariable(pset.vars, "SERVICE", service_name);
+   if (service_name)
+       pg_free(service_name);
+
    /* 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");
index d2c0a49c46c0478dc5f7fd76f5ef6e549caf5dc0..cd329ade12b5d7467dd1f23c68414593bca12628 100644 (file)
@@ -2531,6 +2531,41 @@ session_username(void)
        return PQuser(pset.db);
 }
 
+/*
+ * Return the value of option for keyword in the current connection.
+ *
+ * The caller is responsible for freeing the result value allocated.
+ */
+char *
+get_conninfo_value(const char *keyword)
+{
+   PQconninfoOption *opts;
+   PQconninfoOption *serviceopt = NULL;
+   char       *res = NULL;
+
+   if (pset.db == NULL)
+       return NULL;
+
+   opts = PQconninfo(pset.db);
+   if (opts == NULL)
+       return NULL;
+
+   for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
+   {
+       if (strcmp(opt->keyword, keyword) == 0)
+       {
+           serviceopt = opt;
+           break;
+       }
+   }
+
+   /* Take a copy of the value, as it is freed by PQconninfoFree(). */
+   if (serviceopt && serviceopt->val != NULL)
+       res = pg_strdup(serviceopt->val);
+   PQconninfoFree(opts);
+
+   return res;
+}
 
 /* expand_tilde
  *
index 7f1a23de1e82d9e3227997e9f99ee8df7419b32e..64762ab981755cb2a51983d3a495ad1278c4388c 100644 (file)
@@ -39,6 +39,7 @@ extern bool SendQuery(const char *query);
 extern bool is_superuser(void);
 extern bool standard_strings(void);
 extern const char *session_username(void);
+extern char *get_conninfo_value(const char *keyword);
 
 extern void expand_tilde(char **filename);
 extern void clean_extended_state(void);
index 3aa7d2d06c80ef27e5496e11b6d39364ec1e37d4..b08d7328fbfe7846ffc6194507e9cde82fa1e387 100644 (file)
@@ -169,8 +169,12 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
                    break;
                    /* service name */
                case 's':
-                   if (pset.db && PQservice(pset.db))
-                       strlcpy(buf, PQservice(pset.db), sizeof(buf));
+                   {
+                       const char *service_name = GetVariable(pset.vars, "SERVICE");
+
+                       if (service_name)
+                           strlcpy(buf, service_name, sizeof(buf));
+                   }
                    break;
                    /* backend pid */
                case 'p':
index 0625cf39e9af334862ab3a62193e93151002682a..dbbae642d769acfde14b41139415fbd6f87f8f25 100644 (file)
@@ -205,9 +205,8 @@ PQcancelFinish            202
 PQsocketPoll              203
 PQsetChunkedRowsMode      204
 PQgetCurrentTimeUSec      205
-PQservice                 206
-PQsetAuthDataHook         207
-PQgetAuthDataHook         208
-PQdefaultAuthDataHook     209
-PQfullProtocolVersion     210
-appendPQExpBufferVA       211
+PQsetAuthDataHook         206
+PQgetAuthDataHook         207
+PQdefaultAuthDataHook     208
+PQfullProtocolVersion     209
+appendPQExpBufferVA       210
index 51a9c41658455cf807c82e3f59834d9dfbeb23a7..09eb79812ac6d71556f23cc37f3121d51cf219fa 100644 (file)
@@ -7461,14 +7461,6 @@ PQdb(const PGconn *conn)
    return conn->dbName;
 }
 
-char *
-PQservice(const PGconn *conn)
-{
-   if (!conn)
-       return NULL;
-   return conn->pgservice;
-}
-
 char *
 PQuser(const PGconn *conn)
 {
index 7d3a9df6fd559200282bf589ba8006de87aa7531..af8004f952a568b1030af8bf57e4dbd8fb0f5a58 100644 (file)
@@ -400,7 +400,6 @@ extern int  PQrequestCancel(PGconn *conn);
 
 /* Accessor functions for PGconn objects */
 extern char *PQdb(const PGconn *conn);
-extern char *PQservice(const PGconn *conn);
 extern char *PQuser(const PGconn *conn);
 extern char *PQpass(const PGconn *conn);
 extern char *PQhost(const PGconn *conn);