Skip to content

Commit 246a9ae

Browse files
author
Commitfest Bot
committed
[CF 5872] v6 - Add prompt option to display read-only and read/write states in psql
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5872 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/[email protected] Author(s): Jim Jones
2 parents 9fcd487 + 2d194a7 commit 246a9ae

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5055,6 +5055,22 @@ testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
50555055
</listitem>
50565056
</varlistentry>
50575057

5058+
<varlistentry id="app-psql-prompting-i">
5059+
<term><literal>%i</literal></term>
5060+
<listitem>
5061+
<para>
5062+
Displays the session's read-only status as <literal>read-only</literal>
5063+
if the current transaction is read-only (<literal>transaction_read_only</literal>
5064+
is <literal>on</literal>), the server is in hot standby
5065+
(<literal>in_hot_standby</literal> is <literal>on</literal>), or the
5066+
default transaction mode is read-only (<literal>default_transaction_read_only</literal>
5067+
is <literal>on</literal>); otherwise displays <literal>read-write</literal>.
5068+
Useful for identifying sessions that cannot perform writes, such as in
5069+
replication setups or when explicit read-only transactions are in use.
5070+
</para>
5071+
</listitem>
5072+
</varlistentry>
5073+
50585074
<varlistentry id="app-psql-prompting-x">
50595075
<term><literal>%x</literal></term>
50605076
<listitem>

src/bin/psql/prompt.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
* or a ! if session is not connected to a database;
4545
* in prompt2 -, *, ', or ";
4646
* in prompt3 nothing
47+
* %i - displays "read-only" if in hot standby, or if default_transaction_read_only
48+
* or transaction_read_only are on, "read/write" otherwise.
4749
* %x - transaction status: empty, *, !, ? (unknown or no connection)
4850
* %l - The line number inside the current statement, starting from 1.
4951
* %? - the error code of the last query (not yet implemented)
@@ -258,7 +260,39 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
258260
break;
259261
}
260262
break;
263+
case 'i':
264+
if (pset.db)
265+
{
266+
const char *hs = PQparameterStatus(pset.db, "in_hot_standby");
267+
const char *ro = PQparameterStatus(pset.db, "default_transaction_read_only");
268+
269+
if (!hs || !ro)
270+
strlcpy(buf, _("unknown"), sizeof(buf));
271+
else if (strcmp(hs, "on") == 0 || strcmp(ro, "on") == 0)
272+
strlcpy(buf, _("read-only"), sizeof(buf));
273+
else
274+
{
275+
const char *tr = NULL;
276+
PGresult *res;
277+
278+
res = PQexec(pset.db, "SHOW transaction_read_only");
279+
if (PQresultStatus(res) == PGRES_TUPLES_OK &&
280+
PQntuples(res) == 1)
281+
tr = PQgetvalue(res, 0, 0);
282+
283+
if (!tr)
284+
strlcpy(buf, _("unknown"), sizeof(buf));
285+
else if (strcmp(tr, "on") == 0)
286+
strlcpy(buf, _("read-only"), sizeof(buf));
287+
else
288+
strlcpy(buf, _("read/write"), sizeof(buf));
261289

290+
PQclear(res);
291+
}
292+
}
293+
else
294+
buf[0] = '\0';
295+
break;
262296
case 'x':
263297
if (!pset.db)
264298
buf[0] = '?';

0 commit comments

Comments
 (0)