Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc1664c8

Browse files
Fix pg_sequence_last_value() for unlogged sequences on standbys.
Presently, when this function is called for an unlogged sequence ona standby server, it will error out with a message likeERROR: could not open file "base/5/16388": No such file or directorySince the pg_sequences system view uses pg_sequence_last_value(),it can error similarly. To fix, modify the function to return NULLfor unlogged sequences on standby servers. Since this bug ispresent on all versions since v15, this approach is preferable tomaking the ERROR nicer because we need to repair the pg_sequencesview without modifying its definition on released versions. Forconsistency, this commit also modifies the function to return NULLfor other sessions' temporary sequences. The pg_sequences viewalready appropriately filters out such sequences, so there's no bugthere, but we might as well offer some defense in case someoneinvokes this function directly.Unlogged sequences were first introduced in v15, but temporarysequences are much older, so while the fix for unlogged sequencesis only back-patched to v15, the temporary sequence portion isback-patched to all supported versions.We could also remove the privilege check in the pg_sequences viewdefinition in v18 if we modify this function to return NULL forsequences for which the current user lacks privileges, but that isleft as a future exercise for when v18 development begins.Reviewed-by: Tom Lane, Michael PaquierDiscussion:https://postgr.es/m/20240501005730.GA594666%40nathanxps13Backpatch-through: 12
1 parent52ea653 commitc1664c8

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

‎doc/src/sgml/system-views.sgml

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3009,15 +3009,36 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
30093009
<para>
30103010
The last sequence value written to disk. If caching is used,
30113011
this value can be greater than the last value handed out from the
3012-
sequence. Null if the sequence has not been read from yet. Also, if
3013-
the current user does not have <literal>USAGE</literal>
3014-
or <literal>SELECT</literal> privilege on the sequence, the value is
3015-
null.
3012+
sequence.
30163013
</para></entry>
30173014
</row>
30183015
</tbody>
30193016
</tgroup>
30203017
</table>
3018+
3019+
<para>
3020+
The <structfield>last_value</structfield> column will read as null if any of
3021+
the following are true:
3022+
<itemizedlist>
3023+
<listitem>
3024+
<para>
3025+
The sequence has not been read from yet.
3026+
</para>
3027+
</listitem>
3028+
<listitem>
3029+
<para>
3030+
The current user does not have <literal>USAGE</literal> or
3031+
<literal>SELECT</literal> privilege on the sequence.
3032+
</para>
3033+
</listitem>
3034+
<listitem>
3035+
<para>
3036+
The sequence is unlogged and the server is a standby.
3037+
</para>
3038+
</listitem>
3039+
</itemizedlist>
3040+
</para>
3041+
30213042
</sect1>
30223043

30233044
<sect1 id="view-pg-settings">

‎src/backend/commands/sequence.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,11 +1795,8 @@ pg_sequence_last_value(PG_FUNCTION_ARGS)
17951795
Oidrelid=PG_GETARG_OID(0);
17961796
SeqTableelm;
17971797
Relationseqrel;
1798-
Bufferbuf;
1799-
HeapTupleDataseqtuple;
1800-
Form_pg_sequence_dataseq;
1801-
boolis_called;
1802-
int64result;
1798+
boolis_called= false;
1799+
int64result=0;
18031800

18041801
/* open and lock sequence */
18051802
init_sequence(relid,&elm,&seqrel);
@@ -1810,12 +1807,28 @@ pg_sequence_last_value(PG_FUNCTION_ARGS)
18101807
errmsg("permission denied for sequence %s",
18111808
RelationGetRelationName(seqrel))));
18121809

1813-
seq=read_seq_tuple(seqrel,&buf,&seqtuple);
1810+
/*
1811+
* We return NULL for other sessions' temporary sequences. The
1812+
* pg_sequences system view already filters those out, but this offers a
1813+
* defense against ERRORs in case someone invokes this function directly.
1814+
*
1815+
* Also, for the benefit of the pg_sequences view, we return NULL for
1816+
* unlogged sequences on standbys instead of throwing an error.
1817+
*/
1818+
if (!RELATION_IS_OTHER_TEMP(seqrel)&&
1819+
(RelationIsPermanent(seqrel)|| !RecoveryInProgress()))
1820+
{
1821+
Bufferbuf;
1822+
HeapTupleDataseqtuple;
1823+
Form_pg_sequence_dataseq;
1824+
1825+
seq=read_seq_tuple(seqrel,&buf,&seqtuple);
18141826

1815-
is_called=seq->is_called;
1816-
result=seq->last_value;
1827+
is_called=seq->is_called;
1828+
result=seq->last_value;
18171829

1818-
UnlockReleaseBuffer(buf);
1830+
UnlockReleaseBuffer(buf);
1831+
}
18191832
relation_close(seqrel,NoLock);
18201833

18211834
if (is_called)

‎src/test/recovery/t/001_stream_rep.pl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@
7676
print"standby 2:$result\n";
7777
is($result,qq(33|0|t),'check streamed sequence content on standby 2');
7878

79+
# Check pg_sequence_last_value() returns NULL for unlogged sequence on standby
80+
$node_primary->safe_psql('postgres',
81+
"CREATE UNLOGGED SEQUENCE ulseq; SELECT nextval('ulseq')");
82+
$node_primary->wait_for_replay_catchup($node_standby_1);
83+
is($node_standby_1->safe_psql('postgres',
84+
"SELECT pg_sequence_last_value('ulseq'::regclass) IS NULL"),
85+
't','pg_sequence_last_value() on unlogged sequence on standby 1');
86+
7987
# Check that only READ-only queries can run on standbys
8088
is($node_standby_1->psql('postgres','INSERT INTO tab_int VALUES (1)'),
8189
3,'read-only queries on standby 1');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp