diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index 656d399..d839324 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -147,23 +147,25 @@ should_apply_changes_for_rel(LogicalRepRelMapEntry *rel) * * Also switches to ApplyContext as necessary. */ -static bool +static void ensure_transaction(void) { - if (IsTransactionState()) + if (!IsTransactionState()) { - if (CurrentMemoryContext != ApplyContext) - MemoryContextSwitchTo(ApplyContext); - return false; - } + StartTransactionCommand(); - StartTransactionCommand(); + if (!MySubscriptionValid) + reread_subscription(); + } - if (!MySubscriptionValid) - reread_subscription(); + if (CurrentMemoryContext != ApplyContext) + MemoryContextSwitchTo(ApplyContext); - MemoryContextSwitchTo(ApplyContext); - return true; + /* + * Free memory that was used to construct tuples in logicalrep_read_* + * functions. + */ + MemoryContextReset(ApplyContext); }