Skip to content

Commit 2812059

Browse files
Fix pg_sequence_last_value() for unlogged sequences on standbys.
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
1 parent 157b1e6 commit 2812059

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/backend/commands/sequence.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,11 +1857,8 @@ pg_sequence_last_value(PG_FUNCTION_ARGS)
18571857
Oid relid = PG_GETARG_OID(0);
18581858
SeqTable elm;
18591859
Relation seqrel;
1860-
Buffer buf;
1861-
HeapTupleData seqtuple;
1862-
Form_pg_sequence_data seq;
1863-
bool is_called;
1864-
int64 result;
1860+
bool is_called = false;
1861+
int64 result = 0;
18651862

18661863
/* open and lock sequence */
18671864
init_sequence(relid, &elm, &seqrel);
@@ -1872,12 +1869,24 @@ pg_sequence_last_value(PG_FUNCTION_ARGS)
18721869
errmsg("permission denied for sequence %s",
18731870
RelationGetRelationName(seqrel))));
18741871

1875-
seq = read_seq_tuple(seqrel, &buf, &seqtuple);
1872+
/*
1873+
* We return NULL for other sessions' temporary sequences. The
1874+
* pg_sequences system view already filters those out, but this offers a
1875+
* defense against ERRORs in case someone invokes this function directly.
1876+
*/
1877+
if (!RELATION_IS_OTHER_TEMP(seqrel))
1878+
{
1879+
Buffer buf;
1880+
HeapTupleData seqtuple;
1881+
Form_pg_sequence_data seq;
18761882

1877-
is_called = seq->is_called;
1878-
result = seq->last_value;
1883+
seq = read_seq_tuple(seqrel, &buf, &seqtuple);
18791884

1880-
UnlockReleaseBuffer(buf);
1885+
is_called = seq->is_called;
1886+
result = seq->last_value;
1887+
1888+
UnlockReleaseBuffer(buf);
1889+
}
18811890
relation_close(seqrel, NoLock);
18821891

18831892
if (is_called)

0 commit comments

Comments
 (0)