Skip to content

Commit 8f4fe8d

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 0b5f557 commit 8f4fe8d

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
@@ -46,6 +46,7 @@
4646
#include "replication/logical.h"
4747
#include "replication/logicallauncher.h"
4848
#include "replication/origin.h"
49+
#include "replication/snapbuild.h"
4950
#include "replication/syncrep.h"
5051
#include "replication/walsender.h"
5152
#include "storage/condition_variable.h"
@@ -2677,6 +2678,9 @@ AbortTransaction(void)
26772678
/* Forget about any active REINDEX. */
26782679
ResetReindexState(s->nestingLevel);
26792680

2681+
/* Reset snapshot export state. */
2682+
SnapBuildResetExportedSnapshotState();
2683+
26802684
/* If in parallel mode, clean up workers and exit parallel mode. */
26812685
if (IsInParallelMode())
26822686
{
@@ -4987,6 +4991,11 @@ AbortSubTransaction(void)
49874991
/* Forget about any active REINDEX. */
49884992
ResetReindexState(s->nestingLevel);
49894993

4994+
/*
4995+
* No need for SnapBuildResetExportedSnapshotState() here, snapshot
4996+
* exports are not supported in subtransactions.
4997+
*/
4998+
49904999
/* Exit from parallel mode, if necessary. */
49915000
if (IsInParallelMode())
49925001
{

src/backend/replication/logical/snapbuild.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,17 +691,33 @@ SnapBuildGetOrBuildSnapshot(SnapBuild *builder, TransactionId xid)
691691
void
692692
SnapBuildClearExportedSnapshot(void)
693693
{
694+
ResourceOwner tmpResOwner;
695+
694696
/* nothing exported, that is the usual case */
695697
if (!ExportInProgress)
696698
return;
697699

698700
if (!IsTransactionState())
699701
elog(ERROR, "clearing exported snapshot in wrong transaction state");
700702

701-
/* make sure nothing could have ever happened */
703+
/*
704+
* AbortCurrentTransaction() takes care of resetting the snapshot state,
705+
* so remember SavedResourceOwnerDuringExport.
706+
*/
707+
tmpResOwner = SavedResourceOwnerDuringExport;
708+
709+
/* make sure nothing could have ever happened */
702710
AbortCurrentTransaction();
703711

704-
CurrentResourceOwner = SavedResourceOwnerDuringExport;
712+
CurrentResourceOwner = tmpResOwner;
713+
}
714+
715+
/*
716+
* Clear snapshot export state during transaction abort.
717+
*/
718+
void
719+
SnapBuildResetExportedSnapshotState(void)
720+
{
705721
SavedResourceOwnerDuringExport = NULL;
706722
ExportInProgress = false;
707723
}

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)