diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index 6e03343b464..843a8478436 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -45,6 +45,7 @@ #include "replication/logical.h" #include "replication/logicallauncher.h" #include "replication/origin.h" +#include "replication/snapbuild.h" #include "replication/syncrep.h" #include "replication/walsender.h" #include "storage/condition_variable.h" @@ -2582,6 +2583,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()) { @@ -4692,6 +4696,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()) { diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c index 1c7dc68d743..1010a2e869f 100644 --- a/src/backend/replication/logical/snapbuild.c +++ b/src/backend/replication/logical/snapbuild.c @@ -697,6 +697,8 @@ SnapBuildGetOrBuildSnapshot(SnapBuild *builder, TransactionId xid) void SnapBuildClearExportedSnapshot(void) { + ResourceOwner tmpResOwner; + /* nothing exported, that is the usual case */ if (!ExportInProgress) return; @@ -704,10 +706,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; } diff --git a/src/include/replication/snapbuild.h b/src/include/replication/snapbuild.h index 7653717f83f..b95f56eec3d 100644 --- a/src/include/replication/snapbuild.h +++ b/src/include/replication/snapbuild.h @@ -69,6 +69,7 @@ extern void SnapBuildSnapDecRefcount(Snapshot snap); extern Snapshot SnapBuildInitialSnapshot(SnapBuild *builder); extern const char *SnapBuildExportSnapshot(SnapBuild *snapstate); extern void SnapBuildClearExportedSnapshot(void); +extern void SnapBuildResetExportedSnapshotState(void); extern SnapBuildState SnapBuildCurrentState(SnapBuild *snapstate); extern Snapshot SnapBuildGetOrBuildSnapshot(SnapBuild *builder,