+ aclcheck_error_type(ACLCHECK_NOT_OWNER, HeapTupleGetOid(tup));
+}
+
+
+/*
+ * DefineRange
+ * Registers a new range type.
+ */
+ObjectAddress
+DefineRange(CreateRangeStmt *stmt)
+{
+ char *typeName;
+ Oid typeNamespace;
+ Oid typoid;
+ char *rangeArrayName;
+ Oid rangeArrayOid;
+ Oid rangeSubtype = InvalidOid;
+ List *rangeSubOpclassName = NIL;
+ List *rangeCollationName = NIL;
+ List *rangeCanonicalName = NIL;
+ List *rangeSubtypeDiffName = NIL;
+ Oid rangeSubOpclass;
+ Oid rangeCollation;
+ regproc rangeCanonical;
+ regproc rangeSubtypeDiff;
+ int16 subtyplen;
+ bool subtypbyval;
+ char subtypalign;
+ char alignment;
+ AclResult aclresult;
+ ListCell *lc;
+ ObjectAddress address;
+
+ /* Convert list of names to a name and namespace */
+ typeNamespace = QualifiedNameGetCreationNamespace(stmt->typeName,
+ &typeName);
+
+ /* Check we have creation rights in target namespace */
+ aclresult = pg_namespace_aclcheck(typeNamespace, GetUserId(), ACL_CREATE);
+ if (aclresult != ACLCHECK_OK)
+ aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
+ get_namespace_name(typeNamespace));
+
+ /*
+ * Look to see if type already exists.
+ */
+ typoid = GetSysCacheOid2(TYPENAMENSP,
+ CStringGetDatum(typeName),
+ ObjectIdGetDatum(typeNamespace));
+
+ /*
+ * If it's not a shell, see if it's an autogenerated array type, and if so
+ * rename it out of the way.
+ */
+ if (OidIsValid(typoid) && get_typisdefined(typoid))
+ {
+ if (moveArrayTypeName(typoid, typeName, typeNamespace))
+ typoid = InvalidOid;
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_DUPLICATE_OBJECT),
+ errmsg("type \"%s\" already exists", typeName)));
+ }
+
+ /*
+ * If it doesn't exist, create it as a shell, so that the OID is known for
+ * use in the range function definitions.
+ */
+ if (!OidIsValid(typoid))
+ {
+ address = TypeShellMake(typeName, typeNamespace, GetUserId());
+ typoid = address.objectId;
+ /* Make new shell type visible for modification below */
+ CommandCounterIncrement();
+ }
+
+ /* Extract the parameters from the parameter list */
+ foreach(lc, stmt->params)
+ {
+ DefElem *defel = (DefElem *) lfirst(lc);
+
+ if (pg_strcasecmp(defel->defname, "subtype") == 0)
+ {
+ if (OidIsValid(rangeSubtype))
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("conflicting or redundant options")));
+ /* we can look up the subtype name immediately */
+ rangeSubtype = typenameTypeId(NULL, defGetTypeName(defel));
+ }
+ else if (pg_strcasecmp(defel->defname, "subtype_opclass") == 0)
+ {
+ if (rangeSubOpclassName != NIL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("conflicting or redundant options")));
+ rangeSubOpclassName = defGetQualifiedName(defel);
+ }
+ else if (pg_strcasecmp(defel->defname, "collation") == 0)
+ {
+ if (rangeCollationName != NIL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("conflicting or redundant options")));
+ rangeCollationName = defGetQualifiedName(defel);
+ }
+ else if (pg_strcasecmp(defel->defname, "canonical") == 0)
+ {
+ if (rangeCanonicalName != NIL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("conflicting or redundant options")));
+ rangeCanonicalName = defGetQualifiedName(defel);
+ }
+ else if (pg_strcasecmp(defel->defname, "subtype_diff") == 0)
+ {
+ if (rangeSubtypeDiffName != NIL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("conflicting or redundant options")));
+ rangeSubtypeDiffName = defGetQualifiedName(defel);
+ }
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("type attribute \"%s\" not recognized",
+ defel->defname)));
+ }
+
+ /* Must have a subtype */
+ if (!OidIsValid(rangeSubtype))
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("type attribute \"subtype\" is required")));
+ /* disallow ranges of pseudotypes */
+ if (get_typtype(rangeSubtype) == TYPTYPE_PSEUDO)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("range subtype cannot be %s",
+ format_type_be(rangeSubtype))));
+
+ /* Identify subopclass */
+ rangeSubOpclass = findRangeSubOpclass(rangeSubOpclassName, rangeSubtype);
+
+ /* Identify collation to use, if any */
+ if (type_is_collatable(rangeSubtype))
+ {
+ if (rangeCollationName != NIL)
+ rangeCollation = get_collation_oid(rangeCollationName, false);
+ else
+ rangeCollation = get_typcollation(rangeSubtype);
+ }
+ else
+ {
+ if (rangeCollationName != NIL)
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("range collation specified but subtype does not support collation")));
+ rangeCollation = InvalidOid;
+ }
+
+ /* Identify support functions, if provided */
+ if (rangeCanonicalName != NIL)
+ rangeCanonical = findRangeCanonicalFunction(rangeCanonicalName,
+ typoid);
+ else
+ rangeCanonical = InvalidOid;
+
+ if (rangeSubtypeDiffName != NIL)
+ rangeSubtypeDiff = findRangeSubtypeDiffFunction(rangeSubtypeDiffName,
+ rangeSubtype);
+ else
+ rangeSubtypeDiff = InvalidOid;
+
+ get_typlenbyvalalign(rangeSubtype,
+ &subtyplen, &subtypbyval, &subtypalign);
+
+ /* alignment must be 'i' or 'd' for ranges */
+ alignment = (subtypalign == 'd') ? 'd' : 'i';
+
+ /* Allocate OID for array type */
+ rangeArrayOid = AssignTypeArrayOid();
+
+ /* Create the pg_type entry */
+ address =
+ TypeCreate(InvalidOid, /* no predetermined type OID */
+ typeName, /* type name */
+ typeNamespace, /* namespace */
+ InvalidOid, /* relation oid (n/a here) */
+ 0, /* relation kind (ditto) */
+ GetUserId(), /* owner's ID */
+ -1, /* internal size (always varlena) */
+ TYPTYPE_RANGE, /* type-type (range type) */
+ TYPCATEGORY_RANGE, /* type-category (range type) */
+ false, /* range types are never preferred */
+ DEFAULT_TYPDELIM, /* array element delimiter */
+ F_RANGE_IN, /* input procedure */
+ F_RANGE_OUT, /* output procedure */
+ F_RANGE_RECV, /* receive procedure */
+ F_RANGE_SEND, /* send procedure */
+ InvalidOid, /* typmodin procedure - none */
+ InvalidOid, /* typmodout procedure - none */
+ F_RANGE_TYPANALYZE, /* analyze procedure */
+ InvalidOid, /* element type ID - none */
+ false, /* this is not an array type */
+ rangeArrayOid, /* array type we are about to create */
+ InvalidOid, /* base type ID (only for domains) */
+ NULL, /* never a default type value */
+ NULL, /* no binary form available either */
+ false, /* never passed by value */
+ alignment, /* alignment */
+ 'x', /* TOAST strategy (always extended) */
+ -1, /* typMod (Domains only) */
+ 0, /* Array dimensions of typbasetype */
+ false, /* Type NOT NULL */
+ InvalidOid); /* type's collation (ranges never have one) */
+ Assert(typoid == address.objectId);
+
+ /* Create the entry in pg_range */
+ RangeCreate(typoid, rangeSubtype, rangeCollation, rangeSubOpclass,
+ rangeCanonical, rangeSubtypeDiff);
+
+ /*
+ * Create the array type that goes with it.
+ */
+ rangeArrayName = makeArrayTypeName(typeName, typeNamespace);
+
+ TypeCreate(rangeArrayOid, /* force assignment of this type OID */
+ rangeArrayName, /* type name */
+ typeNamespace, /* 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 */
+ DEFAULT_TYPDELIM, /* 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 */
+ typoid, /* 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, /* alignment - same as range's */
+ 'x', /* ARRAY is always toastable */
+ -1, /* typMod (Domains only) */
+ 0, /* Array dimensions of typbasetype */
+ false, /* Type NOT NULL */
+ InvalidOid); /* typcollation */
+
+ pfree(rangeArrayName);
+
+ /* And create the constructor functions for this range type */
+ makeRangeConstructors(typeName, typeNamespace, typoid, rangeSubtype);
+
+ return address;
+}
+
+/*
+ * Because there may exist several range types over the same subtype, the
+ * range type can't be uniquely determined from the subtype. So it's
+ * impossible to define a polymorphic constructor; we have to generate new
+ * constructor functions explicitly for each range type.
+ *
+ * We actually define 4 functions, with 0 through 3 arguments. This is just
+ * to offer more convenience for the user.
+ */
+static void
+makeRangeConstructors(const char *name, Oid namespace,
+ Oid rangeOid, Oid subtype)
+{
+ static const char *const prosrc[2] = {"range_constructor2",
+ "range_constructor3"};
+ static const int pronargs[2] = {2, 3};
+
+ Oid constructorArgTypes[3];
+ ObjectAddress myself,
+ referenced;
+ int i;
+
+ constructorArgTypes[0] = subtype;
+ constructorArgTypes[1] = subtype;
+ constructorArgTypes[2] = TEXTOID;
+
+ referenced.classId = TypeRelationId;
+ referenced.objectId = rangeOid;
+ referenced.objectSubId = 0;
+
+ for (i = 0; i < lengthof(prosrc); i++)
+ {
+ oidvector *constructorArgTypesVector;
+
+ constructorArgTypesVector = buildoidvector(constructorArgTypes,
+ pronargs[i]);
+
+ myself = ProcedureCreate(name, /* name: same as range type */
+ namespace, /* namespace */
+ false, /* replace */
+ false, /* returns set */
+ rangeOid, /* return type */
+ BOOTSTRAP_SUPERUSERID, /* proowner */
+ INTERNALlanguageId, /* language */
+ F_FMGR_INTERNAL_VALIDATOR, /* language validator */
+ prosrc[i], /* prosrc */
+ NULL, /* probin */
+ false, /* isAgg */
+ false, /* isWindowFunc */
+ false, /* security_definer */
+ false, /* leakproof */
+ false, /* isStrict */
+ PROVOLATILE_IMMUTABLE, /* volatility */
+ PROPARALLEL_SAFE, /* parallel safety */
+ constructorArgTypesVector, /* parameterTypes */
+ PointerGetDatum(NULL), /* allParameterTypes */
+ PointerGetDatum(NULL), /* parameterModes */
+ PointerGetDatum(NULL), /* parameterNames */
+ NIL, /* parameterDefaults */
+ PointerGetDatum(NULL), /* trftypes */
+ PointerGetDatum(NULL), /* proconfig */
+ 1.0, /* procost */
+ 0.0); /* prorows */
+
+ /*
+ * Make the constructors internally-dependent on the range type so
+ * that they go away silently when the type is dropped. Note that
+ * pg_dump depends on this choice to avoid dumping the constructors.
+ */
+ recordDependencyOn(&myself, &referenced, DEPENDENCY_INTERNAL);
+ }