mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
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
This commit is contained in:
@ -3501,7 +3501,7 @@ cost_qual_eval_walker(Node *node, cost_qual_eval_context *context)
|
||||
/*
|
||||
* Aggref and WindowFunc nodes are (and should be) treated like Vars,
|
||||
* ie, zero execution cost in the current model, because they behave
|
||||
* essentially like Vars in execQual.c. We disregard the costs of
|
||||
* essentially like Vars at execution. We disregard the costs of
|
||||
* their input expressions for the same reason. The actual execution
|
||||
* costs of the aggregate/window functions and their arguments have to
|
||||
* be factored into plan-node-specific costing of the Agg or WindowAgg
|
||||
|
@ -5013,7 +5013,7 @@ make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
|
||||
* bloat the sort dataset, and because it might cause unexpected output order
|
||||
* if the sort isn't stable. However there's a constraint on that: all SRFs
|
||||
* in the tlist should be evaluated at the same plan step, so that they can
|
||||
* run in sync in ExecTargetList. So if any SRFs are in sort columns, we
|
||||
* run in sync in nodeProjectSet. So if any SRFs are in sort columns, we
|
||||
* mustn't postpone any SRFs. (Note that in principle that policy should
|
||||
* probably get applied to the group/window input targetlists too, but we
|
||||
* have not done that historically.) Lastly, expensive expressions are
|
||||
|
@ -3395,7 +3395,7 @@ eval_const_expressions_mutator(Node *node,
|
||||
* Else, make a scalar (argisrow == false) NullTest
|
||||
* for this field. Scalar semantics are required
|
||||
* because IS [NOT] NULL doesn't recurse; see comments
|
||||
* in ExecEvalNullTest().
|
||||
* in ExecEvalRowNullInt().
|
||||
*/
|
||||
newntest = makeNode(NullTest);
|
||||
newntest->arg = (Expr *) relem;
|
||||
@ -3539,8 +3539,8 @@ eval_const_expressions_mutator(Node *node,
|
||||
* FALSE: drop (does not affect result)
|
||||
* TRUE: force result to TRUE
|
||||
* NULL: keep only one
|
||||
* We must keep one NULL input because ExecEvalOr returns NULL when no input
|
||||
* is TRUE and at least one is NULL. We don't actually include the NULL
|
||||
* We must keep one NULL input because OR expressions evaluate to NULL when no
|
||||
* input is TRUE and at least one is NULL. We don't actually include the NULL
|
||||
* here, that's supposed to be done by the caller.
|
||||
*
|
||||
* The output arguments *haveNull and *forceTrue must be initialized FALSE
|
||||
@ -3651,9 +3651,9 @@ simplify_or_arguments(List *args,
|
||||
* TRUE: drop (does not affect result)
|
||||
* FALSE: force result to FALSE
|
||||
* NULL: keep only one
|
||||
* We must keep one NULL input because ExecEvalAnd returns NULL when no input
|
||||
* is FALSE and at least one is NULL. We don't actually include the NULL
|
||||
* here, that's supposed to be done by the caller.
|
||||
* We must keep one NULL input because AND expressions evaluate to NULL when
|
||||
* no input is FALSE and at least one is NULL. We don't actually include the
|
||||
* NULL here, that's supposed to be done by the caller.
|
||||
*
|
||||
* The output arguments *haveNull and *forceFalse must be initialized FALSE
|
||||
* by the caller. They will be set TRUE if a null constant or false constant,
|
||||
|
Reference in New Issue
Block a user