Commit d8653f46 authored by Peter Eisentraut's avatar Peter Eisentraut

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
parent 36962349
...@@ -122,6 +122,10 @@ static void apply_handle_update_internal(ResultRelInfo *relinfo, ...@@ -122,6 +122,10 @@ static void apply_handle_update_internal(ResultRelInfo *relinfo,
static void apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate, static void apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate,
TupleTableSlot *remoteslot, TupleTableSlot *remoteslot,
LogicalRepRelation *remoterel); LogicalRepRelation *remoterel);
static bool FindReplTupleInLocalRel(EState *estate, Relation localrel,
LogicalRepRelation *remoterel,
TupleTableSlot *remoteslot,
TupleTableSlot **localslot);
/* /*
* Should this worker apply changes for given relation. * Should this worker apply changes for given relation.
...@@ -788,33 +792,17 @@ apply_handle_update_internal(ResultRelInfo *relinfo, ...@@ -788,33 +792,17 @@ apply_handle_update_internal(ResultRelInfo *relinfo,
LogicalRepRelMapEntry *relmapentry) LogicalRepRelMapEntry *relmapentry)
{ {
Relation localrel = relinfo->ri_RelationDesc; Relation localrel = relinfo->ri_RelationDesc;
Oid idxoid;
EPQState epqstate; EPQState epqstate;
TupleTableSlot *localslot; TupleTableSlot *localslot;
bool found; bool found;
MemoryContext oldctx; MemoryContext oldctx;
localslot = table_slot_create(localrel, &estate->es_tupleTable);
EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1); EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
ExecOpenIndices(relinfo, false); ExecOpenIndices(relinfo, false);
/* found = FindReplTupleInLocalRel(estate, localrel,
* Try to find tuple using either replica identity index, primary key or &relmapentry->remoterel,
* if needed, sequential scan. remoteslot, &localslot);
*/
idxoid = GetRelationIdentityOrPK(localrel);
Assert(OidIsValid(idxoid) ||
(relmapentry->remoterel.replident == REPLICA_IDENTITY_FULL));
if (OidIsValid(idxoid))
found = RelationFindReplTupleByIndex(localrel, idxoid,
LockTupleExclusive,
remoteslot, localslot);
else
found = RelationFindReplTupleSeq(localrel, LockTupleExclusive,
remoteslot, localslot);
ExecClearTuple(remoteslot); ExecClearTuple(remoteslot);
/* /*
...@@ -922,31 +910,15 @@ apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate, ...@@ -922,31 +910,15 @@ apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate,
LogicalRepRelation *remoterel) LogicalRepRelation *remoterel)
{ {
Relation localrel = relinfo->ri_RelationDesc; Relation localrel = relinfo->ri_RelationDesc;
Oid idxoid;
EPQState epqstate; EPQState epqstate;
TupleTableSlot *localslot; TupleTableSlot *localslot;
bool found; bool found;
localslot = table_slot_create(localrel, &estate->es_tupleTable);
EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1); EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
ExecOpenIndices(relinfo, false); ExecOpenIndices(relinfo, false);
/* found = FindReplTupleInLocalRel(estate, localrel, remoterel,
* Try to find tuple using either replica identity index, primary key or remoteslot, &localslot);
* if needed, sequential scan.
*/
idxoid = GetRelationIdentityOrPK(localrel);
Assert(OidIsValid(idxoid) ||
(remoterel->replident == REPLICA_IDENTITY_FULL));
if (OidIsValid(idxoid))
found = RelationFindReplTupleByIndex(localrel, idxoid,
LockTupleExclusive,
remoteslot, localslot);
else
found = RelationFindReplTupleSeq(localrel, LockTupleExclusive,
remoteslot, localslot);
/* If found delete it. */ /* If found delete it. */
if (found) if (found)
...@@ -970,6 +942,39 @@ apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate, ...@@ -970,6 +942,39 @@ apply_handle_delete_internal(ResultRelInfo *relinfo, EState *estate,
EvalPlanQualEnd(&epqstate); EvalPlanQualEnd(&epqstate);
} }
/*
* Try to find a tuple received from the publication side (in 'remoteslot') in
* the corresponding local relation using either replica identity index,
* primary key or if needed, sequential scan.
*
* Local tuple, if found, is returned in '*localslot'.
*/
static bool
FindReplTupleInLocalRel(EState *estate, Relation localrel,
LogicalRepRelation *remoterel,
TupleTableSlot *remoteslot,
TupleTableSlot **localslot)
{
Oid idxoid;
bool found;
*localslot = table_slot_create(localrel, &estate->es_tupleTable);
idxoid = GetRelationIdentityOrPK(localrel);
Assert(OidIsValid(idxoid) ||
(remoterel->replident == REPLICA_IDENTITY_FULL));
if (OidIsValid(idxoid))
found = RelationFindReplTupleByIndex(localrel, idxoid,
LockTupleExclusive,
remoteslot, *localslot);
else
found = RelationFindReplTupleSeq(localrel, LockTupleExclusive,
remoteslot, *localslot);
return found;
}
/* /*
* Handle TRUNCATE message. * Handle TRUNCATE message.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment