Hi

pá 3. 2. 2023 v 20:27 odesílatel Corey Huinker <corey.huin...@gmail.com>
napsal:

>
>
> On Fri, Feb 3, 2023 at 5:42 AM Pavel Stehule <pavel.steh...@gmail.com>
> wrote:
>
>> 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
>>
>
> Interesting, and probably useful.
>
> It needs a corresponding line in UnsyncVariables():
>
> SetVariable(pset.vars, "BACKEND_PID", NULL);
>
> That will set the variable back to null when the connection goes away.
>

with doc and unsetting variable

Regards

Pavel


>
>
>
>
>
From e883f551227116e87c643d588f8c957f2defaeeb Mon Sep 17 00:00:00 2001
From: "ok...@github.com" <pavel.steh...@gmail.com>
Date: Sat, 4 Feb 2023 18:29:42 +0100
Subject: [PATCH] implementation of BACKEND_PID psql's variable

---
 doc/src/sgml/ref/psql-ref.sgml | 11 +++++++++++
 src/bin/psql/command.c         |  4 ++++
 src/bin/psql/help.c            |  2 ++
 3 files changed, 17 insertions(+)

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index dc6528dc11..92180a2eae 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -3902,6 +3902,17 @@ bar
         </listitem>
       </varlistentry>
 
+      <varlistentry id="app-psql-variables-backend-pid">
+        <term><varname>BACKEND_PID</varname></term>
+        <listitem>
+        <para>
+        The id of server process of the current connection.
+        This is set every time you connect to a database (including
+        program start-up), but can be changed or unset.
+        </para>
+        </listitem>
+      </varlistentry>
+
       <varlistentry id="app-psql-variables-comp-keyword-case">
         <term><varname>COMP_KEYWORD_CASE</varname></term>
         <listitem>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index b5201edf55..5a9b0e1569 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");
@@ -3817,6 +3820,7 @@ UnsyncVariables(void)
 	SetVariable(pset.vars, "ENCODING", NULL);
 	SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
 	SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
+	SetVariable(pset.vars, "BACKEND_PID", NULL);
 }
 
 
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