#define SxactIsOnFinishedList(sxact) (!SHMQueueIsDetached(&((sxact)->finishedLink)))
+/*
+ * Note that a sxact is marked "prepared" once it has passed
+ * PreCommit_CheckForSerializationFailure, even if it isn't using
+ * 2PC. This is the point at which it can no longer be aborted.
+ *
+ * The PREPARED flag remains set after commit, so SxactIsCommitted
+ * implies SxactIsPrepared.
+ */
#define SxactIsCommitted(sxact) (((sxact)->flags & SXACT_FLAG_COMMITTED) != 0)
#define SxactIsPrepared(sxact) (((sxact)->flags & SXACT_FLAG_PREPARED) != 0)
#define SxactIsRolledBack(sxact) (((sxact)->flags & SXACT_FLAG_ROLLED_BACK) != 0)
*/
MySerializableXact->flags |= SXACT_FLAG_DOOMED;
MySerializableXact->flags |= SXACT_FLAG_ROLLED_BACK;
+ /*
+ * If the transaction was previously prepared, but is now failing due
+ * to a ROLLBACK PREPARED or (hopefully very rare) error after the
+ * prepare, clear the prepared flag. This simplifies conflict
+ * checking.
+ */
+ MySerializableXact->flags &= ~SXACT_FLAG_PREPARED;
}
if (!topLevelIsDeclaredReadOnly)
* - the writer committed before T2
* - the reader is a READ ONLY transaction and the reader was concurrent
* with T2 (= reader acquired its snapshot before T2 committed)
+ *
+ * We also handle the case that T2 is prepared but not yet committed
+ * here. In that case T2 has already checked for conflicts, so if it
+ * commits first, making the above conflict real, it's too late for it
+ * to abort.
*------------------------------------------------------------------------
*/
if (!failure)
{
SERIALIZABLEXACT *t2 = conflict->sxactIn;
- if (SxactIsCommitted(t2)
+ /*
+ * Note that if T2 is merely prepared but not yet committed, we
+ * rely on t->commitSeqNo being InvalidSerCommitSeqNo, which is
+ * larger than any valid commit sequence number.
+ */
+ if (SxactIsPrepared(t2)
&& (!SxactIsCommitted(reader)
|| t2->commitSeqNo <= reader->commitSeqNo)
&& (!SxactIsCommitted(writer)
}
/*------------------------------------------------------------------------
- * Check whether the reader has become a pivot with a committed writer:
+ * Check whether the reader has become a pivot with a writer
+ * that's committed (or prepared):
*
* T0 ------> R ------> W
* rw rw
* - T0 is READ ONLY, and overlaps the writer
*------------------------------------------------------------------------
*/
- if (!failure && SxactIsCommitted(writer) && !SxactIsReadOnly(reader))
+ if (!failure && SxactIsPrepared(writer) && !SxactIsReadOnly(reader))
{
if (SxactHasSummaryConflictIn(reader))
{
{
SERIALIZABLEXACT *t0 = conflict->sxactOut;
+ /*
+ * Note that if the writer is merely prepared but not yet
+ * committed, we rely on writer->commitSeqNo being
+ * InvalidSerCommitSeqNo, which is larger than any valid commit
+ * sequence number.
+ */
if (!SxactIsDoomed(t0)
&& (!SxactIsCommitted(t0)
|| t0->commitSeqNo >= writer->commitSeqNo)
ddd
(2 rows)
-INSERT INTO pxtest1 VALUES ('fff');
-- This should fail, because the two transactions have a write-skew anomaly
-PREPARE TRANSACTION 'foo5';
+INSERT INTO pxtest1 VALUES ('fff');
ERROR: could not serialize access due to read/write dependencies among transactions
-DETAIL: Canceled on commit attempt with conflict in from prepared pivot.
+DETAIL: Canceled on identification as a pivot, during write.
HINT: The transaction might succeed if retried.
+PREPARE TRANSACTION 'foo5';
SELECT gid FROM pg_prepared_xacts;
gid
------