diff options
author | Andres Freund <andres@anarazel.de> | 2017-03-14 15:45:36 -0700 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2017-03-25 14:52:06 -0700 |
commit | b8d7f053c5c2bf2a7e8734fe3327f6a8bc711755 (patch) | |
tree | 6fd5db4d05a3dec9bed6b8cc4c98ca9d3f80425e /src/backend/executor/nodeProjectSet.c | |
parent | 7d3957e53ebf26fc8d72dee1dacc2c827cc07caa (diff) | |
download | postgresql-b8d7f053c5c2bf2a7e8734fe3327f6a8bc711755.tar.gz postgresql-b8d7f053c5c2bf2a7e8734fe3327f6a8bc711755.zip |
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/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
Diffstat (limited to 'src/backend/executor/nodeProjectSet.c')
-rw-r--r-- | src/backend/executor/nodeProjectSet.c | 61 |
1 files changed, 42 insertions, 19 deletions
diff --git a/src/backend/executor/nodeProjectSet.c b/src/backend/executor/nodeProjectSet.c index eae0f1dad93..01048cc8268 100644 --- a/src/backend/executor/nodeProjectSet.c +++ b/src/backend/executor/nodeProjectSet.c @@ -24,6 +24,7 @@ #include "executor/executor.h" #include "executor/nodeProjectSet.h" +#include "nodes/nodeFuncs.h" #include "utils/memutils.h" @@ -119,10 +120,9 @@ ExecProjectSRF(ProjectSetState *node, bool continuing) { TupleTableSlot *resultSlot = node->ps.ps_ResultTupleSlot; ExprContext *econtext = node->ps.ps_ExprContext; - bool hassrf PG_USED_FOR_ASSERTS_ONLY = false; + bool hassrf PG_USED_FOR_ASSERTS_ONLY; bool hasresult; int argno; - ListCell *lc; ExecClearTuple(resultSlot); @@ -132,11 +132,10 @@ ExecProjectSRF(ProjectSetState *node, bool continuing) */ node->pending_srf_tuples = false; - hasresult = false; - argno = 0; - foreach(lc, node->ps.targetlist) + hassrf = hasresult = false; + for (argno = 0; argno < node->nelems; argno++) { - GenericExprState *gstate = (GenericExprState *) lfirst(lc); + Node *elem = node->elems[argno]; ExprDoneCond *isdone = &node->elemdone[argno]; Datum *result = &resultSlot->tts_values[argno]; bool *isnull = &resultSlot->tts_isnull[argno]; @@ -151,13 +150,12 @@ ExecProjectSRF(ProjectSetState *node, bool continuing) *isnull = true; hassrf = true; } - else if (IsA(gstate->arg, FuncExprState) && - ((FuncExprState *) gstate->arg)->funcReturnsSet) + else if (IsA(elem, SetExprState)) { /* * Evaluate SRF - possibly continuing previously started output. */ - *result = ExecMakeFunctionResultSet((FuncExprState *) gstate->arg, + *result = ExecMakeFunctionResultSet((SetExprState *) elem, econtext, isnull, isdone); if (*isdone != ExprEndResult) @@ -169,11 +167,9 @@ ExecProjectSRF(ProjectSetState *node, bool continuing) else { /* Non-SRF tlist expression, just evaluate normally. */ - *result = ExecEvalExpr(gstate->arg, econtext, isnull); + *result = ExecEvalExpr((ExprState *) elem, econtext, isnull); *isdone = ExprSingleResult; } - - argno++; } /* ProjectSet should not be used if there's no SRFs */ @@ -204,6 +200,8 @@ ProjectSetState * ExecInitProjectSet(ProjectSet *node, EState *estate, int eflags) { ProjectSetState *state; + ListCell *lc; + int off; /* check for unsupported flags */ Assert(!(eflags & (EXEC_FLAG_MARK | EXEC_FLAG_BACKWARD))); @@ -229,12 +227,7 @@ ExecInitProjectSet(ProjectSet *node, EState *estate, int eflags) */ ExecInitResultTupleSlot(estate, &state->ps); - /* - * initialize child expressions - */ - state->ps.targetlist = (List *) - ExecInitExpr((Expr *) node->plan.targetlist, - (PlanState *) state); + /* We don't support any qual on ProjectSet nodes */ Assert(node->plan.qual == NIL); /* @@ -252,11 +245,41 @@ ExecInitProjectSet(ProjectSet *node, EState *estate, int eflags) */ ExecAssignResultTypeFromTL(&state->ps); - /* Create workspace for per-SRF is-done state */ + /* Create workspace for per-tlist-entry expr state & SRF-is-done state */ state->nelems = list_length(node->plan.targetlist); + state->elems = (Node **) + palloc(sizeof(Node *) * state->nelems); state->elemdone = (ExprDoneCond *) palloc(sizeof(ExprDoneCond) * state->nelems); + /* + * Build expressions to evaluate targetlist. We can't use + * ExecBuildProjectionInfo here, since that doesn't deal with SRFs. + * Instead compile each expression separately, using + * ExecInitFunctionResultSet where applicable. + */ + off = 0; + foreach(lc, node->plan.targetlist) + { + TargetEntry *te = (TargetEntry *) lfirst(lc); + Expr *expr = te->expr; + + if ((IsA(expr, FuncExpr) &&((FuncExpr *) expr)->funcretset) || + (IsA(expr, OpExpr) &&((OpExpr *) expr)->opretset)) + { + state->elems[off] = (Node *) + ExecInitFunctionResultSet(expr, state->ps.ps_ExprContext, + &state->ps); + } + else + { + Assert(!expression_returns_set((Node *) expr)); + state->elems[off] = (Node *) ExecInitExpr(expr, &state->ps); + } + + off++; + } + return state; } |