Skip to content

Commit b767402

Browse files
jimjonesbrCommitfest Bot
authored andcommitted
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 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.
1 parent b463288 commit b767402

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5044,6 +5044,20 @@ testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
50445044
</listitem>
50455045
</varlistentry>
50465046

5047+
<varlistentry id="app-psql-prompting-i">
5048+
<term><literal>%i</literal></term>
5049+
<listitem>
5050+
<para>
5051+
Displays the session's read-only status as <literal>read-only</literal>
5052+
if the server is in hot standby (<literal>in_hot_standby</literal> is
5053+
<literal>on</literal>) or the default transaction mode is read-only
5054+
(<literal>default_transaction_read_only</literal> is <literal>on</literal>),
5055+
or <literal>read-write</literal> otherwise. Useful for identifying
5056+
sessions that cannot perform writes, such as in replication setups.
5057+
</para>
5058+
</listitem>
5059+
</varlistentry>
5060+
50475061
<varlistentry id="app-psql-prompting-x">
50485062
<term><literal>%x</literal></term>
50495063
<listitem>

src/bin/psql/prompt.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
* or a ! if session is not connected to a database;
4444
* in prompt2 -, *, ', or ";
4545
* in prompt3 nothing
46+
* %i - displays "read-only" if in hot standby or default_transaction_read_only
47+
* is on, "read/write" otherwise.
4648
* %x - transaction status: empty, *, !, ? (unknown or no connection)
4749
* %l - The line number inside the current statement, starting from 1.
4850
* %? - the error code of the last query (not yet implemented)
@@ -247,7 +249,21 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
247249
break;
248250
}
249251
break;
252+
case 'i':
253+
if (pset.db)
254+
{
255+
const char *hs = PQparameterStatus(pset.db, "in_hot_standby");
256+
const char *ro = PQparameterStatus(pset.db, "default_transaction_read_only");
250257

258+
if ((hs && strcmp(hs, "on") == 0) ||
259+
(ro && strcmp(ro, "on") == 0))
260+
strlcpy(buf, "read-only", sizeof(buf));
261+
else
262+
strlcpy(buf, "read/write", sizeof(buf));
263+
}
264+
else
265+
buf[0] = '\0';
266+
break;
251267
case 'x':
252268
if (!pset.db)
253269
buf[0] = '?';

0 commit comments

Comments
 (0)