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 1tNTAB-00BFLa-Ns for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Dec 2024 08:42:43 +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 1tNTA9-00GZhX-36 for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Dec 2024 08:42:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tNTA8-00GZhO-NA for pgsql-hackers@lists.postgresql.org; Tue, 17 Dec 2024 08:42:41 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tNTA6-0007RA-Rc for pgsql-hackers@lists.postgresql.org; Tue, 17 Dec 2024 08:42:41 +0000 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-43618283dedso49008285e9.3 for ; Tue, 17 Dec 2024 00:42:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734424958; x=1735029758; darn=lists.postgresql.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :subject:cc:to:from:date:sender:message-id:from:to:cc:subject:date :message-id:reply-to; bh=7QfrggBFZIQzotXgQCk0L4tD/+8sQpohZqBkWbDfBTk=; b=ZmoYJfNqCSFLfxhM1/UBfO0piu2v8owU+dJ1MWsVa8YNOvS4h9W+j2nv871GJkqCT0 K9kgNqbOlgzqW5ig0wVF4PW/7zLfEMxrVG3pi4oqS5AL6Q7KaiejMufFXB6T95i2+Hd3 QqRYWS6xl2spwIqPE1spf1aF9YLBHakTEdy4NlxZcfawJUaVCH/kp/IOYr52TBMcNLZZ DBQOVaGHzDkRIYm45mtKkwHT3BAHt/5Cod2hroZuoEARZyVcS9RdZ9ir7m/dg0ho9g5n 1n4SvXUNEkyegUSYxPnsk7TT3RKO8VKMDvYBId/Ts3VS8HXiYbgf2gYh6YxSeE8QFV2j pZqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734424958; x=1735029758; h=user-agent:in-reply-to:content-disposition:mime-version:references :subject:cc:to:from:date:sender:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=7QfrggBFZIQzotXgQCk0L4tD/+8sQpohZqBkWbDfBTk=; b=aOuBET7ovoBVVjOTtXIeVFNvBp/J9DPwMCVJFu+wLJ1UPdfdteMmzsaOmaMpkgCZyc JBmOung+Ve4I0Z9WenMQfOyMIZ1bZ3oovT6PnbmCC1VoOSjxxuQ0SWhMZ9J6NeadT4kE 0PnC6jZ9ciyLmvS8/VBppxVkLOkIoaQfiQ+jbTjkERkNIvzxg8BRmmWxN8g/7Y7Pw0Yy 22ZTlXCKV4AZU6myaGrk8FJRmS9syG1yQr1cdpfLiKY8l6i6/g78c+/O2woAP9kDQb/2 gf15ZACU2D+CMtDYqDzs2pS/RuqqAEUzoys4+1x+B2t/OsyM48sd8lTryc13q5pfnRlW RSgA== X-Forwarded-Encrypted: i=1; AJvYcCUxbz3gXE6avXEirDuaUqG57HiMt6rBRkq3xel/teDC9NXnZd0T3VjQubDylXNQ4vC7INWXo/XmpNHJbzNm@lists.postgresql.org X-Gm-Message-State: AOJu0YwZf3NmBk39OEwZ1WYBkWauG3zthT+c1HJGpM1ZH4VCiDSyqUw1 bE0UQj/AofCNIl1P6hIaffwCUoyBA6j0lCPBYdprTKtRddtQwMkI X-Gm-Gg: ASbGncu3nV56a9q0KaNM0h1V3V/ocdDqepI+bzQ07hZL8au8rnab8Opa0/mHDKicce7 aF2RqkC713keBhxC/P/cwGfbUWJy+UH/dND2Y3VVxnGdX4e4yWA7y/VJhrJaNQEll+lIW3N2cWl yCvVXKX1bEprTHlnUp/uAlOknYU2lsIrY7bUvpOLNKp99Djxp0VkYFRlvYu4tW7XIYyyVJd6IVo qit5uCRcE9nTJ25RF0fKWZULYO7sDw8ypeudt9DO5a9ZyDRTQAYNUpwm7zM1erQl2AGkK+s24YU nbIcorbaeFoDjppj X-Google-Smtp-Source: AGHT+IFE8gHq0BWIqAuGh/3voe1RsWKgLSrBI4feRw/fdOdm8Rd7RU/FbRsLtvATWmINWzsfWAnf3A== X-Received: by 2002:a05:600c:511c:b0:426:647b:1bfc with SMTP id 5b1f17b1804b1-4362aaa9a92mr147769065e9.30.1734424957584; Tue, 17 Dec 2024 00:42:37 -0800 (PST) Received: from lightning.caipicrew.dd-dns.de ([2001:a61:10dc:9701:1f93:5b09:ad65:dcaa]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-388c8046c66sm10384786f8f.69.2024.12.17.00.42.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 00:42:36 -0800 (PST) Message-ID: <6761397c.df0a0220.68885.210d@mx.google.com> X-Google-Original-Message-ID: <20241217084235.GA32563@caipicrew.dd-dns.de;lightning.caipicrew.dd-dns.de> Sender: Michael Banck Received: from mbanck by lightning.caipicrew.dd-dns.de with local (Exim 4.92) (envelope-from ) id 1tNTA4-000765-4x; Tue, 17 Dec 2024 09:42:36 +0100 Date: Tue, 17 Dec 2024 09:42:36 +0100 From: Michael Banck To: Michael Paquier Cc: Greg Sabino Mullane , PostgreSQL Hackers Subject: Re: [PATCH] Add support for displaying database service in psql prompt References: <6723c612.050a0220.1567f4.b94a@mx.google.com> <6748cfd4.050a0220.1537dc.43bb@mx.google.com> <6760a25e.a70a0220.2e9d8f.d542@mx.google.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Kj7319i9nmIyA2yE" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Kj7319i9nmIyA2yE Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, On Tue, Dec 17, 2024 at 04:43:21PM +0900, Michael Paquier wrote: > On Mon, Dec 16, 2024 at 10:57:49PM +0100, Michael Banck wrote: > > Thanks, I have added the documentation now in v2. > > The doc additions seem fine to me. I've just grabbed three tiny nits, > nothing critical. Thanks for the further review. > + case 's': > + if (PQservice(pset.db)) > + strlcpy(buf, PQservice(pset.db), sizeof(buf)); > + break; > > Other code paths of get_prompt check for pset.db being NULL. True > that it does not matter when calling PQservice() with a connection > that does not exist. For consistency with the surroundings this > should be done at least? Ok, I've done that. > > + > + The service from pg_service.conf, if applicable. > + > > pg_service.conf is not especially true as it depends on the > environment used. For psql, perhaps just use "The service name, if > applicable". No need to be fancy. Done. > + > + %s > + The name of the service entry, if any. > + > > Other entries don't use "if any", would just cut it. Done. V3 attached. Michael --Kj7319i9nmIyA2yE Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-Add-PQservice-to-PGConn.patch" From 1e71982edce8744f48906d2857d22727691b7267 Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Thu, 31 Oct 2024 18:27:52 +0100 Subject: [PATCH v3 1/2] Add PQservice to PGConn. This adds the content of the database service (if any) to PGConn. One use for this would be for psql to display the service as part of the prompt. It also adds PGservice() as a new connection status unction. --- doc/src/sgml/libpq.sgml | 20 ++++++++++++++++++++ src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/fe-connect.c | 11 ++++++++++- src/interfaces/libpq/libpq-fe.h | 1 + src/interfaces/libpq/libpq-int.h | 1 + 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 01f259fd0d..105b22b317 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -2530,6 +2530,26 @@ char *PQport(const PGconn *conn); + + PQservicePQservice + + + + Returns the service of the active connection. + + +char *PQservice(const PGconn *conn); + + + + + returns NULL if the + conn argument is NULL. + Otherwise, if there was no service provided, it returns an empty string. + + + + PQttyPQtty diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index 5d8213e0b5..2ad2cbf5ca 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -205,3 +205,4 @@ PQcancelFinish 202 PQsocketPoll 203 PQsetChunkedRowsMode 204 PQgetCurrentTimeUSec 205 +PQservice 206 diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index aaf87e8e88..ddcc7b60ab 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -190,7 +190,8 @@ typedef struct _internalPQconninfoOption static const internalPQconninfoOption PQconninfoOptions[] = { {"service", "PGSERVICE", NULL, NULL, - "Database-Service", "", 20, -1}, + "Database-Service", "", 20, + offsetof(struct pg_conn, pgservice)}, {"user", "PGUSER", NULL, NULL, "Database-User", "", 20, @@ -7040,6 +7041,14 @@ PQdb(const PGconn *conn) return conn->dbName; } +char * +PQservice(const PGconn *conn) +{ + if (!conn) + return NULL; + return conn->pgservice; +} + char * PQuser(const PGconn *conn) { diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 15012c770c..5947e7c766 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -385,6 +385,7 @@ extern int PQrequestCancel(PGconn *conn); /* Accessor functions for PGconn objects */ extern char *PQdb(const PGconn *conn); +extern char *PQservice(const PGconn *conn); extern char *PQuser(const PGconn *conn); extern char *PQpass(const PGconn *conn); extern char *PQhost(const PGconn *conn); diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index 08cc391cbd..dcebca9898 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -394,6 +394,7 @@ struct pg_conn char *fbappname; /* fallback application name */ char *dbName; /* database name */ char *replication; /* connect as the replication standby? */ + char *pgservice; /* Postgres service, if any */ char *pguser; /* Postgres username and password, if any */ char *pgpass; char *pgpassfile; /* path to a file containing password(s) */ -- 2.39.5 --Kj7319i9nmIyA2yE Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0002-Add-support-for-database-service-to-psql-prompt.patch" From f70fc781adf0833ecd148d672f22d6a1d860c365 Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Thu, 31 Oct 2024 18:49:14 +0100 Subject: [PATCH v3 2/2] Add support for database service to psql prompt. This adds a new psql variable SERVICE as well as the string '%s' for the psql PROMPT, displaying the service (from PGSERVICEFILE) if so desired. --- doc/src/sgml/ref/psql-ref.sgml | 14 ++++++++++++++ src/bin/psql/command.c | 2 ++ src/bin/psql/prompt.c | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index e42073ed74..6ba85d9acf 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -4380,6 +4380,15 @@ bar + + SERVICE + + + The service name, if applicable. + + + + SHELL_ERROR @@ -4685,6 +4694,11 @@ testdb=> INSERT INTO my_table VALUES (:'content'); (tilde) if the database is your default database. + + %s + The name of the service entry. + + %# diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 1f3cbb11f7..cd16f27947 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -4082,6 +4082,7 @@ SyncVariables(void) pset.sversion = PQserverVersion(pset.db); SetVariable(pset.vars, "DBNAME", PQdb(pset.db)); + SetVariable(pset.vars, "SERVICE", PQservice(pset.db)); SetVariable(pset.vars, "USER", PQuser(pset.db)); SetVariable(pset.vars, "HOST", PQhost(pset.db)); SetVariable(pset.vars, "PORT", PQport(pset.db)); @@ -4115,6 +4116,7 @@ void UnsyncVariables(void) { SetVariable(pset.vars, "DBNAME", NULL); + SetVariable(pset.vars, "SERVICE", NULL); SetVariable(pset.vars, "USER", NULL); SetVariable(pset.vars, "HOST", NULL); SetVariable(pset.vars, "PORT", NULL); diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index 0d99d00ac9..3b3f079229 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -33,6 +33,7 @@ * %p - backend pid * %> - database server port number * %n - database user name + * %s - service * %/ - current database * %~ - like %/ but "~" when database name equals user name * %w - whitespace of the same width as the most recent output of PROMPT1 @@ -246,6 +247,11 @@ get_prompt(promptStatus_t status, ConditionalStack cstack) } break; + case 's': + if (pset.db && PQservice(pset.db)) + strlcpy(buf, PQservice(pset.db), sizeof(buf)); + break; + case 'l': snprintf(buf, sizeof(buf), UINT64_FORMAT, pset.stmt_lineno); break; -- 2.39.5 --Kj7319i9nmIyA2yE--