Hello. Suggested patch introduces an %r substitution in psql's prompt. This substitution allows to display whether user is connected to master or replica right in a prompt.
Usage example: ``` $ cat ~/.psqlrc \set PROMPT1 '%p (%r) =# ' $ psql -p 5432 psql (9.6beta4) Type "help" for help. 20638 (m) =# \q $ psql -p 5433 psql (9.6beta4) Type "help" for help. 20647 (r) =# \q ``` Currently I'm working on some functionality involving master-slave replication. Sometimes I accidentally connect to a master instead of replica and vice versa. Presence of functionality described above would make my work much more convenient. Hopefully there are other people in PostgreSQL community who feel that way. -- Best regards, Aleksander Alekseev
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index d9bce25..ce15a66 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -3628,6 +3628,13 @@ testdb=> <userinput>INSERT INTO my_table VALUES (:'content');</userinput> </varlistentry> <varlistentry> + <term><literal>%r</literal></term> + <listitem> + <para>Equals <literal>m</literal> if current instance is a master and <literal>r</literal> if it's a replica.</para> + </listitem> + </varlistentry> + + <varlistentry> <term><literal>%R</literal></term> <listitem> <para> diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index d4625a6..4436561 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -31,6 +31,7 @@ #include <utime.h> #endif +#include "access/xlog.h" #include "access/htup_details.h" #include "catalog/pg_authid.h" #include "libpq/libpq.h" @@ -565,7 +566,9 @@ InitializeSessionUserId(const char *rolename, Oid roleid) SetConfigOption("is_superuser", AuthenticatedUserIsSuperuser ? "on" : "off", PGC_INTERNAL, PGC_S_OVERRIDE); - + SetConfigOption("is_master", + RecoveryInProgress() ? "off" : "on", + PGC_INTERNAL, PGC_S_OVERRIDE); ReleaseSysCache(roleTup); } diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 9c93df0..d3eb49a 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -471,6 +471,7 @@ int huge_pages; */ static char *syslog_ident_str; static bool session_auth_is_superuser; +static bool session_is_master; static double phony_random_seed; static char *client_encoding_string; static char *datestyle_string; @@ -900,6 +901,16 @@ static struct config_bool ConfigureNamesBool[] = NULL, NULL, NULL }, { + {"is_master", PGC_INTERNAL, UNGROUPED, + gettext_noop("Shows whether the current instance is master or replica."), + NULL, + GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE + }, + &session_is_master, + false, + NULL, NULL, NULL + }, + { {"bonjour", PGC_POSTMASTER, CONN_AUTH_SETTINGS, gettext_noop("Enables advertising the server via Bonjour."), NULL diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 2450b9c..da667af 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1919,22 +1919,18 @@ is_select_command(const char *query) return false; } - /* - * Test if the current user is a database superuser. - * - * Note: this will correctly detect superuserness only with a protocol-3.0 - * or newer backend; otherwise it will always say "false". + * If given parameter status is "on" returns true, otherwise returns false. */ -bool -is_superuser(void) +static bool +internal_get_bool_parameter_status(const char* param_name) { const char *val; if (!pset.db) return false; - val = PQparameterStatus(pset.db, "is_superuser"); + val = PQparameterStatus(pset.db, param_name); if (val && strcmp(val, "on") == 0) return true; @@ -1944,6 +1940,19 @@ is_superuser(void) /* + * Test if the current user is a database superuser. + * + * Note: this will correctly detect superuserness only with a protocol-3.0 + * or newer backend; otherwise it will always say "false". + */ +bool +is_superuser(void) +{ + return internal_get_bool_parameter_status("is_superuser"); +} + + +/* * Test if the current session uses standard string literals. * * Note: With a pre-protocol-3.0 connection this will always say "false", @@ -1952,19 +1961,17 @@ is_superuser(void) bool standard_strings(void) { - const char *val; - - if (!pset.db) - return false; - - val = PQparameterStatus(pset.db, "standard_conforming_strings"); - - if (val && strcmp(val, "on") == 0) - return true; - - return false; + return internal_get_bool_parameter_status("standard_conforming_strings"); } +/* + * Test if the current instance is a master. + */ +bool +is_master(void) +{ + return internal_get_bool_parameter_status("is_master"); +} /* * Return the session user of the current connection. diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index bdcb58f..5a6635f 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -39,6 +39,7 @@ extern int PSQLexecWatch(const char *query, const printQueryOpt *opt); extern bool SendQuery(const char *query); extern bool is_superuser(void); +extern bool is_master(void); extern bool standard_strings(void); extern const char *session_username(void); diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index fb08d67..f648f61 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -256,6 +256,14 @@ get_prompt(promptStatus_t status) buf[0] = '>'; break; + case 'r': + if (is_master()) + buf[0] = 'm'; + else + buf[0] = 'r'; + break; + + /* execute command */ case '`': {
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers