Hi

We can simply allow an access to backend process id thru psql variable. I
propose the name "BACKEND_PID". The advantages of usage are simple
accessibility by command \set, and less typing then using function
pg_backend_pid, because psql variables are supported by tab complete
routine. Implementation is very simple, because we can use the function
PQbackendPID.

Comments, notes?

Regards

Pavel
From 153994fd93571964766ca054b0f7fe342ac72a6f Mon Sep 17 00:00:00 2001
From: "ok...@github.com" <pavel.steh...@gmail.com>
Date: Fri, 3 Feb 2023 11:40:41 +0100
Subject: [PATCH] implementation of BACKEND_PID psql's variable

---
 src/bin/psql/command.c | 3 +++
 src/bin/psql/help.c    | 2 ++
 2 files changed, 5 insertions(+)

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index b5201edf55..934dd26c61 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3783,6 +3783,9 @@ SyncVariables(void)
 	SetVariable(pset.vars, "PORT", PQport(pset.db));
 	SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
 
+	snprintf(vbuf, sizeof(vbuf), "%d", PQbackendPID(pset.db));
+	SetVariable(pset.vars, "BACKEND_PID", vbuf);
+
 	/* 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");
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index e45c4aaca5..61c6edd0ba 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -396,6 +396,8 @@ helpVariables(unsigned short int pager)
 
 	HELP0("  AUTOCOMMIT\n"
 		  "    if set, successful SQL commands are automatically committed\n");
+	HELP0("  BACKEND_PID\n"
+		  "    id of server process of the current connection\n");
 	HELP0("  COMP_KEYWORD_CASE\n"
 		  "    determines the case used to complete SQL key words\n"
 		  "    [lower, upper, preserve-lower, preserve-upper]\n");
-- 
2.39.1

Reply via email to