libpq: Add PQapplicationname() function
Dear hackers, here is a libpq function: PQapplicationname(), I think it may be of some use.
Looking forward to your reply, thank you.
Attachments:
v1_0001-libpq-Add-PQapplicationname-function.patchapplication/octet-stream; charset=utf-8; name=v1_0001-libpq-Add-PQapplicationname-function.patchDownload
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
On Tue, 29 Apr 2025 at 19:22, "songjinzhou" <tsinghualucky912@foxmail.com> wrote:
Dear hackers, here is a libpq function: PQapplicationname(), I think it may be of some use.
Looking forward to your reply, thank you.
I believe the use cases for this are narrow.
Generally, clients set the application_name and then don't need to
retrieve it. Its primary use seems to be on the server-side, for
monitoring through pg_stat_activity.
Could you explain your use case?
Assuming we do require this functionality, should we then also consider
the interplay between application_name and fallback_application_name?
Additionally, if a SET application_name TO xxx command is issued during
a subsequent connection, will the appname field within the PGconn structure
be updated promptly?
--
Regrads,
Japin Li
On Tuesday, April 29, 2025, songjinzhou <tsinghualucky912@foxmail.com>
wrote:
Dear hackers, here is a libpq function: PQapplicationname(), I think it
may be of some use.
You really should provide at least one reasoned use case for why this
should exist.
Looking forward to your reply, thank you.
This isn’t even the correct code to use for this. PQparameterStatus is
where you’d access application_name. I don’t see a good reason in
providing multiple ways to get access to the same data value.
David J.