Skip to content

Commit f49bf82

Browse files
committed
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
1 parent ae6abeb commit f49bf82

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/backend/access/transam/xact.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "pgstat.h"
4545
#include "replication/logical.h"
4646
#include "replication/origin.h"
47+
#include "replication/snapbuild.h"
4748
#include "replication/syncrep.h"
4849
#include "replication/walsender.h"
4950
#include "storage/fd.h"
@@ -2553,6 +2554,9 @@ AbortTransaction(void)
25532554
/* Forget about any active REINDEX. */
25542555
ResetReindexState(s->nestingLevel);
25552556

2557+
/* Reset snapshot export state. */
2558+
SnapBuildResetExportedSnapshotState();
2559+
25562560
/* If in parallel mode, clean up workers and exit parallel mode. */
25572561
if (IsInParallelMode())
25582562
{
@@ -4658,6 +4662,11 @@ AbortSubTransaction(void)
46584662
/* Forget about any active REINDEX. */
46594663
ResetReindexState(s->nestingLevel);
46604664

4665+
/*
4666+
* No need for SnapBuildResetExportedSnapshotState() here, snapshot
4667+
* exports are not supported in subtransactions.
4668+
*/
4669+
46614670
/* Exit from parallel mode, if necessary. */
46624671
if (IsInParallelMode())
46634672
{

src/backend/replication/logical/snapbuild.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,17 +679,33 @@ SnapBuildGetOrBuildSnapshot(SnapBuild *builder, TransactionId xid)
679679
void
680680
SnapBuildClearExportedSnapshot(void)
681681
{
682+
ResourceOwner tmpResOwner;
683+
682684
/* nothing exported, that is the usual case */
683685
if (!ExportInProgress)
684686
return;
685687

686688
if (!IsTransactionState())
687689
elog(ERROR, "clearing exported snapshot in wrong transaction state");
688690

689-
/* make sure nothing could have ever happened */
691+
/*
692+
* AbortCurrentTransaction() takes care of resetting the snapshot state,
693+
* so remember SavedResourceOwnerDuringExport.
694+
*/
695+
tmpResOwner = SavedResourceOwnerDuringExport;
696+
697+
/* make sure nothing could have ever happened */
690698
AbortCurrentTransaction();
691699

692-
CurrentResourceOwner = SavedResourceOwnerDuringExport;
700+
CurrentResourceOwner = tmpResOwner;
701+
}
702+
703+
/*
704+
* Clear snapshot export state during transaction abort.
705+
*/
706+
void
707+
SnapBuildResetExportedSnapshotState(void)
708+
{
693709
SavedResourceOwnerDuringExport = NULL;
694710
ExportInProgress = false;
695711
}

src/include/replication/snapbuild.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extern void SnapBuildSnapDecRefcount(Snapshot snap);
6868

6969
extern const char *SnapBuildExportSnapshot(SnapBuild *snapstate);
7070
extern void SnapBuildClearExportedSnapshot(void);
71+
extern void SnapBuildResetExportedSnapshotState(void);
7172

7273
extern SnapBuildState SnapBuildCurrentState(SnapBuild *snapstate);
7374
extern Snapshot SnapBuildGetOrBuildSnapshot(SnapBuild *builder,

0 commit comments

Comments
 (0)