commit 17a83481661e531669d6904920a9109424fa3339
Author: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Date:   Tue Jun 15 15:05:22 2021 +0200

    Fixes of snapshot handling
    
    PostgreSQL commit ef94805096229ee3573624465a76ca11d2bd8529 (and in
    other branches) made some changes to snapshot handling in SPI.  This
    also included some fixes to the snapshot handling in logical
    replication, for instance that a snapshot needs to be help around
    AFTER trigger execution.  These changes made pglogical 2 fail on some
    assertions.  This patch makes the corresponding fixes in pglogical 2.

diff --git a/pglogical_apply_heap.c b/pglogical_apply_heap.c
index 9882518..9888ace 100644
--- a/pglogical_apply_heap.c
+++ b/pglogical_apply_heap.c
@@ -250,6 +250,8 @@ init_apply_exec_state(PGLogicalRelation *rel)
 {
 	ApplyExecState	   *aestate = palloc0(sizeof(ApplyExecState));
 
+	PushActiveSnapshot(GetTransactionSnapshot());
+
 	/* Initialize the executor state. */
 	aestate->estate = create_estate_for_relation(rel->rel, true);
 	aestate->resultRelInfo = aestate->estate->es_result_relation_info;
@@ -286,6 +288,8 @@ finish_apply_exec_state(ApplyExecState *aestate)
 	/* Free the memory. */
 	FreeExecutorState(aestate->estate);
 	pfree(aestate);
+
+	PopActiveSnapshot();
 }
 
 /*
@@ -313,9 +317,6 @@ pglogical_apply_heap_insert(PGLogicalRelation *rel, PGLogicalTupleData *newtup)
 	ExecSetSlotDescriptor(localslot, RelationGetDescr(rel->rel));
 #endif
 
-	/* Get snapshot */
-	PushActiveSnapshot(GetTransactionSnapshot());
-
 	ExecOpenIndices(aestate->resultRelInfo
 #if PG_VERSION_NUM >= 90500
 					, false
@@ -356,7 +357,6 @@ pglogical_apply_heap_insert(PGLogicalRelation *rel, PGLogicalTupleData *newtup)
 		if (aestate->slot == NULL)		/* "do nothing" */
 #endif
 		{
-			PopActiveSnapshot();
 			finish_apply_exec_state(aestate);
 			return;
 		}
@@ -424,7 +424,6 @@ pglogical_apply_heap_insert(PGLogicalRelation *rel, PGLogicalTupleData *newtup)
 				if (aestate->slot == NULL)		/* "do nothing" */
 #endif
 				{
-					PopActiveSnapshot();
 					finish_apply_exec_state(aestate);
 					return;
 				}
@@ -494,7 +493,6 @@ pglogical_apply_heap_insert(PGLogicalRelation *rel, PGLogicalTupleData *newtup)
 #endif
 	}
 
-	PopActiveSnapshot();
 	finish_apply_exec_state(aestate);
 
 	CommandCounterIncrement();
@@ -579,7 +577,6 @@ pglogical_apply_heap_update(PGLogicalRelation *rel, PGLogicalTupleData *oldtup,
 			if (aestate->slot == NULL)		/* "do nothing" */
 #endif
 			{
-				PopActiveSnapshot();
 				finish_apply_exec_state(aestate);
 				return;
 			}
@@ -689,7 +686,6 @@ pglogical_apply_heap_update(PGLogicalRelation *rel, PGLogicalTupleData *oldtup,
 	}
 
 	/* Cleanup. */
-	PopActiveSnapshot();
 	finish_apply_exec_state(aestate);
 
 	CommandCounterIncrement();
@@ -733,7 +729,6 @@ pglogical_apply_heap_delete(PGLogicalRelation *rel, PGLogicalTupleData *oldtup)
 
 			if (!dodelete)		/* "do nothing" */
 			{
-				PopActiveSnapshot();
 				finish_apply_exec_state(aestate);
 				pglogical_relation_close(rel, NoLock);
 				return;
@@ -760,7 +755,6 @@ pglogical_apply_heap_delete(PGLogicalRelation *rel, PGLogicalTupleData *oldtup)
 	}
 
 	/* Cleanup. */
-	PopActiveSnapshot();
 	finish_apply_exec_state(aestate);
 
 	CommandCounterIncrement();
diff --git a/pglogical_output_plugin.c b/pglogical_output_plugin.c
index 11fa818..abbe00c 100644
--- a/pglogical_output_plugin.c
+++ b/pglogical_output_plugin.c
@@ -28,6 +28,7 @@
 #include "utils/inval.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
+#include "utils/snapmgr.h"
 #include "replication/origin.h"
 
 #include "pglogical_output_plugin.h"
@@ -618,6 +619,8 @@ pglogical_change_filter(PGLogicalOutputData *data, Relation relation,
 			return false;
 		}
 
+		PushActiveSnapshot(GetTransactionSnapshot());
+
 		estate = create_estate_for_relation(relation, false);
 		econtext = prepare_per_tuple_econtext(estate, tupdesc);
 
@@ -643,6 +646,8 @@ pglogical_change_filter(PGLogicalOutputData *data, Relation relation,
 
 		ExecDropSingleTupleTableSlot(econtext->ecxt_scantuple);
 		FreeExecutorState(estate);
+
+		PopActiveSnapshot();
 	}
 
 	/* Make sure caller is aware of any attribute filter. */
