Skip to content

Commit 668c525

Browse files
committed
Improve memory context handling around ExprContext
1 parent e874370 commit 668c525

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

pglogical_apply.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ fill_tuple_defaults(PGLogicalRelation *rel, ExprContext *econtext,
316316
int *defmap;
317317
ExprState **defexprs;
318318

319+
/* We got all the data via replication, no need to evaluate anything. */
320+
if (num_phys_attrs == rel->natts)
321+
return;
322+
319323
defmap = (int *) palloc(num_phys_attrs * sizeof(int));
320324
defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
321325

@@ -357,11 +361,13 @@ handle_insert(StringInfo s)
357361
PGLogicalTupleData newtup;
358362
PGLogicalRelation *rel;
359363
EState *estate;
364+
ExprContext *econtext;
360365
Oid conflicts;
361366
TupleTableSlot *localslot,
362367
*applyslot;
363368
HeapTuple remotetuple;
364369
HeapTuple applytuple;
370+
MemoryContext oldcontext = CurrentMemoryContext;
365371
PGLogicalConflictResolution resolution;
366372
bool started_tx = ensure_transaction();
367373

@@ -376,7 +382,11 @@ handle_insert(StringInfo s)
376382

377383
/* Initialize the executor state. */
378384
estate = create_estate_for_relation(rel->rel);
379-
fill_tuple_defaults(rel, GetPerTupleExprContext(estate), &newtup);
385+
econtext = GetPerTupleExprContext(estate);
386+
387+
MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
388+
fill_tuple_defaults(rel, econtext, &newtup);
389+
380390
localslot = ExecInitExtraTupleSlot(estate);
381391
applyslot = ExecInitExtraTupleSlot(estate);
382392
ExecSetSlotDescriptor(localslot, RelationGetDescr(rel->rel));
@@ -392,6 +402,7 @@ handle_insert(StringInfo s)
392402

393403
remotetuple = heap_form_tuple(RelationGetDescr(rel->rel),
394404
newtup.values, newtup.nulls);
405+
MemoryContextSwitchTo(oldcontext);
395406

396407
if (OidIsValid(conflicts))
397408
{
@@ -483,6 +494,8 @@ handle_update(StringInfo s)
483494
PGLogicalTupleData *searchtup;
484495
PGLogicalRelation *rel;
485496
EState *estate;
497+
ExprContext *econtext;
498+
MemoryContext oldcontext = CurrentMemoryContext;
486499
bool found;
487500
bool hasoldtup;
488501
TupleTableSlot *localslot,
@@ -503,7 +516,11 @@ handle_update(StringInfo s)
503516

504517
/* Initialize the executor state. */
505518
estate = create_estate_for_relation(rel->rel);
506-
fill_tuple_defaults(rel, GetPerTupleExprContext(estate), &newtup);
519+
econtext = GetPerTupleExprContext(estate);
520+
521+
MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
522+
fill_tuple_defaults(rel, econtext, &newtup);
523+
507524
localslot = ExecInitExtraTupleSlot(estate);
508525
applyslot = ExecInitExtraTupleSlot(estate);
509526
ExecSetSlotDescriptor(localslot, RelationGetDescr(rel->rel));
@@ -517,6 +534,8 @@ handle_update(StringInfo s)
517534
remotetuple = heap_form_tuple(RelationGetDescr(rel->rel),
518535
newtup.values, newtup.nulls);
519536

537+
MemoryContextSwitchTo(oldcontext);
538+
520539
/*
521540
* Tuple found.
522541
*

0 commit comments

Comments
 (0)