summaryrefslogtreecommitdiff
path: root/src/backend/access/transam/xact.c
diff options
context:
space:
mode:
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>2014-05-15 16:37:50 +0300
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>2014-05-15 16:37:50 +0300
commitbb38fb0d43c8d7ff54072bfd8bd63154e536b384 (patch)
tree766798e1a2f8a5752257bbd4409276281aa090f2 /src/backend/access/transam/xact.c
parentff810b4928882bfdc4ebe1ce603c47830aba3132 (diff)
Fix race condition in preparing a transaction for two-phase commit.
To lock a prepared transaction's shared memory entry, we used to mark it with the XID of the backend. When the XID was no longer active according to the proc array, the entry was implicitly considered as not locked anymore. However, when preparing a transaction, the backend's proc array entry was cleared before transfering the locks (and some other state) to the prepared transaction's dummy PGPROC entry, so there was a window where another backend could finish the transaction before it was in fact fully prepared. To fix, rewrite the locking mechanism of global transaction entries. Instead of an XID, just have simple locked-or-not flag in each entry (we store the locking backend's backend id rather than a simple boolean, but that's just for debugging purposes). The backend is responsible for explicitly unlocking the entry, and to make sure that that happens, install a callback to unlock it on abort or process exit. Backpatch to all supported versions.
Diffstat (limited to 'src/backend/access/transam/xact.c')
-rw-r--r--src/backend/access/transam/xact.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 3e744097c79..5b5d31b33dc 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -2231,9 +2231,13 @@ PrepareTransaction(void)
ProcArrayClearTransaction(MyProc);
/*
- * This is all post-transaction cleanup. Note that if an error is raised
- * here, it's too late to abort the transaction. This should be just
- * noncritical resource releasing. See notes in CommitTransaction.
+ * In normal commit-processing, this is all non-critical post-transaction
+ * cleanup. When the transaction is prepared, however, it's important that
+ * the locks and other per-backend resources are transfered to the
+ * prepared transaction's PGPROC entry. Note that if an error is raised
+ * here, it's too late to abort the transaction. XXX: This probably should
+ * be in a critical section, to force a PANIC if any of this fails, but
+ * that cure could be worse than the disease.
*/
CallXactCallbacks(XACT_EVENT_PREPARE);
@@ -2268,6 +2272,14 @@ PrepareTransaction(void)
RESOURCE_RELEASE_AFTER_LOCKS,
true, true);
+ /*
+ * Allow another backend to finish the transaction. After
+ * PostPrepare_Twophase(), the transaction is completely detached from
+ * our backend. The rest is just non-critical cleanup of backend-local
+ * state.
+ */
+ PostPrepare_Twophase();
+
/* Check we've released all catcache entries */
AtEOXact_CatCache(true);
@@ -2394,6 +2406,7 @@ AbortTransaction(void)
AtEOXact_LargeObject(false);
AtAbort_Notify();
AtEOXact_RelationMap(false);
+ AtAbort_Twophase();
/*
* Advertise the fact that we aborted in pg_clog (assuming that we got as