diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 1a339600bc4..662dfb2ed58 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -5044,6 +5044,20 @@ testdb=> INSERT INTO my_table VALUES (:'content');
+
+ %i
+
+
+ Displays the session's read-only status as read-only
+ if the server is in hot standby (in_hot_standby is
+ on) or the default transaction mode is read-only
+ (default_transaction_read_only is on),
+ or read-write otherwise. Useful for identifying
+ sessions that cannot perform writes, such as in replication setups.
+
+
+
+
%x
diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index b08d7328fbf..1d0eca5cfc0 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,21 @@ 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 && strcmp(hs, "on") == 0) ||
+ (ro && 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] = '?';