Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 73cc7cc

Browse files
committed
do not fetch heap tuples if it's not necessary
1 parent 6347967 commit 73cc7cc

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

init.c

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,22 @@ crossmatch_begin(CustomScanState *node, EState *estate, int eflags)
449449

450450
scan_state->values = palloc(sizeof(Datum) * nlist);
451451
scan_state->nulls = palloc(sizeof(bool) * nlist);
452+
453+
/* Store blank tuple in case scan tlist is empty */
454+
if (scan_state->scan_tlist == NIL)
455+
{
456+
TupleDesc tupdesc = node->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
457+
scan_state->stored_tuple = heap_form_tuple(tupdesc, NULL, NULL);
458+
}
459+
else
460+
scan_state->stored_tuple = NULL;
452461
}
453462

454463
static TupleTableSlot *
455464
crossmatch_exec(CustomScanState *node)
456465
{
457466
CrossmatchScanState *scan_state = (CrossmatchScanState *) node;
458467
TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
459-
TupleDesc tupdesc = node->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
460468
HeapTuple htup = scan_state->stored_tuple;
461469

462470
for(;;)
@@ -479,19 +487,29 @@ crossmatch_exec(CustomScanState *node)
479487
return ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
480488
}
481489

482-
htup1.t_self = p_tids[0];
483-
heap_fetch(scan_state->outer, SnapshotSelf, &htup1, &buf1, false, NULL);
484-
values[0] = heap_getattr(&htup1, 1, scan_state->outer->rd_att, &nulls[0]);
490+
/* We don't have to fetch tuples if scan tlist is empty */
491+
if (scan_state->scan_tlist != NIL)
492+
{
493+
TupleDesc tupdesc = node->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
494+
495+
htup1.t_self = p_tids[0];
496+
heap_fetch(scan_state->outer, SnapshotSelf,
497+
&htup1, &buf1, false, NULL);
498+
values[0] = heap_getattr(&htup1, 1, scan_state->outer->rd_att,
499+
&nulls[0]);
485500

486-
htup2.t_self = p_tids[1];
487-
heap_fetch(scan_state->inner, SnapshotSelf, &htup2, &buf2, false, NULL);
488-
values[1] = heap_getattr(&htup2, 1, scan_state->inner->rd_att, &nulls[1]);
501+
htup2.t_self = p_tids[1];
502+
heap_fetch(scan_state->inner, SnapshotSelf,
503+
&htup2, &buf2, false, NULL);
504+
values[1] = heap_getattr(&htup2, 1, scan_state->inner->rd_att,
505+
&nulls[1]);
489506

490-
ReleaseBuffer(buf1);
491-
ReleaseBuffer(buf2);
507+
ReleaseBuffer(buf1);
508+
ReleaseBuffer(buf2);
492509

493-
htup = heap_form_tuple(tupdesc, values, nulls);
494-
scan_state->stored_tuple = htup;
510+
htup = heap_form_tuple(tupdesc, values, nulls);
511+
scan_state->stored_tuple = htup;
512+
}
495513
}
496514

497515
if (node->ss.ps.ps_ProjInfo)
@@ -515,7 +533,8 @@ crossmatch_exec(CustomScanState *node)
515533
node->ss.ps.ps_TupFromTlist = false;
516534
}
517535
else
518-
return ExecStoreTuple(htup, node->ss.ps.ps_ResultTupleSlot, InvalidBuffer, false);
536+
return ExecStoreTuple(htup, node->ss.ps.ps_ResultTupleSlot,
537+
InvalidBuffer, false);
519538
}
520539
}
521540

0 commit comments

Comments
 (0)