public inbox for [email protected]  
help / color / mirror / Atom feed
[PATCH 03/18] pg_restore: "must be specified" and --list
5+ messages / 4 participants
[nested] [flat]

* [PATCH 03/18] pg_restore: "must be specified" and --list
@ 2020-12-06 04:43 Justin Pryzby <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Justin Pryzby @ 2020-12-06 04:43 UTC (permalink / raw)

This was discussed here, but the idea got lost.
https://www.postgresql.org/message-id/flat/20190612170201.GA11881%40alvherre.pgsql#2984347ab074e6f19...
---
 src/bin/pg_dump/pg_restore.c   | 2 +-
 src/bin/pg_dump/t/001_basic.pl | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 589b4aed53..f6e6e41329 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -305,7 +305,7 @@ main(int argc, char **argv)
 	/* Complain if neither -f nor -d was specified (except if dumping TOC) */
 	if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
 	{
-		pg_log_error("one of -d/--dbname and -f/--file must be specified");
+		pg_log_error("one of -d/--dbname, -f/--file, or -l/--list must be specified");
 		exit_nicely(1);
 	}
 
diff --git a/src/bin/pg_dump/t/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl
index 083fb3ad08..8280914c2a 100644
--- a/src/bin/pg_dump/t/001_basic.pl
+++ b/src/bin/pg_dump/t/001_basic.pl
@@ -63,8 +63,8 @@ command_fails_like(
 
 command_fails_like(
 	['pg_restore'],
-	qr{\Qpg_restore: error: one of -d/--dbname and -f/--file must be specified\E},
-	'pg_restore: error: one of -d/--dbname and -f/--file must be specified');
+	qr{\Qpg_restore: error: one of -d/--dbname, -f/--file, or -l/--list must be specified\E},
+	'pg_restore: error: one of -d/--dbname, -f/--file, or -l/--list must be specified');
 
 command_fails_like(
 	[ 'pg_restore', '-s', '-a', '-f -' ],
-- 
2.17.0


--lc9FT7cWel8HagAv
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment; filename="0004-typos-in-master.patch"



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Psql meta-command conninfo+
@ 2024-02-06 17:27 Maiquel Grassi <[email protected]>
  2024-02-06 18:19 ` Re: Psql meta-command conninfo+ Nathan Bossart <[email protected]>
  0 siblings, 1 reply; 5+ messages in thread

From: Maiquel Grassi @ 2024-02-06 17:27 UTC (permalink / raw)
  To: pgsql-hackers

Hi,


I'm seeking to improve the \conninfo meta-command in psql. Currently, it provides limited information about the current connection. I believe that expanding it using the concept of "plus" [+] could ease the work of DBAs, SysAdmins, DevOps, etc., who manage a large volume of databases and/or multiple PostgreSQL servers. The objective of this enhancement is to obtain quick information about the current connection (session). I believe that for a PostgreSQL administrator, it is not feasible to write a plpgsql function and apply it to all databases (for example, imagine managing over 200 databases). I have an example on GitHub https://github.com/maiquelgrassi/DBA-toolkit/blob/main/cluster/dba_whoami_function.sql of a plpgsql function demonstrating exactly what I believe is impractical for the daily routine of a PostgreSQL professional. I see psql's meta-commands as significant allies in daily work in productive environments.


Note: As this is a prototype, I will adjust the rest (documentation, tests, etc.) once an agreement is reached.

Use cases for both the current and improved command bellow.

Connection 1 ("remote server"):

[postgres@localhost bin]$ ./psql -h 192.168.0.5 -p 5433 -U postgres -d postgres

psql (17devel, server 16.1)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "192.168.0.5" at port "5433".
postgres=# \conninfo+
 Current Connection Information
   Attribute    |     Value
----------------+----------------
 Database       | postgres
 User           | postgres
 Server Version | 16.1
 Server Address | 192.168.0.5/32
 Server Port    | 5433
 Client Address | 192.168.0.5/32
 Client Port    | 52716
 Session PID    | 21624
(8 rows)


Connection 2 (socket):

[postgres@localhost bin]$ ./psql

psql (17devel)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432".
postgres=# \conninfo+
      Current Connection Information
    Attribute     |         Value
------------------+-----------------------
 Info             | Connected via socket!
 Database         | postgres
 User             | postgres
 Socket Directory | /tmp
 Server Version   | 17devel
 Server Port      | 5432
 Session PID      | 27586
(7 rows)

Connection 3 (localhost):
[postgres@localhost bin]$ ./psql -h localhost
psql (17devel)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "localhost" (address "::1") at port "5432".
postgres=# \conninfo+
Current Connection Information
   Attribute    |   Value
----------------+-----------
 Database       | postgres
 User           | postgres
 Host           | localhost
 Server Version | 17devel
 Server Address | ::1/128
 Server Port    | 5432
 Client Address | ::1/128
 Client Port    | 46824
 Session PID    | 27598
(9 rows)

Connection 4 (127.0.0.1):
[postgres@localhost bin]$ ./psql -h 127.0.0.1
psql (17devel)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "127.0.0.1" at port "5432".
postgres=# \conninfo+
Current Connection Information
   Attribute    |    Value
----------------+--------------
 Database       | postgres
 User           | postgres
 Server Version | 17devel
 Server Address | 127.0.0.1/32
 Server Port    | 5432
 Client Address | 127.0.0.1/32
 Client Port    | 34876
 Session PID    | 27624
(8 rows)

Regards,
Maiquel O. Grassi.


Attachments:

  [application/octet-stream] v1-0001-psql-meta-command-conninfo-plus.patch (11.3K, ../../CP8P284MB24965CB63DAC00FC0EA4A475EC462@CP8P284MB2496.BRAP284.PROD.OUTLOOK.COM/3-v1-0001-psql-meta-command-conninfo-plus.patch)
  download | inline diff:
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 5c906e4..ad8e370 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);
@@ -317,8 +318,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)
@@ -643,47 +644,74 @@ 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		success = true;
+
 	if (active_branch)
 	{
 		char	   *db = PQdb(pset.db);
+		char	   *pattern;
+		bool		show_verbose;
 
-		if (db == NULL)
-			printf(_("You are currently not connected to a database.\n"));
-		else
+		pattern = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
+
+		show_verbose = strchr(cmd, '+') ? true : false;
+
+		/*
+		 * \conninfo+
+		 */
+		if (show_verbose)
 		{
-			char	   *host = PQhost(pset.db);
-			char	   *hostaddr = PQhostaddr(pset.db);
+			success = listConnectionInformation(pattern, show_verbose);
+			free(pattern);
+			printSSLInfo();
+			printGSSInfo();
+		}
 
-			if (is_unixsock_path(host))
-			{
-				/* hostaddr overrides host */
-				if (hostaddr && *hostaddr)
-					printf(_("You are connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n"),
-						   db, PQuser(pset.db), hostaddr, PQport(pset.db));
-				else
-					printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
-						   db, PQuser(pset.db), host, PQport(pset.db));
-			}
+		/*
+		 * \conninfo
+		 */
+		else
+		{
+			if (db == NULL)
+				printf(_("You are currently not connected to a database.\n"));
 			else
 			{
-				if (hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
-					printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n"),
-						   db, PQuser(pset.db), host, hostaddr, PQport(pset.db));
+				char	   *host = PQhost(pset.db);
+				char	   *hostaddr = PQhostaddr(pset.db);
+
+				if (is_unixsock_path(host))
+				{
+					/* hostaddr overrides host */
+					if (hostaddr && *hostaddr)
+						printf(_("You are connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n"),
+							   db, PQuser(pset.db), hostaddr, PQport(pset.db));
+					else
+						printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
+							   db, PQuser(pset.db), host, PQport(pset.db));
+				}
 				else
-					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));
+				{
+					if (hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
+						printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n"),
+							   db, PQuser(pset.db), host, hostaddr, PQport(pset.db));
+					else
+						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));
+				}
+				printSSLInfo();
+				printGSSInfo();
 			}
-			printSSLInfo();
-			printGSSInfo();
 		}
 	}
+	else
+		ignore_slash_options(scan_state);
 
-	return PSQL_CMD_SKIP_LINE;
+	return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
 }
 
 /*
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index b6a4eb1..115376b 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -26,6 +26,7 @@
 #include "fe_utils/mbprint.h"
 #include "fe_utils/print.h"
 #include "fe_utils/string_utils.h"
+#include "libpq/pqcomm.h"
 #include "settings.h"
 #include "variables.h"
 
@@ -901,6 +902,120 @@ error_return:
 	return false;
 }
 
+/*
+ * listConnectionInformation
+ *
+ * for \conninfo+
+ */
+bool
+listConnectionInformation(const char *pattern, bool verbose)
+{
+    PGresult   *res;
+    PQExpBufferData buf;
+    printQueryOpt myopt = pset.popt;
+
+    char       *host = PQhost(pset.db);
+    char       *hostaddr = PQhostaddr(pset.db);
+
+    initPQExpBuffer(&buf);
+
+    if (is_unixsock_path(host))
+    {
+        /* hostaddr overrides host */
+        if (hostaddr && *hostaddr)
+        {
+            printfPQExpBuffer(&buf,
+                              "SELECT\n"
+                              "\"Attribute\",\n"
+                              "\"Value\"\n"
+                              "FROM\n"
+                              "(\n"
+                              "VALUES\n"
+                              "('Database',pg_catalog.current_database()),\n"
+                              "('User',pg_catalog.current_user()),\n"
+                              "('Server Version',pg_catalog.current_setting('server_version')),\n"
+                              "('Server Address',pg_catalog.inet_server_addr()::text),\n"
+                              "('Server Port',pg_catalog.current_setting('port')),\n"
+                              "('Client Address',pg_catalog.inet_client_addr()::text),\n"
+                              "('Client Port',pg_catalog.inet_client_port()::text),\n"
+                              "('Session PID',pg_catalog.pg_backend_pid()::text)\n"
+                              ") AS tab(\"Attribute\",\"Value\");");
+        }
+        else
+        {
+            printfPQExpBuffer(&buf,
+                              "SELECT\n"
+                              "\"Attribute\",\n"
+                              "\"Value\"\n"
+                              "FROM\n"
+                              "(\n"
+                              "VALUES\n"
+                              "('Info','Connected via socket!'),\n"
+                              "('Database',pg_catalog.current_database()),\n"
+                              "('User',pg_catalog.current_user()),\n"
+                              "('Socket Directory',pg_catalog.current_setting('unix_socket_directories')),\n"
+                              "('Server Version',pg_catalog.current_setting('server_version')),\n"
+                              "('Server Port',pg_catalog.current_setting('port')),\n"
+                              "('Session PID',pg_catalog.pg_backend_pid()::text)\n"
+                              ") AS tab(\"Attribute\",\"Value\");");
+        }
+    }
+    else
+    {
+        if (hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
+        {
+            printfPQExpBuffer(&buf,
+                              "SELECT\n"
+                              "\"Attribute\",\n"
+                              "\"Value\"\n"
+                              "FROM\n"
+                              "(\n"
+                              "VALUES\n"
+                              "('Database',pg_catalog.current_database()),\n"
+                              "('User',pg_catalog.current_user()),\n"
+                              "('Host','localhost'),\n"
+                              "('Server Version',pg_catalog.current_setting('server_version')),\n"
+                              "('Server Address',pg_catalog.inet_server_addr()::text),\n"
+                              "('Server Port',pg_catalog.current_setting('port')),\n"
+                              "('Client Address',pg_catalog.inet_client_addr()::text),\n"
+                              "('Client Port',pg_catalog.inet_client_port()::text),\n"
+                              "('Session PID',pg_catalog.pg_backend_pid()::text)\n"
+                              ") AS tab(\"Attribute\",\"Value\");");
+        }
+        else
+        {
+            printfPQExpBuffer(&buf,
+                              "SELECT\n"
+                              "\"Attribute\",\n"
+                              "\"Value\"\n"
+                              "FROM\n"
+                              "(\n"
+                              "VALUES\n"
+                              "('Database',pg_catalog.current_database()),\n"
+                              "('User',pg_catalog.current_user()),\n"
+                              "('Server Version',pg_catalog.current_setting('server_version')),\n"
+                              "('Server Address',pg_catalog.inet_server_addr()::text),\n"
+                              "('Server Port',pg_catalog.current_setting('port')),\n"
+                              "('Client Address',pg_catalog.inet_client_addr()::text),\n"
+                              "('Client Port',pg_catalog.inet_client_port()::text),\n"
+                              "('Session PID',pg_catalog.pg_backend_pid()::text)\n"
+                              ") AS tab(\"Attribute\",\"Value\");");
+        }
+    }
+
+    res = PSQLexec(buf.data);
+    termPQExpBuffer(&buf);
+    if (!res)
+        return false;
+
+    myopt.title = _("Current Connection Information");
+    myopt.translate_header = true;
+
+    printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
+
+    PQclear(res);
+    return true;
+}
 
 /*
  * listAllDbs
diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h
index 273f974..b638c71 100644
--- a/src/bin/psql/describe.h
+++ b/src/bin/psql/describe.h
@@ -149,4 +149,7 @@ extern bool listOpFamilyFunctions(const char *access_method_pattern,
 /* \dl or \lo_list */
 extern bool listLargeObjects(bool verbose);
 
+/* \conninfo */
+extern bool listConnectionInformation(const char *pattern, bool verbose);
+
 #endif							/* DESCRIBE_H */
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 4e79a81..2c5426d 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");


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Psql meta-command conninfo+
  2024-02-06 17:27 Psql meta-command conninfo+ Maiquel Grassi <[email protected]>
@ 2024-02-06 18:19 ` Nathan Bossart <[email protected]>
  2024-02-06 19:10   ` Re: Psql meta-command conninfo+ Erik Wienhold <[email protected]>
  0 siblings, 1 reply; 5+ messages in thread

From: Nathan Bossart @ 2024-02-06 18:19 UTC (permalink / raw)
  To: Maiquel Grassi <[email protected]>; +Cc: pgsql-hackers

On Tue, Feb 06, 2024 at 05:27:01PM +0000, Maiquel Grassi wrote:
> I'm seeking to improve the \conninfo meta-command in psql. Currently, it
> provides limited information about the current connection. I believe that
> expanding it using the concept of "plus" [+] could ease the work of DBAs,
> SysAdmins, DevOps, etc., who manage a large volume of databases and/or
> multiple PostgreSQL servers. The objective of this enhancement is to
> obtain quick information about the current connection (session). I
> believe that for a PostgreSQL administrator, it is not feasible to write
> a plpgsql function and apply it to all databases (for example, imagine
> managing over 200 databases). I have an example on GitHub
> https://github.com/maiquelgrassi/DBA-toolkit/blob/main/cluster/dba_whoami_function.sql
> of a plpgsql function demonstrating exactly what I believe is impractical
> for the daily routine of a PostgreSQL professional. I see psql's
> meta-commands as significant allies in daily work in productive
> environments.

This seems like a reasonable idea to me.

> postgres=# \conninfo+
>  Current Connection Information
>    Attribute    |     Value
> ----------------+----------------
>  Database       | postgres
>  User           | postgres
>  Server Version | 16.1
>  Server Address | 192.168.0.5/32
>  Server Port    | 5433
>  Client Address | 192.168.0.5/32
>  Client Port    | 52716
>  Session PID    | 21624
> (8 rows)

My first reaction is that this should instead return a single row with the
same set of columns for any connection type (the not-applicable ones would
just be set to NULL).  That would match the other meta-commands like \l and
\du, and you could still get an expanded display with \x if needed.  Also,
I think it would simplify the code a bit.

-- 
Nathan Bossart
Amazon Web Services: https://aws.amazon.com






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Psql meta-command conninfo+
  2024-02-06 17:27 Psql meta-command conninfo+ Maiquel Grassi <[email protected]>
  2024-02-06 18:19 ` Re: Psql meta-command conninfo+ Nathan Bossart <[email protected]>
@ 2024-02-06 19:10   ` Erik Wienhold <[email protected]>
  2024-02-06 20:52     ` RE: Psql meta-command conninfo+ Maiquel Grassi <[email protected]>
  0 siblings, 1 reply; 5+ messages in thread

From: Erik Wienhold @ 2024-02-06 19:10 UTC (permalink / raw)
  To: Nathan Bossart <[email protected]>; +Cc: Maiquel Grassi <[email protected]>; pgsql-hackers

On 2024-02-06 19:19 +0100, Nathan Bossart wrote:
> On Tue, Feb 06, 2024 at 05:27:01PM +0000, Maiquel Grassi wrote:
> > postgres=# \conninfo+
> >  Current Connection Information
> >    Attribute    |     Value
> > ----------------+----------------
> >  Database       | postgres
> >  User           | postgres
> >  Server Version | 16.1
> >  Server Address | 192.168.0.5/32
> >  Server Port    | 5433
> >  Client Address | 192.168.0.5/32
> >  Client Port    | 52716
> >  Session PID    | 21624
> > (8 rows)
> 
> My first reaction is that this should instead return a single row with the
> same set of columns for any connection type (the not-applicable ones would
> just be set to NULL).  That would match the other meta-commands like \l and
> \du, and you could still get an expanded display with \x if needed.  Also,
> I think it would simplify the code a bit.

+1 for a single-row result and triggering expanded display with \x for
consistency with other commands.

-- 
Erik






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* RE: Psql meta-command conninfo+
  2024-02-06 17:27 Psql meta-command conninfo+ Maiquel Grassi <[email protected]>
  2024-02-06 18:19 ` Re: Psql meta-command conninfo+ Nathan Bossart <[email protected]>
  2024-02-06 19:10   ` Re: Psql meta-command conninfo+ Erik Wienhold <[email protected]>
@ 2024-02-06 20:52     ` Maiquel Grassi <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Maiquel Grassi @ 2024-02-06 20:52 UTC (permalink / raw)
  To: Erik Wienhold <[email protected]>; Nathan Bossart <[email protected]>; +Cc: pgsql-hackers

  On 2024-02-06 19:19 +0100, Nathan Bossart wrote:
  > On Tue, Feb 06, 2024 at 05:27:01PM +0000, Maiquel Grassi wrote:
  > > postgres=# \conninfo+
  > >  Current Connection Information
  > >    Attribute    |     Value
  > > ----------------+----------------
  > >  Database       | postgres
  > >  User           | postgres
  > >  Server Version | 16.1
  > >  Server Address | 192.168.0.5/32
  > >  Server Port    | 5433
  > >  Client Address | 192.168.0.5/32
  > >  Client Port    | 52716
  > >  Session PID    | 21624
  > > (8 rows)
  >
  > My first reaction is that this should instead return a single row with the
  > same set of columns for any connection type (the not-applicable ones would
  > just be set to NULL).  That would match the other meta-commands like \l and
  > \du, and you could still get an expanded display with \x if needed.  Also,
  > I think it would simplify the code a bit.

  +1 for a single-row result and triggering expanded display with \x for
  consistency with other commands.

--//--

I made the adjustment in the code and updated the patch. I believe this is the format suggested by you all. Would this be it?

[postgres@localhost bin]$ ./psql -h 192.168.0.220 -p 5433 -U postgres -d postgres
psql (17devel, server 16.1)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "192.168.0.220" at port "5433".
postgres=# \conninfo+
  Current Connection Information
   Attribute    |      Value
----------------+------------------
 Database       | postgres
 User           | postgres
 Server Version | 16.1
 Server Address | 192.168.0.220/32
 Server Port    | 5433
 Client Address | 192.168.0.220/32
 Client Port    | 56606
 Session PID    | 2424
(8 rows)

[postgres@localhost bin]$ ./psql
psql (17devel)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432".
postgres=# \conninfo+
Current Connection Information
   Attribute    |  Value
----------------+----------
 Database       | postgres
 User           | postgres
 Server Version | 17devel
 Server Address |
 Server Port    | 5432
 Client Address |
 Client Port    |
 Session PID    | 30216
(8 rows)

[postgres@localhost bin]$ ./psql -h localhost
psql (17devel)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "localhost" (address "::1") at port "5432".
postgres=# \conninfo+
Current Connection Information
   Attribute    |  Value
----------------+----------
 Database       | postgres
 User           | postgres
 Server Version | 17devel
 Server Address | ::1/128
 Server Port    | 5432
 Client Address | ::1/128
 Client Port    | 46872
 Session PID    | 30220
(8 rows)

[postgres@localhost bin]$ ./psql -h 127.0.0.1
psql (17devel)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "127.0.0.1" at port "5432".
postgres=# \conninfo+
Current Connection Information
   Attribute    |    Value
----------------+--------------
 Database       | postgres
 User           | postgres
 Server Version | 17devel
 Server Address | 127.0.0.1/32
 Server Port    | 5432
 Client Address | 127.0.0.1/32
 Client Port    | 34924
 Session PID    | 30223
(8 rows)

Regards,
Maiquel O. Grassi.


Attachments:

  [application/octet-stream] v2-0001-psql-meta-command-conninfo-plus.patch (7.5K, ../../CP8P284MB2496B51FC5ED9BD6527D459DEC462@CP8P284MB2496.BRAP284.PROD.OUTLOOK.COM/3-v2-0001-psql-meta-command-conninfo-plus.patch)
  download | inline diff:
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 5c906e4..ad8e370 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);
@@ -317,8 +318,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)
@@ -643,47 +644,74 @@ 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		success = true;
+
 	if (active_branch)
 	{
 		char	   *db = PQdb(pset.db);
+		char	   *pattern;
+		bool		show_verbose;
 
-		if (db == NULL)
-			printf(_("You are currently not connected to a database.\n"));
-		else
+		pattern = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
+
+		show_verbose = strchr(cmd, '+') ? true : false;
+
+		/*
+		 * \conninfo+
+		 */
+		if (show_verbose)
 		{
-			char	   *host = PQhost(pset.db);
-			char	   *hostaddr = PQhostaddr(pset.db);
+			success = listConnectionInformation(pattern, show_verbose);
+			free(pattern);
+			printSSLInfo();
+			printGSSInfo();
+		}
 
-			if (is_unixsock_path(host))
-			{
-				/* hostaddr overrides host */
-				if (hostaddr && *hostaddr)
-					printf(_("You are connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n"),
-						   db, PQuser(pset.db), hostaddr, PQport(pset.db));
-				else
-					printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
-						   db, PQuser(pset.db), host, PQport(pset.db));
-			}
+		/*
+		 * \conninfo
+		 */
+		else
+		{
+			if (db == NULL)
+				printf(_("You are currently not connected to a database.\n"));
 			else
 			{
-				if (hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
-					printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n"),
-						   db, PQuser(pset.db), host, hostaddr, PQport(pset.db));
+				char	   *host = PQhost(pset.db);
+				char	   *hostaddr = PQhostaddr(pset.db);
+
+				if (is_unixsock_path(host))
+				{
+					/* hostaddr overrides host */
+					if (hostaddr && *hostaddr)
+						printf(_("You are connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n"),
+							   db, PQuser(pset.db), hostaddr, PQport(pset.db));
+					else
+						printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
+							   db, PQuser(pset.db), host, PQport(pset.db));
+				}
 				else
-					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));
+				{
+					if (hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
+						printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n"),
+							   db, PQuser(pset.db), host, hostaddr, PQport(pset.db));
+					else
+						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));
+				}
+				printSSLInfo();
+				printGSSInfo();
 			}
-			printSSLInfo();
-			printGSSInfo();
 		}
 	}
+	else
+		ignore_slash_options(scan_state);
 
-	return PSQL_CMD_SKIP_LINE;
+	return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
 }
 
 /*
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index b6a4eb1..a84da54 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -901,6 +901,50 @@ error_return:
 	return false;
 }
 
+/*
+ * listConnectionInformation
+ *
+ * for \conninfo+
+ */
+bool
+listConnectionInformation(const char *pattern, bool verbose)
+{
+    PGresult   *res;
+    PQExpBufferData buf;
+    printQueryOpt myopt = pset.popt;
+
+    initPQExpBuffer(&buf);
+
+    printfPQExpBuffer(&buf,
+                      "SELECT\n"
+                      "\"Attribute\",\n"
+                      "\"Value\"\n"
+                      "FROM\n"
+                      "(\n"
+                      "VALUES\n"
+                      "('Database',pg_catalog.current_database()),\n"
+                      "('User',pg_catalog.current_user()),\n"
+                      "('Server Version',pg_catalog.current_setting('server_version')),\n"
+                      "('Server Address',NULLIF(pg_catalog.inet_server_addr()::text,'')),\n"
+                      "('Server Port',pg_catalog.current_setting('port')),\n"
+                      "('Client Address',NULLIF(pg_catalog.inet_client_addr()::text,'')),\n"
+                      "('Client Port',NULLIF(pg_catalog.inet_client_port()::text,'')),\n"
+                      "('Session PID',pg_catalog.pg_backend_pid()::text)\n"
+                      ") AS tab(\"Attribute\",\"Value\");");
+
+    res = PSQLexec(buf.data);
+    termPQExpBuffer(&buf);
+    if (!res)
+        return false;
+
+    myopt.title = _("Current Connection Information");
+    myopt.translate_header = true;
+
+    printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
+
+    PQclear(res);
+    return true;
+}
 
 /*
  * listAllDbs
diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h
index 273f974..b638c71 100644
--- a/src/bin/psql/describe.h
+++ b/src/bin/psql/describe.h
@@ -149,4 +149,7 @@ extern bool listOpFamilyFunctions(const char *access_method_pattern,
 /* \dl or \lo_list */
 extern bool listLargeObjects(bool verbose);
 
+/* \conninfo */
+extern bool listConnectionInformation(const char *pattern, bool verbose);
+
 #endif							/* DESCRIBE_H */
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 4e79a81..2c5426d 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");


^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2024-02-06 20:52 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-12-06 04:43 [PATCH 03/18] pg_restore: "must be specified" and --list Justin Pryzby <[email protected]>
2024-02-06 17:27 Psql meta-command conninfo+ Maiquel Grassi <[email protected]>
2024-02-06 18:19 ` Re: Psql meta-command conninfo+ Nathan Bossart <[email protected]>
2024-02-06 19:10   ` Re: Psql meta-command conninfo+ Erik Wienhold <[email protected]>
2024-02-06 20:52     ` RE: Psql meta-command conninfo+ Maiquel Grassi <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox