Skip to content

Commit 409f9ca

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 384f1ab commit 409f9ca

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"
@@ -2698,6 +2699,9 @@ AbortTransaction(void)
26982699
/* Reset logical streaming state. */
26992700
ResetLogicalStreamingState();
27002701

2702+
/* Reset snapshot export state. */
2703+
SnapBuildResetExportedSnapshotState();
2704+
27012705
/* If in parallel mode, clean up workers and exit parallel mode. */
27022706
if (IsInParallelMode())
27032707
{
@@ -5010,6 +5014,11 @@ AbortSubTransaction(void)
50105014
/* Reset logical streaming state. */
50115015
ResetLogicalStreamingState();
50125016

5017+
/*
5018+
* No need for SnapBuildResetExportedSnapshotState() here, snapshot
5019+
* exports are not supported in subtransactions.
5020+
*/
5021+
50135022
/* Exit from parallel mode, if necessary. */
50145023
if (IsInParallelMode())
50155024
{

src/backend/replication/logical/snapbuild.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,17 +682,33 @@ SnapBuildGetOrBuildSnapshot(SnapBuild *builder, TransactionId xid)
682682
void
683683
SnapBuildClearExportedSnapshot(void)
684684
{
685+
ResourceOwner tmpResOwner;
686+
685687
/* nothing exported, that is the usual case */
686688
if (!ExportInProgress)
687689
return;
688690

689691
if (!IsTransactionState())
690692
elog(ERROR, "clearing exported snapshot in wrong transaction state");
691693

692-
/* make sure nothing could have ever happened */
694+
/*
695+
* AbortCurrentTransaction() takes care of resetting the snapshot state,
696+
* so remember SavedResourceOwnerDuringExport.
697+
*/
698+
tmpResOwner = SavedResourceOwnerDuringExport;
699+
700+
/* make sure nothing could have ever happened */
693701
AbortCurrentTransaction();
694702

695-
CurrentResourceOwner = SavedResourceOwnerDuringExport;
703+
CurrentResourceOwner = tmpResOwner;
704+
}
705+
706+
/*
707+
* Clear snapshot export state during transaction abort.
708+
*/
709+
void
710+
SnapBuildResetExportedSnapshotState(void)
711+
{
696712
SavedResourceOwnerDuringExport = NULL;
697713
ExportInProgress = false;
698714
}

src/include/replication/snapbuild.h

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

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

0 commit comments

Comments
 (0)