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 1tNJ6E-00A4U4-1P for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Dec 2024 21:57:58 +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 1tNJ6B-00BXQM-6z for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Dec 2024 21:57:56 +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 1tNJ6A-00BXQE-Og for pgsql-hackers@lists.postgresql.org; Mon, 16 Dec 2024 21:57:56 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tNJ68-003BZl-Ul for pgsql-hackers@lists.postgresql.org; Mon, 16 Dec 2024 21:57:54 +0000 Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-5d3e6f6cf69so767359a12.1 for ; Mon, 16 Dec 2024 13:57:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734386271; x=1734991071; 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=arIYbw/5CIeGi8LmjKIXqZoApdLTejEvu1xMqe3zqXY=; b=hs9BYXm1/MrwLMcZeYqoNBbV/DezfPUN3f2Gc7ZxMcQP+G/mcJSnRKApw3Y1xtK0bs Vx8CmmD26+0Vmkl56qkM7Aoi1SUeTTlMc3UNjRn9F6UeazioMClu5LGyhOZmmIR4r5JC LMuVn+dAotYHSZ4/OiMs+CaL/e9ewZ92yzlp/Hh9obpl0P4pykmTElBDiXC8+n2XSV4y YZgjXhJdQij38IClhO/DtRNY4sN9x0ZSkHat3QBfareak7kwWsYlgjpU70b3cmp12Ey4 3eMqnNrtJ9vC0on66mtC2evN64UGDSl4hRf2j4uH3cgmPt4WxlZdl/4j5QEqAw7IKT6q pdqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734386271; x=1734991071; 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=arIYbw/5CIeGi8LmjKIXqZoApdLTejEvu1xMqe3zqXY=; b=wuQwGIATtzoBHo816YGZN2ir0J1x3JncwGlU9i2kZDyePKqOrXtUcIMCldDPTPOy7Q oN3pftH464b11fejYa1rHOD2jWXob1bJlCro21C7ZXMJHcvM2VnpQn9YBXKyfLSa7NRx eZB/KI14CYTesnFBVQPdBC61GvPaBMq4nP/zop3L34By8+PdXc6K6IKpVx+8uUKP3t/b DSvdiMFoEBBIiCiUeewjoTQA1D3pC7XPPZl00EzcqdG1qJuxkThbSUGG8GOvX3an+6pf r+TCxQnASv8j0QgNNpTHbyx8Cux2Qsp6rezUH9pfdzr3Lpq6N0GuX5E4JK3UONBPNyiL 44fQ== X-Forwarded-Encrypted: i=1; AJvYcCXMag0qMzyAFKQUi4PXSj3ydltjaRWfxRGdQCahgs9NJeWnyHHAZ6A2edj9NFx8nL+27/ZiBFPg46jlz0JT@lists.postgresql.org X-Gm-Message-State: AOJu0YzFIJmcPfg6b/4oAjxEcZ7lDbClnVbwbZXJaxtU79s97F0XrpA9 4jl3mReXWvWmKEaASpPkm8iIuAkJTG0GWeSZnKUKjlfUJ93Rm0VGnniQRg== X-Gm-Gg: ASbGncu7h3Tv/3Ok+OVO7C1JKzwsl35i5uTLk+j85v6MGPyl2cRM3ea+T0p2BbPtMPX 7uaF9UgSoaYBEhp0vRCtTejjSTx642ZNdiXsTJpYpRrk8VGFJcpXBDA74otFIbxuzcvbv9ED/+S 4y6aDae0D8gyBsLBOZ5XsPyYLShlImaPfrsLXK5/2rrRxYJGa12Sc/vGlXhRmAEJEYnNfzYEO7B t7+43P22p2pYMHR39mxYh82X2UP/p5vMyt6rIZwvuIgeKywPZmSc+oHZSmZBgPvARFKSgPW1Zae CN1SBK3glljhbmd6 X-Google-Smtp-Source: AGHT+IEVWYOAJ3ROQXZ6c238cCbsAtTs737CjBWlUU5/bxHUvc8fjNBUOqnTQqEhst613dp2p9PcYw== X-Received: by 2002:a05:6402:3495:b0:5d0:e615:39fe with SMTP id 4fb4d7f45d1cf-5d63c3202abmr13807798a12.18.1734386271123; Mon, 16 Dec 2024 13:57:51 -0800 (PST) Received: from lightning.caipicrew.dd-dns.de ([2001:a61:10dc:9701:1f93:5b09:ad65:dcaa]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5d652f25a0bsm3576993a12.65.2024.12.16.13.57.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2024 13:57:50 -0800 (PST) Message-ID: <6760a25e.a70a0220.2e9d8f.d542@mx.google.com> X-Google-Original-Message-ID: <20241216215749.GC1150@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 1tNJ65-000288-9P; Mon, 16 Dec 2024 22:57:49 +0100 Date: Mon, 16 Dec 2024 22:57:49 +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> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="gj572EiMnwbLXET9" 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 --gj572EiMnwbLXET9 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi Michael, On Tue, Dec 10, 2024 at 04:38:24PM +0900, Michael Paquier wrote: > On Thu, Nov 28, 2024 at 09:17:23PM +0100, Michael Banck wrote: > > On Tue, Nov 19, 2024 at 07:47:58PM -0500, Greg Sabino Mullane wrote: > > > Compiled and tested: works fine, so +1 from me. Honestly, I was surprised > > > %s was still available. :) > > > > Thanks. Was that full review? You kept the commitfest item in "Needs > > Review" state. > > No objections to this proposal, that can be useful. No objections > with the use of 's' for the shortcut in the psql prompt. Cool. > Back to your patch, you are missing two things at quick glance: > - Documentation for the new libpq API in 0001. > - Documentation for the new SERVICE and its new %s (see section called > "Prompting" on the psql page. > > So please make sure to provide these with the next version of the > patch. Thanks, I have added the documentation now in v2. Michael --gj572EiMnwbLXET9 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-Add-PQservice-to-PGConn.patch" From 666b80297f1cb918230b5104e2e8dce08b711394 Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Thu, 31 Oct 2024 18:27:52 +0100 Subject: [PATCH v2 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 --gj572EiMnwbLXET9 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0002-Add-support-for-database-service-to-psql-prompt.patch" From 5faa624871e3267a780d794e9ad2e9f42fd42311 Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Thu, 31 Oct 2024 18:49:14 +0100 Subject: [PATCH v2 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..af78e0e87b 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 from pg_service.conf, 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, if any. + + %# 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..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 --gj572EiMnwbLXET9--