| From: | Jim Jones <jim(dot)jones(at)uni-muenster(dot)de> |
|---|---|
| To: | Nathan Bossart <nathandbossart(at)gmail(dot)com> |
| Cc: | Fujii Masao <masao(dot)fujii(at)gmail(dot)com>, Srinath Reddy Sadipiralla <srinath2133(at)gmail(dot)com>, Greg Sabino Mullane <htamfids(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> |
| Subject: | Re: display hot standby state in psql prompt |
| Date: | 2025-10-31 21:00:51 |
| Message-ID: | 4cb32b3b-ae54-49f8-8252-33c45fd3286e@uni-muenster.de |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On 30/10/2025 11:16, Jim Jones wrote:
> 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
> {
> const char *tr = NULL;
> PGresult *res;
>
> res = PQexec(pset.db, "SHOW transaction_read_only");
> if (PQresultStatus(res) == PGRES_TUPLES_OK &&
> PQntuples(res) == 1)
> tr = PQgetvalue(res, 0, 0);
>
> if (!tr)
> strlcpy(buf, _("unknown"), sizeof(buf));
> else if (strcmp(tr, "on") == 0)
> strlcpy(buf, _("read-only"), sizeof(buf));
> else
> strlcpy(buf, _("read/write"), sizeof(buf));
>
> PQclear(res);
> }
While reviewing another patch, I had another idea to further minimise
the overhead of checking transaction_read_only, namely, to check it only
when within a transaction block:
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
{
PGTransactionStatusType tstatus = PQtransactionStatus(pset.db);
/*
* Check transaction_read_only only when in a transaction
* block. When idle (not in a transaction), the value of
* transaction_read_only is the same as
* default_transaction_read_only, which we already checked
* above. Avoiding the query improves performance,
* especially for prompt redisplays.
*/
if (tstatus == PQTRANS_IDLE)
strlcpy(buf, _("read/write"), sizeof(buf));
else
{
/* In a transaction block, need to check transaction_read_only */
const char *tr = NULL;
PGresult *res;
res = PQexec(pset.db, "SHOW transaction_read_only");
if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
tr = PQgetvalue(res, 0, 0);
if (!tr)
strlcpy(buf, _("unknown"), sizeof(buf));
else if (strcmp(tr, "on") == 0)
strlcpy(buf, _("read-only"), sizeof(buf));
else
strlcpy(buf, _("read/write"), sizeof(buf));
PQclear(res);
}
}
The idea is to skip the test if tstatus == PQTRANS_IDLE, which indicates
that it is not in a transaction block, making the check for
transaction_read_only unnecessary.
Thoughts on this approach?
v7 attached.
Best, Jim
| Attachment | Content-Type | Size |
|---|---|---|
| v7-0001-Add-i-prompt-escape-to-indicate-server-read-only-.patch | text/x-patch | 5.4 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Thomas Munro | 2025-10-31 21:13:38 | Re: meson's in-tree libpq header search order vs -Dextra_include_dirs |
| Previous Message | Jacob Champion | 2025-10-31 20:57:36 | Re: Channel binding for post-quantum cryptography |