Skip to content

Commit d1a6a08

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 2e2a232 commit d1a6a08

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
@@ -45,6 +45,7 @@
4545
#include "replication/logical.h"
4646
#include "replication/logicallauncher.h"
4747
#include "replication/origin.h"
48+
#include "replication/snapbuild.h"
4849
#include "replication/syncrep.h"
4950
#include "replication/walsender.h"
5051
#include "storage/condition_variable.h"
@@ -2582,6 +2583,9 @@ AbortTransaction(void)
25822583
/* Forget about any active REINDEX. */
25832584
ResetReindexState(s->nestingLevel);
25842585

2586+
/* Reset snapshot export state. */
2587+
SnapBuildResetExportedSnapshotState();
2588+
25852589
/* If in parallel mode, clean up workers and exit parallel mode. */
25862590
if (IsInParallelMode())
25872591
{
@@ -4692,6 +4696,11 @@ AbortSubTransaction(void)
46924696
/* Forget about any active REINDEX. */
46934697
ResetReindexState(s->nestingLevel);
46944698

4699+
/*
4700+
* No need for SnapBuildResetExportedSnapshotState() here, snapshot
4701+
* exports are not supported in subtransactions.
4702+
*/
4703+
46954704
/* Exit from parallel mode, if necessary. */
46964705
if (IsInParallelMode())
46974706
{

src/backend/replication/logical/snapbuild.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,17 +697,33 @@ SnapBuildGetOrBuildSnapshot(SnapBuild *builder, TransactionId xid)
697697
void
698698
SnapBuildClearExportedSnapshot(void)
699699
{
700+
ResourceOwner tmpResOwner;
701+
700702
/* nothing exported, that is the usual case */
701703
if (!ExportInProgress)
702704
return;
703705

704706
if (!IsTransactionState())
705707
elog(ERROR, "clearing exported snapshot in wrong transaction state");
706708

707-
/* make sure nothing could have ever happened */
709+
/*
710+
* AbortCurrentTransaction() takes care of resetting the snapshot state,
711+
* so remember SavedResourceOwnerDuringExport.
712+
*/
713+
tmpResOwner = SavedResourceOwnerDuringExport;
714+
715+
/* make sure nothing could have ever happened */
708716
AbortCurrentTransaction();
709717

710-
CurrentResourceOwner = SavedResourceOwnerDuringExport;
718+
CurrentResourceOwner = tmpResOwner;
719+
}
720+
721+
/*
722+
* Clear snapshot export state during transaction abort.
723+
*/
724+
void
725+
SnapBuildResetExportedSnapshotState(void)
726+
{
711727
SavedResourceOwnerDuringExport = NULL;
712728
ExportInProgress = false;
713729
}

src/include/replication/snapbuild.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ extern void SnapBuildSnapDecRefcount(Snapshot snap);
6969
extern Snapshot SnapBuildInitialSnapshot(SnapBuild *builder);
7070
extern const char *SnapBuildExportSnapshot(SnapBuild *snapstate);
7171
extern void SnapBuildClearExportedSnapshot(void);
72+
extern void SnapBuildResetExportedSnapshotState(void);
7273

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

0 commit comments

Comments
 (0)