Clean up after erroneous SELECT FOR UPDATE/SHARE on a sequence.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 2 Jun 2011 19:31:28 +0000 (15:31 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 2 Jun 2011 19:31:28 +0000 (15:31 -0400)
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.

src/backend/commands/sequence.c

index 9404efa379f352b8b16efb3e32abd7f3d0d94436..84ac3a0d0f7cd69f16fa5cecafac1b6d14d09658 100644 (file)
@@ -950,6 +950,22 @@ read_info(SeqTable elm, Relation rel, Buffer *buf)
    Assert(ItemIdIsUsed(lp));
    tuple.t_data = (HeapTupleHeader) PageGetItem((Page) page, lp);
 
+   /*
+    * Previous releases of Postgres neglected to prevent SELECT FOR UPDATE
+    * on a sequence, which would leave a non-frozen XID in the sequence
+    * tuple's xmax, which eventually leads to clog access failures or worse.
+    * If we see this has happened, clean up after it.  We treat this like a
+    * hint bit update, ie, don't bother to WAL-log it, since we can certainly
+    * do this again if the update gets lost.
+    */
+   if (HeapTupleHeaderGetXmax(tuple.t_data) != InvalidTransactionId)
+   {
+       HeapTupleHeaderSetXmax(tuple.t_data, InvalidTransactionId);
+       tuple.t_data->t_infomask &= ~HEAP_XMAX_COMMITTED;
+       tuple.t_data->t_infomask |= HEAP_XMAX_INVALID;
+       SetBufferCommitInfoNeedsSave(*buf);
+   }
+
    seq = (Form_pg_sequence) GETSTRUCT(&tuple);
 
    elm->increment = seq->increment_by;