From: Michael Banck <mbanck@gmx.net>
To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: [PATCH] Add support for displaying database service in psql prompt
Date: Thu, 31 Oct 2024 19:01:54 +0100
Message-ID: <6723c612.050a0220.1567f4.b94a@mx.google.com> (raw)
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
Attachments:download all (2 files, .tar.gz)v1-0001-Add-PQservice-to-PGConn.patch (text/x-diff, 2.8K) download
From f876195acb797a5ac58c17409fdd75d18581c292 Mon Sep 17 00:00:00 2001
From: Michael Banck <michael.banck@credativ.de>
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.txtindex 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 206diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.cindex 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.hindex 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.hindex 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
v1-0002-Add-support-for-database-service-to-psql-prompt.patch (text/x-diff, 2.0K) download
From 3257e93d20353ff348b15df9b45207ec45839ed5 Mon Sep 17 00:00:00 2001
From: Michael Banck <michael.banck@credativ.de>
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.cindex 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.cindex 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: mbanck@gmx.net, pgsql-hackers@lists.postgresql.org
Subject: Re: [PATCH] Add support for displaying database service in psql prompt
In-Reply-To: <6723c612.050a0220.1567f4.b94a@mx.google.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox