]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Reset properly snapshot export state during transaction abort
authorMichael Paquier <michael@paquier.xyz>
Mon, 18 Oct 2021 02:57:07 +0000 (11:57 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 18 Oct 2021 02:57:07 +0000 (11:57 +0900)
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

src/backend/access/transam/xact.c
src/backend/replication/logical/snapbuild.c
src/include/replication/snapbuild.h

index 2ccb582100e1399181e0bb7db4d2330d93b58f33..70ab926e3113e56c13c919ab58407eab6b305a09 100644 (file)
@@ -44,6 +44,7 @@
 #include "pgstat.h"
 #include "replication/logical.h"
 #include "replication/origin.h"
+#include "replication/snapbuild.h"
 #include "replication/syncrep.h"
 #include "replication/walsender.h"
 #include "storage/fd.h"
@@ -2553,6 +2554,9 @@ AbortTransaction(void)
        /* Forget about any active REINDEX. */
        ResetReindexState(s->nestingLevel);
 
+       /* Reset snapshot export state. */
+       SnapBuildResetExportedSnapshotState();
+
        /* If in parallel mode, clean up workers and exit parallel mode. */
        if (IsInParallelMode())
        {
@@ -4658,6 +4662,11 @@ AbortSubTransaction(void)
        /* Forget about any active REINDEX. */
        ResetReindexState(s->nestingLevel);
 
+       /*
+        * No need for SnapBuildResetExportedSnapshotState() here, snapshot
+        * exports are not supported in subtransactions.
+        */
+
        /* Exit from parallel mode, if necessary. */
        if (IsInParallelMode())
        {
index bba558705d15836405f7f3b2535e08e8a99a2393..51260bce03817de5c10e3c96a524c0ef0eb2b3a0 100644 (file)
@@ -679,6 +679,8 @@ SnapBuildGetOrBuildSnapshot(SnapBuild *builder, TransactionId xid)
 void
 SnapBuildClearExportedSnapshot(void)
 {
+       ResourceOwner tmpResOwner;
+
        /* nothing exported, that is the usual case */
        if (!ExportInProgress)
                return;
@@ -686,10 +688,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;
 }
index 80e91c9c254c6075a7926ca410b874cca34fb312..f0cc3a7022a21f458e4460240d292ab5e4181005 100644 (file)
@@ -68,6 +68,7 @@ extern void SnapBuildSnapDecRefcount(Snapshot snap);
 
 extern const char *SnapBuildExportSnapshot(SnapBuild *snapstate);
 extern void SnapBuildClearExportedSnapshot(void);
+extern void SnapBuildResetExportedSnapshotState(void);
 
 extern SnapBuildState SnapBuildCurrentState(SnapBuild *snapstate);
 extern Snapshot SnapBuildGetOrBuildSnapshot(SnapBuild *builder,