Skip to content

Commit d2870eb

Browse files
alvherreCommitfest Bot
authored andcommitted
Change \conninfo to use tabular format
Also display more fields than before. Author: Maiquel Grassi <[email protected]> Author: Hunaid Sohail <[email protected]> Discussion: https://postgr.es/m/CP8P284MB24965CB63DAC00FC0EA4A475EC462@CP8P284MB2496.BRAP284.PROD.OUTLOOK.COM
1 parent dfd8e6c commit d2870eb

File tree

2 files changed

+162
-33
lines changed

2 files changed

+162
-33
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,9 +1070,10 @@ INSERT INTO tbls1 VALUES ($1, $2) \parse stmt1
10701070
<varlistentry id="app-psql-meta-command-conninfo">
10711071
<term><literal>\conninfo</literal></term>
10721072
<listitem>
1073-
<para>
1074-
Outputs information about the current database connection.
1075-
</para>
1073+
<para>
1074+
Outputs information about the current database connection,
1075+
including TLS-related information if TLS is in use.
1076+
</para>
10761077
</listitem>
10771078
</varlistentry>
10781079

src/bin/psql/command.c

Lines changed: 158 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -763,41 +763,169 @@ exec_command_close(PsqlScanState scan_state, bool active_branch, const char *cmd
763763
static backslashResult
764764
exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
765765
{
766-
if (active_branch)
767-
{
768-
char *db = PQdb(pset.db);
769-
770-
if (db == NULL)
771-
printf(_("You are currently not connected to a database.\n"));
766+
printTableContent cont;
767+
int rows,
768+
cols;
769+
char *db;
770+
char *host;
771+
bool print_hostaddr;
772+
char *hostaddr;
773+
char *protocol_version,
774+
*backend_pid;
775+
int ssl_in_use,
776+
password_used,
777+
gssapi_used;
778+
char *paramval;
779+
780+
if (!active_branch)
781+
return PSQL_CMD_SKIP_LINE;
782+
783+
db = PQdb(pset.db);
784+
if (db == NULL)
785+
{
786+
printf(_("You are currently not connected to a database.\n"));
787+
return PSQL_CMD_SKIP_LINE;
788+
}
789+
790+
/* Get values for the parameters */
791+
host = PQhost(pset.db);
792+
hostaddr = PQhostaddr(pset.db);
793+
protocol_version = psprintf("%d", PQprotocolVersion(pset.db));
794+
ssl_in_use = PQsslInUse(pset.db);
795+
password_used = PQconnectionUsedPassword(pset.db);
796+
gssapi_used = PQconnectionUsedGSSAPI(pset.db);
797+
backend_pid = psprintf("%d", PQbackendPID(pset.db));
798+
799+
/* Only print hostaddr if it differs from host, and not if unixsock */
800+
print_hostaddr = (!is_unixsock_path(host) &&
801+
hostaddr && *hostaddr && strcmp(host, hostaddr) != 0);
802+
803+
/* Determine the exact number of rows to print */
804+
rows = 12;
805+
cols = 2;
806+
if (ssl_in_use)
807+
rows += 6;
808+
if (print_hostaddr)
809+
rows++;
810+
811+
/* Set it all up */
812+
printTableInit(&cont, &pset.popt.topt, _("Connection Information"), cols, rows);
813+
printTableAddHeader(&cont, _("Parameter"), true, 'l');
814+
printTableAddHeader(&cont, _("Value"), true, 'l');
815+
816+
/* Database */
817+
printTableAddCell(&cont, _("Database"), false, false);
818+
printTableAddCell(&cont, db, false, false);
819+
820+
/* Client User */
821+
printTableAddCell(&cont, _("Client User"), false, false);
822+
printTableAddCell(&cont, PQuser(pset.db), false, false);
823+
824+
/* Host/hostaddr/socket */
825+
if (is_unixsock_path(host))
826+
{
827+
/* hostaddr if specified overrides socket, so suppress the latter */
828+
if (hostaddr && *hostaddr)
829+
{
830+
printTableAddCell(&cont, _("Host Address"), false, false);
831+
printTableAddCell(&cont, hostaddr, false, false);
832+
}
772833
else
773834
{
774-
char *host = PQhost(pset.db);
775-
char *hostaddr = PQhostaddr(pset.db);
776-
777-
if (is_unixsock_path(host))
778-
{
779-
/* hostaddr overrides host */
780-
if (hostaddr && *hostaddr)
781-
printf(_("You are connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n"),
782-
db, PQuser(pset.db), hostaddr, PQport(pset.db));
783-
else
784-
printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
785-
db, PQuser(pset.db), host, PQport(pset.db));
786-
}
787-
else
788-
{
789-
if (hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
790-
printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n"),
791-
db, PQuser(pset.db), host, hostaddr, PQport(pset.db));
792-
else
793-
printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
794-
db, PQuser(pset.db), host, PQport(pset.db));
795-
}
796-
printSSLInfo();
797-
printGSSInfo();
835+
printTableAddCell(&cont, _("Socket Directory"), false, false);
836+
printTableAddCell(&cont, host, false, false);
837+
}
838+
}
839+
else
840+
{
841+
printTableAddCell(&cont, _("Host"), false, false);
842+
printTableAddCell(&cont, host, false, false);
843+
if (print_hostaddr)
844+
{
845+
printTableAddCell(&cont, _("Host Address"), false, false);
846+
printTableAddCell(&cont, hostaddr, false, false);
798847
}
799848
}
800849

850+
/* Port */
851+
printTableAddCell(&cont, _("Port"), false, false);
852+
printTableAddCell(&cont, PQport(pset.db), false, false);
853+
854+
/* Options */
855+
printTableAddCell(&cont, _("Options"), false, false);
856+
printTableAddCell(&cont, PQoptions(pset.db), false, false);
857+
858+
/* Protocol Version */
859+
printTableAddCell(&cont, _("Protocol Version"), false, false);
860+
printTableAddCell(&cont, protocol_version, false, false);
861+
862+
/* Password Used */
863+
printTableAddCell(&cont, _("Password Used?"), false, false);
864+
printTableAddCell(&cont, password_used ? _("true") : _("false"), false, false);
865+
866+
/* GSSAPI Authenticated */
867+
printTableAddCell(&cont, _("GSSAPI Authenticated?"), false, false);
868+
printTableAddCell(&cont, gssapi_used ? _("true") : _("false"), false, false);
869+
870+
/* Backend PID */
871+
printTableAddCell(&cont, _("Backend PID"), false, false);
872+
printTableAddCell(&cont, backend_pid, false, false);
873+
874+
/* TLS Connection */
875+
printTableAddCell(&cont, _("TLS Connection?"), false, false);
876+
printTableAddCell(&cont, ssl_in_use ? _("true") : _("false"), false, false);
877+
878+
/* TLS Information */
879+
if (ssl_in_use)
880+
{
881+
char *library,
882+
*protocol,
883+
*key_bits,
884+
*cipher,
885+
*compression,
886+
*alpn;
887+
888+
library = (char *) PQsslAttribute(pset.db, "library");
889+
protocol = (char *) PQsslAttribute(pset.db, "protocol");
890+
key_bits = (char *) PQsslAttribute(pset.db, "key_bits");
891+
cipher = (char *) PQsslAttribute(pset.db, "cipher");
892+
compression = (char *) PQsslAttribute(pset.db, "compression");
893+
alpn = (char *) PQsslAttribute(pset.db, "alpn");
894+
895+
printTableAddCell(&cont, _("TLS Library"), false, false);
896+
printTableAddCell(&cont, library ? library : _("unknown"), false, false);
897+
898+
printTableAddCell(&cont, _("TLS Protocol"), false, false);
899+
printTableAddCell(&cont, protocol ? protocol : _("unknown"), false, false);
900+
901+
printTableAddCell(&cont, _("TLS Key Bits"), false, false);
902+
printTableAddCell(&cont, key_bits ? key_bits : _("unknown"), false, false);
903+
904+
printTableAddCell(&cont, _("TLS Cipher"), false, false);
905+
printTableAddCell(&cont, cipher ? cipher : _("unknown"), false, false);
906+
907+
printTableAddCell(&cont, _("TLS Compression"), false, false);
908+
printTableAddCell(&cont, (compression && strcmp(compression, "off") != 0) ?
909+
_("true") : _("false"), false, false);
910+
911+
printTableAddCell(&cont, _("ALPN"), false, false);
912+
printTableAddCell(&cont, (alpn && alpn[0] != '\0') ? alpn : _("none"), false, false);
913+
}
914+
915+
paramval = (char *) PQparameterStatus(pset.db, "is_superuser");
916+
printTableAddCell(&cont, "Superuser?", false, false);
917+
printTableAddCell(&cont, paramval ? paramval : _("unknown"), false, false);
918+
919+
paramval = (char *) PQparameterStatus(pset.db, "in_hot_standby");
920+
printTableAddCell(&cont, "Hot standby?", false, false);
921+
printTableAddCell(&cont, paramval ? paramval : _("unknown"), false, false);
922+
923+
printTable(&cont, pset.queryFout, false, pset.logfile);
924+
printTableCleanup(&cont);
925+
926+
pfree(protocol_version);
927+
pfree(backend_pid);
928+
801929
return PSQL_CMD_SKIP_LINE;
802930
}
803931

0 commit comments

Comments
 (0)