Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tlXcQ-00Cqte-6C for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Feb 2025 18:19:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tlXcO-00E0hJ-Qh for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Feb 2025 18:19:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tlXcN-00E0hB-QX for pgsql-hackers@lists.postgresql.org; Fri, 21 Feb 2025 18:19:20 +0000 Received: from fhigh-b6-smtp.messagingengine.com ([202.12.124.157]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tlXcL-0029Fc-1u for pgsql-hackers@postgresql.org; Fri, 21 Feb 2025 18:19:18 +0000 Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfhigh.stl.internal (Postfix) with ESMTP id 0989E2540188; Fri, 21 Feb 2025 13:19:16 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-08.internal (MEProxy); Fri, 21 Feb 2025 13:19:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1740161955; x=1740248355; bh=7 UnKVzkk1U8TiMEmMdq3nFNYyteI4Z22ZN8Rb2yzMpg=; b=evH4JtQdVKdfWA5m/ UQ53grprDpgkLqXpxIopRlgoMDfKYBbopwSpMggpAr97Ox3qFfo/4vTgoQqpwId0 RP7udmNt4BpFc/2fH2SY3GBiHmR0Z8Pos8nfo4gAVO+BXporzObu/goyJf3U443E qsF1yjdVsoytuX0s78nxi7fDQyBAfLNm3PI0kl2vCLFKAFTbvZQNQa3ABMbFaBxH 0GAAJQCS3Wfn29ezU+zh4Me+RmGAwU9s2SEH/gmzKhHrnEsaY6PnyYhVF3sNvNNN +nMkELTMoWOdanaFJTDVIFMESoxI3FTw2TuIYIi1rilC0lo7J4aa77i/MWZWZ/Ra ilHDQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdejtdejfecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpeffhffvvefukfggtggugfgjsehmkeerredttdej necuhfhrohhmpeetlhhvrghrohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvh hhrdhnohdqihhprdhorhhgqeenucggtffrrghtthgvrhhnpeduleekkefgtddttedtkefg uddvieffleetgeejiefhteehkeevfeettdduvdfhueenucffohhmrghinhepvghnthgvrh hprhhishgvuggsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehm rghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrghdpnhgspg hrtghpthhtohepudefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehsihhmshgv ihhhsegrmhgriihonhdrtghomhdprhgtphhtthhopehpvghtvghrsegvihhsvghnthhrrg huthdrohhrghdprhgtphhtthhopegvfihivgesvgifihgvrdhnrghmvgdprhgtphhtthho pegurghvihgurdhgrdhjohhhnhhsthhonhesghhmrghilhdrtghomhdprhgtphhtthhope guvggrnhdrrgdrrhgrshhhvggvugesghhmrghilhdrtghomhdprhgtphhtthhopehhuhhn rghiughpghhmlhesghhmrghilhdrtghomhdprhgtphhtthhopehnrghthhgrnhgusghosh hsrghrthesghhmrghilhdrtghomhdprhgtphhtthhopehsrghmihhmshgvihhhsehgmhgr ihhlrdgtohhmpdhrtghpthhtohepghhrrghsshhisehhohhtmhgrihhlrdgtohhmrdgsrh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 21 Feb 2025 13:19:13 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1740161950; bh=jK/qRddEw/PvWBn1/0T9hCEeUGVEdzjA4V+BkoWJgAg=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=m7cn64EVWzvuV8cx5NWHtj8ps3peYgPyaFFjJkDPfONWUewnX3fh1sTd1/rIgULFb kxhQHBEE79OJGiOLADBSqlhzpxwumgDIhnjHcougVfJOASE9QI+FV+c7usj5wcGt6z 56IeUU87+r1q6mumkYJjItWCWOnsToPYuGu93k+qORSXdQNndM128M+IaNqO3/dp4+ zIKnxLVH5AjT5+o4oFDqFZeqvymzKWn25MUwDqD5laAGxpabYbJhoLFWu+lh0QXOTh klZyyn5HNbW7fGuF3mByDWt1ulT2MYTuiXyk0TpOAi11RofEgJ+Vk9ZmTAngpV+ge3 jA9O+icyfdrfA== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 3CEC48F; Fri, 21 Feb 2025 19:19:10 +0100 (CET) Date: Fri, 21 Feb 2025 19:19:10 +0100 From: Alvaro Herrera To: Hunaid Sohail Cc: Maiquel Grassi , Sami Imseih , "pgsql-hackers@postgresql.org" , "David G. Johnston" , Jim Jones , Tom Lane , Nathan Bossart , "Imseih (AWS), Sami" , Peter Eisentraut , Pavel Luzanov , Erik Wienhold , Dean Rasheed Subject: Re: Psql meta-command conninfo+ Message-ID: <202502211819.uevfb34wedav@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2nvccac7zpqpydf3" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --2nvccac7zpqpydf3 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit I suggest the attached, which gets 99% there with 10% of the complexity, and has \conninfo (no plus sign) output this: Connection Information Parámetro │ Valor ───────────────────────┼──────────────────────── Base de Datos │ alvherre Client User │ alvherre Host │ 192.168.178.37 Port │ 55432 Opciones │ Protocol Version │ 3 Password Used? │ false GSSAPI Authenticated? │ false Backend PID │ 1589499 TLS Connection? │ true TLS Library │ OpenSSL TLS Protocol │ TLSv1.3 TLS Key Bits │ 256 TLS Cipher │ TLS_AES_256_GCM_SHA384 TLS Compression │ false ALPN │ postgresql Superuser? │ on Hot standby? │ off (18 filas) I have added the parameters is_superuser and in_hot_standby only, and stayed away from the libpq part of the patch to enumerate parameters. ISTM a hardcoded list is fine. Maybe keeping track of 'role' via ParameterStatus messages is a good idea for reasons unrelated to this patch -- maybe it can be useful for applications to be aware of role changes -- but I'm not 100% sure about that, and in particular I'm not sure how heavy the protocol traffic is going to be if such messages are emitted every time you run a security invoker function or things like that. So I'm leaving that part out for now, and it's easy to do both the libpq patch and \conninfo. Also, I don't see why we have to keep the current free-format \conninfo. When you run a query, you get things like # select 99 as "Luftballons"; Luftballons ───────────── 99 You don't get "There were only 99 Luftballons" or any such nonsense, and I don't see why \conninfo gets to play different rules. So I got rid of \conninfo+. Do people hate the question marks? -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/ --2nvccac7zpqpydf3 Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-Change-conninfo-to-use-tabular-format.patch" From 6bb3628336d7a51d4d1afd92c03fd133c652a02c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Herrera?= Date: Fri, 21 Feb 2025 18:31:13 +0100 Subject: [PATCH] Change \conninfo to use tabular format Also display more fields than before. Author: Maiquel Grassi Author: Hunaid Sohail Discussion: https://postgr.es/m/CP8P284MB24965CB63DAC00FC0EA4A475EC462@CP8P284MB2496.BRAP284.PROD.OUTLOOK.COM --- doc/src/sgml/ref/psql-ref.sgml | 7 +- src/bin/psql/command.c | 186 ++++++++++++++++++++++++++++----- 2 files changed, 161 insertions(+), 32 deletions(-) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index a8a0b694bbf..b6fe75efdd4 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1070,9 +1070,10 @@ INSERT INTO tbls1 VALUES ($1, $2) \parse stmt1 \conninfo - - Outputs information about the current database connection. - + + Outputs information about the current database connection, + including TLS-related information if TLS is in use. + diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 12d99eec5a3..1eef1983d83 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -763,40 +763,168 @@ exec_command_close(PsqlScanState scan_state, bool active_branch, const char *cmd static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch) { - if (active_branch) - { - char *db = PQdb(pset.db); + printTableContent cont; + int rows, + cols; + char *db; + char *host; + bool print_hostaddr; + char *hostaddr; + char *protocol_version, + *backend_pid; + int ssl_in_use, + password_used, + gssapi_used; + char *paramval; - if (db == NULL) - printf(_("You are currently not connected to a database.\n")); + if (!active_branch) + return PSQL_CMD_SKIP_LINE; + + db = PQdb(pset.db); + if (db == NULL) + { + printf(_("You are currently not connected to a database.\n")); + return PSQL_CMD_SKIP_LINE; + } + + /* Get values for the parameters */ + host = PQhost(pset.db); + hostaddr = PQhostaddr(pset.db); + protocol_version = psprintf("%d", PQprotocolVersion(pset.db)); + ssl_in_use = PQsslInUse(pset.db); + password_used = PQconnectionUsedPassword(pset.db); + gssapi_used = PQconnectionUsedGSSAPI(pset.db); + backend_pid = psprintf("%d", PQbackendPID(pset.db)); + + /* Only print hostaddr if it differs from host, and not if unixsock */ + print_hostaddr = (!is_unixsock_path(host) && + hostaddr && *hostaddr && strcmp(host, hostaddr) != 0); + + /* Determine the exact number of rows to print */ + rows = 12; + cols = 2; + if (ssl_in_use) + rows += 6; + if (print_hostaddr) + rows++; + + /* Set it all up */ + printTableInit(&cont, &pset.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/hostaddr/socket */ + if (is_unixsock_path(host)) + { + /* hostaddr if specified overrides socket, so suppress the latter */ + if (hostaddr && *hostaddr) + { + printTableAddCell(&cont, _("Host Address"), false, false); + printTableAddCell(&cont, hostaddr, false, false); + } else { - 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 - { - 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(); + printTableAddCell(&cont, _("Socket Directory"), false, false); + printTableAddCell(&cont, host, false, false); } } + else + { + printTableAddCell(&cont, _("Host"), false, false); + printTableAddCell(&cont, host, false, false); + if (print_hostaddr) + { + 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); + + /* TLS Connection */ + printTableAddCell(&cont, _("TLS Connection?"), false, false); + printTableAddCell(&cont, ssl_in_use ? _("true") : _("false"), false, false); + + /* TLS Information */ + if (ssl_in_use) + { + char *library, + *protocol, + *key_bits, + *cipher, + *compression, + *alpn; + + 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"); + + printTableAddCell(&cont, _("TLS Library"), false, false); + printTableAddCell(&cont, library ? library : _("unknown"), false, false); + + printTableAddCell(&cont, _("TLS Protocol"), false, false); + printTableAddCell(&cont, protocol ? protocol : _("unknown"), false, false); + + printTableAddCell(&cont, _("TLS Key Bits"), false, false); + printTableAddCell(&cont, key_bits ? key_bits : _("unknown"), false, false); + + printTableAddCell(&cont, _("TLS Cipher"), false, false); + printTableAddCell(&cont, cipher ? cipher : _("unknown"), false, false); + + printTableAddCell(&cont, _("TLS Compression"), false, false); + printTableAddCell(&cont, (compression && strcmp(compression, "off") != 0) ? + _("true") : _("false"), false, false); + + printTableAddCell(&cont, _("ALPN"), false, false); + printTableAddCell(&cont, (alpn && alpn[0] != '\0') ? alpn : _("none"), false, false); + } + + paramval = (char *) PQparameterStatus(pset.db, "is_superuser"); + printTableAddCell(&cont, "Superuser?", false, false); + printTableAddCell(&cont, paramval ? paramval : _("unknown"), false, false); + + paramval = (char *) PQparameterStatus(pset.db, "in_hot_standby"); + printTableAddCell(&cont, "Hot standby?", false, false); + printTableAddCell(&cont, paramval ? paramval : _("unknown"), false, false); + + printTable(&cont, pset.queryFout, false, pset.logfile); + printTableCleanup(&cont); + + pfree(protocol_version); + pfree(backend_pid); return PSQL_CMD_SKIP_LINE; } -- 2.39.5 --2nvccac7zpqpydf3--