From b011b1cc780fee4030147070db84dcc62edd10a9 Mon Sep 17 00:00:00 2001
From: Hunaid Sohail <hunaid2000@gmail.com>
Date: Fri, 13 Sep 2024 09:37:10 +0500
Subject: [PATCH v33] Add psql meta command conninfo+

---
 doc/src/sgml/ref/psql-ref.sgml | 26 +++++++++++--
 src/bin/psql/command.c         | 67 +++++++++++++++++++++++++++++++---
 src/bin/psql/help.c            |  2 +-
 3 files changed, 85 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..7ad28287c1 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,59 @@ 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)
+			{
+				printQueryOpt popt = pset.popt;
+				printTableContent cont;
+				char	protocol_version[10];
+				char	backend_pid[10];
+				int		cols,
+						rows;
+				int		ssl_in_use,
+						gssapi_used;
+				char	*client_encoding,
+						*server_encoding,
+						*session_user;
+				char	*headers[] =
+				{
+					gettext_noop("Protocol Version"),
+					gettext_noop("SSL Connection"),
+					gettext_noop("GSSAPI Authenticated"),
+					gettext_noop("Client Encoding"),
+					gettext_noop("Server Encoding"),
+					gettext_noop("Session User"),
+					gettext_noop("Backend PID")
+				};
+
+				/* Get values for the parameters */
+				sprintf(protocol_version, "%d", PQprotocolVersion(pset.db));
+				ssl_in_use = PQsslInUse(pset.db);
+				gssapi_used = PQconnectionUsedGSSAPI(pset.db);
+				client_encoding = PQparameterStatus(pset.db, "client_encoding");
+				server_encoding = PQparameterStatus(pset.db, "server_encoding");
+				session_user = PQparameterStatus(pset.db, "session_authorization");
+				sprintf(backend_pid, "%d", PQbackendPID(pset.db));
+
+				/* Print the information in a table */
+				cols = 7;
+				rows = 1;
+				printTableInit(&cont, &popt.topt, _("Connection Information"), cols, rows);
+
+				for (int i = 0; i < cols; i++)
+					printTableAddHeader(&cont, headers[i], true, 'l');
+
+				printTableAddCell(&cont, protocol_version, false, false);
+				printTableAddCell(&cont, ssl_in_use ? _("yes") : _("no"), false, false);
+				printTableAddCell(&cont, gssapi_used ? _("yes") : _("no"), false, false);
+				printTableAddCell(&cont, client_encoding ? client_encoding : _("(none)"), false, false);
+				printTableAddCell(&cont, server_encoding ? server_encoding : _("(none)"), false, false);
+				printTableAddCell(&cont, session_user ? session_user : _("(none)"), false, false);
+				printTableAddCell(&cont, backend_pid, false, false);
+
+				printTable(&cont, pset.queryFout, false, pset.logfile);
+				printTableCleanup(&cont);
+			}
 			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

