mirror of
https://github.com/postgres/postgres.git
synced 2025-09-02 04:21:28 +03:00
Drop no-op CoerceToDomain nodes from expressions at planning time.
If a domain has no constraints, then CoerceToDomain doesn't really do anything and can be simplified to a RelabelType. This not only eliminates cycles at execution, but allows the planner to optimize better (for instance, match the coerced expression to an index on the underlying column). However, we do have to support invalidating the plan later if a constraint gets added to the domain. That's comparable to the case of a change to a SQL function that had been inlined into a plan, so all the necessary logic already exists for plans depending on functions. We need only duplicate or share that logic for domains. ALTER DOMAIN ADD/DROP CONSTRAINT need to be taught to send out sinval messages for the domain's pg_type entry, since those operations don't update that row. (ALTER DOMAIN SET/DROP NOT NULL do update that row, so no code change is needed for them.) Testing this revealed what's really a pre-existing bug in plpgsql: it caches the SQL-expression-tree expansion of type coercions and had no provision for invalidating entries in that cache. Up to now that was only a problem if such an expression had inlined a SQL function that got changed, which is unlikely though not impossible. But failing to track changes of domain constraints breaks an existing regression test case and would likely cause practical problems too. We could fix that locally in plpgsql, but what seems like a better idea is to build some generic infrastructure in plancache.c to store standalone expressions and track invalidation events for them. (It's tempting to wonder whether plpgsql's "simple expression" stuff could use this code with lower overhead than its current use of the heavyweight plancache APIs. But I've left that idea for later.) Other stuff fixed in passing: * Allow estimate_expression_value() to drop CoerceToDomain unconditionally, effectively assuming that the coercion will succeed. This will improve planner selectivity estimates for cases involving estimatable expressions that are coerced to domains. We could have done this independently of everything else here, but there wasn't previously any need for eval_const_expressions_mutator to know about CoerceToDomain at all. * Use a dlist for plancache.c's list of cached plans, rather than a manually threaded singly-linked list. That eliminates a potential performance problem in DropCachedPlan. * Fix a couple of inconsistencies in typecmds.c about whether operations on domains drop RowExclusiveLock on pg_type. Our common practice is that DDL operations do drop catalog locks, so standardize on that choice. Discussion: https://postgr.es/m/19958.1544122124@sss.pgh.pa.us
This commit is contained in:
@@ -3699,6 +3699,70 @@ eval_const_expressions_mutator(Node *node,
|
||||
newbtest->location = btest->location;
|
||||
return (Node *) newbtest;
|
||||
}
|
||||
case T_CoerceToDomain:
|
||||
{
|
||||
/*
|
||||
* If the domain currently has no constraints, we replace the
|
||||
* CoerceToDomain node with a simple RelabelType, which is
|
||||
* both far faster to execute and more amenable to later
|
||||
* optimization. We must then mark the plan as needing to be
|
||||
* rebuilt if the domain's constraints change.
|
||||
*
|
||||
* Also, in estimation mode, always replace CoerceToDomain
|
||||
* nodes, effectively assuming that the coercion will succeed.
|
||||
*/
|
||||
CoerceToDomain *cdomain = (CoerceToDomain *) node;
|
||||
CoerceToDomain *newcdomain;
|
||||
Node *arg;
|
||||
|
||||
arg = eval_const_expressions_mutator((Node *) cdomain->arg,
|
||||
context);
|
||||
if (context->estimate ||
|
||||
!DomainHasConstraints(cdomain->resulttype))
|
||||
{
|
||||
/* Record dependency, if this isn't estimation mode */
|
||||
if (context->root && !context->estimate)
|
||||
record_plan_type_dependency(context->root,
|
||||
cdomain->resulttype);
|
||||
|
||||
/* Generate RelabelType to substitute for CoerceToDomain */
|
||||
/* This should match the RelabelType logic above */
|
||||
|
||||
while (arg && IsA(arg, RelabelType))
|
||||
arg = (Node *) ((RelabelType *) arg)->arg;
|
||||
|
||||
if (arg && IsA(arg, Const))
|
||||
{
|
||||
Const *con = (Const *) arg;
|
||||
|
||||
con->consttype = cdomain->resulttype;
|
||||
con->consttypmod = cdomain->resulttypmod;
|
||||
con->constcollid = cdomain->resultcollid;
|
||||
return (Node *) con;
|
||||
}
|
||||
else
|
||||
{
|
||||
RelabelType *newrelabel = makeNode(RelabelType);
|
||||
|
||||
newrelabel->arg = (Expr *) arg;
|
||||
newrelabel->resulttype = cdomain->resulttype;
|
||||
newrelabel->resulttypmod = cdomain->resulttypmod;
|
||||
newrelabel->resultcollid = cdomain->resultcollid;
|
||||
newrelabel->relabelformat = cdomain->coercionformat;
|
||||
newrelabel->location = cdomain->location;
|
||||
return (Node *) newrelabel;
|
||||
}
|
||||
}
|
||||
|
||||
newcdomain = makeNode(CoerceToDomain);
|
||||
newcdomain->arg = (Expr *) arg;
|
||||
newcdomain->resulttype = cdomain->resulttype;
|
||||
newcdomain->resulttypmod = cdomain->resulttypmod;
|
||||
newcdomain->resultcollid = cdomain->resultcollid;
|
||||
newcdomain->coercionformat = cdomain->coercionformat;
|
||||
newcdomain->location = cdomain->location;
|
||||
return (Node *) newcdomain;
|
||||
}
|
||||
case T_PlaceHolderVar:
|
||||
|
||||
/*
|
||||
@@ -3770,7 +3834,7 @@ eval_const_expressions_mutator(Node *node,
|
||||
* For any node type not handled above, copy the node unchanged but
|
||||
* const-simplify its subexpressions. This is the correct thing for node
|
||||
* types whose behavior might change between planning and execution, such
|
||||
* as CoerceToDomain. It's also a safe default for new node types not
|
||||
* as CurrentOfExpr. It's also a safe default for new node types not
|
||||
* known to this routine.
|
||||
*/
|
||||
return ece_generic_processing(node);
|
||||
|
Reference in New Issue
Block a user