Hi
>> >> >> But afaict there's no problem with using pqParseInput3() and >> PQexecFinish() even if the message isn't handled as part of the >> transaction. Some other messages that pqParseInput3 handles which are >> not part of the transaction are 'N' (Notice) and 'K' (secret key). >> > > I have to recheck it > here is new version based on usage of PQexecFinish Regards Pavel > > Regards > > Pavel > >
From 4567473ebd7fec9dc836793e8e80244b22e5c96b Mon Sep 17 00:00:00 2001 From: "ok...@github.com" <pavel.steh...@gmail.com> Date: Mon, 24 Jul 2023 20:18:16 +0200 Subject: [PATCH 2/2] Implementation of %N prompt placeholder It is based on forcing reporting feature"role" GUC to client. --- doc/src/sgml/ref/psql-ref.sgml | 19 +++++++++++++++++- src/bin/psql/command.c | 13 +++++++++++++ src/bin/psql/prompt.c | 35 ++++++++++++++++++++++++++++++++++ src/bin/psql/settings.h | 1 + src/bin/psql/startup.c | 32 +++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 182e58353f..b5da83013e 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -4567,7 +4567,24 @@ testdb=> <userinput>INSERT INTO my_table VALUES (:'content');</userinput> <listitem><para>The port number at which the database server is listening.</para></listitem> </varlistentry> - <varlistentry id="app-psql-prompting-n"> + <varlistentry id="app-psql-prompting-n-uc"> + <term><literal>%N</literal></term> + <listitem> + <para> + The database role name. This value is specified by command + <command>SET ROLE</command>. Until execution of this command + the value is set to the database session user name. + </para> + + <para> + This substitution requires <productname>PostgreSQL</productname> + version 16 and up. When you use older version, the empty string + is used instead. + </para> + </listitem> + </varlistentry> + + <varlistentry id="app-psql-prompting-n-lc"> <term><literal>%n</literal></term> <listitem> <para> diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 1300869d79..bf67febc11 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3861,6 +3861,7 @@ SyncVariables(void) { char vbuf[32]; const char *server_version; + PGresult *result; /* get stuff from connection */ pset.encoding = PQclientEncoding(pset.db); @@ -3890,6 +3891,18 @@ SyncVariables(void) /* send stuff to it, too */ PQsetErrorVerbosity(pset.db, pset.verbosity); PQsetErrorContextVisibility(pset.db, pset.show_context); + + /* link role GUC when it is needed for prompt */ + if (pset.prompt_shows_role) + result = PQlinkParameterStatus(pset.db, "role"); + else + result = PQunlinkParameterStatus(pset.db, "role"); + + if (PQresultStatus(result) != PGRES_COMMAND_OK) + pg_log_info("cannot set REPORT flag on configuration variable \"role\": %s", + PQerrorMessage(pset.db)); + + PQclear(result); } /* diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index 969cd9908e..b0f5158c59 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -165,6 +165,41 @@ get_prompt(promptStatus_t status, ConditionalStack cstack) if (pset.db) strlcpy(buf, session_username(), sizeof(buf)); break; + /* DB server user role */ + case 'N': + if (pset.db) + { + int minServerMajor; + int serverMajor; + const char *rolename = NULL; + + /* + * This feature requires GUC "role" to be marked + * as GUC_REPORT. Without it is hard to specify fallback + * result. Returning empty value can be messy, returning + * PQuser like session_username can be messy too. + * Exec query is not too practical too, because it doesn't + * work when session is not in transactional state, and + * CURRENT_ROLE returns different result when role is not + * explicitly specified by SET ROLE. + */ + minServerMajor = 1600; + serverMajor = PQserverVersion(pset.db) / 100; + if (serverMajor >= minServerMajor) + { + rolename = PQparameterStatus(pset.db, "role"); + + /* fallback when role is not set yet */ + if (rolename && strcmp(rolename, "none") == 0) + rolename = session_username(); + } + + if (rolename) + strlcpy(buf, rolename, sizeof(buf)); + else + buf[0] = '\0'; + } + break; /* backend pid */ case 'p': if (pset.db) diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 1106954236..cb7c12bd1d 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -154,6 +154,7 @@ typedef struct _psqlSettings PGVerbosity verbosity; /* current error verbosity level */ bool show_all_results; PGContextVisibility show_context; /* current context display level */ + bool prompt_shows_role; } PsqlSettings; extern PsqlSettings pset; diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 5a28b6f713..0dac396525 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -1094,10 +1094,40 @@ histcontrol_hook(const char *newval) return true; } +static void +prompt_needs_role_parameter_status(void) +{ + PGresult *result; + + if (!pset.db) + return; + + pset.prompt_shows_role = false; + + if (pset.prompt1 && strstr(pset.prompt1, "%N")) + pset.prompt_shows_role = true; + else if (pset.prompt2 && strstr(pset.prompt2, "%N")) + pset.prompt_shows_role = true; + else if (pset.prompt3 && strstr(pset.prompt3, "%N")) + pset.prompt_shows_role = true; + + if (pset.prompt_shows_role) + result = PQlinkParameterStatus(pset.db, "role"); + else + result = PQunlinkParameterStatus(pset.db, "role"); + + if (PQresultStatus(result) != PGRES_COMMAND_OK) + pg_log_info("cannot set REPORT flag on configuration variable \"role\": %s", + PQerrorMessage(pset.db)); + + PQclear(result); +} + static bool prompt1_hook(const char *newval) { pset.prompt1 = newval ? newval : ""; + prompt_needs_role_parameter_status(); return true; } @@ -1105,6 +1135,7 @@ static bool prompt2_hook(const char *newval) { pset.prompt2 = newval ? newval : ""; + prompt_needs_role_parameter_status(); return true; } @@ -1112,6 +1143,7 @@ static bool prompt3_hook(const char *newval) { pset.prompt3 = newval ? newval : ""; + prompt_needs_role_parameter_status(); return true; } -- 2.41.0
From 3d1d739ef973e9e58bd930c02657abd42c38d732 Mon Sep 17 00:00:00 2001 From: "ok...@github.com" <pavel.steh...@gmail.com> Date: Mon, 24 Jul 2023 20:13:17 +0200 Subject: [PATCH 1/2] Protocol ReportGUC message This patch implements dynamic reporting changes of GUC to client side. The flags per GUC can be changed by functions SetGUCOptionFlag and UsetGUCOptionFlag. --- doc/src/sgml/protocol.sgml | 37 +++++++++++++++++++++++++++++ src/backend/tcop/postgres.c | 32 +++++++++++++++++++++++++ src/backend/utils/misc/guc.c | 31 ++++++++++++++++++++++++ src/include/libpq/protocol.h | 1 + src/include/utils/guc.h | 2 ++ src/interfaces/libpq/exports.txt | 2 ++ src/interfaces/libpq/fe-exec.c | 36 +++++++++++++++++++++++++++- src/interfaces/libpq/fe-protocol3.c | 1 - src/interfaces/libpq/fe-trace.c | 12 ++++++++++ src/interfaces/libpq/libpq-fe.h | 3 +++ src/interfaces/libpq/libpq-int.h | 4 +++- 11 files changed, 158 insertions(+), 3 deletions(-) diff --git a/doc/src/sgml/protocol.sgml b/doc/src/sgml/protocol.sgml index b11d9a6ba3..fe6c4042fd 100644 --- a/doc/src/sgml/protocol.sgml +++ b/doc/src/sgml/protocol.sgml @@ -5401,6 +5401,43 @@ psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;" </listitem> </varlistentry> + <varlistentry id="protocol-message-formats-ReportGUC"> + <term>ReportGUC (F)</term> + <listitem> + <variablelist> + <varlistentry> + <term>Byte1('r')</term> + <listitem> + <para> + Identifies the message type. ReportGUC is sent by + frontend when the changes of specified GUC option + should be (or should not be) reported to state parameter. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term>Byte1</term> + <listitem> + <para> + 't' when reporting should be enables, 'f' if reporting should be + disabled. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term>String</term> + <listitem> + <para> + The name of GUC option. + </para> + </listitem> + </varlistentry> + </variablelist> + </listitem> + </varlistentry> + <varlistentry id="protocol-message-formats-RowDescription"> <term>RowDescription (B)</term> <listitem> diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index e4756f8be2..0e4ddbf8ee 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -451,6 +451,11 @@ SocketBackend(StringInfo inBuf) doing_extended_query_message = false; break; + case PqMsg_ReportGUC: + maxmsglen = PQ_SMALL_MESSAGE_LIMIT; + doing_extended_query_message = false; + break; + default: /* @@ -4835,6 +4840,33 @@ PostgresMain(const char *dbname, const char *username) send_ready_for_query = true; break; + case PqMsg_ReportGUC: + { + const char *name; + const char *status; + int is_set; + + is_set = pq_getmsgbyte(&input_message); + name = pq_getmsgstring(&input_message); + pq_getmsgend(&input_message); + + if (is_set == 't') + { + SetGUCOptionFlag(name, GUC_REPORT); + status = "SET REPORT_GUC"; + } + else + { + UnsetGUCOptionFlag(name, GUC_REPORT); + status = "UNSET REPORT_GUC"; + } + + pq_puttextmessage(PqMsg_CommandComplete, status); + + valgrind_report_error_query("ReportGUC message"); + } + break; + /* * 'X' means that the frontend is closing down the socket. EOF * means unexpected loss of frontend connection. Either way, diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 84e7ad4d90..4a1e968255 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -2532,6 +2532,37 @@ BeginReportingGUCOptions(void) } } +/* + * Allow to set / unset dynamicaly flags to GUC variables + */ +void +SetGUCOptionFlag(const char *name, int flag) +{ + struct config_generic *conf; + + /* only GUC_REPORT flag is supported now */ + Assert(flag == GUC_REPORT); + + conf = find_option(name, false, true, ERROR); + conf->flags |= flag; + + if (flag == GUC_REPORT) + /* force transmit value of related option to client Parameter Status */ + ReportGUCOption(conf); +} + +void +UnsetGUCOptionFlag(const char *name, int flag) +{ + struct config_generic *conf; + + /* only GUC_REPORT flag is supported now */ + Assert(flag == GUC_REPORT); + + conf = find_option(name, false, true, ERROR); + conf->flags &= ~flag; +} + /* * ReportChangedGUCOptions: report recently-changed GUC_REPORT variables * diff --git a/src/include/libpq/protocol.h b/src/include/libpq/protocol.h index cc46f4b586..bd2c368b54 100644 --- a/src/include/libpq/protocol.h +++ b/src/include/libpq/protocol.h @@ -27,6 +27,7 @@ #define PqMsg_Sync 'S' #define PqMsg_Terminate 'X' #define PqMsg_CopyFail 'f' +#define PqMsg_ReportGUC 'r' #define PqMsg_GSSResponse 'p' #define PqMsg_PasswordMessage 'p' #define PqMsg_SASLInitialResponse 'p' diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h index e89083ee0e..c200ca9b34 100644 --- a/src/include/utils/guc.h +++ b/src/include/utils/guc.h @@ -370,6 +370,8 @@ extern void ResetAllOptions(void); extern void AtStart_GUC(void); extern int NewGUCNestLevel(void); extern void AtEOXact_GUC(bool isCommit, int nestLevel); +extern void SetGUCOptionFlag(const char *name, int flag); +extern void UnsetGUCOptionFlag(const char *name, int flag); extern void BeginReportingGUCOptions(void); extern void ReportChangedGUCOptions(void); extern void ParseLongOption(const char *string, char **name, char **value); diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index 850734ac96..7e101368d5 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -191,3 +191,5 @@ PQclosePrepared 188 PQclosePortal 189 PQsendClosePrepared 190 PQsendClosePortal 191 +PQlinkParameterStatus 192 +PQunlinkParameterStatus 193 diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index 974d462d4b..c34a826fb1 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -1069,6 +1069,33 @@ pqSaveMessageField(PGresult *res, char code, const char *value) res->errFields = pfield; } +/* + * Add GUC_REPORT flag to specified setting and wait for synchronization + * with state parameters. + */ +PGresult * +PQlinkParameterStatus(PGconn *conn, const char *paramName) +{ + if (!PQexecStart(conn)) + return NULL; + if (!PQsendTypedCommand(conn, PqMsg_ReportGUC, 't', paramName)) + return NULL; + return PQexecFinish(conn); +} + +/* + * Remove GUC_REPORT flag from specified setting. + */ +PGresult * +PQunlinkParameterStatus(PGconn *conn, const char *paramName) +{ + if (!PQexecStart(conn)) + return NULL; + if (!PQsendTypedCommand(conn, PqMsg_ReportGUC, 'f', paramName)) + return NULL; + return PQexecFinish(conn); +} + /* * pqSaveParameterStatus - remember parameter status sent by backend */ @@ -2543,11 +2570,14 @@ PQsendClosePortal(PGconn *conn, const char *portal) * * Available options for "command" are * PqMsg_Close for Close; or - * PqMsg_Describe for Describe. + * PqMsg_Describe for Describe; or + * PqMsg_ReportGUC for (un)set GUC_REPORT flag. * * Available options for "type" are * 'S' to run a command on a prepared statement; or * 'P' to run a command on a portal. + * 't' to set GUC_REPORT flag + * 'f' to unset GUC_REPORT flag * * Returns 1 on success and 0 on failure. */ @@ -2591,6 +2621,10 @@ PQsendTypedCommand(PGconn *conn, char command, char type, const char *target) { entry->queryclass = PGQUERY_DESCRIBE; } + else if (command == PqMsg_ReportGUC) + { + entry->queryclass = PGQUERY_SETTING; + } else { libpq_append_conn_error(conn, "unknown command type provided"); diff --git a/src/interfaces/libpq/fe-protocol3.c b/src/interfaces/libpq/fe-protocol3.c index 5613c56b14..90d4e17e6f 100644 --- a/src/interfaces/libpq/fe-protocol3.c +++ b/src/interfaces/libpq/fe-protocol3.c @@ -2003,7 +2003,6 @@ pqEndcopy3(PGconn *conn) return 1; } - /* * PQfn - Send a function call to the POSTGRES backend. * diff --git a/src/interfaces/libpq/fe-trace.c b/src/interfaces/libpq/fe-trace.c index b18e3deab6..2a439080e1 100644 --- a/src/interfaces/libpq/fe-trace.c +++ b/src/interfaces/libpq/fe-trace.c @@ -522,6 +522,15 @@ pqTraceOutputZ(FILE *f, const char *message, int *cursor) pqTraceOutputByte1(f, message, cursor); } +/* ReportGUC */ +static void +pqTraceOutputr(FILE *f, const char *message, int *cursor) +{ + fprintf(f, "ReportGUC\t"); + pqTraceOutputByte1(f, message, cursor); + pqTraceOutputString(f, message, cursor, false); +} + /* * Print the given message to the trace output stream. */ @@ -644,6 +653,9 @@ pqTraceOutputMessage(PGconn *conn, const char *message, bool toServer) case PqMsg_AuthenticationRequest: pqTraceOutputR(conn->Pfdebug, message, &logCursor); break; + case PqMsg_ReportGUC: + pqTraceOutputr(conn->Pfdebug, message, &logCursor); + break; case PqMsg_PortalSuspended: fprintf(conn->Pfdebug, "PortalSuspended"); /* No message content */ diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 97762d56f5..ba3ad7e0aa 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -593,6 +593,9 @@ extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_lengt size_t *to_length); +/* Control of dynamic propagation settings to state parameters */ +extern PGresult *PQlinkParameterStatus(PGconn *conn, const char *paramName); +extern PGresult *PQunlinkParameterStatus(PGconn *conn, const char *paramName); /* === in fe-print.c === */ diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index c745facfec..081e228f2b 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -322,7 +322,8 @@ typedef enum PGQUERY_PREPARE, /* Parse only (PQprepare) */ PGQUERY_DESCRIBE, /* Describe Statement or Portal */ PGQUERY_SYNC, /* Sync (at end of a pipeline) */ - PGQUERY_CLOSE /* Close Statement or Portal */ + PGQUERY_CLOSE, /* Close Statement or Portal */ + PGQUERY_SETTING /* setting GUC_REPORT flag */ } PGQueryClass; /* @@ -718,6 +719,7 @@ extern PGresult *pqFunctionCall3(PGconn *conn, Oid fnid, int *result_buf, int *actual_result_len, int result_is_int, const PQArgBlock *args, int nargs); +extern int pqSendReportGUCMessage(PGconn *conn, const char *paramName, bool create_flag); /* === in fe-misc.c === */ -- 2.41.0