Hi,

I have read the entire thread discussion. I understood the importance of
this enhancement related to /conninfo+ meta command. I really appreciate
the efforts of Maiquel and suggestions made by the reviewers. According to
best of my understanding, libpq API should be used instead of creating
server query for conninfo+ meta command. Building on the patch (v29)
provided by Maiquel, I made changes to retrieve some extra information
related to connection from libpq API.

Extra information includes:
- Protocol Version
- SSL Connection
- GSSAPI Authenticated
- Client Encoding
- Server Encoding
- Session User
- Backend PID

Output of \conninfo+:

   1.

   $ bin/psql --port=5430 postgres -h localhost
   psql (18devel)
   Type "help" for help.

   postgres=# \conninfo+
   You are connected to database "postgres" as user "hunaid" on host
"localhost" (address "127.0.0.1") at port "5430".
   Protocol Version: 3
   SSL Connection: no
   GSSAPI Authenticated: no
   Client Encoding: UTF8
   Server Encoding: UTF8
   Session User: hunaid
   Backend PID: 163816



I have also edited the documentation and added it to the patch. Please let
me know if any changes are required.

Regards,
Hunaid Sohail

On Wed, Jun 5, 2024 at 5:32 PM Maiquel Grassi <gra...@hotmail.com.br> wrote:

> From a quick skim of the latest messages in this thread, it sounds like
> there are a couple of folks who think \conninfo (and consequently
> \conninfo+) should only report values from the libpq API.  I think they
> would prefer server-side information to be provided via a system view or
> maybe an entirely new psql meta-command.
>
> IIUC a new system view would more-or-less just gather information from
> other system views and functions.  A view would be nice because it could be
> used outside psql, but I'm not sure to what extent we are comfortable
> adding system views built on other system views.  Something like
> \sessioninfo (as proposed by Sami) would look more similar to what you have
> in your latest patch, i.e., we'd teach psql about a complicated query for
> gathering all this disparate information.
>
> IMHO a good way to help move this forward is to try implementing it as a
> system view so that we can compare the two approaches.  I've had luck in
> the past with implementing something a couple different ways to help drive
> consensus.
>
> --//--
>
> Great Nathan, we can follow that path of the view. I leave it open for
> anyone in the thread who has been following from the beginning to start the
> development of the view. Even you, if you have the interest and time to do
> it. At this exact moment, I am involved in a "my baby born" project, so I
> am unable to stop to look into this. If someone wants to start, I would
> appreciate it.
> Regards,
> Maiquel Grassi.
>
ÿþdiff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml

index 07419a3b92..fb3d52fb16 100644

--- a/doc/src/sgml/ref/psql-ref.sgml

+++ b/doc/src/sgml/ref/psql-ref.sgml

@@ -1030,11 +1030,29 @@ INSERT INTO tbl1 VALUES ($1, $2) \bind 'first value' 'second value' \g

       </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 180781ecd0..8fd314ed37 100644

--- a/src/bin/psql/command.c

+++ b/src/bin/psql/command.c

@@ -68,7 +68,8 @@ static backslashResult exec_command_C(PsqlScanState scan_state, bool active_bran

 static backslashResult exec_command_connect(PsqlScanState scan_state, bool active_branch);

 static backslashResult exec_command_cd(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);

@@ -318,8 +319,8 @@ exec_command(const char *cmd,

 		status = exec_command_connect(scan_state, active_branch);

 	else if (strcmp(cmd, "cd") == 0)

 		status = exec_command_cd(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)

@@ -644,11 +645,14 @@ exec_command_cd(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);

@@ -679,6 +683,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 6f58a11074..955675e3e1 100644

--- a/src/bin/psql/help.c

+++ b/src/bin/psql/help.c

@@ -310,7 +310,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("  \\password [USERNAME]   securely change the password for a user\n");

 	HELP0("\n");

Reply via email to