Skip to content

Commit a67da49

Browse files
author
Amit Kapila
committed
Avoid duplicate table scans for cross-partition updates during logical replication.
When performing a cross-partition update in the apply worker, it needlessly scans the old partition twice, resulting in noticeable overhead. This commit optimizes it by removing the redundant table scan. Author: Hou Zhijie Reviewed-by: Hayato Kuroda, Amit Kapila Discussion: https://postgr.es/m/OS0PR01MB571623E39984D94CBB5341D994AB2@OS0PR01MB5716.jpnprd01.prod.outlook.com
1 parent a7f107d commit a67da49

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/backend/replication/logical/worker.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2991,6 +2991,7 @@ apply_handle_tuple_routing(ApplyExecutionData *edata,
29912991
ResultRelInfo *partrelinfo_new;
29922992
Relation partrel_new;
29932993
bool found;
2994+
EPQState epqstate;
29942995

29952996
/* Get the matching local tuple from the partition. */
29962997
found = FindReplTupleInLocalRel(edata, partrel,
@@ -3021,6 +3022,9 @@ apply_handle_tuple_routing(ApplyExecutionData *edata,
30213022
newtup);
30223023
MemoryContextSwitchTo(oldctx);
30233024

3025+
EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1, NIL);
3026+
ExecOpenIndices(partrelinfo, false);
3027+
30243028
/*
30253029
* Does the updated tuple still satisfy the current
30263030
* partition's constraint?
@@ -3036,18 +3040,11 @@ apply_handle_tuple_routing(ApplyExecutionData *edata,
30363040
* work already done above to find the local tuple in the
30373041
* partition.
30383042
*/
3039-
EPQState epqstate;
3040-
3041-
EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1, NIL);
3042-
ExecOpenIndices(partrelinfo, false);
3043-
30443043
EvalPlanQualSetSlot(&epqstate, remoteslot_part);
30453044
TargetPrivilegesCheck(partrelinfo->ri_RelationDesc,
30463045
ACL_UPDATE);
30473046
ExecSimpleRelationUpdate(partrelinfo, estate, &epqstate,
30483047
localslot, remoteslot_part);
3049-
ExecCloseIndices(partrelinfo);
3050-
EvalPlanQualEnd(&epqstate);
30513048
}
30523049
else
30533050
{
@@ -3091,9 +3088,9 @@ apply_handle_tuple_routing(ApplyExecutionData *edata,
30913088
RelationGetRelationName(partrel_new));
30923089

30933090
/* DELETE old tuple found in the old partition. */
3094-
apply_handle_delete_internal(edata, partrelinfo,
3095-
localslot,
3096-
part_entry->localindexoid);
3091+
EvalPlanQualSetSlot(&epqstate, localslot);
3092+
TargetPrivilegesCheck(partrelinfo->ri_RelationDesc, ACL_DELETE);
3093+
ExecSimpleRelationDelete(partrelinfo, estate, &epqstate, localslot);
30973094

30983095
/* INSERT new tuple into the new partition. */
30993096

@@ -3123,6 +3120,9 @@ apply_handle_tuple_routing(ApplyExecutionData *edata,
31233120
apply_handle_insert_internal(edata, partrelinfo_new,
31243121
remoteslot_part);
31253122
}
3123+
3124+
ExecCloseIndices(partrelinfo);
3125+
EvalPlanQualEnd(&epqstate);
31263126
}
31273127
break;
31283128

0 commit comments

Comments
 (0)