v31-0001-Add-psql-meta-command-conninfo-plus.patch

application/octet-stream

Filename: v31-0001-Add-psql-meta-command-conninfo-plus.patch
Type: application/octet-stream
Part: 0
Message: Re: Psql meta-command conninfo+

Patch

Format: format-patch
Series: patch v31-0001
Subject: Add psql meta command conninfo+
File+
doc/src/sgml/ref/psql-ref.sgml 22 4
src/bin/psql/command.c 28 5
src/bin/psql/help.c 1 1
From 65f34ab5065630672aeb13fd16c529b9573e1234 Mon Sep 17 00:00:00 2001
From: Hunaid Sohail <hunaid2000@gmail.com>
Date: Sun, 25 Aug 2024 16:28:43 +0500
Subject: [PATCH v31] Add psql meta command conninfo+

---
 doc/src/sgml/ref/psql-ref.sgml | 26 ++++++++++++++++++++++----
 src/bin/psql/command.c         | 33 ++++++++++++++++++++++++++++-----
 src/bin/psql/help.c            |  2 +-
 3 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 3fd9959ed1..3f8d72b42e 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1060,11 +1060,29 @@ INSERT INTO tbls1 VALUES ($1, $2) \parse stmt1
       </varlistentry>
 
       <varlistentry id="app-psql-meta-command-conninfo">
-        <term><literal>\conninfo</literal></term>
+        <term><literal>\conninfo[+]</literal></term>
         <listitem>
-        <para>
-        Outputs information about the current database connection.
-        </para>
+          <para>
+            Outputs a string displaying information about the current
+            database connection. When <literal>+</literal> is appended,
+            more details about the connection are displayed in table
+            format:
+            <simplelist>
+              <member><literal>Protocol Version:</literal> The version of the PostgreSQL protocol used for
+                this connection.</member>
+              <member><literal>SSL Connection:</literal> True if the current connection to the server
+                uses SSL, and false otherwise.</member>
+              <member><literal>GSSAPI Authenticated:</literal> True if GSSAPI is in use, or false if
+                GSSAPI is not in use on this connection.</member>
+              <member><literal>Client Encoding:</literal> The encoding used by the client for this connection.</member>
+              <member><literal>Server Encoding:</literal> The encoding used by the server for this connection.</member>
+              <member><literal>Session User:</literal> The session user's name;
+                see the <function>session_user()</function> function in
+                <xref linkend="functions-info-session-table"/> for more details.</member>
+              <member><literal>Backend PID:</literal> The process ID of the backend for the
+                connection.</member>
+            </simplelist>
+          </para>
         </listitem>
       </varlistentry>
 
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 4dfc7b2d85..59a2465ec3 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -72,7 +72,8 @@ static backslashResult exec_command_cd(PsqlScanState scan_state, bool active_bra
 									   const char *cmd);
 static backslashResult exec_command_close(PsqlScanState scan_state, bool active_branch,
 										  const char *cmd);
-static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch);
+static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch,
+											 const char *cmd);
 static backslashResult exec_command_copy(PsqlScanState scan_state, bool active_branch);
 static backslashResult exec_command_copyright(PsqlScanState scan_state, bool active_branch);
 static backslashResult exec_command_crosstabview(PsqlScanState scan_state, bool active_branch);
@@ -328,8 +329,8 @@ exec_command(const char *cmd,
 		status = exec_command_cd(scan_state, active_branch, cmd);
 	else if (strcmp(cmd, "close") == 0)
 		status = exec_command_close(scan_state, active_branch, cmd);
-	else if (strcmp(cmd, "conninfo") == 0)
-		status = exec_command_conninfo(scan_state, active_branch);
+	else if (strcmp(cmd, "conninfo") == 0 || strcmp(cmd, "conninfo+") == 0)
+		status = exec_command_conninfo(scan_state, active_branch, cmd);
 	else if (pg_strcasecmp(cmd, "copy") == 0)
 		status = exec_command_copy(scan_state, active_branch);
 	else if (strcmp(cmd, "copyright") == 0)
@@ -739,11 +740,14 @@ exec_command_close(PsqlScanState scan_state, bool active_branch, const char *cmd
 }
 
 /*
- * \conninfo -- display information about the current connection
+ * \conninfo, \conninfo+ -- display information about the current connection
  */
 static backslashResult
-exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
+exec_command_conninfo(PsqlScanState scan_state, bool active_branch, const char *cmd)
 {
+	bool show_verbose;
+    show_verbose = strchr(cmd, '+') ? true : false;
+
 	if (active_branch)
 	{
 		char	   *db = PQdb(pset.db);
@@ -774,6 +778,25 @@ exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
 					printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
 						   db, PQuser(pset.db), host, PQport(pset.db));
 			}
+			/* Print some additional information about the connection */
+			if (show_verbose)
+			{
+				int protocol_version = PQprotocolVersion(pset.db);
+				int ssl_in_use = PQsslInUse(pset.db);
+				int gssapi_used = PQconnectionUsedGSSAPI(pset.db);
+				const char *client_encoding = PQparameterStatus(pset.db, "client_encoding");
+				const char *server_encoding = PQparameterStatus(pset.db, "server_encoding");
+				const char* session_user = PQparameterStatus(pset.db, "session_authorization");
+				int backend_pid = PQbackendPID(pset.db);
+				
+				printf(_("Protocol Version: %d\n"), protocol_version);
+				printf(_("SSL Connection: %s\n"), ssl_in_use ? _("yes") : _("no"));
+				printf(_("GSSAPI Authenticated: %s\n"), gssapi_used ? _("yes") : _("no"));
+				printf(_("Client Encoding: %s\n"), client_encoding ? client_encoding : _("(none)"));
+				printf(_("Server Encoding: %s\n"), server_encoding ? server_encoding : _("(none)"));
+				printf(_("Session User: %s\n"), session_user ? session_user : _("(none)"));
+				printf(_("Backend PID: %d\n"), backend_pid);
+			}
 			printSSLInfo();
 			printGSSInfo();
 		}
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 19d20c5878..ce6d6ae184 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -313,7 +313,7 @@ slashUsage(unsigned short int pager)
 	else
 		HELP0("  \\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
 			  "                         connect to new database (currently no connection)\n");
-	HELP0("  \\conninfo              display information about current connection\n");
+	HELP0("  \\conninfo[+]           display information about current connection\n");
 	HELP0("  \\encoding [ENCODING]   show or set client encoding\n");
 	HELP0("  \\parse STMT_NAME       create a prepared statement\n");
 	HELP0("  \\password [USERNAME]   securely change the password for a user\n");
-- 
2.34.1