aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/typecmds.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2017-09-30 13:40:56 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2017-09-30 13:40:56 -0400
commitc12d570fa147d0ec273df53de3a2802925d551ba (patch)
tree781667b2e5ee4fe98ec4d1c9042cff5bfa2bc829 /src/backend/commands/typecmds.c
parent248e33756b425335d94a32ffc8e9aace04f82c31 (diff)
downloadpostgresql-c12d570fa147d0ec273df53de3a2802925d551ba.tar.gz
postgresql-c12d570fa147d0ec273df53de3a2802925d551ba.zip
Support arrays over domains.
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
Diffstat (limited to 'src/backend/commands/typecmds.c')
-rw-r--r--src/backend/commands/typecmds.c50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c
index 4c490ed5c1b..c1b87e09e74 100644
--- a/src/backend/commands/typecmds.c
+++ b/src/backend/commands/typecmds.c
@@ -729,6 +729,7 @@ ObjectAddress
DefineDomain(CreateDomainStmt *stmt)
{
char *domainName;
+ char *domainArrayName;
Oid domainNamespace;
AclResult aclresult;
int16 internalLength;
@@ -757,6 +758,7 @@ DefineDomain(CreateDomainStmt *stmt)
Oid basetypeoid;
Oid old_type_oid;
Oid domaincoll;
+ Oid domainArrayOid;
Form_pg_type baseType;
int32 basetypeMod;
Oid baseColl;
@@ -1027,6 +1029,9 @@ DefineDomain(CreateDomainStmt *stmt)
}
}
+ /* Allocate OID for array type */
+ domainArrayOid = AssignTypeArrayOid();
+
/*
* Have TypeCreate do all the real work.
*/
@@ -1051,7 +1056,7 @@ DefineDomain(CreateDomainStmt *stmt)
analyzeProcedure, /* analyze procedure */
InvalidOid, /* no array element type */
false, /* this isn't an array */
- InvalidOid, /* no arrays for domains (yet) */
+ domainArrayOid, /* array type we are about to create */
basetypeoid, /* base type ID */
defaultValue, /* default type value (text) */
defaultValueBin, /* default type value (binary) */
@@ -1064,6 +1069,48 @@ DefineDomain(CreateDomainStmt *stmt)
domaincoll); /* type's collation */
/*
+ * Create the array type that goes with it.
+ */
+ domainArrayName = makeArrayTypeName(domainName, domainNamespace);
+
+ /* alignment must be 'i' or 'd' for arrays */
+ alignment = (alignment == 'd') ? 'd' : 'i';
+
+ TypeCreate(domainArrayOid, /* force assignment of this type OID */
+ domainArrayName, /* type name */
+ domainNamespace, /* namespace */
+ InvalidOid, /* relation oid (n/a here) */
+ 0, /* relation kind (ditto) */
+ GetUserId(), /* owner's ID */
+ -1, /* internal size (always varlena) */
+ TYPTYPE_BASE, /* type-type (base type) */
+ TYPCATEGORY_ARRAY, /* type-category (array) */
+ false, /* array types are never preferred */
+ delimiter, /* array element delimiter */
+ F_ARRAY_IN, /* input procedure */
+ F_ARRAY_OUT, /* output procedure */
+ F_ARRAY_RECV, /* receive procedure */
+ F_ARRAY_SEND, /* send procedure */
+ InvalidOid, /* typmodin procedure - none */
+ InvalidOid, /* typmodout procedure - none */
+ F_ARRAY_TYPANALYZE, /* analyze procedure */
+ address.objectId, /* element type ID */
+ true, /* yes this is an array type */
+ InvalidOid, /* no further array type */
+ InvalidOid, /* base type ID */
+ NULL, /* never a default type value */
+ NULL, /* binary default isn't sent either */
+ false, /* never passed by value */
+ alignment, /* see above */
+ 'x', /* ARRAY is always toastable */
+ -1, /* typMod (Domains only) */
+ 0, /* Array dimensions of typbasetype */
+ false, /* Type NOT NULL */
+ domaincoll); /* type's collation */
+
+ pfree(domainArrayName);
+
+ /*
* Process constraints which refer to the domain ID returned by TypeCreate
*/
foreach(listptr, schema)
@@ -1139,6 +1186,7 @@ DefineEnum(CreateEnumStmt *stmt)
errmsg("type \"%s\" already exists", enumName)));
}
+ /* Allocate OID for array type */
enumArrayOid = AssignTypeArrayOid();
/* Create the pg_type entry */