Skip to content

Commit 8742e27

Browse files
authored
Improve appendonly_getnextslot to optimize tuple retrieval (#1025)
Refactor appendonly_getnextslot to use a function pointer array for improved readability and maintainability. - Introduce getnext_impl array to select the appropriate getnext function based on aos_pushdown_qual. - This change simplifies conditional logic and enhances code clarity.
1 parent 7847a15 commit 8742e27

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

src/backend/access/appendonly/appendonlyam.c

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,30 +1632,36 @@ appendonly_endscan(TableScanDesc scan)
16321632
pfree(aoscan);
16331633
}
16341634

1635-
/* ----------------
1636-
* appendonly_getnextslot - retrieve next tuple in scan
1637-
* ----------------
1638-
*/
1639-
bool
1640-
appendonly_getnextslot(TableScanDesc scan, ScanDirection direction, TupleTableSlot *slot)
1635+
1636+
static pg_attribute_hot_inline bool
1637+
appendonly_getnextslot_noqual(AppendOnlyScanDesc aoscan, ScanDirection direction, TupleTableSlot *slot)
16411638
{
1642-
AppendOnlyScanDesc aoscan = (AppendOnlyScanDesc) scan;
16431639
while (appendonlygettup(aoscan, direction, aoscan->rs_base.rs_nkeys, aoscan->aos_key, slot))
16441640
{
1641+
pgstat_count_heap_getnext(aoscan->aos_rd);
1642+
return true;
1643+
}
1644+
if (slot)
1645+
ExecClearTuple(slot);
1646+
return false;
1647+
}
1648+
1649+
static bool
1650+
appendonly_getnextslot_withqual(AppendOnlyScanDesc aoscan, ScanDirection direction, TupleTableSlot *slot)
1651+
{
1652+
while (appendonlygettup(aoscan, direction, aoscan->rs_base.rs_nkeys, aoscan->aos_key, slot))
1653+
{
1654+
Assert(aoscan->aos_pushdown_qual);
1655+
/*
1656+
* place the current tuple into the expr context
1657+
*/
1658+
aoscan->aos_pushdown_econtext->ecxt_scantuple = slot;
16451659
/* predicate pushdown test */
1646-
if (aoscan->aos_pushdown_qual)
1660+
if (!ExecQual(aoscan->aos_pushdown_qual, aoscan->aos_pushdown_econtext))
16471661
{
1648-
/*
1649-
* place the current tuple into the expr context
1650-
*/
1651-
aoscan->aos_pushdown_econtext->ecxt_scantuple = slot;
1652-
1653-
if (!ExecQual(aoscan->aos_pushdown_qual, aoscan->aos_pushdown_econtext))
1654-
{
1655-
/* Tuple fails qual, so free per-tuple memory and try again. */
1656-
ResetExprContext(aoscan->aos_pushdown_econtext);
1657-
continue;
1658-
}
1662+
/* Tuple fails qual, so free per-tuple memory and try again. */
1663+
ResetExprContext(aoscan->aos_pushdown_econtext);
1664+
continue;
16591665
}
16601666

16611667
pgstat_count_heap_getnext(aoscan->aos_rd);
@@ -1669,6 +1675,25 @@ appendonly_getnextslot(TableScanDesc scan, ScanDirection direction, TupleTableSl
16691675
return false;
16701676
}
16711677

1678+
/* ----------------
1679+
* appendonly_getnextslot - retrieve next tuple in scan
1680+
* ----------------
1681+
*/
1682+
pg_attribute_hot bool
1683+
appendonly_getnextslot(TableScanDesc scan, ScanDirection direction, TupleTableSlot *slot)
1684+
{
1685+
AppendOnlyScanDesc aoscan = (AppendOnlyScanDesc) scan;
1686+
/*
1687+
* [0] = noqual, [1] = withqual
1688+
*/
1689+
static bool (* const getnext_impl[2])(AppendOnlyScanDesc, ScanDirection, TupleTableSlot*) = {
1690+
appendonly_getnextslot_noqual,
1691+
appendonly_getnextslot_withqual
1692+
};
1693+
1694+
return getnext_impl[!!aoscan->aos_pushdown_qual](aoscan, direction, slot);
1695+
}
1696+
16721697
uint32
16731698
appendonly_scan_flags(Relation relation)
16741699
{

0 commit comments

Comments
 (0)