summaryrefslogtreecommitdiff
path: root/src/backend/replication/logical/snapbuild.c
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2021-10-18 11:56:54 +0900
committerMichael Paquier <michael@paquier.xyz>2021-10-18 11:56:54 +0900
commita207b8521346bf9575d7d0c7585a3de8250d4ef8 (patch)
treee875f562d175cb519b8851c4d330d10a1f4e2e0f /src/backend/replication/logical/snapbuild.c
parent5b0b2983a095f2f6ed18d9c3387582e4b33e95a1 (diff)
Reset properly snapshot export state during transaction abort
During a replication slot creation, an ERROR generated in the same transaction as the one creating a to-be-exported snapshot would have left the backend in an inconsistent state, as the associated static export snapshot state was not being reset on transaction abort, but only on the follow-up command received by the WAL sender that created this snapshot on replication slot creation. This would trigger inconsistency failures if this session tried to export again a snapshot, like during the creation of a replication slot. Note that a snapshot export cannot happen in a transaction block, so there is no need to worry resetting this state for subtransaction aborts. Also, this inconsistent state would very unlikely show up to users. For example, one case where this could happen is an out-of-memory error when building the initial snapshot to-be-exported. Dilip found this problem while poking at a different patch, that caused an error in this code path for reasons unrelated to HEAD. Author: Dilip Kumar Reviewed-by: Michael Paquier, Zhihong Yu Discussion: https://postgr.es/m/CAFiTN-s0zA1Kj0ozGHwkYkHwa5U0zUE94RSc_g81WrpcETB5=w@mail.gmail.com Backpatch-through: 9.6
Diffstat (limited to 'src/backend/replication/logical/snapbuild.c')
-rw-r--r--src/backend/replication/logical/snapbuild.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c
index 4ec0003550e..5a1bce5acc0 100644
--- a/src/backend/replication/logical/snapbuild.c
+++ b/src/backend/replication/logical/snapbuild.c
@@ -696,6 +696,8 @@ SnapBuildGetOrBuildSnapshot(SnapBuild *builder, TransactionId xid)
void
SnapBuildClearExportedSnapshot(void)
{
+ ResourceOwner tmpResOwner;
+
/* nothing exported, that is the usual case */
if (!ExportInProgress)
return;
@@ -703,10 +705,24 @@ SnapBuildClearExportedSnapshot(void)
if (!IsTransactionState())
elog(ERROR, "clearing exported snapshot in wrong transaction state");
- /* make sure nothing could have ever happened */
+ /*
+ * AbortCurrentTransaction() takes care of resetting the snapshot state,
+ * so remember SavedResourceOwnerDuringExport.
+ */
+ tmpResOwner = SavedResourceOwnerDuringExport;
+
+ /* make sure nothing could have ever happened */
AbortCurrentTransaction();
- CurrentResourceOwner = SavedResourceOwnerDuringExport;
+ CurrentResourceOwner = tmpResOwner;
+}
+
+/*
+ * Clear snapshot export state during transaction abort.
+ */
+void
+SnapBuildResetExportedSnapshotState(void)
+{
SavedResourceOwnerDuringExport = NULL;
ExportInProgress = false;
}