mirror of
https://github.com/postgres/postgres.git
synced 2025-07-11 10:01:57 +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:
40
src/backend/utils/cache/typcache.c
vendored
40
src/backend/utils/cache/typcache.c
vendored
@ -96,11 +96,11 @@ static TypeCacheEntry *firstDomainTypeEntry = NULL;
|
||||
* this struct for the common case of a constraint-less domain; we just set
|
||||
* domainData to NULL to indicate that.
|
||||
*
|
||||
* Within a DomainConstraintCache, we abuse the DomainConstraintState node
|
||||
* type a bit: check_expr fields point to expression plan trees, not plan
|
||||
* state trees. When needed, expression state trees are built by flat-copying
|
||||
* the DomainConstraintState nodes and applying ExecInitExpr to check_expr.
|
||||
* Such a state tree is not part of the DomainConstraintCache, but is
|
||||
* Within a DomainConstraintCache, we store expression plan trees, but the
|
||||
* check_exprstate fields of the DomainConstraintState nodes are just NULL.
|
||||
* When needed, expression evaluation nodes are built by flat-copying the
|
||||
* DomainConstraintState nodes and applying ExecInitExpr to check_expr.
|
||||
* Such a node tree is not part of the DomainConstraintCache, but is
|
||||
* considered to belong to a DomainConstraintRef.
|
||||
*/
|
||||
struct DomainConstraintCache
|
||||
@ -779,8 +779,8 @@ load_domaintype_info(TypeCacheEntry *typentry)
|
||||
r = makeNode(DomainConstraintState);
|
||||
r->constrainttype = DOM_CONSTRAINT_CHECK;
|
||||
r->name = pstrdup(NameStr(c->conname));
|
||||
/* Must cast here because we're not storing an expr state node */
|
||||
r->check_expr = (ExprState *) check_expr;
|
||||
r->check_expr = check_expr;
|
||||
r->check_exprstate = NULL;
|
||||
|
||||
MemoryContextSwitchTo(oldcxt);
|
||||
|
||||
@ -859,6 +859,7 @@ load_domaintype_info(TypeCacheEntry *typentry)
|
||||
r->constrainttype = DOM_CONSTRAINT_NOTNULL;
|
||||
r->name = pstrdup("NOT NULL");
|
||||
r->check_expr = NULL;
|
||||
r->check_exprstate = NULL;
|
||||
|
||||
/* lcons to apply the nullness check FIRST */
|
||||
dcc->constraints = lcons(r, dcc->constraints);
|
||||
@ -946,8 +947,8 @@ prep_domain_constraints(List *constraints, MemoryContext execctx)
|
||||
newr = makeNode(DomainConstraintState);
|
||||
newr->constrainttype = r->constrainttype;
|
||||
newr->name = r->name;
|
||||
/* Must cast here because cache items contain expr plan trees */
|
||||
newr->check_expr = ExecInitExpr((Expr *) r->check_expr, NULL);
|
||||
newr->check_expr = r->check_expr;
|
||||
newr->check_exprstate = ExecInitExpr(r->check_expr, NULL);
|
||||
|
||||
result = lappend(result, newr);
|
||||
}
|
||||
@ -962,13 +963,18 @@ prep_domain_constraints(List *constraints, MemoryContext execctx)
|
||||
*
|
||||
* Caller must tell us the MemoryContext in which the DomainConstraintRef
|
||||
* lives. The ref will be cleaned up when that context is reset/deleted.
|
||||
*
|
||||
* Caller must also tell us whether it wants check_exprstate fields to be
|
||||
* computed in the DomainConstraintState nodes attached to this ref.
|
||||
* If it doesn't, we need not make a copy of the DomainConstraintState list.
|
||||
*/
|
||||
void
|
||||
InitDomainConstraintRef(Oid type_id, DomainConstraintRef *ref,
|
||||
MemoryContext refctx)
|
||||
MemoryContext refctx, bool need_exprstate)
|
||||
{
|
||||
/* Look up the typcache entry --- we assume it survives indefinitely */
|
||||
ref->tcache = lookup_type_cache(type_id, TYPECACHE_DOMAIN_INFO);
|
||||
ref->need_exprstate = need_exprstate;
|
||||
/* For safety, establish the callback before acquiring a refcount */
|
||||
ref->refctx = refctx;
|
||||
ref->dcc = NULL;
|
||||
@ -980,8 +986,11 @@ InitDomainConstraintRef(Oid type_id, DomainConstraintRef *ref,
|
||||
{
|
||||
ref->dcc = ref->tcache->domainData;
|
||||
ref->dcc->dccRefCount++;
|
||||
ref->constraints = prep_domain_constraints(ref->dcc->constraints,
|
||||
ref->refctx);
|
||||
if (ref->need_exprstate)
|
||||
ref->constraints = prep_domain_constraints(ref->dcc->constraints,
|
||||
ref->refctx);
|
||||
else
|
||||
ref->constraints = ref->dcc->constraints;
|
||||
}
|
||||
else
|
||||
ref->constraints = NIL;
|
||||
@ -1032,8 +1041,11 @@ UpdateDomainConstraintRef(DomainConstraintRef *ref)
|
||||
{
|
||||
ref->dcc = dcc;
|
||||
dcc->dccRefCount++;
|
||||
ref->constraints = prep_domain_constraints(dcc->constraints,
|
||||
ref->refctx);
|
||||
if (ref->need_exprstate)
|
||||
ref->constraints = prep_domain_constraints(dcc->constraints,
|
||||
ref->refctx);
|
||||
else
|
||||
ref->constraints = dcc->constraints;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user