Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 26383da

Browse files
committed
Rearrange logrep worker's snapshot handling some more.
It turns out that worker.c's code path for TRUNCATE was also careless about establishing a snapshot while executing user-defined code, allowing the checks added by commit 84f5c29 to fail when a trigger is fired in that context. We could just wrap Push/PopActiveSnapshot around the truncate call, but it seems better to establish a policy of holding a snapshot throughout execution of a replication step. To help with that and possible future requirements, replace the previous ensure_transaction calls with pairs of begin/end_replication_step calls. Per report from Mark Dilger. Back-patch to v11, like the previous changes. Discussion: https://postgr.es/m/B4A3AF82-79ED-4F4C-A4E5-CD2622098972@enterprisedb.com
1 parent 2208d71 commit 26383da

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

src/backend/replication/logical/worker.c

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -138,30 +138,41 @@ should_apply_changes_for_rel(LogicalRepRelMapEntry *rel)
138138
}
139139

140140
/*
141-
* Make sure that we started local transaction.
141+
* Begin one step (one INSERT, UPDATE, etc) of a replication transaction.
142142
*
143-
* Also switches to ApplyMessageContext as necessary.
143+
* Start a transaction, if this is the first step (else we keep using the
144+
* existing transaction).
145+
* Also provide a global snapshot and ensure we run in ApplyMessageContext.
144146
*/
145-
static bool
146-
ensure_transaction(void)
147+
static void
148+
begin_replication_step(void)
147149
{
148-
if (IsTransactionState())
149-
{
150-
SetCurrentStatementStartTimestamp();
151-
152-
if (CurrentMemoryContext != ApplyMessageContext)
153-
MemoryContextSwitchTo(ApplyMessageContext);
150+
SetCurrentStatementStartTimestamp();
154151

155-
return false;
152+
if (!IsTransactionState())
153+
{
154+
StartTransactionCommand();
155+
maybe_reread_subscription();
156156
}
157157

158-
SetCurrentStatementStartTimestamp();
159-
StartTransactionCommand();
160-
161-
maybe_reread_subscription();
158+
PushActiveSnapshot(GetTransactionSnapshot());
162159

163160
MemoryContextSwitchTo(ApplyMessageContext);
164-
return true;
161+
}
162+
163+
/*
164+
* Finish up one step of a replication transaction.
165+
* Callers of begin_replication_step() must also call this.
166+
*
167+
* We don't close out the transaction here, but we should increment
168+
* the command counter to make the effects of this step visible.
169+
*/
170+
static void
171+
end_replication_step(void)
172+
{
173+
PopActiveSnapshot();
174+
175+
CommandCounterIncrement();
165176
}
166177

167178

@@ -178,13 +189,6 @@ create_estate_for_relation(LogicalRepRelMapEntry *rel)
178189
ResultRelInfo *resultRelInfo;
179190
RangeTblEntry *rte;
180191

181-
/*
182-
* Input functions may need an active snapshot, as may AFTER triggers
183-
* invoked during finish_estate. For safety, ensure an active snapshot
184-
* exists throughout all our usage of the executor.
185-
*/
186-
PushActiveSnapshot(GetTransactionSnapshot());
187-
188192
estate = CreateExecutorState();
189193

190194
rte = makeNode(RangeTblEntry);
@@ -222,7 +226,6 @@ finish_estate(EState *estate)
222226
/* Cleanup. */
223227
ExecResetTupleTable(estate->es_tupleTable, false);
224228
FreeExecutorState(estate);
225-
PopActiveSnapshot();
226229
}
227230

228231
/*
@@ -612,7 +615,7 @@ apply_handle_insert(StringInfo s)
612615
TupleTableSlot *remoteslot;
613616
MemoryContext oldctx;
614617

615-
ensure_transaction();
618+
begin_replication_step();
616619

617620
relid = logicalrep_read_insert(s, &newtup);
618621
rel = logicalrep_rel_open(relid, RowExclusiveLock);
@@ -623,6 +626,7 @@ apply_handle_insert(StringInfo s)
623626
* transaction so it's safe to unlock it.
624627
*/
625628
logicalrep_rel_close(rel, RowExclusiveLock);
629+
end_replication_step();
626630
return;
627631
}
628632

@@ -650,7 +654,7 @@ apply_handle_insert(StringInfo s)
650654

651655
logicalrep_rel_close(rel, NoLock);
652656

653-
CommandCounterIncrement();
657+
end_replication_step();
654658
}
655659

656660
/*
@@ -708,7 +712,7 @@ apply_handle_update(StringInfo s)
708712
bool found;
709713
MemoryContext oldctx;
710714

711-
ensure_transaction();
715+
begin_replication_step();
712716

713717
relid = logicalrep_read_update(s, &has_oldtup, &oldtup,
714718
&newtup);
@@ -720,6 +724,7 @@ apply_handle_update(StringInfo s)
720724
* transaction so it's safe to unlock it.
721725
*/
722726
logicalrep_rel_close(rel, RowExclusiveLock);
727+
end_replication_step();
723728
return;
724729
}
725730

@@ -825,7 +830,7 @@ apply_handle_update(StringInfo s)
825830

826831
logicalrep_rel_close(rel, NoLock);
827832

828-
CommandCounterIncrement();
833+
end_replication_step();
829834
}
830835

831836
/*
@@ -847,7 +852,7 @@ apply_handle_delete(StringInfo s)
847852
bool found;
848853
MemoryContext oldctx;
849854

850-
ensure_transaction();
855+
begin_replication_step();
851856

852857
relid = logicalrep_read_delete(s, &oldtup);
853858
rel = logicalrep_rel_open(relid, RowExclusiveLock);
@@ -858,6 +863,7 @@ apply_handle_delete(StringInfo s)
858863
* transaction so it's safe to unlock it.
859864
*/
860865
logicalrep_rel_close(rel, RowExclusiveLock);
866+
end_replication_step();
861867
return;
862868
}
863869

@@ -920,7 +926,7 @@ apply_handle_delete(StringInfo s)
920926

921927
logicalrep_rel_close(rel, NoLock);
922928

923-
CommandCounterIncrement();
929+
end_replication_step();
924930
}
925931

926932
/*
@@ -941,7 +947,7 @@ apply_handle_truncate(StringInfo s)
941947
ListCell *lc;
942948
LOCKMODE lockmode = AccessExclusiveLock;
943949

944-
ensure_transaction();
950+
begin_replication_step();
945951

946952
remote_relids = logicalrep_read_truncate(s, &cascade, &restart_seqs);
947953

@@ -982,7 +988,7 @@ apply_handle_truncate(StringInfo s)
982988
logicalrep_rel_close(rel, NoLock);
983989
}
984990

985-
CommandCounterIncrement();
991+
end_replication_step();
986992
}
987993

988994

0 commit comments

Comments
 (0)