public inbox for [email protected]
help / color / mirror / Atom feedFrom: Jim Jones <[email protected]>
To: Fujii Masao <[email protected]>
Cc: Srinath Reddy Sadipiralla <[email protected]>
Cc: Greg Sabino Mullane <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Subject: Re: display hot standby state in psql prompt
Date: Mon, 27 Oct 2025 09:08:17 +0100
Message-ID: <[email protected]> (raw)
In-Reply-To: <CAHGQGwHzNr71zQ7i73Jt55gnW0D-DYJtdoXHKEs0BgsjXAXw5Q@mail.gmail.com>
References: <[email protected]>
<CAKAnmm+qDTsofqT6ShjjpKn3+WMNovttomwO0mTYcZgg7R7kDQ@mail.gmail.com>
<[email protected]>
<CAKAnmmLf3Qpv54zM2u5N5XbbGDONtiz3F6yhM142O2mdQ3=f_A@mail.gmail.com>
<[email protected]>
<CAKAnmmJD=hCvFes5mrcn_PdmY5C8sioWxSoN_jzikZrrSeOMkw@mail.gmail.com>
<[email protected]>
<CAFC+b6oEYiW4Nh7vVWyYi5n6g1ac2=E71JP5NaoT2ARjzsTR6g@mail.gmail.com>
<[email protected]>
<CAHGQGwHnx96uspkmOsrpytew0A25wm4ozpe2=GzZpHoAph+t0g@mail.gmail.com>
<[email protected]>
<CAHGQGwHzNr71zQ7i73Jt55gnW0D-DYJtdoXHKEs0BgsjXAXw5Q@mail.gmail.com>
On 27/10/2025 04:32, Fujii Masao wrote:
> I think the "hs &&" and "ro &&" checks are no longer needed,
> since we've already confirmed they're not NULL at that point.
Right. Checks removed.
> Also, should "unknown" be marked for translation, as in the \conninfo code?
> I'm not sure whether showing a translated string in the psql prompt is
> desirable, though.
Here I am not sure if it's applicable. In this file there are other
strings that are not marked for translation, "abort", "on", "off". I
changed the string to _("unknown") for now, but we can remove it in
further iterations if we agree it's not desirable :)
v5 attached.
Thanks for the review.
Best, Jim
Attachments:
[text/x-patch] v5-0001-Add-i-prompt-escape-to-indicate-server-read-only-.patch (3.4K, ../[email protected]/2-v5-0001-Add-i-prompt-escape-to-indicate-server-read-only-.patch)
download | inline diff:
From 1323e4b555fbd63796a759d0b6741b8258ad88eb Mon Sep 17 00:00:00 2001
From: Jim Jones <[email protected]>
Date: Mon, 27 Oct 2025 08:36:48 +0100
Subject: [PATCH v5] Add %i prompt escape to indicate server read-only status
This patch introduces a new prompt escape `%i` for psql, which shows
whether the connected server is operating in read-only or read/write
mode. It expands to `read-only` if either the server is in hot standby
mode (`in_hot_standby = on`) or the session's default transaction
mode is read-only (`default_transaction_read_only = on`). Otherwise,
it displays `read/write`.
This is useful for distinguishing read-only sessions (e.g. connected
to a standby, or using a default read-only transaction mode) from
read/write ones at a glance, especially when working with multiple
connections in replicated or restricted environments.
---
doc/src/sgml/ref/psql-ref.sgml | 14 ++++++++++++++
src/bin/psql/prompt.c | 17 +++++++++++++++++
2 files changed, 31 insertions(+)
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 1a339600bc..662dfb2ed5 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -5044,6 +5044,20 @@ testdb=> <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
</listitem>
</varlistentry>
+ <varlistentry id="app-psql-prompting-i">
+ <term><literal>%i</literal></term>
+ <listitem>
+ <para>
+ Displays the session's read-only status as <literal>read-only</literal>
+ if the server is in hot standby (<literal>in_hot_standby</literal> is
+ <literal>on</literal>) or the default transaction mode is read-only
+ (<literal>default_transaction_read_only</literal> is <literal>on</literal>),
+ or <literal>read-write</literal> otherwise. Useful for identifying
+ sessions that cannot perform writes, such as in replication setups.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry id="app-psql-prompting-x">
<term><literal>%x</literal></term>
<listitem>
diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index b08d7328fb..fdcb779d43 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -43,6 +43,8 @@
* or a ! if session is not connected to a database;
* in prompt2 -, *, ', or ";
* in prompt3 nothing
+ * %i - displays "read-only" if in hot standby or default_transaction_read_only
+ * is on, "read/write" otherwise.
* %x - transaction status: empty, *, !, ? (unknown or no connection)
* %l - The line number inside the current statement, starting from 1.
* %? - the error code of the last query (not yet implemented)
@@ -247,7 +249,22 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
break;
}
break;
+ case 'i':
+ if (pset.db)
+ {
+ const char *hs = PQparameterStatus(pset.db, "in_hot_standby");
+ const char *ro = PQparameterStatus(pset.db, "default_transaction_read_only");
+ if (!hs || !ro)
+ strlcpy(buf, _("unknown"), sizeof(buf));
+ else if (strcmp(hs, "on") == 0 || strcmp(ro, "on") == 0)
+ strlcpy(buf, "read-only", sizeof(buf));
+ else
+ strlcpy(buf, "read/write", sizeof(buf));
+ }
+ else
+ buf[0] = '\0';
+ break;
case 'x':
if (!pset.db)
buf[0] = '?';
--
2.43.0
view thread (43+ messages) latest in thread
reply
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: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected]
Subject: Re: display hot standby state in psql prompt
In-Reply-To: <[email protected]>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox