From 3c53ee3a9723319cdee6687dbc698ca76df7d5ce Mon Sep 17 00:00:00 2001
From: songjinzhou <tsinghualucky912@foxmail.com>
Date: Tue, 29 Apr 2025 03:32:49 -0700
Subject: [PATCH] libpq: Add PQapplicationname() function

---
 doc/src/sgml/libpq.sgml           | 20 ++++++++++++++++++++
 src/interfaces/libpq/exports.txt  |  1 +
 src/interfaces/libpq/fe-connect.c |  8 ++++++++
 src/interfaces/libpq/libpq-fe.h   |  1 +
 4 files changed, 30 insertions(+)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 8cdd2997d4..642f45dc0c 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -2760,6 +2760,26 @@ char *PQservice(const PGconn *conn);
      </listitem>
     </varlistentry>
 
+    <varlistentry id="libpq-PQapplicationname">
+     <term><function>PQapplicationname</function><indexterm><primary>PQapplicationname</primary></indexterm></term>
+
+     <listitem>
+      <para>
+       Returns the application_name of the active connection.
+
+<synopsis>
+char *PQapplicationname(const PGconn *conn);
+</synopsis>
+      </para>
+
+      <para>
+       <xref linkend="libpq-PQapplicationname"/> returns <symbol>NULL</symbol> if the
+       <parameter>conn</parameter> argument is <symbol>NULL</symbol>.
+       Otherwise, if there was no application_name provided, it returns an empty string.
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry id="libpq-PQtty">
      <term><function>PQtty</function><indexterm><primary>PQtty</primary></indexterm></term>
 
diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt
index d514376685..8188b8fe13 100644
--- a/src/interfaces/libpq/exports.txt
+++ b/src/interfaces/libpq/exports.txt
@@ -210,3 +210,4 @@ PQsetAuthDataHook         207
 PQgetAuthDataHook         208
 PQdefaultAuthDataHook     209
 PQfullProtocolVersion     210
+PQapplicationname         211
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index bd51e6115c..5df60bd59a 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -7459,6 +7459,14 @@ PQservice(const PGconn *conn)
 	return conn->pgservice;
 }
 
+char *
+PQapplicationname(const PGconn *conn)
+{
+	if (!conn)
+		return NULL;
+	return conn->appname;
+}
+
 char *
 PQuser(const PGconn *conn)
 {
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index 7d3a9df6fd..8494089a87 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -401,6 +401,7 @@ extern int	PQrequestCancel(PGconn *conn);
 /* Accessor functions for PGconn objects */
 extern char *PQdb(const PGconn *conn);
 extern char *PQservice(const PGconn *conn);
+extern char *PQapplicationname(const PGconn *conn);
 extern char *PQuser(const PGconn *conn);
 extern char *PQpass(const PGconn *conn);
 extern char *PQhost(const PGconn *conn);
-- 
2.43.0

