agora inbox for [email protected]
help / color / mirror / Atom feedFrom: Nathan Bossart <[email protected]>
Subject: [PATCH v4 1/1] Fix pg_sequence_last_value() for unlogged sequences on standbys.
Date: Fri, 10 May 2024 15:55:24 -0500
Presently, when this function is called for an unlogged sequence on
a standby server, it will error out with a message like
ERROR: could not open file "base/5/16388": No such file or directory
Since the pg_sequences system view uses pg_sequence_last_value(),
it can error similarly. To fix, modify the function to return NULL
for unlogged sequences on standby servers. Since this bug is
present on all versions since v15, this approach is preferable to
making the ERROR nicer because we need to repair the pg_sequences
view without modifying its definition on released versions. For
consistency, this commit also modifies the function to return NULL
for other sessions' temporary sequences. The pg_sequences view
already appropriately filters out such sequences, so there's no bug
there, but we might as well offer some defense in case someone
invokes this function directly.
Unlogged sequences were first introduced in v15, but temporary
sequences are much older, so while the fix for unlogged sequences
is only back-patched to v15, the temporary sequence portion is
back-patched to all supported versions.
We could also remove the privilege check in the pg_sequences view
definition in v18 if we modify this function to return NULL for
sequences for which the current user lacks privileges, but that is
left as a future exercise for when v18 development begins.
Reviewed-by: Tom Lane, Michael Paquier
Discussion: https://postgr.es/m/20240501005730.GA594666%40nathanxps13
Backpatch-through: 12
---
doc/src/sgml/catalogs.sgml | 29 +++++++++++++++++++++++++----
src/backend/commands/sequence.c | 27 ++++++++++++++++++---------
2 files changed, 43 insertions(+), 13 deletions(-)
diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml
index 118e325464..2337ce8fd2 100644
--- a/doc/src/sgml/catalogs.sgml
+++ b/doc/src/sgml/catalogs.sgml
@@ -10381,14 +10381,35 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
<entry></entry>
<entry>The last sequence value written to disk. If caching is used,
this value can be greater than the last value handed out from the
- sequence. Null if the sequence has not been read from yet. Also, if
- the current user does not have <literal>USAGE</literal>
- or <literal>SELECT</literal> privilege on the sequence, the value is
- null.</entry>
+ sequence.</entry>
</row>
</tbody>
</tgroup>
</table>
+
+ <para>
+ The <structfield>last_value</structfield> column will read as null if any of
+ the following are true:
+ <itemizedlist>
+ <listitem>
+ <para>
+ The sequence has not been read from yet.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ The current user does not have <literal>USAGE</literal> or
+ <literal>SELECT</literal> privilege on the sequence.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ The sequence is a temporary sequence created by another session.
+ </para>
+ </listitem>
+ </itemizedlist>
+ </para>
+
</sect1>
<sect1 id="view-pg-settings">
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index 0577184f82..daaf8ee3d4 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -1857,11 +1857,8 @@ pg_sequence_last_value(PG_FUNCTION_ARGS)
Oid relid = PG_GETARG_OID(0);
SeqTable elm;
Relation seqrel;
- Buffer buf;
- HeapTupleData seqtuple;
- Form_pg_sequence_data seq;
- bool is_called;
- int64 result;
+ bool is_called = false;
+ int64 result = 0;
/* open and lock sequence */
init_sequence(relid, &elm, &seqrel);
@@ -1872,12 +1869,24 @@ pg_sequence_last_value(PG_FUNCTION_ARGS)
errmsg("permission denied for sequence %s",
RelationGetRelationName(seqrel))));
- seq = read_seq_tuple(seqrel, &buf, &seqtuple);
+ /*
+ * We return NULL for other sessions' temporary sequences. The
+ * pg_sequences system view already filters those out, but this offers a
+ * defense against ERRORs in case someone invokes this function directly.
+ */
+ if (!RELATION_IS_OTHER_TEMP(seqrel))
+ {
+ Buffer buf;
+ HeapTupleData seqtuple;
+ Form_pg_sequence_data seq;
- is_called = seq->is_called;
- result = seq->last_value;
+ seq = read_seq_tuple(seqrel, &buf, &seqtuple);
- UnlockReleaseBuffer(buf);
+ is_called = seq->is_called;
+ result = seq->last_value;
+
+ UnlockReleaseBuffer(buf);
+ }
relation_close(seqrel, NoLock);
if (is_called)
--
2.25.1
--Q68bSM7Ycu6FN28Q--
view thread (7+ messages)
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]
Subject: Re: [PATCH v4 1/1] Fix pg_sequence_last_value() for unlogged sequences on standbys.
In-Reply-To: <no-message-id-66413@localhost>
* 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