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

Commit d8653f4

Browse files
committed
Refactor code to look up local replication tuple
This unifies some duplicate code. Author: Amit Langote <amitlangote09@gmail.com> Discussion: https://www.postgresql.org/message-id/CA+HiwqFjYE5anArxvkjr37AQMd52L-LZtz9Ld2QrLQ3YfcYhTw@mail.gmail.com
1 parent 3696234 commit d8653f4

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

src/backend/replication/logical/worker.c

+42-37
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ static void apply_handle_update_internal(ResultRelInfo *relinfo,
122122
static void apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate,
123123
TupleTableSlot *remoteslot,
124124
LogicalRepRelation *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,
788792
LogicalRepRelMapEntry *relmapentry)
789793
{
790794
Relation localrel = relinfo->ri_RelationDesc;
791-
Oid idxoid;
792795
EPQState epqstate;
793796
TupleTableSlot *localslot;
794797
bool found;
795798
MemoryContext oldctx;
796799

797-
localslot = table_slot_create(localrel, &estate->es_tupleTable);
798800
EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
799-
800801
ExecOpenIndices(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);
818806
ExecClearTuple(remoteslot);
819807

820808
/*
@@ -922,31 +910,15 @@ apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate,
922910
LogicalRepRelation *remoterel)
923911
{
924912
Relation localrel = relinfo->ri_RelationDesc;
925-
Oid idxoid;
926913
EPQState epqstate;
927914
TupleTableSlot *localslot;
928915
bool found;
929916

930-
localslot = table_slot_create(localrel, &estate->es_tupleTable);
931917
EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
932-
933918
ExecOpenIndices(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. */
952924
if (found)
@@ -970,6 +942,39 @@ apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate,
970942
EvalPlanQualEnd(&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
*

0 commit comments

Comments
 (0)