Skip to content

Commit 19fed64

Browse files
committed
Clean up after erroneous SELECT FOR UPDATE/SHARE on a sequence.
My previous commit disallowed this operation, but did nothing about cleaning up the damage if one had already been done. With the operation disallowed, it's okay to just forcibly clear xmax in a sequence's tuple, since any value seen there could not represent a live transaction's lock. So, any sequence-specific operation will repair the problem automatically, whether or not the user has already seen "could not access status of transaction" failures.
1 parent d369ddd commit 19fed64

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/backend/commands/sequence.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,22 @@ read_info(SeqTable elm, Relation rel, Buffer *buf)
949949
Assert(ItemIdIsNormal(lp));
950950
tuple.t_data = (HeapTupleHeader) PageGetItem((Page) page, lp);
951951

952+
/*
953+
* Previous releases of Postgres neglected to prevent SELECT FOR UPDATE
954+
* on a sequence, which would leave a non-frozen XID in the sequence
955+
* tuple's xmax, which eventually leads to clog access failures or worse.
956+
* If we see this has happened, clean up after it. We treat this like a
957+
* hint bit update, ie, don't bother to WAL-log it, since we can certainly
958+
* do this again if the update gets lost.
959+
*/
960+
if (HeapTupleHeaderGetXmax(tuple.t_data) != InvalidTransactionId)
961+
{
962+
HeapTupleHeaderSetXmax(tuple.t_data, InvalidTransactionId);
963+
tuple.t_data->t_infomask &= ~HEAP_XMAX_COMMITTED;
964+
tuple.t_data->t_infomask |= HEAP_XMAX_INVALID;
965+
SetBufferCommitInfoNeedsSave(*buf);
966+
}
967+
952968
seq = (Form_pg_sequence) GETSTRUCT(&tuple);
953969

954970
/* this is a handy place to update our copy of the increment */

0 commit comments

Comments
 (0)