From 5af78dfd2fe816a956fd6d77b0be2aae89be6030 Mon Sep 17 00:00:00 2001
From: Hunaid2000 <hunaid2000@gmail.com>
Date: Thu, 16 Jan 2025 15:04:53 +0500
Subject: [PATCH v37 1/3] Add PQparameterNames() to libpq to return parameters
 reported by server

This function in libpq allows users to retrieve all parameter names
reported by the server. This is useful for specifying parameter names
when calling PQparameterStatus().

The function can also accommodate any future parameters that may
be reported by the server, providing flexibility.
---
 doc/src/sgml/libpq.sgml           | 25 ++++++++++++++++++++++++
 src/interfaces/libpq/exports.txt  |  1 +
 src/interfaces/libpq/fe-connect.c | 32 +++++++++++++++++++++++++++++++
 src/interfaces/libpq/libpq-fe.h   |  1 +
 4 files changed, 59 insertions(+)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index e04acf1c20..370534e8a3 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -2736,6 +2736,31 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName);
      </listitem>
     </varlistentry>
 
+    <varlistentry id="libpq-PQparameterNames">
+     <term><function>PQparameterNames</function><indexterm><primary>PQparameterNames</primary></indexterm></term>
+
+     <listitem>
+      <para>
+       Returns palloc'd array of parameter names reported by the server
+       that can be used in <function>PQparameterStatus()</function>.
+       The array is terminated by a NULL pointer.
+
+<synopsis>
+const char **PQparameterNames(const PGconn *conn, int *len);
+</synopsis>
+      </para>
+
+      <para>
+       If <literal>conn</literal> or <literal>conn->pstatus</literal> is NULL,
+       NULL is returned and <literal>len</literal> is set to zero.
+       If <literal>conn</literal> is not NULL, the parameter names are returned
+       in a palloc'd array of strings, and <literal>len</literal> is set to the
+       number of elements in the array. It is the caller's responsibility to
+       free the array when it is no longer needed.
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry id="libpq-PQfullProtocolVersion">
      <term><function>PQfullProtocolVersion</function><indexterm><primary>PQfullProtocolVersion</primary></indexterm></term>
 
diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt
index 2ad2cbf5ca..7bf93e031e 100644
--- a/src/interfaces/libpq/exports.txt
+++ b/src/interfaces/libpq/exports.txt
@@ -206,3 +206,4 @@ PQsocketPoll              203
 PQsetChunkedRowsMode      204
 PQgetCurrentTimeUSec      205
 PQservice                 206
+PQparameterNames          207
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 7878e2e33a..b5085ef695 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -7234,6 +7234,38 @@ PQparameterStatus(const PGconn *conn, const char *paramName)
 	return NULL;
 }
 
+const char **
+PQparameterNames(const PGconn *conn, int *len)
+{
+	const pgParameterStatus *pstatus;
+	const char **params;
+	int			count;
+
+	if (len)
+		*len = 0;
+
+	if (!conn || conn->pstatus == NULL)
+		return NULL;
+
+	/* Count the active parameter status entries */
+	for (count = 0, pstatus = conn->pstatus; pstatus != NULL; pstatus = pstatus->next)
+		count++;
+
+	/* Allocate and fill the params array */
+	params = (const char **) palloc((count + 1) * sizeof(char *));
+	if (!params)
+		return NULL;
+
+	for (count = 0, pstatus = conn->pstatus; pstatus != NULL; pstatus = pstatus->next)
+		params[count++] = pstatus->name;
+	params[count] = NULL;
+
+	if (len)
+		*len = count;
+
+	return params;
+}
+
 int
 PQprotocolVersion(const PGconn *conn)
 {
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index cce9ce60c5..5b2ec3e9f5 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -397,6 +397,7 @@ extern ConnStatusType PQstatus(const PGconn *conn);
 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
 extern const char *PQparameterStatus(const PGconn *conn,
 									 const char *paramName);
+extern const char **PQparameterNames(const PGconn *conn, int *len);
 extern int	PQprotocolVersion(const PGconn *conn);
 extern int	PQfullProtocolVersion(const PGconn *conn);
 extern int	PQserverVersion(const PGconn *conn);
-- 
2.34.1

