Faster expression evaluation and targetlist projection

Enterprise / PostgreSQL - Andres Freund [anarazel.de] - 25 March 2017 17:52 EDT

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

b8d7f05 Faster expression evaluation and targetlist projection.
contrib/postgres_fdw/postgres_fdw.c | 2 +-
src/backend/bootstrap/bootstrap.c | 2 +-
src/backend/catalog/index.c | 43 +-
src/backend/catalog/partition.c | 3 +-
src/backend/catalog/toasting.c | 2 +-
src/backend/commands/analyze.c | 10 +-
src/backend/commands/explain.c | 2 +-
src/backend/commands/indexcmds.c | 4 +-
src/backend/commands/prepare.c | 4 +-
src/backend/commands/tablecmds.c | 25 +-
src/backend/commands/trigger.c | 10 +-
src/backend/executor/Makefile | 7 +-
src/backend/executor/README | 176 +-
src/backend/executor/execExpr.c | 2665 +++++++++++++++
src/backend/executor/execExprInterp.c | 3525 +++++++++++++++++++
src/backend/executor/execIndexing.c | 20 +-
src/backend/executor/execMain.c | 39 +-
src/backend/executor/execQual.c | 5313 -----------------------------
src/backend/executor/execSRF.c | 924 +++++
src/backend/executor/execScan.c | 10 +-
src/backend/executor/execUtils.c | 338 +-
src/backend/executor/functions.c | 2 +-
src/backend/executor/nodeAgg.c | 34 +-
src/backend/executor/nodeBitmapHeapscan.c | 17 +-
src/backend/executor/nodeCtescan.c | 8 +-
src/backend/executor/nodeCustom.c | 8 +-
src/backend/executor/nodeForeignscan.c | 15 +-
src/backend/executor/nodeFunctionscan.c | 19 +-
src/backend/executor/nodeGather.c | 8 +-
src/backend/executor/nodeGatherMerge.c | 8 +-
src/backend/executor/nodeGroup.c | 12 +-
src/backend/executor/nodeHash.c | 12 +-
src/backend/executor/nodeHashjoin.c | 44 +-
src/backend/executor/nodeIndexonlyscan.c | 15 +-
src/backend/executor/nodeIndexscan.c | 24 +-
src/backend/executor/nodeMergejoin.c | 33 +-
src/backend/executor/nodeModifyTable.c | 38 +-
src/backend/executor/nodeNestloop.c | 23 +-
src/backend/executor/nodeProjectSet.c | 61 +-
src/backend/executor/nodeResult.c | 16 +-
src/backend/executor/nodeSamplescan.c | 17 +-
src/backend/executor/nodeSeqscan.c | 8 +-
src/backend/executor/nodeSubplan.c | 96 +-
src/backend/executor/nodeSubqueryscan.c | 8 +-
src/backend/executor/nodeTableFuncscan.c | 20 +-
src/backend/executor/nodeTidscan.c | 139 +-
src/backend/executor/nodeValuesscan.c | 10 +-
src/backend/executor/nodeWindowAgg.c | 8 +-
src/backend/executor/nodeWorktablescan.c | 8 +-
src/backend/optimizer/path/costsize.c | 2 +-
src/backend/optimizer/plan/planner.c | 2 +-
src/backend/optimizer/util/clauses.c | 12 +-
src/backend/utils/adt/domains.c | 29 +-
src/backend/utils/adt/ruleutils.c | 4 +-
src/backend/utils/adt/xml.c | 45 +-
src/backend/utils/cache/typcache.c | 40 +-
src/include/executor/execExpr.h | 642 ++++
src/include/executor/execdebug.h | 23 +-
src/include/executor/executor.h | 178 +-
src/include/executor/nodeSubplan.h | 4 +
src/include/fmgr.h | 8 +
src/include/nodes/execnodes.h | 504 +--
src/include/nodes/nodes.h | 30 +-
src/include/utils/typcache.h | 3 +-
src/include/utils/xml.h | 4 +-
src/pl/plpgsql/src/pl_exec.c | 5 +-
src/test/regress/expected/case.out | 2 +-
src/test/regress/expected/privileges.out | 20 +-
src/test/regress/sql/case.sql | 2 +-
src/test/regress/sql/privileges.sql | 5 +-
src/tools/pgindent/typedefs.list | 6 +-
71 files changed, 8871 insertions(+), 6534 deletions(-)

Upstream: git.postgresql.org


  • Share