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 1t6ZUh-00DvAm-6M for pgsql-hackers@arkaria.postgresql.org; Thu, 31 Oct 2024 18:02:03 +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 1t6ZUf-004MOb-GM for pgsql-hackers@arkaria.postgresql.org; Thu, 31 Oct 2024 18:02:01 +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 1t6ZUf-004MOL-1g for pgsql-hackers@lists.postgresql.org; Thu, 31 Oct 2024 18:02:01 +0000 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t6ZUc-003pEp-HS for pgsql-hackers@lists.postgresql.org; Thu, 31 Oct 2024 18:02:00 +0000 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-539e13375d3so1406656e87.3 for ; Thu, 31 Oct 2024 11:01:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730397716; x=1731002516; darn=lists.postgresql.org; h=user-agent:content-disposition:mime-version:subject:to:from:date :sender:message-id:from:to:cc:subject:date:message-id:reply-to; bh=D7gZ/Ow11T9NXdgCADfD9zxm3bvbxoTcMIidS38PnUE=; b=YRqLGxufqIklW3SuenreGOOBc0kfdcIOjOcKbV5lLnDoWxYgSuiUHls19y3j6h+V2t EwHg+48oPASc0/GH3wvJ/Df8cc4SoHsweZwX+hH0ahIJtR0l2dNi83Nrod677WFdwdJd YtNH7fEoJD8h8LkXnoA6luPRAGFQ3pvJpVs/pzgko5OOpn4I6Dn+ycWtkZVSryKz+zpX 2nHZ0B4sGWq+VeYH69Ie1/qvfOKsgc6fhG/771s/fOka01xNckPPIWGziVRMZt8LVXgq giEwfasaB6MJsBxstte/tCYzuKVGDHC/3Ui4szG8lcu0xdgZ5omooOHn2aU2HPRKz41F G1/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730397716; x=1731002516; h=user-agent:content-disposition:mime-version:subject:to:from:date :sender:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=D7gZ/Ow11T9NXdgCADfD9zxm3bvbxoTcMIidS38PnUE=; b=pCEL9MWn9P+zmmOB0kz+wgGuLyyMG+B7LyQFZCCU+mRqIIvd3/ujF9d/EZJJutb8YH 2mbkqLYskPx3o31jGBGHb51RYhZw5QjdZKgtC52t6iG8iq93J2d+Wo6tL2aI4LD8l44o Siz1th3zV03dKFvirgfnKOLnTvzhrXlbOGndqXRBXxfpHblK/zc/2LcjTRW9jfUiGnKM AGPRPeQAaumeGwMS40nhVW/BOUSee8Mg7z0UjrtT40QwvS3sJGnnsjbTsaeNfcBsjk3J 2KKP3rtj+EvQAej1nwrx1EMMlp3cqaO834uE51Wo9KYNb1zaZYnlDZjTrxh25lKLpX/p IelQ== X-Gm-Message-State: AOJu0YyVOCBnZ9LD12rkWjOIOxq9/J6xaAvfFgI2e3s0HFvfRESG+N+0 ozt9JyvJzLgt4mibo1YSmT2SUsIJlgW+/DRLf8aHzXqao+f25K5U8Auq4g== X-Google-Smtp-Source: AGHT+IHpnpvSpxUUfNeclOJICguo4nVcxNKiWzbg9KqwXTkpWUL8sdtV6RLkMnHPJ+mr6DNRVCd8SA== X-Received: by 2002:a05:6512:31cb:b0:536:53e3:fe9d with SMTP id 2adb3069b0e04-53c79e30300mr2483128e87.18.1730397715481; Thu, 31 Oct 2024 11:01:55 -0700 (PDT) Received: from lightning.caipicrew.dd-dns.de ([2001:a61:a49:e301:666c:78e:5e29:e70c]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4327d5ac002sm34222595e9.5.2024.10.31.11.01.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 11:01:54 -0700 (PDT) Message-ID: <6723c612.050a0220.1567f4.b94a@mx.google.com> X-Google-Original-Message-ID: <20241031180153.GB5500@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 1t6ZUY-0004pl-6E for pgsql-hackers@lists.postgresql.org; Thu, 31 Oct 2024 19:01:54 +0100 Date: Thu, 31 Oct 2024 19:01:54 +0100 From: Michael Banck To: PostgreSQL Hackers Subject: [PATCH] Add support for displaying database service in psql prompt MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="dDRMvlgZJXvWKvBx" Content-Disposition: inline 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 --dDRMvlgZJXvWKvBx Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, I think it might be useful to sometimes display the database service (i.e. what is defined in ~/.pg_service.conf and used via psql service=foo) in the psql prompt, e.g. if you have 'test' and 'prod' services, but both have the same database name. This was also suggested to me during a recent customer training. I chose the '%s' tag for it. I had to add the service to PGConn as PQservice (first patch) to libpq and then use it in psql in the second patch. Michael --dDRMvlgZJXvWKvBx Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-Add-PQservice-to-PGConn.patch" From f876195acb797a5ac58c17409fdd75d18581c292 Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Thu, 31 Oct 2024 18:27:52 +0100 Subject: [PATCH v1 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. --- 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 + 4 files changed, 13 insertions(+), 1 deletion(-) 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 61c025ff3b..8d809ee4cb 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, @@ -7052,6 +7053,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 --dDRMvlgZJXvWKvBx Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0002-Add-support-for-database-service-to-psql-prompt.patch" From 3257e93d20353ff348b15df9b45207ec45839ed5 Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Thu, 31 Oct 2024 18:49:14 +0100 Subject: [PATCH v1 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. --- src/bin/psql/command.c | 2 ++ src/bin/psql/prompt.c | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 328d78c73f..e8f2573649 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -4085,6 +4085,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)); @@ -4118,6 +4119,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..de3d976103 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 (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 --dDRMvlgZJXvWKvBx--