diff options
| author | Andres Freund | 2017-03-14 22:45:36 +0000 |
|---|---|---|
| committer | Andres Freund | 2017-03-25 21:52:06 +0000 |
| commit | b8d7f053c5c2bf2a7e8734fe3327f6a8bc711755 (patch) | |
| tree | 6fd5db4d05a3dec9bed6b8cc4c98ca9d3f80425e /src/backend/executor/nodeTidscan.c | |
| parent | 7d3957e53ebf26fc8d72dee1dacc2c827cc07caa (diff) | |
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/[email protected]
Diffstat (limited to 'src/backend/executor/nodeTidscan.c')
| -rw-r--r-- | src/backend/executor/nodeTidscan.c | 139 |
1 files changed, 95 insertions, 44 deletions
diff --git a/src/backend/executor/nodeTidscan.c b/src/backend/executor/nodeTidscan.c index 13ed886577a..4860ec0f4da 100644 --- a/src/backend/executor/nodeTidscan.c +++ b/src/backend/executor/nodeTidscan.c @@ -38,21 +38,94 @@ ((Var *) (node))->varattno == SelfItemPointerAttributeNumber && \ ((Var *) (node))->varlevelsup == 0) -static void TidListCreate(TidScanState *tidstate); +/* one element in tss_tidexprs */ +typedef struct TidExpr +{ + ExprState *exprstate; /* ExprState for a TID-yielding subexpr */ + bool isarray; /* if true, it yields tid[] not just tid */ + CurrentOfExpr *cexpr; /* alternatively, we can have CURRENT OF */ +} TidExpr; + +static void TidExprListCreate(TidScanState *tidstate); +static void TidListEval(TidScanState *tidstate); static int itemptr_comparator(const void *a, const void *b); static TupleTableSlot *TidNext(TidScanState *node); /* + * Extract the qual subexpressions that yield TIDs to search for, + * and compile them into ExprStates if they're ordinary expressions. + * + * CURRENT OF is a special case that we can't compile usefully; + * just drop it into the TidExpr list as-is. + */ +static void +TidExprListCreate(TidScanState *tidstate) +{ + TidScan *node = (TidScan *) tidstate->ss.ps.plan; + ListCell *l; + + tidstate->tss_tidexprs = NIL; + tidstate->tss_isCurrentOf = false; + + foreach(l, node->tidquals) + { + Expr *expr = (Expr *) lfirst(l); + TidExpr *tidexpr = (TidExpr *) palloc0(sizeof(TidExpr)); + + if (is_opclause(expr)) + { + Node *arg1; + Node *arg2; + + arg1 = get_leftop(expr); + arg2 = get_rightop(expr); + if (IsCTIDVar(arg1)) + tidexpr->exprstate = ExecInitExpr((Expr *) arg2, + &tidstate->ss.ps); + else if (IsCTIDVar(arg2)) + tidexpr->exprstate = ExecInitExpr((Expr *) arg1, + &tidstate->ss.ps); + else + elog(ERROR, "could not identify CTID variable"); + tidexpr->isarray = false; + } + else if (expr && IsA(expr, ScalarArrayOpExpr)) + { + ScalarArrayOpExpr *saex = (ScalarArrayOpExpr *) expr; + + Assert(IsCTIDVar(linitial(saex->args))); + tidexpr->exprstate = ExecInitExpr(lsecond(saex->args), + &tidstate->ss.ps); + tidexpr->isarray = true; + } + else if (expr && IsA(expr, CurrentOfExpr)) + { + CurrentOfExpr *cexpr = (CurrentOfExpr *) expr; + + tidexpr->cexpr = cexpr; + tidstate->tss_isCurrentOf = true; + } + else + elog(ERROR, "could not identify CTID expression"); + + tidstate->tss_tidexprs = lappend(tidstate->tss_tidexprs, tidexpr); + } + + /* CurrentOfExpr could never appear OR'd with something else */ + Assert(list_length(tidstate->tss_tidexprs) == 1 || + !tidstate->tss_isCurrentOf); +} + +/* * Compute the list of TIDs to be visited, by evaluating the expressions * for them. * * (The result is actually an array, not a list.) */ static void -TidListCreate(TidScanState *tidstate) +TidListEval(TidScanState *tidstate) { - List *evalList = tidstate->tss_tidquals; ExprContext *econtext = tidstate->ss.ps.ps_ExprContext; BlockNumber nblocks; ItemPointerData *tidList; @@ -73,36 +146,21 @@ TidListCreate(TidScanState *tidstate) * are simple OpExprs or CurrentOfExprs. If there are any * ScalarArrayOpExprs, we may have to enlarge the array. */ - numAllocTids = list_length(evalList); + numAllocTids = list_length(tidstate->tss_tidexprs); tidList = (ItemPointerData *) palloc(numAllocTids * sizeof(ItemPointerData)); numTids = 0; - tidstate->tss_isCurrentOf = false; - foreach(l, evalList) + foreach(l, tidstate->tss_tidexprs) { - ExprState *exstate = (ExprState *) lfirst(l); - Expr *expr = exstate->expr; + TidExpr *tidexpr = (TidExpr *) lfirst(l); ItemPointer itemptr; bool isNull; - if (is_opclause(expr)) + if (tidexpr->exprstate && !tidexpr->isarray) { - FuncExprState *fexstate = (FuncExprState *) exstate; - Node *arg1; - Node *arg2; - - arg1 = get_leftop(expr); - arg2 = get_rightop(expr); - if (IsCTIDVar(arg1)) - exstate = (ExprState *) lsecond(fexstate->args); - else if (IsCTIDVar(arg2)) - exstate = (ExprState *) linitial(fexstate->args); - else - elog(ERROR, "could not identify CTID variable"); - itemptr = (ItemPointer) - DatumGetPointer(ExecEvalExprSwitchContext(exstate, + DatumGetPointer(ExecEvalExprSwitchContext(tidexpr->exprstate, econtext, &isNull)); if (!isNull && @@ -119,9 +177,8 @@ TidListCreate(TidScanState *tidstate) tidList[numTids++] = *itemptr; } } - else if (expr && IsA(expr, ScalarArrayOpExpr)) + else if (tidexpr->exprstate && tidexpr->isarray) { - ScalarArrayOpExprState *saexstate = (ScalarArrayOpExprState *) exstate; Datum arraydatum; ArrayType *itemarray; Datum *ipdatums; @@ -129,8 +186,7 @@ TidListCreate(TidScanState *tidstate) int ndatums; int i; - exstate = (ExprState *) lsecond(saexstate->fxprstate.args); - arraydatum = ExecEvalExprSwitchContext(exstate, + arraydatum = ExecEvalExprSwitchContext(tidexpr->exprstate, econtext, &isNull); if (isNull) @@ -159,12 +215,12 @@ TidListCreate(TidScanState *tidstate) pfree(ipdatums); pfree(ipnulls); } - else if (expr && IsA(expr, CurrentOfExpr)) + else { - CurrentOfExpr *cexpr = (CurrentOfExpr *) expr; ItemPointerData cursor_tid; - if (execCurrentOf(cexpr, econtext, + Assert(tidexpr->cexpr); + if (execCurrentOf(tidexpr->cexpr, econtext, RelationGetRelid(tidstate->ss.ss_currentRelation), &cursor_tid)) { @@ -176,11 +232,8 @@ TidListCreate(TidScanState *tidstate) numAllocTids * sizeof(ItemPointerData)); } tidList[numTids++] = cursor_tid; - tidstate->tss_isCurrentOf = true; } } - else - elog(ERROR, "could not identify CTID expression"); } /* @@ -272,11 +325,15 @@ TidNext(TidScanState *node) * First time through, compute the list of TIDs to be visited */ if (node->tss_TidList == NULL) - TidListCreate(node); + TidListEval(node); tidList = node->tss_TidList; numTids = node->tss_NumTids; + /* + * We use node->tss_htup as the tuple pointer; note this can't just be a + * local variable here, as the scan tuple slot will keep a pointer to it. + */ tuple = &(node->tss_htup); /* @@ -470,16 +527,10 @@ ExecInitTidScan(TidScan *node, EState *estate, int eflags) /* * initialize child expressions */ - tidstate->ss.ps.targetlist = (List *) - ExecInitExpr((Expr *) node->scan.plan.targetlist, - (PlanState *) tidstate); - tidstate->ss.ps.qual = (List *) - ExecInitExpr((Expr *) node->scan.plan.qual, - (PlanState *) tidstate); - - tidstate->tss_tidquals = (List *) - ExecInitExpr((Expr *) node->tidquals, - (PlanState *) tidstate); + tidstate->ss.ps.qual = + ExecInitQual(node->scan.plan.qual, (PlanState *) tidstate); + + TidExprListCreate(tidstate); /* * tuple table initialization |
