From ba1651054d8d1f1a05664f3fe7b29354f861e0ee Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Wed, 9 Jul 2025 16:27:56 +0900
Subject: [PATCH v12 2/2] psql enhancement related servicefile option on
 connection string

---
 src/bin/psql/command.c            |  7 +++++++
 src/interfaces/libpq/fe-connect.c | 22 ++++++++++++++++++++++
 doc/src/sgml/ref/psql-ref.sgml    |  9 +++++++++
 3 files changed, 38 insertions(+)

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 0a55901b14e1..0e00d73487c3 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -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);
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index f9d626d9991c..17262fcf939a 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -6158,6 +6158,28 @@ parseServiceFile(const char *serviceFile,
 	}
 
 exit:
+
+	/* If service was found successfully, set servicefile option if not already set */
+	if (*group_found && result == 0)
+	{
+		for (i = 0; options[i].keyword; i++)
+		{
+			if (strcmp(options[i].keyword, "servicefile") != 0)
+				continue;
+
+			if (options[i].val != NULL)
+				break;
+
+			options[i].val = strdup(serviceFile);
+			if (options[i].val == NULL)
+			{
+				libpq_append_error(errorMessage, "out of memory");
+				return 3;
+			}
+			break;
+		}
+	}
+
 	fclose(f);
 
 	return result;
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 95f4cac2467e..4f7b11175c67 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -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>
-- 
2.50.0

