@@ -122,6 +122,10 @@ static void apply_handle_update_internal(ResultRelInfo *relinfo,
122122static void apply_handle_delete_internal (ResultRelInfo * relinfo ,EState * estate ,
123123TupleTableSlot * remoteslot ,
124124LogicalRepRelation * remoterel );
125+ static bool FindReplTupleInLocalRel (EState * estate ,Relation localrel ,
126+ LogicalRepRelation * remoterel ,
127+ TupleTableSlot * remoteslot ,
128+ TupleTableSlot * * localslot );
125129
126130/*
127131 * Should this worker apply changes for given relation.
@@ -788,33 +792,17 @@ apply_handle_update_internal(ResultRelInfo *relinfo,
788792LogicalRepRelMapEntry * relmapentry )
789793{
790794Relation localrel = relinfo -> ri_RelationDesc ;
791- Oid idxoid ;
792795EPQState epqstate ;
793796TupleTableSlot * localslot ;
794797bool found ;
795798MemoryContext oldctx ;
796799
797- localslot = table_slot_create (localrel ,& estate -> es_tupleTable );
798800EvalPlanQualInit (& epqstate ,estate ,NULL ,NIL ,-1 );
799-
800801ExecOpenIndices (relinfo , false);
801802
802- /*
803- * Try to find tuple using either replica identity index, primary key or
804- * if needed, sequential scan.
805- */
806- idxoid = GetRelationIdentityOrPK (localrel );
807- Assert (OidIsValid (idxoid )||
808- (relmapentry -> remoterel .replident == REPLICA_IDENTITY_FULL ));
809-
810- if (OidIsValid (idxoid ))
811- found = RelationFindReplTupleByIndex (localrel ,idxoid ,
812- LockTupleExclusive ,
813- remoteslot ,localslot );
814- else
815- found = RelationFindReplTupleSeq (localrel ,LockTupleExclusive ,
816- remoteslot ,localslot );
817-
803+ found = FindReplTupleInLocalRel (estate ,localrel ,
804+ & relmapentry -> remoterel ,
805+ remoteslot ,& localslot );
818806ExecClearTuple (remoteslot );
819807
820808/*
@@ -922,31 +910,15 @@ apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate,
922910LogicalRepRelation * remoterel )
923911{
924912Relation localrel = relinfo -> ri_RelationDesc ;
925- Oid idxoid ;
926913EPQState epqstate ;
927914TupleTableSlot * localslot ;
928915bool found ;
929916
930- localslot = table_slot_create (localrel ,& estate -> es_tupleTable );
931917EvalPlanQualInit (& epqstate ,estate ,NULL ,NIL ,-1 );
932-
933918ExecOpenIndices (relinfo , false);
934919
935- /*
936- * Try to find tuple using either replica identity index, primary key or
937- * if needed, sequential scan.
938- */
939- idxoid = GetRelationIdentityOrPK (localrel );
940- Assert (OidIsValid (idxoid )||
941- (remoterel -> replident == REPLICA_IDENTITY_FULL ));
942-
943- if (OidIsValid (idxoid ))
944- found = RelationFindReplTupleByIndex (localrel ,idxoid ,
945- LockTupleExclusive ,
946- remoteslot ,localslot );
947- else
948- found = RelationFindReplTupleSeq (localrel ,LockTupleExclusive ,
949- remoteslot ,localslot );
920+ found = FindReplTupleInLocalRel (estate ,localrel ,remoterel ,
921+ remoteslot ,& localslot );
950922
951923/* If found delete it. */
952924if (found )
@@ -970,6 +942,39 @@ apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate,
970942EvalPlanQualEnd (& epqstate );
971943}
972944
945+ /*
946+ * Try to find a tuple received from the publication side (in 'remoteslot') in
947+ * the corresponding local relation using either replica identity index,
948+ * primary key or if needed, sequential scan.
949+ *
950+ * Local tuple, if found, is returned in '*localslot'.
951+ */
952+ static bool
953+ FindReplTupleInLocalRel (EState * estate ,Relation localrel ,
954+ LogicalRepRelation * remoterel ,
955+ TupleTableSlot * remoteslot ,
956+ TupleTableSlot * * localslot )
957+ {
958+ Oid idxoid ;
959+ bool found ;
960+
961+ * localslot = table_slot_create (localrel ,& estate -> es_tupleTable );
962+
963+ idxoid = GetRelationIdentityOrPK (localrel );
964+ Assert (OidIsValid (idxoid )||
965+ (remoterel -> replident == REPLICA_IDENTITY_FULL ));
966+
967+ if (OidIsValid (idxoid ))
968+ found = RelationFindReplTupleByIndex (localrel ,idxoid ,
969+ LockTupleExclusive ,
970+ remoteslot ,* localslot );
971+ else
972+ found = RelationFindReplTupleSeq (localrel ,LockTupleExclusive ,
973+ remoteslot ,* localslot );
974+
975+ return found ;
976+ }
977+
973978/*
974979 * Handle TRUNCATE message.
975980 *