From 3b42bd47db4488f320b166d4c580193bd2c02de8 Mon Sep 17 00:00:00 2001 From: "okbob@github.com" Date: Sat, 4 Feb 2023 21:16:30 +0100 Subject: [PATCH] implementation of psql prompt substitution %N this substitution shows used role specified by command SET ROLE. it can show string ERRA0000 (SQL code - feature not supported) when the variable "role" is not reported (PostgreSQL 15 and older). --- doc/src/sgml/libpq.sgml | 4 +++- doc/src/sgml/ref/psql-ref.sgml | 21 +++++++++++++++- src/backend/utils/misc/guc_tables.c | 2 +- src/bin/psql/prompt.c | 37 +++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 0e7ae70c70..6a2f92b368 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -2270,6 +2270,7 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName); in_hot_standby, is_superuser, session_authorization, + role, DateStyle, IntervalStyle, TimeZone, @@ -2284,7 +2285,8 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName); 9.0; default_transaction_read_only and in_hot_standby were not reported by releases before - 14.) + 14; + role was not reported by releases before 16;) Note that server_version, server_encoding and diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index dc6528dc11..5e313f06ab 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -4509,7 +4509,26 @@ testdb=> INSERT INTO my_table VALUES (:'content'); The port number at which the database server is listening. - + + %N + + + The database role name. This value is specified by command + SET ROLE. Until execution of this command + the value is none. The value is same like + the value of the parameter role (can be + displayed by SHOW. + + + + This substitution requires PostgreSQL + version 15 and up. When you use older version, the error value + ERR0A000 is used instead. + + + + + %n diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c index b46e3b8c55..3188fd015d 100644 --- a/src/backend/utils/misc/guc_tables.c +++ b/src/backend/utils/misc/guc_tables.c @@ -4144,7 +4144,7 @@ struct config_string ConfigureNamesString[] = {"role", PGC_USERSET, UNGROUPED, gettext_noop("Sets the current role."), NULL, - GUC_IS_NAME | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_NOT_WHILE_SEC_REST + GUC_IS_NAME | GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_NOT_WHILE_SEC_REST }, &role_string, "none", diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index 969cd9908e..ef720c8496 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -165,6 +165,43 @@ 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; + + /* + * 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) + { + const char *val; + + val = PQparameterStatus(pset.db, "role"); + if (val) + strlcpy(buf, val, sizeof(buf)); + else + /* missing data */ + strlcpy(buf, "ERR02000", sizeof(buf)); + } + else + { + /* feature not supported */ + strlcpy(buf, "ERR0A000", sizeof(buf)); + } + } + break; /* backend pid */ case 'p': if (pset.db) -- 2.39.1