Support arrays over domains

Enterprise / PostgreSQL - Tom Lane [sss.pgh.pa.us] - 30 September 2017 13:40 EDT

Allowing arrays with a domain type as their element type was left un-done in the original domain patch, but not for any very good reason. This omission leads to such surprising results as array_agg() not working on a domain column, because the parser can't identify a suitable output type for the polymorphic aggregate.

In order to fix this, first clean up the APIs of coerce_to_domain() and some internal functions in parse_coerce.c so that we consistently pass around a CoercionContext along with CoercionForm. Previously, we sometimes passed an "isExplicit" boolean flag instead, which is strictly less information; and coerce_to_domain() didn't even get that, but instead had to reverse-engineer isExplicit from CoercionForm. That's contrary to the documentation in primnodes.h that says that CoercionForm only affects display and not semantics. I don't think this change fixes any live bugs, but it makes things more consistent. The main reason for doing it though is that now build_coercion_expression() receives ccontext, which it needs in order to be able to recursively invoke coerce_to_target_type().

Next, reimplement ArrayCoerceExpr so that the node does not directly know any details of what has to be done to the individual array elements while performing the array coercion. Instead, the per-element processing is represented by a sub-expression whose input is a source array element and whose output is a target array element. This simplifies life in parse_coerce.c, because it can build that sub-expression by a recursive invocation of coerce_to_target_type(). The executor now handles the per-element processing as a compiled expression instead of hard-wired code. The main advantage of this is that we can use a single ArrayCoerceExpr to handle as many as three successive steps per element: base type conversion, typmod coercion, and domain constraint checking. The old code used two stacked ArrayCoerceExprs to handle type + typmod coercion, which was pretty inefficient, and adding yet another array deconstruction to do domain constraint checking seemed very unappetizing.

In the case where we just need a single, very simple coercion function, doing this straightforwardly leads to a noticeable increase in the per-array-element runtime cost. Hence, add an additional shortcut evalfunc in execExprInterp.c that skips unnecessary overhead for that specific form of expression. The runtime speed of simple cases is within 1% or so of where it was before, while cases that previously required two levels of array processing are significantly faster.

Finally, create an implicit array type for every domain type, as we do for base types, enums, etc. Everything except the array-coercion case seems to just work without further effort.

Tom Lane, reviewed by Andrew Dunstan

Discussion: https://postgr.es/m/9852.1499791473@sss.pgh.pa.us

c12d570 Support arrays over domains.
contrib/pg_stat_statements/pg_stat_statements.c | 1 +
doc/src/sgml/array.sgml | 5 +-
src/backend/catalog/dependency.c | 9 +-
src/backend/commands/typecmds.c | 50 +++++++-
src/backend/executor/execExpr.c | 58 +++++----
src/backend/executor/execExprInterp.c | 86 ++++++++-----
src/backend/nodes/copyfuncs.c | 3 +-
src/backend/nodes/equalfuncs.c | 3 +-
src/backend/nodes/nodeFuncs.c | 20 +--
src/backend/nodes/outfuncs.c | 3 +-
src/backend/nodes/readfuncs.c | 3 +-
src/backend/optimizer/path/costsize.c | 13 +-
src/backend/optimizer/plan/setrefs.c | 6 -
src/backend/optimizer/prep/preptlist.c | 2 +-
src/backend/optimizer/util/clauses.c | 46 ++++---
src/backend/parser/parse_coerce.c | 155 ++++++++++++++----------
src/backend/rewrite/rewriteHandler.c | 4 +-
src/backend/rewrite/rewriteManip.c | 2 +-
src/backend/utils/adt/arrayfuncs.c | 85 +++++--------
src/backend/utils/adt/selfuncs.c | 15 ++-
src/backend/utils/fmgr/fmgr.c | 12 +-
src/include/catalog/catversion.h | 2 +-
src/include/executor/execExpr.h | 7 +-
src/include/nodes/primnodes.h | 14 +--
src/include/parser/parse_coerce.h | 5 +-
src/include/utils/array.h | 9 +-
src/test/regress/expected/domain.out | 95 +++++++++++++++
src/test/regress/sql/domain.sql | 43 +++++++
28 files changed, 492 insertions(+), 264 deletions(-)

Upstream: git.postgresql.org


  • Share