v37-0003-Add-psql-meta-command-conninfo.patch
application/octet-stream
Filename: v37-0003-Add-psql-meta-command-conninfo.patch
Type: application/octet-stream
Part: 2
Message:
Re: Psql meta-command conninfo+
Patch
Format: format-patch
Series: patch v37-0003
Subject: Add psql meta command conninfo+
| File | + | − |
|---|---|---|
| doc/src/sgml/ref/psql-ref.sgml | 12 | 4 |
| src/bin/psql/command.c | 197 | 21 |
| src/bin/psql/help.c | 1 | 1 |
From bc999b0b19c687346772dfac0b43ba49c2277144 Mon Sep 17 00:00:00 2001
From: Hunaid2000 <hunaid2000@gmail.com>
Date: Thu, 16 Jan 2025 15:17:53 +0500
Subject: [PATCH v37 3/3] Add psql meta command conninfo+
---
doc/src/sgml/ref/psql-ref.sgml | 16 ++-
src/bin/psql/command.c | 218 +++++++++++++++++++++++++++++----
src/bin/psql/help.c | 2 +-
3 files changed, 210 insertions(+), 26 deletions(-)
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index f3044fac1f..e6d8fb63da 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1068,11 +1068,19 @@ 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 information about the current database connection.
+ When <literal>+</literal> is appended, all details
+ about the connection are displayed in table format.
+ It also shows current parameter settings of the server.
+ For more information, see
+ <xref linkend="libpq-PQparameterStatus"/>.
+ If the connection is not using SSL, SSL-related information
+ (SSL Library, Protocol, Key bits, Cipher, Compression,
+ and ALPN) will not be displayed.
+ </para>
</listitem>
</varlistentry>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 613583145e..7bae75eb3d 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -69,7 +69,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);
@@ -176,6 +177,7 @@ static int count_lines_in_buf(PQExpBuffer buf);
static void print_with_linenumbers(FILE *output, char *lines, bool is_func);
static void minimal_error_message(PGresult *res);
+static void printVerboseConnInfo(char *db, char *host, char *hostaddr);
static void printSSLInfo(void);
static void printGSSInfo(void);
static bool printPsetInfo(const char *param, printQueryOpt *popt);
@@ -325,8 +327,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)
@@ -743,8 +745,10 @@ exec_command_close(PsqlScanState scan_state, bool active_branch, const char *cmd
* \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 = strchr(cmd, '+') ? true : false;
+
if (active_branch)
{
char *db = PQdb(pset.db);
@@ -756,27 +760,37 @@ exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
char *host = PQhost(pset.db);
char *hostaddr = PQhostaddr(pset.db);
- if (is_unixsock_path(host))
+ if (!show_verbose)
{
- /* 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));
+ 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\" via socket in \"%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();
}
+
+ /*
+ * Print additional information about the connection in
+ * tabular format
+ */
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));
- 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();
+ printVerboseConnInfo(db, host, hostaddr);
}
}
@@ -4050,6 +4064,168 @@ connection_warnings(bool in_startup)
}
+/*
+ * printVerboseConnInfo
+ *
+ * Prints extra information about the connection
+ * in tabular form.
+ */
+static void
+printVerboseConnInfo(char *db, char *host, char *hostaddr)
+{
+ printQueryOpt popt = pset.popt;
+ printTableContent cont;
+ char *protocol_version,
+ *backend_pid;
+ int rows,
+ cols;
+ int ssl_in_use,
+ password_used,
+ gssapi_used;
+ char *library,
+ *protocol,
+ *key_bits,
+ *cipher,
+ *compression,
+ *alpn;
+ const char **params;
+ int param_count;
+
+ /* Get values for the parameters */
+ protocol_version = psprintf("%d", PQprotocolVersion(pset.db));
+ ssl_in_use = PQsslInUse(pset.db);
+ library = (char *) PQsslAttribute(pset.db, "library");
+ protocol = (char *) PQsslAttribute(pset.db, "protocol");
+ key_bits = (char *) PQsslAttribute(pset.db, "key_bits");
+ cipher = (char *) PQsslAttribute(pset.db, "cipher");
+ compression = (char *) PQsslAttribute(pset.db, "compression");
+ alpn = (char *) PQsslAttribute(pset.db, "alpn");
+ password_used = PQconnectionUsedPassword(pset.db);
+ gssapi_used = PQconnectionUsedGSSAPI(pset.db);
+ backend_pid = psprintf("%d", PQbackendPID(pset.db));
+ params = PQparameterNames(pset.db, ¶m_count);
+
+ /* Fixed number of rows */
+ rows = 10;
+ cols = 2;
+ /* +1 row if hostaddr is different from host */
+ if (!is_unixsock_path(host) && hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
+ rows++;
+ /* +6 rows if SSL is in use */
+ if (ssl_in_use)
+ rows += 6;
+ /* +param_count for parameter status */
+ rows += param_count;
+
+ /* Print the information in a table */
+ printTableInit(&cont, &popt.topt, _("Connection Information"), cols, rows);
+ printTableAddHeader(&cont, _("Parameter"), true, 'l');
+ printTableAddHeader(&cont, _("Value"), true, 'l');
+
+ /* Database */
+ printTableAddCell(&cont, _("Database"), false, false);
+ printTableAddCell(&cont, db, false, false);
+
+ /* Client User */
+ printTableAddCell(&cont, _("Client User"), false, false);
+ printTableAddCell(&cont, PQuser(pset.db), false, false);
+
+ /* Host/Socket Information */
+ if (is_unixsock_path(host))
+ {
+ if (hostaddr && *hostaddr)
+ {
+ printTableAddCell(&cont, _("Host Address"), false, false);
+ printTableAddCell(&cont, hostaddr, false, false);
+ }
+ else
+ {
+ printTableAddCell(&cont, _("Socket Directory"), false, false);
+ printTableAddCell(&cont, host, false, false);
+ }
+ }
+ else
+ {
+ printTableAddCell(&cont, _("Host"), false, false);
+ printTableAddCell(&cont, host, false, false);
+ if (hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
+ {
+ printTableAddCell(&cont, _("Host Address"), false, false);
+ printTableAddCell(&cont, hostaddr, false, false);
+ }
+ }
+
+ /* Port */
+ printTableAddCell(&cont, _("Port"), false, false);
+ printTableAddCell(&cont, PQport(pset.db), false, false);
+
+ /* Options */
+ printTableAddCell(&cont, _("Options"), false, false);
+ printTableAddCell(&cont, PQoptions(pset.db), false, false);
+
+ /* Protocol Version */
+ printTableAddCell(&cont, _("Protocol Version"), false, false);
+ printTableAddCell(&cont, protocol_version, false, false);
+
+ /* Password Used */
+ printTableAddCell(&cont, _("Password Used"), false, false);
+ printTableAddCell(&cont, password_used ? _("true") : _("false"), false, false);
+
+ /* GSSAPI Authenticated */
+ printTableAddCell(&cont, _("GSSAPI Authenticated"), false, false);
+ printTableAddCell(&cont, gssapi_used ? _("true") : _("false"), false, false);
+
+ /* Backend PID */
+ printTableAddCell(&cont, _("Backend PID"), false, false);
+ printTableAddCell(&cont, backend_pid, false, false);
+
+ /* SSL Connection */
+ printTableAddCell(&cont, _("SSL Connection"), false, false);
+ printTableAddCell(&cont, ssl_in_use ? _("true") : _("false"), false, false);
+
+ /* SSL Information */
+ if (ssl_in_use)
+ {
+ printTableAddCell(&cont, _("SSL Library"), false, false);
+ printTableAddCell(&cont, library ? library : _("unknown"), false, false);
+
+ printTableAddCell(&cont, _("SSL Protocol"), false, false);
+ printTableAddCell(&cont, protocol ? protocol : _("unknown"), false, false);
+
+ printTableAddCell(&cont, _("SSL Key Bits"), false, false);
+ printTableAddCell(&cont, key_bits ? key_bits : _("unknown"), false, false);
+
+ printTableAddCell(&cont, _("SSL Cipher"), false, false);
+ printTableAddCell(&cont, cipher ? cipher : _("unknown"), false, false);
+
+ printTableAddCell(&cont, _("SSL Compression"), false, false);
+ printTableAddCell(&cont, (compression && strcmp(compression, "off") != 0) ? _("on") : _("off"), false, false);
+
+ printTableAddCell(&cont, _("ALPN"), false, false);
+ printTableAddCell(&cont, (alpn && alpn[0] != '\0') ? alpn : _("none"), false, false);
+ }
+
+ /* Parameter status */
+ if (params)
+ {
+ for (int i = 0; i < param_count; i++)
+ {
+ char *value = (char *) PQparameterStatus(pset.db, params[i]);
+
+ printTableAddCell(&cont, (char *) params[i], false, false);
+ printTableAddCell(&cont, value ? value : _("none"), false, false);
+ }
+
+ pfree(params);
+ }
+
+ printTable(&cont, pset.queryFout, false, pset.logfile);
+ printTableCleanup(&cont);
+
+ pfree(protocol_version);
+ pfree(backend_pid);
+}
+
/*
* printSSLInfo
*
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index da8e1ade5d..7177371957 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -311,7 +311,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");
--
2.34.1