PostgreSQL Source Code git master
Loading...
Searching...
No Matches
typecmds.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * typecmds.c
4 * Routines for SQL commands that manipulate types (and domains).
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/commands/typecmds.c
12 *
13 * DESCRIPTION
14 * The "DefineFoo" routines take the parse tree and pick out the
15 * appropriate arguments/flags, passing the results to the
16 * corresponding "FooCreate" routines (in src/backend/catalog) that do
17 * the actual catalog-munging. These routines also verify permission
18 * of the user to execute the command.
19 *
20 * NOTES
21 * These things must be defined and committed in the following order:
22 * "create function":
23 * input/output, recv/send functions
24 * "create type":
25 * type
26 * "create operator":
27 * operators
28 *
29 *
30 *-------------------------------------------------------------------------
31 */
32#include "postgres.h"
33
34#include "access/genam.h"
35#include "access/htup_details.h"
36#include "access/relation.h"
37#include "access/table.h"
38#include "access/tableam.h"
39#include "access/xact.h"
41#include "catalog/catalog.h"
42#include "catalog/heap.h"
44#include "catalog/pg_am.h"
45#include "catalog/pg_authid.h"
46#include "catalog/pg_cast.h"
49#include "catalog/pg_depend.h"
50#include "catalog/pg_enum.h"
51#include "catalog/pg_language.h"
53#include "catalog/pg_proc.h"
54#include "catalog/pg_range.h"
55#include "catalog/pg_type.h"
56#include "commands/defrem.h"
57#include "commands/tablecmds.h"
58#include "commands/typecmds.h"
59#include "executor/executor.h"
60#include "miscadmin.h"
61#include "nodes/makefuncs.h"
62#include "optimizer/optimizer.h"
63#include "parser/parse_coerce.h"
65#include "parser/parse_expr.h"
66#include "parser/parse_func.h"
67#include "parser/parse_type.h"
68#include "utils/builtins.h"
69#include "utils/fmgroids.h"
70#include "utils/inval.h"
71#include "utils/lsyscache.h"
72#include "utils/rel.h"
73#include "utils/ruleutils.h"
74#include "utils/snapmgr.h"
75#include "utils/syscache.h"
76
77
78/* result structure for get_rels_with_domain() */
79typedef struct
80{
81 Relation rel; /* opened and locked relation */
82 int natts; /* number of attributes of interest */
83 int *atts; /* attribute numbers */
84 /* atts[] is of allocated length RelationGetNumberOfAttributes(rel) */
86
87/* parameter structure for AlterTypeRecurse() */
88typedef struct
89{
90 /* Flags indicating which type attributes to update */
98 /* New values for relevant attributes */
99 char storage;
107
108/* Potentially set by pg_upgrade_support functions */
112
113static void makeRangeConstructors(const char *name, Oid namespace,
114 Oid rangeOid, Oid subtype,
116static void makeMultirangeConstructors(const char *name, Oid namespace,
120static Oid findTypeInputFunction(List *procname, Oid typeOid);
121static Oid findTypeOutputFunction(List *procname, Oid typeOid);
122static Oid findTypeReceiveFunction(List *procname, Oid typeOid);
123static Oid findTypeSendFunction(List *procname, Oid typeOid);
124static Oid findTypeTypmodinFunction(List *procname);
125static Oid findTypeTypmodoutFunction(List *procname);
126static Oid findTypeAnalyzeFunction(List *procname, Oid typeOid);
127static Oid findTypeSubscriptingFunction(List *procname, Oid typeOid);
128static Oid findRangeSubOpclass(List *opcname, Oid subtype);
129static Oid findRangeCanonicalFunction(List *procname, Oid typeOid);
130static Oid findRangeSubtypeDiffFunction(List *procname, Oid subtype);
131static void validateDomainCheckConstraint(Oid domainoid, const char *ccbin, LOCKMODE lockmode);
134static void checkEnumOwner(HeapTuple tup);
137 int typMod, Constraint *constr,
138 const char *domainName, ObjectAddress *constrAddr);
140 ColumnRef *cref);
142 int typMod, Constraint *constr,
143 const char *domainName, ObjectAddress *constrAddr);
144static void AlterTypeRecurse(Oid typeOid, bool isImplicitArray,
147
148
149/*
150 * DefineType
151 * Registers a new base type.
152 */
154DefineType(ParseState *pstate, List *names, List *parameters)
155{
156 char *typeName;
158 int16 internalLength = -1; /* default: variable-length */
159 List *inputName = NIL;
162 List *sendName = NIL;
167 char category = TYPCATEGORY_USER;
168 bool preferred = false;
171 char *defaultValue = NULL;
172 bool byValue = false;
173 char alignment = TYPALIGN_INT; /* default alignment */
174 char storage = TYPSTORAGE_PLAIN; /* default TOAST storage method */
175 Oid collation = InvalidOid;
197 Oid receiveOid = InvalidOid;
198 Oid sendOid = InvalidOid;
199 Oid typmodinOid = InvalidOid;
200 Oid typmodoutOid = InvalidOid;
201 Oid analyzeOid = InvalidOid;
202 Oid subscriptOid = InvalidOid;
203 char *array_type;
205 Oid typoid;
206 ListCell *pl;
207 ObjectAddress address;
208
209 /*
210 * As of Postgres 8.4, we require superuser privilege to create a base
211 * type. This is simple paranoia: there are too many ways to mess up the
212 * system with an incorrect type definition (for instance, representation
213 * parameters that don't match what the C code expects). In practice it
214 * takes superuser privilege to create the I/O functions, and so the
215 * former requirement that you own the I/O functions pretty much forced
216 * superuserness anyway. We're just making doubly sure here.
217 *
218 * XXX re-enable NOT_USED code sections below if you remove this test.
219 */
220 if (!superuser())
223 errmsg("must be superuser to create a base type")));
224
225 /* Convert list of names to a name and namespace */
227
228#ifdef NOT_USED
229 /* XXX this is unnecessary given the superuser check above */
230 /* Check we have creation rights in target namespace */
232 if (aclresult != ACLCHECK_OK)
235#endif
236
237 /*
238 * Look to see if type already exists.
239 */
241 CStringGetDatum(typeName),
243
244 /*
245 * If it's not a shell, see if it's an autogenerated array type, and if so
246 * rename it out of the way.
247 */
248 if (OidIsValid(typoid) && get_typisdefined(typoid))
249 {
250 if (moveArrayTypeName(typoid, typeName, typeNamespace))
251 typoid = InvalidOid;
252 else
255 errmsg("type \"%s\" already exists", typeName)));
256 }
257
258 /*
259 * If this command is a parameterless CREATE TYPE, then we're just here to
260 * make a shell type, so do that (or fail if there already is a shell).
261 */
262 if (parameters == NIL)
263 {
264 if (OidIsValid(typoid))
267 errmsg("type \"%s\" already exists", typeName)));
268
269 address = TypeShellMake(typeName, typeNamespace, GetUserId());
270 return address;
271 }
272
273 /*
274 * Otherwise, we must already have a shell type, since there is no other
275 * way that the I/O functions could have been created.
276 */
277 if (!OidIsValid(typoid))
280 errmsg("type \"%s\" does not exist", typeName),
281 errhint("Create the type as a shell type, then create its I/O functions, then do a full CREATE TYPE.")));
282
283 /* Extract the parameters from the parameter list */
284 foreach(pl, parameters)
285 {
286 DefElem *defel = (DefElem *) lfirst(pl);
287 DefElem **defelp;
288
289 if (strcmp(defel->defname, "like") == 0)
291 else if (strcmp(defel->defname, "internallength") == 0)
293 else if (strcmp(defel->defname, "input") == 0)
295 else if (strcmp(defel->defname, "output") == 0)
297 else if (strcmp(defel->defname, "receive") == 0)
299 else if (strcmp(defel->defname, "send") == 0)
301 else if (strcmp(defel->defname, "typmod_in") == 0)
303 else if (strcmp(defel->defname, "typmod_out") == 0)
305 else if (strcmp(defel->defname, "analyze") == 0 ||
306 strcmp(defel->defname, "analyse") == 0)
308 else if (strcmp(defel->defname, "subscript") == 0)
310 else if (strcmp(defel->defname, "category") == 0)
312 else if (strcmp(defel->defname, "preferred") == 0)
314 else if (strcmp(defel->defname, "delimiter") == 0)
316 else if (strcmp(defel->defname, "element") == 0)
318 else if (strcmp(defel->defname, "default") == 0)
320 else if (strcmp(defel->defname, "passedbyvalue") == 0)
321 defelp = &byValueEl;
322 else if (strcmp(defel->defname, "alignment") == 0)
324 else if (strcmp(defel->defname, "storage") == 0)
325 defelp = &storageEl;
326 else if (strcmp(defel->defname, "collatable") == 0)
328 else
329 {
330 /* WARNING, not ERROR, for historical backwards-compatibility */
333 errmsg("type attribute \"%s\" not recognized",
334 defel->defname),
335 parser_errposition(pstate, defel->location)));
336 continue;
337 }
338 if (*defelp != NULL)
340 *defelp = defel;
341 }
342
343 /*
344 * Now interpret the options; we do this separately so that LIKE can be
345 * overridden by other options regardless of the ordering in the parameter
346 * list.
347 */
348 if (likeTypeEl)
349 {
352
355 internalLength = likeForm->typlen;
356 byValue = likeForm->typbyval;
357 alignment = likeForm->typalign;
358 storage = likeForm->typstorage;
360 }
363 if (inputNameEl)
365 if (outputNameEl)
367 if (receiveNameEl)
369 if (sendNameEl)
371 if (typmodinNameEl)
373 if (typmodoutNameEl)
375 if (analyzeNameEl)
377 if (subscriptNameEl)
379 if (categoryEl)
380 {
381 char *p = defGetString(categoryEl);
382
383 category = p[0];
384 /* restrict to non-control ASCII */
388 errmsg("invalid type category \"%s\": must be simple ASCII",
389 p)));
390 }
391 if (preferredEl)
393 if (delimiterEl)
394 {
395 char *p = defGetString(delimiterEl);
396
397 delimiter = p[0];
398 /* XXX shouldn't we restrict the delimiter? */
399 }
400 if (elemTypeEl)
401 {
403 /* disallow arrays of pseudotypes */
407 errmsg("array element type cannot be %s",
409 }
410 if (defaultValueEl)
412 if (byValueEl)
414 if (alignmentEl)
415 {
416 char *a = defGetString(alignmentEl);
417
418 /*
419 * Note: if argument was an unquoted identifier, parser will have
420 * applied translations to it, so be prepared to recognize translated
421 * type names as well as the nominal form.
422 */
423 if (pg_strcasecmp(a, "double") == 0 ||
424 pg_strcasecmp(a, "float8") == 0 ||
425 pg_strcasecmp(a, "pg_catalog.float8") == 0)
427 else if (pg_strcasecmp(a, "int4") == 0 ||
428 pg_strcasecmp(a, "pg_catalog.int4") == 0)
430 else if (pg_strcasecmp(a, "int2") == 0 ||
431 pg_strcasecmp(a, "pg_catalog.int2") == 0)
433 else if (pg_strcasecmp(a, "char") == 0 ||
434 pg_strcasecmp(a, "pg_catalog.bpchar") == 0)
436 else
439 errmsg("alignment \"%s\" not recognized", a)));
440 }
441 if (storageEl)
442 {
443 char *a = defGetString(storageEl);
444
445 if (pg_strcasecmp(a, "plain") == 0)
447 else if (pg_strcasecmp(a, "external") == 0)
449 else if (pg_strcasecmp(a, "extended") == 0)
451 else if (pg_strcasecmp(a, "main") == 0)
453 else
456 errmsg("storage \"%s\" not recognized", a)));
457 }
458 if (collatableEl)
460
461 /*
462 * make sure we have our required definitions
463 */
464 if (inputName == NIL)
467 errmsg("type input function must be specified")));
468 if (outputName == NIL)
471 errmsg("type output function must be specified")));
472
473 if (typmodinName == NIL && typmodoutName != NIL)
476 errmsg("type modifier output function is useless without a type modifier input function")));
477
478 /*
479 * Convert I/O proc names to OIDs
480 */
483 if (receiveName)
484 receiveOid = findTypeReceiveFunction(receiveName, typoid);
485 if (sendName)
486 sendOid = findTypeSendFunction(sendName, typoid);
487
488 /*
489 * Convert typmodin/out function proc names to OIDs.
490 */
491 if (typmodinName)
493 if (typmodoutName)
495
496 /*
497 * Convert analysis function proc name to an OID. If no analysis function
498 * is specified, we'll use zero to select the built-in default algorithm.
499 */
500 if (analyzeName)
501 analyzeOid = findTypeAnalyzeFunction(analyzeName, typoid);
502
503 /*
504 * Likewise look up the subscripting function if any. If it is not
505 * specified, but a typelem is specified, allow that if
506 * raw_array_subscript_handler can be used. (This is for backwards
507 * compatibility; maybe someday we should throw an error instead.)
508 */
509 if (subscriptName)
510 subscriptOid = findTypeSubscriptingFunction(subscriptName, typoid);
511 else if (OidIsValid(elemType))
512 {
513 if (internalLength > 0 && !byValue && get_typlen(elemType) > 0)
514 subscriptOid = F_RAW_ARRAY_SUBSCRIPT_HANDLER;
515 else
518 errmsg("element type cannot be specified without a subscripting function")));
519 }
520
521 /*
522 * Check permissions on functions. We choose to require the creator/owner
523 * of a type to also own the underlying functions. Since creating a type
524 * is tantamount to granting public execute access on the functions, the
525 * minimum sane check would be for execute-with-grant-option. But we
526 * don't have a way to make the type go away if the grant option is
527 * revoked, so ownership seems better.
528 *
529 * XXX For now, this is all unnecessary given the superuser check above.
530 * If we ever relax that, these calls likely should be moved into
531 * findTypeInputFunction et al, where they could be shared by AlterType.
532 */
533#ifdef NOT_USED
540 if (receiveOid && !object_ownercheck(ProcedureRelationId, receiveOid, GetUserId()))
543 if (sendOid && !object_ownercheck(ProcedureRelationId, sendOid, GetUserId()))
546 if (typmodinOid && !object_ownercheck(ProcedureRelationId, typmodinOid, GetUserId()))
549 if (typmodoutOid && !object_ownercheck(ProcedureRelationId, typmodoutOid, GetUserId()))
552 if (analyzeOid && !object_ownercheck(ProcedureRelationId, analyzeOid, GetUserId()))
555 if (subscriptOid && !object_ownercheck(ProcedureRelationId, subscriptOid, GetUserId()))
558#endif
559
560 /*
561 * OK, we're done checking, time to make the type. We must assign the
562 * array type OID ahead of calling TypeCreate, since the base type and
563 * array type each refer to the other.
564 */
566
567 /*
568 * now have TypeCreate do all the real work.
569 *
570 * Note: the pg_type.oid is stored in user tables as array elements (base
571 * types) in ArrayType and in composite types in DatumTupleFields. This
572 * oid must be preserved by binary upgrades.
573 */
574 address =
575 TypeCreate(InvalidOid, /* no predetermined type OID */
576 typeName, /* type name */
577 typeNamespace, /* namespace */
578 InvalidOid, /* relation oid (n/a here) */
579 0, /* relation kind (ditto) */
580 GetUserId(), /* owner's ID */
581 internalLength, /* internal size */
582 TYPTYPE_BASE, /* type-type (base type) */
583 category, /* type-category */
584 preferred, /* is it a preferred type? */
585 delimiter, /* array element delimiter */
586 inputOid, /* input procedure */
587 outputOid, /* output procedure */
588 receiveOid, /* receive procedure */
589 sendOid, /* send procedure */
590 typmodinOid, /* typmodin procedure */
591 typmodoutOid, /* typmodout procedure */
592 analyzeOid, /* analyze procedure */
593 subscriptOid, /* subscript procedure */
594 elemType, /* element type ID */
595 false, /* this is not an implicit array type */
596 array_oid, /* array type we are about to create */
597 InvalidOid, /* base type ID (only for domains) */
598 defaultValue, /* default type value */
599 NULL, /* no binary form available */
600 byValue, /* passed by value */
601 alignment, /* required alignment */
602 storage, /* TOAST strategy */
603 -1, /* typMod (Domains only) */
604 0, /* Array Dimensions of typbasetype */
605 false, /* Type NOT NULL */
606 collation); /* type's collation */
607 Assert(typoid == address.objectId);
608
609 /*
610 * Create the array type that goes with it.
611 */
612 array_type = makeArrayTypeName(typeName, typeNamespace);
613
614 /* alignment must be TYPALIGN_INT or TYPALIGN_DOUBLE for arrays */
616
617 TypeCreate(array_oid, /* force assignment of this type OID */
618 array_type, /* type name */
619 typeNamespace, /* namespace */
620 InvalidOid, /* relation oid (n/a here) */
621 0, /* relation kind (ditto) */
622 GetUserId(), /* owner's ID */
623 -1, /* internal size (always varlena) */
624 TYPTYPE_BASE, /* type-type (base type) */
625 TYPCATEGORY_ARRAY, /* type-category (array) */
626 false, /* array types are never preferred */
627 delimiter, /* array element delimiter */
628 F_ARRAY_IN, /* input procedure */
629 F_ARRAY_OUT, /* output procedure */
630 F_ARRAY_RECV, /* receive procedure */
631 F_ARRAY_SEND, /* send procedure */
632 typmodinOid, /* typmodin procedure */
633 typmodoutOid, /* typmodout procedure */
634 F_ARRAY_TYPANALYZE, /* analyze procedure */
635 F_ARRAY_SUBSCRIPT_HANDLER, /* array subscript procedure */
636 typoid, /* element type ID */
637 true, /* yes this is an array type */
638 InvalidOid, /* no further array type */
639 InvalidOid, /* base type ID */
640 NULL, /* never a default type value */
641 NULL, /* binary default isn't sent either */
642 false, /* never passed by value */
643 alignment, /* see above */
644 TYPSTORAGE_EXTENDED, /* ARRAY is always toastable */
645 -1, /* typMod (Domains only) */
646 0, /* Array dimensions of typbasetype */
647 false, /* Type NOT NULL */
648 collation); /* type's collation */
649
650 pfree(array_type);
651
652 return address;
653}
654
655/*
656 * Guts of type deletion.
657 */
658void
660{
661 Relation relation;
663
665
667 if (!HeapTupleIsValid(tup))
668 elog(ERROR, "cache lookup failed for type %u", typeOid);
669
670 CatalogTupleDelete(relation, &tup->t_self);
671
672 /*
673 * If it is an enum, delete the pg_enum entries too; we don't bother with
674 * making dependency entries for those, so it has to be done "by hand"
675 * here.
676 */
677 if (((Form_pg_type) GETSTRUCT(tup))->typtype == TYPTYPE_ENUM)
678 EnumValuesDelete(typeOid);
679
680 /*
681 * If it is a range type, delete the pg_range entry too; we don't bother
682 * with making a dependency entry for that, so it has to be done "by hand"
683 * here.
684 */
685 if (((Form_pg_type) GETSTRUCT(tup))->typtype == TYPTYPE_RANGE)
686 RangeDelete(typeOid);
687
689
690 table_close(relation, RowExclusiveLock);
691}
692
693
694/*
695 * DefineDomain
696 * Registers a new domain.
697 */
700{
701 char *domainName;
702 char *domainArrayName;
711 bool byValue;
712 char category;
713 char delimiter;
714 char alignment;
715 char storage;
716 char typtype;
717 Datum datum;
718 bool isnull;
719 char *defaultValue = NULL;
720 char *defaultValueBin = NULL;
721 bool saw_default = false;
722 bool typNotNull = false;
723 bool nullDefined = false;
724 int32 typNDims = list_length(stmt->typeName->arrayBounds);
726 List *schema = stmt->constraints;
732 Form_pg_type baseType;
735 ObjectAddress address;
736
737 /* Convert list of names to a name and namespace */
739 &domainName);
740
741 /* Check we have creation rights in target namespace */
743 ACL_CREATE);
744 if (aclresult != ACLCHECK_OK)
747
748 /*
749 * Check for collision with an existing type name. If there is one and
750 * it's an autogenerated array, we can rename it out of the way.
751 */
756 {
760 errmsg("type \"%s\" already exists", domainName)));
761 }
762
763 /*
764 * Look up the base type.
765 */
766 typeTup = typenameType(pstate, stmt->typeName, &basetypeMod);
767 baseType = (Form_pg_type) GETSTRUCT(typeTup);
768 basetypeoid = baseType->oid;
769
770 /*
771 * Base type must be a plain base type, a composite type, another domain,
772 * an enum or a range type. Domains over pseudotypes would create a
773 * security hole. (It would be shorter to code this to just check for
774 * pseudotypes; but it seems safer to call out the specific typtypes that
775 * are supported, rather than assume that all future typtypes would be
776 * automatically supported.)
777 */
778 typtype = baseType->typtype;
779 if (typtype != TYPTYPE_BASE &&
780 typtype != TYPTYPE_COMPOSITE &&
781 typtype != TYPTYPE_DOMAIN &&
782 typtype != TYPTYPE_ENUM &&
783 typtype != TYPTYPE_RANGE &&
784 typtype != TYPTYPE_MULTIRANGE)
787 errmsg("\"%s\" is not a valid base type for a domain",
788 TypeNameToString(stmt->typeName)),
789 parser_errposition(pstate, stmt->typeName->location)));
790
792 if (aclresult != ACLCHECK_OK)
794
795 /*
796 * Collect the properties of the new domain. Some are inherited from the
797 * base type, some are not. If you change any of this inheritance
798 * behavior, be sure to update AlterTypeRecurse() to match!
799 */
800
801 /*
802 * Identify the collation if any
803 */
804 baseColl = baseType->typcollation;
805 if (stmt->collClause)
806 domaincoll = get_collation_oid(stmt->collClause->collname, false);
807 else
809
810 /* Complain if COLLATE is applied to an uncollatable type */
814 errmsg("collations are not supported by type %s",
816 parser_errposition(pstate, stmt->typeName->location)));
817
818 /* passed by value */
819 byValue = baseType->typbyval;
820
821 /* Required Alignment */
822 alignment = baseType->typalign;
823
824 /* TOAST Strategy */
825 storage = baseType->typstorage;
826
827 /* Storage Length */
828 internalLength = baseType->typlen;
829
830 /* Type Category */
831 category = baseType->typcategory;
832
833 /* Array element Delimiter */
834 delimiter = baseType->typdelim;
835
836 /* I/O Functions */
838 outputProcedure = baseType->typoutput;
840 sendProcedure = baseType->typsend;
841
842 /* Domains never accept typmods, so no typmodin/typmodout needed */
843
844 /* Analysis function */
845 analyzeProcedure = baseType->typanalyze;
846
847 /*
848 * Domains don't need a subscript function, since they are not
849 * subscriptable on their own. If the base type is subscriptable, the
850 * parser will reduce the type to the base type before subscripting.
851 */
852
853 /* Inherited default value */
855 Anum_pg_type_typdefault, &isnull);
856 if (!isnull)
858
859 /* Inherited default binary value */
862 if (!isnull)
864
865 /*
866 * Run through constraints manually to avoid the additional processing
867 * conducted by DefineRelation() and friends.
868 */
869 foreach(listptr, schema)
870 {
871 Constraint *constr = lfirst(listptr);
872
873 if (!IsA(constr, Constraint))
874 elog(ERROR, "unrecognized node type: %d",
875 (int) nodeTag(constr));
876 switch (constr->contype)
877 {
878 case CONSTR_DEFAULT:
879
880 /*
881 * The inherited default value may be overridden by the user
882 * with the DEFAULT <expr> clause ... but only once.
883 */
884 if (saw_default)
887 errmsg("multiple default expressions"),
888 parser_errposition(pstate, constr->location));
889 saw_default = true;
890
891 if (constr->raw_expr)
892 {
894
895 /*
896 * Cook the constr->raw_expr into an expression. Note:
897 * name is strictly for error message
898 */
899 defaultExpr = cookDefault(pstate, constr->raw_expr,
903 0);
904
905 /*
906 * If the expression is just a NULL constant, we treat it
907 * like not having a default.
908 *
909 * Note that if the basetype is another domain, we'll see
910 * a CoerceToDomain expr here and not discard the default.
911 * This is critical because the domain default needs to be
912 * retained to override any default that the base domain
913 * might have.
914 */
915 if (defaultExpr == NULL ||
916 (IsA(defaultExpr, Const) &&
918 {
921 }
922 else
923 {
924 /*
925 * Expression must be stored as a nodeToString result,
926 * but we also require a valid textual representation
927 * (mainly to make life easier for pg_dump).
928 */
931 NIL, false, false);
933 }
934 }
935 else
936 {
937 /* No default (can this still happen?) */
940 }
941 break;
942
943 case CONSTR_NOTNULL:
944 if (nullDefined)
945 {
946 if (!typNotNull)
949 errmsg("conflicting NULL/NOT NULL constraints"),
950 parser_errposition(pstate, constr->location));
951
954 errmsg("redundant NOT NULL constraint definition"),
955 parser_errposition(pstate, constr->location));
956 }
957 if (constr->is_no_inherit)
960 errmsg("not-null constraints for domains cannot be marked NO INHERIT"),
961 parser_errposition(pstate, constr->location));
962 typNotNull = true;
963 nullDefined = true;
964 break;
965
966 case CONSTR_NULL:
967 if (nullDefined && typNotNull)
970 errmsg("conflicting NULL/NOT NULL constraints"),
971 parser_errposition(pstate, constr->location));
972 typNotNull = false;
973 nullDefined = true;
974 break;
975
976 case CONSTR_CHECK:
977
978 /*
979 * Check constraints are handled after domain creation, as
980 * they require the Oid of the domain; at this point we can
981 * only check that they're not marked NO INHERIT, because that
982 * would be bogus.
983 */
984 if (constr->is_no_inherit)
987 errmsg("check constraints for domains cannot be marked NO INHERIT"),
988 parser_errposition(pstate, constr->location));
989
990 break;
991
992 /*
993 * All else are error cases
994 */
995 case CONSTR_UNIQUE:
998 errmsg("unique constraints not possible for domains"),
999 parser_errposition(pstate, constr->location));
1000 break;
1001
1002 case CONSTR_PRIMARY:
1003 ereport(ERROR,
1005 errmsg("primary key constraints not possible for domains"),
1006 parser_errposition(pstate, constr->location)));
1007 break;
1008
1009 case CONSTR_EXCLUSION:
1010 ereport(ERROR,
1012 errmsg("exclusion constraints not possible for domains"),
1013 parser_errposition(pstate, constr->location)));
1014 break;
1015
1016 case CONSTR_FOREIGN:
1017 ereport(ERROR,
1019 errmsg("foreign key constraints not possible for domains"),
1020 parser_errposition(pstate, constr->location)));
1021 break;
1022
1027 ereport(ERROR,
1029 errmsg("specifying constraint deferrability not supported for domains"),
1030 parser_errposition(pstate, constr->location)));
1031 break;
1032
1033 case CONSTR_GENERATED:
1034 case CONSTR_IDENTITY:
1035 ereport(ERROR,
1037 errmsg("specifying GENERATED not supported for domains"),
1038 parser_errposition(pstate, constr->location)));
1039 break;
1040
1043 ereport(ERROR,
1045 errmsg("specifying constraint enforceability not supported for domains"),
1046 parser_errposition(pstate, constr->location)));
1047 break;
1048
1049 /* no default, to let compiler warn about missing case */
1050 }
1051 }
1052
1053 /* Allocate OID for array type */
1055
1056 /*
1057 * Have TypeCreate do all the real work.
1058 */
1059 address =
1060 TypeCreate(InvalidOid, /* no predetermined type OID */
1061 domainName, /* type name */
1062 domainNamespace, /* namespace */
1063 InvalidOid, /* relation oid (n/a here) */
1064 0, /* relation kind (ditto) */
1065 GetUserId(), /* owner's ID */
1066 internalLength, /* internal size */
1067 TYPTYPE_DOMAIN, /* type-type (domain type) */
1068 category, /* type-category */
1069 false, /* domain types are never preferred */
1070 delimiter, /* array element delimiter */
1071 inputProcedure, /* input procedure */
1072 outputProcedure, /* output procedure */
1073 receiveProcedure, /* receive procedure */
1074 sendProcedure, /* send procedure */
1075 InvalidOid, /* typmodin procedure - none */
1076 InvalidOid, /* typmodout procedure - none */
1077 analyzeProcedure, /* analyze procedure */
1078 InvalidOid, /* subscript procedure - none */
1079 InvalidOid, /* no array element type */
1080 false, /* this isn't an array */
1081 domainArrayOid, /* array type we are about to create */
1082 basetypeoid, /* base type ID */
1083 defaultValue, /* default type value (text) */
1084 defaultValueBin, /* default type value (binary) */
1085 byValue, /* passed by value */
1086 alignment, /* required alignment */
1087 storage, /* TOAST strategy */
1088 basetypeMod, /* typeMod value */
1089 typNDims, /* Array dimensions for base type */
1090 typNotNull, /* Type NOT NULL */
1091 domaincoll); /* type's collation */
1092
1093 /*
1094 * Create the array type that goes with it.
1095 */
1097
1098 /* alignment must be TYPALIGN_INT or TYPALIGN_DOUBLE for arrays */
1100
1101 TypeCreate(domainArrayOid, /* force assignment of this type OID */
1102 domainArrayName, /* type name */
1103 domainNamespace, /* namespace */
1104 InvalidOid, /* relation oid (n/a here) */
1105 0, /* relation kind (ditto) */
1106 GetUserId(), /* owner's ID */
1107 -1, /* internal size (always varlena) */
1108 TYPTYPE_BASE, /* type-type (base type) */
1109 TYPCATEGORY_ARRAY, /* type-category (array) */
1110 false, /* array types are never preferred */
1111 delimiter, /* array element delimiter */
1112 F_ARRAY_IN, /* input procedure */
1113 F_ARRAY_OUT, /* output procedure */
1114 F_ARRAY_RECV, /* receive procedure */
1115 F_ARRAY_SEND, /* send procedure */
1116 InvalidOid, /* typmodin procedure - none */
1117 InvalidOid, /* typmodout procedure - none */
1118 F_ARRAY_TYPANALYZE, /* analyze procedure */
1119 F_ARRAY_SUBSCRIPT_HANDLER, /* array subscript procedure */
1120 address.objectId, /* element type ID */
1121 true, /* yes this is an array type */
1122 InvalidOid, /* no further array type */
1123 InvalidOid, /* base type ID */
1124 NULL, /* never a default type value */
1125 NULL, /* binary default isn't sent either */
1126 false, /* never passed by value */
1127 alignment, /* see above */
1128 TYPSTORAGE_EXTENDED, /* ARRAY is always toastable */
1129 -1, /* typMod (Domains only) */
1130 0, /* Array dimensions of typbasetype */
1131 false, /* Type NOT NULL */
1132 domaincoll); /* type's collation */
1133
1135
1136 /*
1137 * Process constraints which refer to the domain ID returned by TypeCreate
1138 */
1139 foreach(listptr, schema)
1140 {
1141 Constraint *constr = lfirst(listptr);
1142
1143 /* it must be a Constraint, per check above */
1144
1145 switch (constr->contype)
1146 {
1147 case CONSTR_CHECK:
1150 constr, domainName, NULL);
1151 break;
1152
1153 case CONSTR_NOTNULL:
1156 constr, domainName, NULL);
1157 break;
1158
1159 /* Other constraint types were fully processed above */
1160
1161 default:
1162 break;
1163 }
1164
1165 /* CCI so we can detect duplicate constraint names */
1167 }
1168
1169 /*
1170 * Now we can clean up.
1171 */
1173
1174 return address;
1175}
1176
1177
1178/*
1179 * DefineEnum
1180 * Registers a new enum.
1181 */
1184{
1185 char *enumName;
1186 char *enumArrayName;
1192
1193 /* Convert list of names to a name and namespace */
1195 &enumName);
1196
1197 /* Check we have creation rights in target namespace */
1199 if (aclresult != ACLCHECK_OK)
1202
1203 /*
1204 * Check for collision with an existing type name. If there is one and
1205 * it's an autogenerated array, we can rename it out of the way.
1206 */
1211 {
1213 ereport(ERROR,
1215 errmsg("type \"%s\" already exists", enumName)));
1216 }
1217
1218 /* Allocate OID for array type */
1220
1221 /* Create the pg_type entry */
1222 enumTypeAddr =
1223 TypeCreate(InvalidOid, /* no predetermined type OID */
1224 enumName, /* type name */
1225 enumNamespace, /* namespace */
1226 InvalidOid, /* relation oid (n/a here) */
1227 0, /* relation kind (ditto) */
1228 GetUserId(), /* owner's ID */
1229 sizeof(Oid), /* internal size */
1230 TYPTYPE_ENUM, /* type-type (enum type) */
1231 TYPCATEGORY_ENUM, /* type-category (enum type) */
1232 false, /* enum types are never preferred */
1233 DEFAULT_TYPDELIM, /* array element delimiter */
1234 F_ENUM_IN, /* input procedure */
1235 F_ENUM_OUT, /* output procedure */
1236 F_ENUM_RECV, /* receive procedure */
1237 F_ENUM_SEND, /* send procedure */
1238 InvalidOid, /* typmodin procedure - none */
1239 InvalidOid, /* typmodout procedure - none */
1240 InvalidOid, /* analyze procedure - default */
1241 InvalidOid, /* subscript procedure - none */
1242 InvalidOid, /* element type ID */
1243 false, /* this is not an array type */
1244 enumArrayOid, /* array type we are about to create */
1245 InvalidOid, /* base type ID (only for domains) */
1246 NULL, /* never a default type value */
1247 NULL, /* binary default isn't sent either */
1248 true, /* always passed by value */
1249 TYPALIGN_INT, /* int alignment */
1250 TYPSTORAGE_PLAIN, /* TOAST strategy always plain */
1251 -1, /* typMod (Domains only) */
1252 0, /* Array dimensions of typbasetype */
1253 false, /* Type NOT NULL */
1254 InvalidOid); /* type's collation */
1255
1256 /* Enter the enum's values into pg_enum */
1257 EnumValuesCreate(enumTypeAddr.objectId, stmt->vals);
1258
1259 /*
1260 * Create the array type that goes with it.
1261 */
1263
1264 TypeCreate(enumArrayOid, /* force assignment of this type OID */
1265 enumArrayName, /* type name */
1266 enumNamespace, /* namespace */
1267 InvalidOid, /* relation oid (n/a here) */
1268 0, /* relation kind (ditto) */
1269 GetUserId(), /* owner's ID */
1270 -1, /* internal size (always varlena) */
1271 TYPTYPE_BASE, /* type-type (base type) */
1272 TYPCATEGORY_ARRAY, /* type-category (array) */
1273 false, /* array types are never preferred */
1274 DEFAULT_TYPDELIM, /* array element delimiter */
1275 F_ARRAY_IN, /* input procedure */
1276 F_ARRAY_OUT, /* output procedure */
1277 F_ARRAY_RECV, /* receive procedure */
1278 F_ARRAY_SEND, /* send procedure */
1279 InvalidOid, /* typmodin procedure - none */
1280 InvalidOid, /* typmodout procedure - none */
1281 F_ARRAY_TYPANALYZE, /* analyze procedure */
1282 F_ARRAY_SUBSCRIPT_HANDLER, /* array subscript procedure */
1283 enumTypeAddr.objectId, /* element type ID */
1284 true, /* yes this is an array type */
1285 InvalidOid, /* no further array type */
1286 InvalidOid, /* base type ID */
1287 NULL, /* never a default type value */
1288 NULL, /* binary default isn't sent either */
1289 false, /* never passed by value */
1290 TYPALIGN_INT, /* enums have int align, so do their arrays */
1291 TYPSTORAGE_EXTENDED, /* ARRAY is always toastable */
1292 -1, /* typMod (Domains only) */
1293 0, /* Array dimensions of typbasetype */
1294 false, /* Type NOT NULL */
1295 InvalidOid); /* type's collation */
1296
1298
1299 return enumTypeAddr;
1300}
1301
1302/*
1303 * AlterEnum
1304 * Adds a new label to an existing enum.
1305 */
1308{
1310 TypeName *typename;
1311 HeapTuple tup;
1312 ObjectAddress address;
1313
1314 /* Make a TypeName so we can use standard type lookup machinery */
1315 typename = makeTypeNameFromNameList(stmt->typeName);
1316 enum_type_oid = typenameTypeId(NULL, typename);
1317
1319 if (!HeapTupleIsValid(tup))
1320 elog(ERROR, "cache lookup failed for type %u", enum_type_oid);
1321
1322 /* Check it's an enum and check user has permission to ALTER the enum */
1324
1326
1327 if (stmt->oldVal)
1328 {
1329 /* Rename an existing label */
1330 RenameEnumLabel(enum_type_oid, stmt->oldVal, stmt->newVal);
1331 }
1332 else
1333 {
1334 /* Add a new label */
1336 stmt->newValNeighbor, stmt->newValIsAfter,
1337 stmt->skipIfNewValExists);
1338 }
1339
1341
1343
1344 return address;
1345}
1346
1347
1348/*
1349 * checkEnumOwner
1350 *
1351 * Check that the type is actually an enum and that the current user
1352 * has permission to do ALTER TYPE on it. Throw an error if not.
1353 */
1354static void
1356{
1358
1359 /* Check that this is actually an enum */
1360 if (typTup->typtype != TYPTYPE_ENUM)
1361 ereport(ERROR,
1363 errmsg("%s is not an enum",
1364 format_type_be(typTup->oid))));
1365
1366 /* Permission check: must own type */
1369}
1370
1371
1372/*
1373 * DefineRange
1374 * Registers a new range type.
1375 *
1376 * Perhaps it might be worthwhile to set pg_type.typelem to the base type,
1377 * and likewise on multiranges to set it to the range type. But having a
1378 * non-zero typelem is treated elsewhere as a synonym for being an array,
1379 * and users might have queries with that same assumption.
1380 */
1383{
1384 char *typeName;
1386 Oid typoid;
1387 char *rangeArrayName;
1388 char *multirangeTypeName = NULL;
1389 char *multirangeArrayName;
1404 bool subtypbyval;
1405 char subtypalign;
1406 char alignment;
1408 ListCell *lc;
1409 ObjectAddress address;
1417
1418 /* Convert list of names to a name and namespace */
1420 &typeName);
1421
1422 /* Check we have creation rights in target namespace */
1424 if (aclresult != ACLCHECK_OK)
1427
1428 /*
1429 * Look to see if type already exists.
1430 */
1432 CStringGetDatum(typeName),
1434
1435 /*
1436 * If it's not a shell, see if it's an autogenerated array type, and if so
1437 * rename it out of the way.
1438 */
1439 if (OidIsValid(typoid) && get_typisdefined(typoid))
1440 {
1441 if (moveArrayTypeName(typoid, typeName, typeNamespace))
1442 typoid = InvalidOid;
1443 else
1444 ereport(ERROR,
1446 errmsg("type \"%s\" already exists", typeName)));
1447 }
1448
1449 /*
1450 * Unlike DefineType(), we don't insist on a shell type existing first, as
1451 * it's only needed if the user wants to specify a canonical function.
1452 */
1453
1454 /* Extract the parameters from the parameter list */
1455 foreach(lc, stmt->params)
1456 {
1457 DefElem *defel = (DefElem *) lfirst(lc);
1458
1459 if (strcmp(defel->defname, "subtype") == 0)
1460 {
1463 /* we can look up the subtype name immediately */
1465 }
1466 else if (strcmp(defel->defname, "subtype_opclass") == 0)
1467 {
1468 if (rangeSubOpclassName != NIL)
1471 }
1472 else if (strcmp(defel->defname, "collation") == 0)
1473 {
1474 if (rangeCollationName != NIL)
1477 }
1478 else if (strcmp(defel->defname, "canonical") == 0)
1479 {
1480 if (rangeCanonicalName != NIL)
1483 }
1484 else if (strcmp(defel->defname, "subtype_diff") == 0)
1485 {
1489 }
1490 else if (strcmp(defel->defname, "multirange_type_name") == 0)
1491 {
1492 if (multirangeTypeName != NULL)
1494 /* we can look up the subtype name immediately */
1497 }
1498 else
1499 ereport(ERROR,
1501 errmsg("type attribute \"%s\" not recognized",
1502 defel->defname)));
1503 }
1504
1505 /* Must have a subtype */
1507 ereport(ERROR,
1509 errmsg("type attribute \"subtype\" is required")));
1510 /* disallow ranges of pseudotypes */
1512 ereport(ERROR,
1514 errmsg("range subtype cannot be %s",
1516
1517 /* Identify subopclass */
1519
1520 /* Identify collation to use, if any */
1522 {
1523 if (rangeCollationName != NIL)
1525 else
1527 }
1528 else
1529 {
1530 if (rangeCollationName != NIL)
1531 ereport(ERROR,
1533 errmsg("range collation specified but subtype does not support collation")));
1535 }
1536
1537 /* Identify support functions, if provided */
1538 if (rangeCanonicalName != NIL)
1539 {
1540 if (!OidIsValid(typoid))
1541 ereport(ERROR,
1543 errmsg("cannot specify a canonical function without a pre-created shell type"),
1544 errhint("Create the type as a shell type, then create its canonicalization function, then do a full CREATE TYPE.")));
1546 typoid);
1547 }
1548 else
1550
1553 rangeSubtype);
1554 else
1556
1559
1560 /* alignment must be TYPALIGN_INT or TYPALIGN_DOUBLE for ranges */
1562
1563 /* Allocate OID for array type, its multirange, and its multirange array */
1567
1568 /* Create the pg_type entry */
1569 address =
1570 TypeCreate(InvalidOid, /* no predetermined type OID */
1571 typeName, /* type name */
1572 typeNamespace, /* namespace */
1573 InvalidOid, /* relation oid (n/a here) */
1574 0, /* relation kind (ditto) */
1575 GetUserId(), /* owner's ID */
1576 -1, /* internal size (always varlena) */
1577 TYPTYPE_RANGE, /* type-type (range type) */
1578 TYPCATEGORY_RANGE, /* type-category (range type) */
1579 false, /* range types are never preferred */
1580 DEFAULT_TYPDELIM, /* array element delimiter */
1581 F_RANGE_IN, /* input procedure */
1582 F_RANGE_OUT, /* output procedure */
1583 F_RANGE_RECV, /* receive procedure */
1584 F_RANGE_SEND, /* send procedure */
1585 InvalidOid, /* typmodin procedure - none */
1586 InvalidOid, /* typmodout procedure - none */
1587 F_RANGE_TYPANALYZE, /* analyze procedure */
1588 InvalidOid, /* subscript procedure - none */
1589 InvalidOid, /* element type ID - none */
1590 false, /* this is not an array type */
1591 rangeArrayOid, /* array type we are about to create */
1592 InvalidOid, /* base type ID (only for domains) */
1593 NULL, /* never a default type value */
1594 NULL, /* no binary form available either */
1595 false, /* never passed by value */
1596 alignment, /* alignment */
1597 TYPSTORAGE_EXTENDED, /* TOAST strategy (always extended) */
1598 -1, /* typMod (Domains only) */
1599 0, /* Array dimensions of typbasetype */
1600 false, /* Type NOT NULL */
1601 InvalidOid); /* type's collation (ranges never have one) */
1602 Assert(typoid == InvalidOid || typoid == address.objectId);
1603 typoid = address.objectId;
1604
1605 /* Create the multirange that goes with it */
1607 {
1609
1610 /*
1611 * Look to see if multirange type already exists.
1612 */
1616
1617 /*
1618 * If it's not a shell, see if it's an autogenerated array type, and
1619 * if so rename it out of the way.
1620 */
1622 {
1624 ereport(ERROR,
1626 errmsg("type \"%s\" already exists", multirangeTypeName)));
1627 }
1628 }
1629 else
1630 {
1631 /* Generate multirange name automatically */
1634 }
1635
1637 TypeCreate(multirangeOid, /* force assignment of this type OID */
1638 multirangeTypeName, /* type name */
1639 multirangeNamespace, /* namespace */
1640 InvalidOid, /* relation oid (n/a here) */
1641 0, /* relation kind (ditto) */
1642 GetUserId(), /* owner's ID */
1643 -1, /* internal size (always varlena) */
1644 TYPTYPE_MULTIRANGE, /* type-type (multirange type) */
1645 TYPCATEGORY_RANGE, /* type-category (range type) */
1646 false, /* multirange types are never preferred */
1647 DEFAULT_TYPDELIM, /* array element delimiter */
1648 F_MULTIRANGE_IN, /* input procedure */
1649 F_MULTIRANGE_OUT, /* output procedure */
1650 F_MULTIRANGE_RECV, /* receive procedure */
1651 F_MULTIRANGE_SEND, /* send procedure */
1652 InvalidOid, /* typmodin procedure - none */
1653 InvalidOid, /* typmodout procedure - none */
1654 F_MULTIRANGE_TYPANALYZE, /* analyze procedure */
1655 InvalidOid, /* subscript procedure - none */
1656 InvalidOid, /* element type ID - none */
1657 false, /* this is not an array type */
1658 multirangeArrayOid, /* array type we are about to create */
1659 InvalidOid, /* base type ID (only for domains) */
1660 NULL, /* never a default type value */
1661 NULL, /* no binary form available either */
1662 false, /* never passed by value */
1663 alignment, /* alignment */
1664 'x', /* TOAST strategy (always extended) */
1665 -1, /* typMod (Domains only) */
1666 0, /* Array dimensions of typbasetype */
1667 false, /* Type NOT NULL */
1668 InvalidOid); /* type's collation (ranges never have one) */
1669 Assert(multirangeOid == mltrngaddress.objectId);
1670
1671 /*
1672 * Create the array type that goes with it.
1673 */
1675
1676 TypeCreate(rangeArrayOid, /* force assignment of this type OID */
1677 rangeArrayName, /* type name */
1678 typeNamespace, /* namespace */
1679 InvalidOid, /* relation oid (n/a here) */
1680 0, /* relation kind (ditto) */
1681 GetUserId(), /* owner's ID */
1682 -1, /* internal size (always varlena) */
1683 TYPTYPE_BASE, /* type-type (base type) */
1684 TYPCATEGORY_ARRAY, /* type-category (array) */
1685 false, /* array types are never preferred */
1686 DEFAULT_TYPDELIM, /* array element delimiter */
1687 F_ARRAY_IN, /* input procedure */
1688 F_ARRAY_OUT, /* output procedure */
1689 F_ARRAY_RECV, /* receive procedure */
1690 F_ARRAY_SEND, /* send procedure */
1691 InvalidOid, /* typmodin procedure - none */
1692 InvalidOid, /* typmodout procedure - none */
1693 F_ARRAY_TYPANALYZE, /* analyze procedure */
1694 F_ARRAY_SUBSCRIPT_HANDLER, /* array subscript procedure */
1695 typoid, /* element type ID */
1696 true, /* yes this is an array type */
1697 InvalidOid, /* no further array type */
1698 InvalidOid, /* base type ID */
1699 NULL, /* never a default type value */
1700 NULL, /* binary default isn't sent either */
1701 false, /* never passed by value */
1702 alignment, /* alignment - same as range's */
1703 TYPSTORAGE_EXTENDED, /* ARRAY is always toastable */
1704 -1, /* typMod (Domains only) */
1705 0, /* Array dimensions of typbasetype */
1706 false, /* Type NOT NULL */
1707 InvalidOid); /* typcollation */
1708
1710
1711 /* Create the multirange's array type */
1712
1714
1715 TypeCreate(multirangeArrayOid, /* force assignment of this type OID */
1716 multirangeArrayName, /* type name */
1717 multirangeNamespace, /* namespace */
1718 InvalidOid, /* relation oid (n/a here) */
1719 0, /* relation kind (ditto) */
1720 GetUserId(), /* owner's ID */
1721 -1, /* internal size (always varlena) */
1722 TYPTYPE_BASE, /* type-type (base type) */
1723 TYPCATEGORY_ARRAY, /* type-category (array) */
1724 false, /* array types are never preferred */
1725 DEFAULT_TYPDELIM, /* array element delimiter */
1726 F_ARRAY_IN, /* input procedure */
1727 F_ARRAY_OUT, /* output procedure */
1728 F_ARRAY_RECV, /* receive procedure */
1729 F_ARRAY_SEND, /* send procedure */
1730 InvalidOid, /* typmodin procedure - none */
1731 InvalidOid, /* typmodout procedure - none */
1732 F_ARRAY_TYPANALYZE, /* analyze procedure */
1733 F_ARRAY_SUBSCRIPT_HANDLER, /* array subscript procedure */
1734 multirangeOid, /* element type ID */
1735 true, /* yes this is an array type */
1736 InvalidOid, /* no further array type */
1737 InvalidOid, /* base type ID */
1738 NULL, /* never a default type value */
1739 NULL, /* binary default isn't sent either */
1740 false, /* never passed by value */
1741 alignment, /* alignment - same as range's */
1742 'x', /* ARRAY is always toastable */
1743 -1, /* typMod (Domains only) */
1744 0, /* Array dimensions of typbasetype */
1745 false, /* Type NOT NULL */
1746 InvalidOid); /* typcollation */
1747
1748 /* Ensure these new types are visible to ProcedureCreate */
1750
1751 /* And create the constructor functions for this range type */
1758
1759 /* Create the entry in pg_range */
1764
1765 /* Create cast from the range type to its multirange type */
1769
1771
1772 return address;
1773}
1774
1775/*
1776 * Because there may exist several range types over the same subtype, the
1777 * range type can't be uniquely determined from the subtype. So it's
1778 * impossible to define a polymorphic constructor; we have to generate new
1779 * constructor functions explicitly for each range type.
1780 *
1781 * We actually define 2 functions, with 2 through 3 arguments. This is just
1782 * to offer more convenience for the user.
1783 *
1784 * The OIDs of the created functions are returned through the pointer
1785 * arguments.
1786 */
1787static void
1788makeRangeConstructors(const char *name, Oid namespace,
1789 Oid rangeOid, Oid subtype,
1791{
1792 static const char *const prosrc[2] = {"range_constructor2",
1793 "range_constructor3"};
1794 static const int pronargs[2] = {2, 3};
1795
1798 referenced;
1799 int i;
1800
1801 constructorArgTypes[0] = subtype;
1802 constructorArgTypes[1] = subtype;
1804
1806 referenced.objectId = rangeOid;
1807 referenced.objectSubId = 0;
1808
1809 for (i = 0; i < lengthof(prosrc); i++)
1810 {
1812
1814 pronargs[i]);
1815
1816 myself = ProcedureCreate(name, /* name: same as range type */
1817 namespace, /* namespace */
1818 false, /* replace */
1819 false, /* returns set */
1820 rangeOid, /* return type */
1821 BOOTSTRAP_SUPERUSERID, /* proowner */
1822 INTERNALlanguageId, /* language */
1823 F_FMGR_INTERNAL_VALIDATOR, /* language validator */
1824 prosrc[i], /* prosrc */
1825 NULL, /* probin */
1826 NULL, /* prosqlbody */
1828 false, /* security_definer */
1829 false, /* leakproof */
1830 false, /* isStrict */
1831 PROVOLATILE_IMMUTABLE, /* volatility */
1832 PROPARALLEL_SAFE, /* parallel safety */
1833 constructorArgTypesVector, /* parameterTypes */
1834 PointerGetDatum(NULL), /* allParameterTypes */
1835 PointerGetDatum(NULL), /* parameterModes */
1836 PointerGetDatum(NULL), /* parameterNames */
1837 NIL, /* parameterDefaults */
1838 PointerGetDatum(NULL), /* trftypes */
1839 NIL, /* trfoids */
1840 PointerGetDatum(NULL), /* proconfig */
1841 InvalidOid, /* prosupport */
1842 1.0, /* procost */
1843 0.0); /* prorows */
1844
1845 /*
1846 * Make the constructors internally-dependent on the range type so
1847 * that they go away silently when the type is dropped. Note that
1848 * pg_dump depends on this choice to avoid dumping the constructors.
1849 */
1851
1852 if (pronargs[i] == 2)
1853 *rangeConstruct2_p = myself.objectId;
1854 else if (pronargs[i] == 3)
1855 *rangeConstruct3_p = myself.objectId;
1856 }
1857}
1858
1859/*
1860 * We make a separate multirange constructor for each range type
1861 * so its name can include the base type, like range constructors do.
1862 * If we had an anyrangearray polymorphic type we could use it here,
1863 * but since each type has its own constructor name there's no need.
1864 *
1865 * The OIDs of the created functions are returned through the pointer
1866 * arguments.
1867 */
1868static void
1869makeMultirangeConstructors(const char *name, Oid namespace,
1872{
1874 referenced;
1875 oidvector *argtypes;
1880
1881 referenced.classId = TypeRelationId;
1882 referenced.objectId = multirangeOid;
1883 referenced.objectSubId = 0;
1884
1885 /* 0-arg constructor - for empty multiranges */
1886 argtypes = buildoidvector(NULL, 0);
1887 myself = ProcedureCreate(name, /* name: same as multirange type */
1888 namespace,
1889 false, /* replace */
1890 false, /* returns set */
1891 multirangeOid, /* return type */
1892 BOOTSTRAP_SUPERUSERID, /* proowner */
1893 INTERNALlanguageId, /* language */
1895 "multirange_constructor0", /* prosrc */
1896 NULL, /* probin */
1897 NULL, /* prosqlbody */
1899 false, /* security_definer */
1900 false, /* leakproof */
1901 true, /* isStrict */
1902 PROVOLATILE_IMMUTABLE, /* volatility */
1903 PROPARALLEL_SAFE, /* parallel safety */
1904 argtypes, /* parameterTypes */
1905 PointerGetDatum(NULL), /* allParameterTypes */
1906 PointerGetDatum(NULL), /* parameterModes */
1907 PointerGetDatum(NULL), /* parameterNames */
1908 NIL, /* parameterDefaults */
1909 PointerGetDatum(NULL), /* trftypes */
1910 NIL, /* trfoids */
1911 PointerGetDatum(NULL), /* proconfig */
1912 InvalidOid, /* prosupport */
1913 1.0, /* procost */
1914 0.0); /* prorows */
1915
1916 /*
1917 * Make the constructor internally-dependent on the multirange type so
1918 * that they go away silently when the type is dropped. Note that pg_dump
1919 * depends on this choice to avoid dumping the constructors.
1920 */
1922 *mltrngConstruct0_p = myself.objectId;
1923 pfree(argtypes);
1924
1925 /*
1926 * 1-arg constructor - for casts
1927 *
1928 * In theory we shouldn't need both this and the vararg (n-arg)
1929 * constructor, but having a separate 1-arg function lets us define casts
1930 * against it.
1931 */
1932 argtypes = buildoidvector(&rangeOid, 1);
1933 myself = ProcedureCreate(name, /* name: same as multirange type */
1934 namespace,
1935 false, /* replace */
1936 false, /* returns set */
1937 multirangeOid, /* return type */
1938 BOOTSTRAP_SUPERUSERID, /* proowner */
1939 INTERNALlanguageId, /* language */
1941 "multirange_constructor1", /* prosrc */
1942 NULL, /* probin */
1943 NULL, /* prosqlbody */
1945 false, /* security_definer */
1946 false, /* leakproof */
1947 true, /* isStrict */
1948 PROVOLATILE_IMMUTABLE, /* volatility */
1949 PROPARALLEL_SAFE, /* parallel safety */
1950 argtypes, /* parameterTypes */
1951 PointerGetDatum(NULL), /* allParameterTypes */
1952 PointerGetDatum(NULL), /* parameterModes */
1953 PointerGetDatum(NULL), /* parameterNames */
1954 NIL, /* parameterDefaults */
1955 PointerGetDatum(NULL), /* trftypes */
1956 NIL, /* trfoids */
1957 PointerGetDatum(NULL), /* proconfig */
1958 InvalidOid, /* prosupport */
1959 1.0, /* procost */
1960 0.0); /* prorows */
1961 /* ditto */
1963 *mltrngConstruct1_p = myself.objectId;
1964 pfree(argtypes);
1965
1966 /* n-arg constructor - vararg */
1967 argtypes = buildoidvector(&rangeArrayOid, 1);
1972 myself = ProcedureCreate(name, /* name: same as multirange type */
1973 namespace,
1974 false, /* replace */
1975 false, /* returns set */
1976 multirangeOid, /* return type */
1977 BOOTSTRAP_SUPERUSERID, /* proowner */
1978 INTERNALlanguageId, /* language */
1980 "multirange_constructor2", /* prosrc */
1981 NULL, /* probin */
1982 NULL, /* prosqlbody */
1984 false, /* security_definer */
1985 false, /* leakproof */
1986 true, /* isStrict */
1987 PROVOLATILE_IMMUTABLE, /* volatility */
1988 PROPARALLEL_SAFE, /* parallel safety */
1989 argtypes, /* parameterTypes */
1990 PointerGetDatum(allParameterTypes), /* allParameterTypes */
1991 PointerGetDatum(parameterModes), /* parameterModes */
1992 PointerGetDatum(NULL), /* parameterNames */
1993 NIL, /* parameterDefaults */
1994 PointerGetDatum(NULL), /* trftypes */
1995 NIL, /* trfoids */
1996 PointerGetDatum(NULL), /* proconfig */
1997 InvalidOid, /* prosupport */
1998 1.0, /* procost */
1999 0.0); /* prorows */
2000 /* ditto */
2002 *mltrngConstruct2_p = myself.objectId;
2003 pfree(argtypes);
2006}
2007
2008/*
2009 * Find suitable I/O and other support functions for a type.
2010 *
2011 * typeOid is the type's OID (which will already exist, if only as a shell
2012 * type).
2013 */
2014
2015static Oid
2016findTypeInputFunction(List *procname, Oid typeOid)
2017{
2018 Oid argList[3];
2019 Oid procOid;
2020 Oid procOid2;
2021
2022 /*
2023 * Input functions can take a single argument of type CSTRING, or three
2024 * arguments (string, typioparam OID, typmod). Whine about ambiguity if
2025 * both forms exist.
2026 */
2027 argList[0] = CSTRINGOID;
2028 argList[1] = OIDOID;
2029 argList[2] = INT4OID;
2030
2031 procOid = LookupFuncName(procname, 1, argList, true);
2032 procOid2 = LookupFuncName(procname, 3, argList, true);
2033 if (OidIsValid(procOid))
2034 {
2035 if (OidIsValid(procOid2))
2036 ereport(ERROR,
2038 errmsg("type input function %s has multiple matches",
2039 NameListToString(procname))));
2040 }
2041 else
2042 {
2043 procOid = procOid2;
2044 /* If not found, reference the 1-argument signature in error msg */
2045 if (!OidIsValid(procOid))
2046 ereport(ERROR,
2048 errmsg("function %s does not exist",
2049 func_signature_string(procname, 1, NIL, argList))));
2050 }
2051
2052 /* Input functions must return the target type. */
2053 if (get_func_rettype(procOid) != typeOid)
2054 ereport(ERROR,
2056 errmsg("type input function %s must return type %s",
2057 NameListToString(procname), format_type_be(typeOid))));
2058
2059 /*
2060 * Print warnings if any of the type's I/O functions are marked volatile.
2061 * There is a general assumption that I/O functions are stable or
2062 * immutable; this allows us for example to mark record_in/record_out
2063 * stable rather than volatile. Ideally we would throw errors not just
2064 * warnings here; but since this check is new as of 9.5, and since the
2065 * volatility marking might be just an error-of-omission and not a true
2066 * indication of how the function behaves, we'll let it pass as a warning
2067 * for now.
2068 */
2072 errmsg("type input function %s should not be volatile",
2073 NameListToString(procname))));
2074
2075 return procOid;
2076}
2077
2078static Oid
2080{
2081 Oid argList[1];
2082 Oid procOid;
2083
2084 /*
2085 * Output functions always take a single argument of the type and return
2086 * cstring.
2087 */
2088 argList[0] = typeOid;
2089
2090 procOid = LookupFuncName(procname, 1, argList, true);
2091 if (!OidIsValid(procOid))
2092 ereport(ERROR,
2094 errmsg("function %s does not exist",
2095 func_signature_string(procname, 1, NIL, argList))));
2096
2098 ereport(ERROR,
2100 errmsg("type output function %s must return type %s",
2101 NameListToString(procname), "cstring")));
2102
2103 /* Just a warning for now, per comments in findTypeInputFunction */
2107 errmsg("type output function %s should not be volatile",
2108 NameListToString(procname))));
2109
2110 return procOid;
2111}
2112
2113static Oid
2115{
2116 Oid argList[3];
2117 Oid procOid;
2118 Oid procOid2;
2119
2120 /*
2121 * Receive functions can take a single argument of type INTERNAL, or three
2122 * arguments (internal, typioparam OID, typmod). Whine about ambiguity if
2123 * both forms exist.
2124 */
2125 argList[0] = INTERNALOID;
2126 argList[1] = OIDOID;
2127 argList[2] = INT4OID;
2128
2129 procOid = LookupFuncName(procname, 1, argList, true);
2130 procOid2 = LookupFuncName(procname, 3, argList, true);
2131 if (OidIsValid(procOid))
2132 {
2133 if (OidIsValid(procOid2))
2134 ereport(ERROR,
2136 errmsg("type receive function %s has multiple matches",
2137 NameListToString(procname))));
2138 }
2139 else
2140 {
2141 procOid = procOid2;
2142 /* If not found, reference the 1-argument signature in error msg */
2143 if (!OidIsValid(procOid))
2144 ereport(ERROR,
2146 errmsg("function %s does not exist",
2147 func_signature_string(procname, 1, NIL, argList))));
2148 }
2149
2150 /* Receive functions must return the target type. */
2151 if (get_func_rettype(procOid) != typeOid)
2152 ereport(ERROR,
2154 errmsg("type receive function %s must return type %s",
2155 NameListToString(procname), format_type_be(typeOid))));
2156
2157 /* Just a warning for now, per comments in findTypeInputFunction */
2161 errmsg("type receive function %s should not be volatile",
2162 NameListToString(procname))));
2163
2164 return procOid;
2165}
2166
2167static Oid
2168findTypeSendFunction(List *procname, Oid typeOid)
2169{
2170 Oid argList[1];
2171 Oid procOid;
2172
2173 /*
2174 * Send functions always take a single argument of the type and return
2175 * bytea.
2176 */
2177 argList[0] = typeOid;
2178
2179 procOid = LookupFuncName(procname, 1, argList, true);
2180 if (!OidIsValid(procOid))
2181 ereport(ERROR,
2183 errmsg("function %s does not exist",
2184 func_signature_string(procname, 1, NIL, argList))));
2185
2187 ereport(ERROR,
2189 errmsg("type send function %s must return type %s",
2190 NameListToString(procname), "bytea")));
2191
2192 /* Just a warning for now, per comments in findTypeInputFunction */
2196 errmsg("type send function %s should not be volatile",
2197 NameListToString(procname))));
2198
2199 return procOid;
2200}
2201
2202static Oid
2204{
2205 Oid argList[1];
2206 Oid procOid;
2207
2208 /*
2209 * typmodin functions always take one cstring[] argument and return int4.
2210 */
2212
2213 procOid = LookupFuncName(procname, 1, argList, true);
2214 if (!OidIsValid(procOid))
2215 ereport(ERROR,
2217 errmsg("function %s does not exist",
2218 func_signature_string(procname, 1, NIL, argList))));
2219
2221 ereport(ERROR,
2223 errmsg("typmod_in function %s must return type %s",
2224 NameListToString(procname), "integer")));
2225
2226 /* Just a warning for now, per comments in findTypeInputFunction */
2230 errmsg("type modifier input function %s should not be volatile",
2231 NameListToString(procname))));
2232
2233 return procOid;
2234}
2235
2236static Oid
2238{
2239 Oid argList[1];
2240 Oid procOid;
2241
2242 /*
2243 * typmodout functions always take one int4 argument and return cstring.
2244 */
2245 argList[0] = INT4OID;
2246
2247 procOid = LookupFuncName(procname, 1, argList, true);
2248 if (!OidIsValid(procOid))
2249 ereport(ERROR,
2251 errmsg("function %s does not exist",
2252 func_signature_string(procname, 1, NIL, argList))));
2253
2255 ereport(ERROR,
2257 errmsg("typmod_out function %s must return type %s",
2258 NameListToString(procname), "cstring")));
2259
2260 /* Just a warning for now, per comments in findTypeInputFunction */
2264 errmsg("type modifier output function %s should not be volatile",
2265 NameListToString(procname))));
2266
2267 return procOid;
2268}
2269
2270static Oid
2272{
2273 Oid argList[1];
2274 Oid procOid;
2275
2276 /*
2277 * Analyze functions always take one INTERNAL argument and return bool.
2278 */
2279 argList[0] = INTERNALOID;
2280
2281 procOid = LookupFuncName(procname, 1, argList, true);
2282 if (!OidIsValid(procOid))
2283 ereport(ERROR,
2285 errmsg("function %s does not exist",
2286 func_signature_string(procname, 1, NIL, argList))));
2287
2289 ereport(ERROR,
2291 errmsg("type analyze function %s must return type %s",
2292 NameListToString(procname), "boolean")));
2293
2294 return procOid;
2295}
2296
2297static Oid
2299{
2300 Oid argList[1];
2301 Oid procOid;
2302
2303 /*
2304 * Subscripting support functions always take one INTERNAL argument and
2305 * return INTERNAL. (The argument is not used, but we must have it to
2306 * maintain type safety.)
2307 */
2308 argList[0] = INTERNALOID;
2309
2310 procOid = LookupFuncName(procname, 1, argList, true);
2311 if (!OidIsValid(procOid))
2312 ereport(ERROR,
2314 errmsg("function %s does not exist",
2315 func_signature_string(procname, 1, NIL, argList))));
2316
2318 ereport(ERROR,
2320 errmsg("type subscripting function %s must return type %s",
2321 NameListToString(procname), "internal")));
2322
2323 /*
2324 * We disallow array_subscript_handler() from being selected explicitly,
2325 * since that must only be applied to autogenerated array types.
2326 */
2328 ereport(ERROR,
2330 errmsg("user-defined types cannot use subscripting function %s",
2331 NameListToString(procname))));
2332
2333 return procOid;
2334}
2335
2336/*
2337 * Find suitable support functions and opclasses for a range type.
2338 */
2339
2340/*
2341 * Find named btree opclass for subtype, or default btree opclass if
2342 * opcname is NIL.
2343 */
2344static Oid
2346{
2347 Oid opcid;
2349
2350 if (opcname != NIL)
2351 {
2353
2354 /*
2355 * Verify that the operator class accepts this datatype. Note we will
2356 * accept binary compatibility.
2357 */
2359 if (!IsBinaryCoercible(subtype, opInputType))
2360 ereport(ERROR,
2362 errmsg("operator class \"%s\" does not accept data type %s",
2364 format_type_be(subtype))));
2365 }
2366 else
2367 {
2369 if (!OidIsValid(opcid))
2370 {
2371 /* We spell the error message identically to ResolveOpClass */
2372 ereport(ERROR,
2374 errmsg("data type %s has no default operator class for access method \"%s\"",
2375 format_type_be(subtype), "btree"),
2376 errhint("You must specify an operator class for the range type or define a default operator class for the subtype.")));
2377 }
2378 }
2379
2380 return opcid;
2381}
2382
2383static Oid
2385{
2386 Oid argList[1];
2387 Oid procOid;
2389
2390 /*
2391 * Range canonical functions must take and return the range type, and must
2392 * be immutable.
2393 */
2394 argList[0] = typeOid;
2395
2396 procOid = LookupFuncName(procname, 1, argList, true);
2397
2398 if (!OidIsValid(procOid))
2399 ereport(ERROR,
2401 errmsg("function %s does not exist",
2402 func_signature_string(procname, 1, NIL, argList))));
2403
2404 if (get_func_rettype(procOid) != typeOid)
2405 ereport(ERROR,
2407 errmsg("range canonical function %s must return range type",
2408 func_signature_string(procname, 1, NIL, argList))));
2409
2411 ereport(ERROR,
2413 errmsg("range canonical function %s must be immutable",
2414 func_signature_string(procname, 1, NIL, argList))));
2415
2416 /* Also, range type's creator must have permission to call function */
2418 if (aclresult != ACLCHECK_OK)
2420
2421 return procOid;
2422}
2423
2424static Oid
2426{
2427 Oid argList[2];
2428 Oid procOid;
2430
2431 /*
2432 * Range subtype diff functions must take two arguments of the subtype,
2433 * must return float8, and must be immutable.
2434 */
2435 argList[0] = subtype;
2436 argList[1] = subtype;
2437
2438 procOid = LookupFuncName(procname, 2, argList, true);
2439
2440 if (!OidIsValid(procOid))
2441 ereport(ERROR,
2443 errmsg("function %s does not exist",
2444 func_signature_string(procname, 2, NIL, argList))));
2445
2447 ereport(ERROR,
2449 errmsg("range subtype diff function %s must return type %s",
2450 func_signature_string(procname, 2, NIL, argList),
2451 "double precision")));
2452
2454 ereport(ERROR,
2456 errmsg("range subtype diff function %s must be immutable",
2457 func_signature_string(procname, 2, NIL, argList))));
2458
2459 /* Also, range type's creator must have permission to call function */
2461 if (aclresult != ACLCHECK_OK)
2463
2464 return procOid;
2465}
2466
2467/*
2468 * AssignTypeArrayOid
2469 *
2470 * Pre-assign the type's array OID for use in pg_type.typarray
2471 */
2472Oid
2474{
2476
2477 /* Use binary-upgrade override for pg_type.typarray? */
2478 if (IsBinaryUpgrade)
2479 {
2481 ereport(ERROR,
2483 errmsg("pg_type array OID value not set when in binary upgrade mode")));
2484
2487 }
2488 else
2489 {
2491
2495 }
2496
2497 return type_array_oid;
2498}
2499
2500/*
2501 * AssignTypeMultirangeOid
2502 *
2503 * Pre-assign the range type's multirange OID for use in pg_type.oid
2504 */
2505Oid
2507{
2509
2510 /* Use binary-upgrade override for pg_type.oid? */
2511 if (IsBinaryUpgrade)
2512 {
2514 ereport(ERROR,
2516 errmsg("pg_type multirange OID value not set when in binary upgrade mode")));
2517
2520 }
2521 else
2522 {
2524
2528 }
2529
2530 return type_multirange_oid;
2531}
2532
2533/*
2534 * AssignTypeMultirangeArrayOid
2535 *
2536 * Pre-assign the range type's multirange array OID for use in pg_type.typarray
2537 */
2538Oid
2540{
2542
2543 /* Use binary-upgrade override for pg_type.oid? */
2544 if (IsBinaryUpgrade)
2545 {
2547 ereport(ERROR,
2549 errmsg("pg_type multirange array OID value not set when in binary upgrade mode")));
2550
2553 }
2554 else
2555 {
2557
2561 }
2562
2564}
2565
2566
2567/*-------------------------------------------------------------------
2568 * DefineCompositeType
2569 *
2570 * Create a Composite Type relation.
2571 * `DefineRelation' does all the work, we just provide the correct
2572 * arguments!
2573 *
2574 * If the relation already exists, then 'DefineRelation' will abort
2575 * the xact...
2576 *
2577 * Return type is the new type's object address.
2578 *-------------------------------------------------------------------
2579 */
2581DefineCompositeType(RangeVar *typevar, List *coldeflist)
2582{
2583 CreateStmt *createStmt = makeNode(CreateStmt);
2586 ObjectAddress address;
2587
2588 /*
2589 * now set the parameters for keys/inheritance etc. All of these are
2590 * uninteresting for composite types...
2591 */
2592 createStmt->relation = typevar;
2593 createStmt->tableElts = coldeflist;
2594 createStmt->inhRelations = NIL;
2595 createStmt->constraints = NIL;
2596 createStmt->options = NIL;
2597 createStmt->oncommit = ONCOMMIT_NOOP;
2598 createStmt->tablespacename = NULL;
2599 createStmt->if_not_exists = false;
2600
2601 /*
2602 * Check for collision with an existing type name. If there is one and
2603 * it's an autogenerated array, we can rename it out of the way. This
2604 * check is here mainly to get a better error message about a "type"
2605 * instead of below about a "relation".
2606 */
2608 NoLock, NULL);
2610 old_type_oid =
2612 CStringGetDatum(createStmt->relation->relname),
2615 {
2617 ereport(ERROR,
2619 errmsg("type \"%s\" already exists", createStmt->relation->relname)));
2620 }
2621
2622 /*
2623 * Finally create the relation. This also creates the type.
2624 */
2625 DefineRelation(createStmt, RELKIND_COMPOSITE_TYPE, InvalidOid, &address,
2626 NULL);
2627
2628 return address;
2629}
2630
2631/*
2632 * AlterDomainDefault
2633 *
2634 * Routine implementing ALTER DOMAIN SET/DROP DEFAULT statements.
2635 *
2636 * Returns ObjectAddress of the modified domain.
2637 */
2640{
2641 TypeName *typename;
2642 Oid domainoid;
2643 HeapTuple tup;
2644 ParseState *pstate;
2645 Relation rel;
2646 char *defaultValue;
2647 Node *defaultExpr = NULL; /* NULL if no default specified */
2649 bool new_record_nulls[Natts_pg_type] = {0};
2650 bool new_record_repl[Natts_pg_type] = {0};
2651 HeapTuple newtuple;
2653 ObjectAddress address;
2654
2655 /* Make a TypeName so we can use standard type lookup machinery */
2656 typename = makeTypeNameFromNameList(names);
2657 domainoid = typenameTypeId(NULL, typename);
2658
2659 /* Look up the domain in the type table */
2661
2663 if (!HeapTupleIsValid(tup))
2664 elog(ERROR, "cache lookup failed for type %u", domainoid);
2666
2667 /* Check it's a domain and check user has permission for ALTER DOMAIN */
2669
2670 /* Setup new tuple */
2671
2672 /* Store the new default into the tuple */
2673 if (defaultRaw)
2674 {
2675 /* Create a dummy ParseState for transformExpr */
2676 pstate = make_parsestate(NULL);
2677
2678 /*
2679 * Cook the colDef->raw_expr into an expression. Note: Name is
2680 * strictly for error message
2681 */
2683 typTup->typbasetype,
2684 typTup->typtypmod,
2685 NameStr(typTup->typname),
2686 0);
2687
2688 /*
2689 * If the expression is just a NULL constant, we treat the command
2690 * like ALTER ... DROP DEFAULT. (But see note for same test in
2691 * DefineDomain.)
2692 */
2693 if (defaultExpr == NULL ||
2695 {
2696 /* Default is NULL, drop it */
2697 defaultExpr = NULL;
2702 }
2703 else
2704 {
2705 /*
2706 * Expression must be stored as a nodeToString result, but we also
2707 * require a valid textual representation (mainly to make life
2708 * easier for pg_dump).
2709 */
2711 NIL, false, false);
2712
2713 /*
2714 * Form an updated tuple with the new default and write it back.
2715 */
2717
2721 }
2722 }
2723 else
2724 {
2725 /* ALTER ... DROP DEFAULT */
2730 }
2731
2732 newtuple = heap_modify_tuple(tup, RelationGetDescr(rel),
2735
2736 CatalogTupleUpdate(rel, &tup->t_self, newtuple);
2737
2738 /* Rebuild dependencies */
2739 GenerateTypeDependencies(newtuple,
2740 rel,
2742 NULL, /* don't have typacl handy */
2743 0, /* relation kind is n/a */
2744 false, /* a domain isn't an implicit array */
2745 false, /* nor is it any kind of dependent type */
2746 false, /* don't touch extension membership */
2747 true); /* We do need to rebuild dependencies */
2748
2750
2752
2753 /* Clean up */
2755 heap_freetuple(newtuple);
2756
2757 return address;
2758}
2759
2760/*
2761 * AlterDomainNotNull
2762 *
2763 * Routine implementing ALTER DOMAIN SET/DROP NOT NULL statements.
2764 *
2765 * Returns ObjectAddress of the modified domain.
2766 */
2769{
2770 TypeName *typename;
2771 Oid domainoid;
2773 HeapTuple tup;
2776
2777 /* Make a TypeName so we can use standard type lookup machinery */
2778 typename = makeTypeNameFromNameList(names);
2779 domainoid = typenameTypeId(NULL, typename);
2780
2781 /* Look up the domain in the type table */
2783
2785 if (!HeapTupleIsValid(tup))
2786 elog(ERROR, "cache lookup failed for type %u", domainoid);
2788
2789 /* Check it's a domain and check user has permission for ALTER DOMAIN */
2791
2792 /* Is the domain already set to the desired constraint? */
2793 if (typTup->typnotnull == notNull)
2794 {
2796 return address;
2797 }
2798
2799 if (notNull)
2800 {
2801 Constraint *constr;
2802
2803 constr = makeNode(Constraint);
2804 constr->contype = CONSTR_NOTNULL;
2805 constr->initially_valid = true;
2806 constr->location = -1;
2807
2809 typTup->typbasetype, typTup->typtypmod,
2810 constr, NameStr(typTup->typname), NULL);
2811
2813 }
2814 else
2815 {
2818
2820 if (conTup == NULL)
2821 elog(ERROR, "could not find not-null constraint on domain \"%s\"", NameStr(typTup->typname));
2822
2825 }
2826
2827 /*
2828 * Okay to update pg_type row. We can scribble on typTup because it's a
2829 * copy.
2830 */
2831 typTup->typnotnull = notNull;
2832
2833 CatalogTupleUpdate(typrel, &tup->t_self, tup);
2834
2836
2838
2839 /* Clean up */
2842
2843 return address;
2844}
2845
2846/*
2847 * AlterDomainDropConstraint
2848 *
2849 * Implements the ALTER DOMAIN DROP CONSTRAINT statement
2850 *
2851 * Returns ObjectAddress of the modified domain.
2852 */
2855 DropBehavior behavior, bool missing_ok)
2856{
2857 TypeName *typename;
2858 Oid domainoid;
2859 HeapTuple tup;
2860 Relation rel;
2863 ScanKeyData skey[3];
2865 bool found = false;
2866 ObjectAddress address;
2867
2868 /* Make a TypeName so we can use standard type lookup machinery */
2869 typename = makeTypeNameFromNameList(names);
2870 domainoid = typenameTypeId(NULL, typename);
2871
2872 /* Look up the domain in the type table */
2874
2876 if (!HeapTupleIsValid(tup))
2877 elog(ERROR, "cache lookup failed for type %u", domainoid);
2878
2879 /* Check it's a domain and check user has permission for ALTER DOMAIN */
2881
2882 /* Grab an appropriate lock on the pg_constraint relation */
2884
2885 /* Find and remove the target constraint */
2886 ScanKeyInit(&skey[0],
2890 ScanKeyInit(&skey[1],
2894 ScanKeyInit(&skey[2],
2898
2900 NULL, 3, skey);
2901
2902 /* There can be at most one matching row */
2903 if ((contup = systable_getnext(conscan)) != NULL)
2904 {
2907
2908 if (construct->contype == CONSTRAINT_NOTNULL)
2909 {
2910 ((Form_pg_type) GETSTRUCT(tup))->typnotnull = false;
2911 CatalogTupleUpdate(rel, &tup->t_self, tup);
2912 }
2913
2914 conobj.classId = ConstraintRelationId;
2915 conobj.objectId = construct->oid;
2916 conobj.objectSubId = 0;
2917
2918 performDeletion(&conobj, behavior, 0);
2919 found = true;
2920 }
2921
2922 /* Clean up after the scan */
2925
2926 if (!found)
2927 {
2928 if (!missing_ok)
2929 ereport(ERROR,
2931 errmsg("constraint \"%s\" of domain \"%s\" does not exist",
2932 constrName, TypeNameToString(typename))));
2933 else
2935 (errmsg("constraint \"%s\" of domain \"%s\" does not exist, skipping",
2936 constrName, TypeNameToString(typename))));
2937 }
2938
2939 /*
2940 * We must send out an sinval message for the domain, to ensure that any
2941 * dependent plans get rebuilt. Since this command doesn't change the
2942 * domain's pg_type row, that won't happen automatically; do it manually.
2943 */
2945
2947
2948 /* Clean up */
2950
2951 return address;
2952}
2953
2954/*
2955 * AlterDomainAddConstraint
2956 *
2957 * Implements the ALTER DOMAIN .. ADD CONSTRAINT statement.
2958 */
2962{
2963 TypeName *typename;
2964 Oid domainoid;
2966 HeapTuple tup;
2968 Constraint *constr;
2969 char *ccbin;
2971
2972 /* Make a TypeName so we can use standard type lookup machinery */
2973 typename = makeTypeNameFromNameList(names);
2974 domainoid = typenameTypeId(NULL, typename);
2975
2976 /* Look up the domain in the type table */
2978
2980 if (!HeapTupleIsValid(tup))
2981 elog(ERROR, "cache lookup failed for type %u", domainoid);
2983
2984 /* Check it's a domain and check user has permission for ALTER DOMAIN */
2986
2988 elog(ERROR, "unrecognized node type: %d",
2989 (int) nodeTag(newConstraint));
2990
2991 constr = (Constraint *) newConstraint;
2992
2993 /* enforced by parser */
2994 Assert(constr->contype == CONSTR_CHECK || constr->contype == CONSTR_NOTNULL);
2995
2996 if (constr->contype == CONSTR_CHECK)
2997 {
2998 /*
2999 * First, process the constraint expression and add an entry to
3000 * pg_constraint.
3001 */
3002
3003 ccbin = domainAddCheckConstraint(domainoid, typTup->typnamespace,
3004 typTup->typbasetype, typTup->typtypmod,
3005 constr, NameStr(typTup->typname), constrAddr);
3006
3007
3008 /*
3009 * If requested to validate the constraint, test all values stored in
3010 * the attributes based on the domain the constraint is being added
3011 * to.
3012 */
3013 if (!constr->skip_validation)
3015
3016 /*
3017 * We must send out an sinval message for the domain, to ensure that
3018 * any dependent plans get rebuilt. Since this command doesn't change
3019 * the domain's pg_type row, that won't happen automatically; do it
3020 * manually.
3021 */
3023 }
3024 else if (constr->contype == CONSTR_NOTNULL)
3025 {
3026 /* Is the domain already set NOT NULL? */
3027 if (typTup->typnotnull)
3028 {
3030 return address;
3031 }
3033 typTup->typbasetype, typTup->typtypmod,
3034 constr, NameStr(typTup->typname), constrAddr);
3035
3036 if (!constr->skip_validation)
3038
3039 typTup->typnotnull = true;
3040 CatalogTupleUpdate(typrel, &tup->t_self, tup);
3041 }
3042
3044
3045 /* Clean up */
3047
3048 return address;
3049}
3050
3051/*
3052 * AlterDomainValidateConstraint
3053 *
3054 * Implements the ALTER DOMAIN .. VALIDATE CONSTRAINT statement.
3055 *
3056 * Return value is the address of the validated constraint. If the constraint
3057 * was already validated, InvalidObjectAddress is returned.
3058 */
3061{
3062 TypeName *typename;
3063 Oid domainoid;
3066 HeapTuple tup;
3069 char *conbin;
3070 SysScanDesc scan;
3071 Datum val;
3072 HeapTuple tuple;
3074 ScanKeyData skey[3];
3076
3077 /* Make a TypeName so we can use standard type lookup machinery */
3078 typename = makeTypeNameFromNameList(names);
3079 domainoid = typenameTypeId(NULL, typename);
3080
3081 /* Look up the domain in the type table */
3083
3085 if (!HeapTupleIsValid(tup))
3086 elog(ERROR, "cache lookup failed for type %u", domainoid);
3087
3088 /* Check it's a domain and check user has permission for ALTER DOMAIN */
3090
3091 /*
3092 * Find and check the target constraint
3093 */
3095
3096 ScanKeyInit(&skey[0],
3100 ScanKeyInit(&skey[1],
3104 ScanKeyInit(&skey[2],
3108
3110 NULL, 3, skey);
3111
3112 /* There can be at most one matching row */
3113 if (!HeapTupleIsValid(tuple = systable_getnext(scan)))
3114 ereport(ERROR,
3116 errmsg("constraint \"%s\" of domain \"%s\" does not exist",
3117 constrName, TypeNameToString(typename))));
3118
3119 con = (Form_pg_constraint) GETSTRUCT(tuple);
3120 if (con->contype != CONSTRAINT_CHECK)
3121 ereport(ERROR,
3123 errmsg("constraint \"%s\" of domain \"%s\" is not a check constraint",
3124 constrName, TypeNameToString(typename))));
3125
3126 if (!con->convalidated)
3127 {
3130
3131 /*
3132 * Locking related relations with ShareUpdateExclusiveLock is ok
3133 * because not-yet-valid constraints are still enforced against
3134 * concurrent inserts or updates.
3135 */
3137
3138 /*
3139 * Now update the catalog, while we have the door open.
3140 */
3141 copyTuple = heap_copytuple(tuple);
3143 copy_con->convalidated = true;
3145
3147
3149
3151 }
3152
3153 systable_endscan(scan);
3154
3157
3159
3160 return address;
3161}
3162
3163/*
3164 * Verify that all columns currently using the domain are not null.
3165 */
3166static void
3168{
3169 List *rels;
3170 ListCell *rt;
3171
3172 /* Fetch relation list with attributes based on this domain */
3173 /* ShareLock is sufficient to prevent concurrent data changes */
3174
3176
3177 foreach(rt, rels)
3178 {
3180 Relation testrel = rtc->rel;
3182 TupleTableSlot *slot;
3183 TableScanDesc scan;
3184 Snapshot snapshot;
3185
3186 /* Scan all tuples in this relation */
3187 snapshot = RegisterSnapshot(GetLatestSnapshot());
3188 scan = table_beginscan(testrel, snapshot, 0, NULL);
3190 while (table_scan_getnextslot(scan, ForwardScanDirection, slot))
3191 {
3192 int i;
3193
3194 /* Test attributes that are of the domain */
3195 for (i = 0; i < rtc->natts; i++)
3196 {
3197 int attnum = rtc->atts[i];
3198 Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
3199
3200 if (slot_attisnull(slot, attnum))
3201 {
3202 /*
3203 * In principle the auxiliary information for this error
3204 * should be errdatatype(), but errtablecol() seems
3205 * considerably more useful in practice. Since this code
3206 * only executes in an ALTER DOMAIN command, the client
3207 * should already know which domain is in question.
3208 */
3209 ereport(ERROR,
3211 errmsg("column \"%s\" of table \"%s\" contains null values",
3212 NameStr(attr->attname),
3215 }
3216 }
3217 }
3219 table_endscan(scan);
3220 UnregisterSnapshot(snapshot);
3221
3222 /* Close each rel after processing, but keep lock */
3224 }
3225}
3226
3227/*
3228 * Verify that all columns currently using the domain satisfy the given check
3229 * constraint expression.
3230 *
3231 * It is used to validate existing constraints and to add newly created check
3232 * constraints to a domain.
3233 *
3234 * The lockmode is used for relations using the domain. It should be
3235 * ShareLock when adding a new constraint to domain. It can be
3236 * ShareUpdateExclusiveLock when validating an existing constraint.
3237 */
3238static void
3240{
3241 Expr *expr = (Expr *) stringToNode(ccbin);
3242 List *rels;
3243 ListCell *rt;
3244 EState *estate;
3245 ExprContext *econtext;
3246 ExprState *exprstate;
3247
3248 /* Need an EState to run ExecEvalExpr */
3249 estate = CreateExecutorState();
3250 econtext = GetPerTupleExprContext(estate);
3251
3252 /* build execution state for expr */
3253 exprstate = ExecPrepareExpr(expr, estate);
3254
3255 /* Fetch relation list with attributes based on this domain */
3256 rels = get_rels_with_domain(domainoid, lockmode);
3257
3258 foreach(rt, rels)
3259 {
3261 Relation testrel = rtc->rel;
3263 TupleTableSlot *slot;
3264 TableScanDesc scan;
3265 Snapshot snapshot;
3266
3267 /* Scan all tuples in this relation */
3268 snapshot = RegisterSnapshot(GetLatestSnapshot());
3269 scan = table_beginscan(testrel, snapshot, 0, NULL);
3271 while (table_scan_getnextslot(scan, ForwardScanDirection, slot))
3272 {
3273 int i;
3274
3275 /* Test attributes that are of the domain */
3276 for (i = 0; i < rtc->natts; i++)
3277 {
3278 int attnum = rtc->atts[i];
3279 Datum d;
3280 bool isNull;
3282
3283 d = slot_getattr(slot, attnum, &isNull);
3284
3285 econtext->domainValue_datum = d;
3286 econtext->domainValue_isNull = isNull;
3287
3289 econtext,
3290 &isNull);
3291
3292 if (!isNull && !DatumGetBool(conResult))
3293 {
3294 Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
3295
3296 /*
3297 * In principle the auxiliary information for this error
3298 * should be errdomainconstraint(), but errtablecol()
3299 * seems considerably more useful in practice. Since this
3300 * code only executes in an ALTER DOMAIN command, the
3301 * client should already know which domain is in question,
3302 * and which constraint too.
3303 */
3304 ereport(ERROR,
3306 errmsg("column \"%s\" of table \"%s\" contains values that violate the new constraint",
3307 NameStr(attr->attname),
3310 }
3311 }
3312
3313 ResetExprContext(econtext);
3314 }
3316 table_endscan(scan);
3317 UnregisterSnapshot(snapshot);
3318
3319 /* Hold relation lock till commit (XXX bad for concurrency) */
3321 }
3322
3323 FreeExecutorState(estate);
3324}
3325
3326/*
3327 * get_rels_with_domain
3328 *
3329 * Fetch all relations / attributes which are using the domain
3330 *
3331 * The result is a list of RelToCheck structs, one for each distinct
3332 * relation, each containing one or more attribute numbers that are of
3333 * the domain type. We have opened each rel and acquired the specified lock
3334 * type on it.
3335 *
3336 * We support nested domains by including attributes that are of derived
3337 * domain types. Current callers do not need to distinguish between attributes
3338 * that are of exactly the given domain and those that are of derived domains.
3339 *
3340 * XXX this is completely broken because there is no way to lock the domain
3341 * to prevent columns from being added or dropped while our command runs.
3342 * We can partially protect against column drops by locking relations as we
3343 * come across them, but there is still a race condition (the window between
3344 * seeing a pg_depend entry and acquiring lock on the relation it references).
3345 * Also, holding locks on all these relations simultaneously creates a non-
3346 * trivial risk of deadlock. We can minimize but not eliminate the deadlock
3347 * risk by using the weakest suitable lock (ShareLock for most callers).
3348 *
3349 * XXX the API for this is not sufficient to support checking domain values
3350 * that are inside container types, such as composite types, arrays, or
3351 * ranges. Currently we just error out if a container type containing the
3352 * target domain is stored anywhere.
3353 *
3354 * Generally used for retrieving a list of tests when adding
3355 * new constraints to a domain.
3356 */
3357static List *
3359{
3360 List *result = NIL;
3363 ScanKeyData key[2];
3366
3367 Assert(lockmode != NoLock);
3368
3369 /* since this function recurses, it could be driven to stack overflow */
3371
3372 /*
3373 * We scan pg_depend to find those things that depend on the domain. (We
3374 * assume we can ignore refobjsubid for a domain.)
3375 */
3377
3378 ScanKeyInit(&key[0],
3382 ScanKeyInit(&key[1],
3386
3388 NULL, 2, key);
3389
3391 {
3393 RelToCheck *rtc = NULL;
3396 int ptr;
3397
3398 /* Check for directly dependent types */
3399 if (pg_depend->classid == TypeRelationId)
3400 {
3401 if (get_typtype(pg_depend->objid) == TYPTYPE_DOMAIN)
3402 {
3403 /*
3404 * This is a sub-domain, so recursively add dependent columns
3405 * to the output list. This is a bit inefficient since we may
3406 * fail to combine RelToCheck entries when attributes of the
3407 * same rel have different derived domain types, but it's
3408 * probably not worth improving.
3409 */
3410 result = list_concat(result,
3412 lockmode));
3413 }
3414 else
3415 {
3416 /*
3417 * Otherwise, it is some container type using the domain, so
3418 * fail if there are any columns of this type.
3419 */
3421 NULL,
3423 }
3424 continue;
3425 }
3426
3427 /* Else, ignore dependees that aren't user columns of relations */
3428 /* (we assume system columns are never of domain types) */
3429 if (pg_depend->classid != RelationRelationId ||
3430 pg_depend->objsubid <= 0)
3431 continue;
3432
3433 /* See if we already have an entry for this relation */
3434 foreach(rellist, result)
3435 {
3437
3438 if (RelationGetRelid(rt->rel) == pg_depend->objid)
3439 {
3440 rtc = rt;
3441 break;
3442 }
3443 }
3444
3445 if (rtc == NULL)
3446 {
3447 /* First attribute found for this relation */
3448 Relation rel;
3449
3450 /* Acquire requested lock on relation */
3451 rel = relation_open(pg_depend->objid, lockmode);
3452
3453 /*
3454 * Check to see if rowtype is stored anyplace as a composite-type
3455 * column; if so we have to fail, for now anyway.
3456 */
3457 if (OidIsValid(rel->rd_rel->reltype))
3459 NULL,
3461
3462 /*
3463 * Otherwise, we can ignore relations except those with both
3464 * storage and user-chosen column types.
3465 *
3466 * XXX If an index-only scan could satisfy "col::some_domain" from
3467 * a suitable expression index, this should also check expression
3468 * index columns.
3469 */
3470 if (rel->rd_rel->relkind != RELKIND_RELATION &&
3471 rel->rd_rel->relkind != RELKIND_MATVIEW)
3472 {
3473 relation_close(rel, lockmode);
3474 continue;
3475 }
3476
3477 /* Build the RelToCheck entry with enough space for all atts */
3479 rtc->rel = rel;
3480 rtc->natts = 0;
3482 result = lappend(result, rtc);
3483 }
3484
3485 /*
3486 * Confirm column has not been dropped, and is of the expected type.
3487 * This defends against an ALTER DROP COLUMN occurring just before we
3488 * acquired lock ... but if the whole table were dropped, we'd still
3489 * have a problem.
3490 */
3491 if (pg_depend->objsubid > RelationGetNumberOfAttributes(rtc->rel))
3492 continue;
3493 pg_att = TupleDescAttr(rtc->rel->rd_att, pg_depend->objsubid - 1);
3494 if (pg_att->attisdropped || pg_att->atttypid != domainOid)
3495 continue;
3496
3497 /*
3498 * Okay, add column to result. We store the columns in column-number
3499 * order; this is just a hack to improve predictability of regression
3500 * test output ...
3501 */
3503
3504 ptr = rtc->natts++;
3505 while (ptr > 0 && rtc->atts[ptr - 1] > pg_depend->objsubid)
3506 {
3507 rtc->atts[ptr] = rtc->atts[ptr - 1];
3508 ptr--;
3509 }
3510 rtc->atts[ptr] = pg_depend->objsubid;
3511 }
3512
3514
3516
3517 return result;
3518}
3519
3520/*
3521 * checkDomainOwner
3522 *
3523 * Check that the type is actually a domain and that the current user
3524 * has permission to do ALTER DOMAIN on it. Throw an error if not.
3525 */
3526void
3528{
3530
3531 /* Check that this is actually a domain */
3532 if (typTup->typtype != TYPTYPE_DOMAIN)
3533 ereport(ERROR,
3535 errmsg("%s is not a domain",
3536 format_type_be(typTup->oid))));
3537
3538 /* Permission check: must own type */
3541}
3542
3543/*
3544 * domainAddCheckConstraint - code shared between CREATE and ALTER DOMAIN
3545 */
3546static char *
3548 int typMod, Constraint *constr,
3549 const char *domainName, ObjectAddress *constrAddr)
3550{
3551 Node *expr;
3552 char *ccbin;
3553 ParseState *pstate;
3555 Oid ccoid;
3556
3557 Assert(constr->contype == CONSTR_CHECK);
3558
3559 /*
3560 * Assign or validate constraint name
3561 */
3562 if (constr->conname)
3563 {
3565 domainOid,
3566 constr->conname))
3567 ereport(ERROR,
3569 errmsg("constraint \"%s\" for domain \"%s\" already exists",
3570 constr->conname, domainName)));
3571 }
3572 else
3574 NULL,
3575 "check",
3577 NIL);
3578
3579 /*
3580 * Convert the A_EXPR in raw_expr into an EXPR
3581 */
3582 pstate = make_parsestate(NULL);
3583
3584 /*
3585 * Set up a CoerceToDomainValue to represent the occurrence of VALUE in
3586 * the expression. Note that it will appear to have the type of the base
3587 * type, not the domain. This seems correct since within the check
3588 * expression, we should not assume the input value can be considered a
3589 * member of the domain.
3590 */
3592 domVal->typeId = baseTypeOid;
3593 domVal->typeMod = typMod;
3594 domVal->collation = get_typcollation(baseTypeOid);
3595 domVal->location = -1; /* will be set when/if used */
3596
3598 pstate->p_ref_hook_state = domVal;
3599
3600 expr = transformExpr(pstate, constr->raw_expr, EXPR_KIND_DOMAIN_CHECK);
3601
3602 /*
3603 * Make sure it yields a boolean result.
3604 */
3605 expr = coerce_to_boolean(pstate, expr, "CHECK");
3606
3607 /*
3608 * Fix up collation information.
3609 */
3610 assign_expr_collations(pstate, expr);
3611
3612 /*
3613 * Domains don't allow variables (this is probably dead code now that
3614 * add_missing_from is history, but let's be sure).
3615 */
3616 if (pstate->p_rtable != NIL ||
3617 contain_var_clause(expr))
3618 ereport(ERROR,
3620 errmsg("cannot use table references in domain check constraint")));
3621
3622 /*
3623 * Convert to string form for storage.
3624 */
3625 ccbin = nodeToString(expr);
3626
3627 /*
3628 * Store the constraint in pg_constraint
3629 */
3630 ccoid =
3631 CreateConstraintEntry(constr->conname, /* Constraint Name */
3632 domainNamespace, /* namespace */
3633 CONSTRAINT_CHECK, /* Constraint Type */
3634 false, /* Is Deferrable */
3635 false, /* Is Deferred */
3636 true, /* Is Enforced */
3637 !constr->skip_validation, /* Is Validated */
3638 InvalidOid, /* no parent constraint */
3639 InvalidOid, /* not a relation constraint */
3640 NULL,
3641 0,
3642 0,
3643 domainOid, /* domain constraint */
3644 InvalidOid, /* no associated index */
3645 InvalidOid, /* Foreign key fields */
3646 NULL,
3647 NULL,
3648 NULL,
3649 NULL,
3650 0,
3651 ' ',
3652 ' ',
3653 NULL,
3654 0,
3655 ' ',
3656 NULL, /* not an exclusion constraint */
3657 expr, /* Tree form of check constraint */
3658 ccbin, /* Binary form of check constraint */
3659 true, /* is local */
3660 0, /* inhcount */
3661 false, /* connoinherit */
3662 false, /* conperiod */
3663 false); /* is_internal */
3664 if (constrAddr)
3666
3667 /*
3668 * Return the compiled constraint expression so the calling routine can
3669 * perform any additional required tests.
3670 */
3671 return ccbin;
3672}
3673
3674/* Parser pre_columnref_hook for domain CHECK constraint parsing */
3675static Node *
3677{
3678 /*
3679 * Check for a reference to "value", and if that's what it is, replace
3680 * with a CoerceToDomainValue as prepared for us by
3681 * domainAddCheckConstraint. (We handle VALUE as a name, not a keyword, to
3682 * avoid breaking a lot of applications that have used VALUE as a column
3683 * name in the past.)
3684 */
3685 if (list_length(cref->fields) == 1)
3686 {
3687 Node *field1 = (Node *) linitial(cref->fields);
3688 char *colname;
3689
3690 colname = strVal(field1);
3691 if (strcmp(colname, "value") == 0)
3692 {
3694
3695 /* Propagate location knowledge, if any */
3696 domVal->location = cref->location;
3697 return (Node *) domVal;
3698 }
3699 }
3700 return NULL;
3701}
3702
3703/*
3704 * domainAddNotNullConstraint - code shared between CREATE and ALTER DOMAIN
3705 */
3706static void
3708 int typMod, Constraint *constr,
3709 const char *domainName, ObjectAddress *constrAddr)
3710{
3711 Oid ccoid;
3712
3713 Assert(constr->contype == CONSTR_NOTNULL);
3714
3715 /*
3716 * Assign or validate constraint name
3717 */
3718 if (constr->conname)
3719 {
3721 domainOid,
3722 constr->conname))
3723 ereport(ERROR,
3725 errmsg("constraint \"%s\" for domain \"%s\" already exists",
3726 constr->conname, domainName)));
3727 }
3728 else
3730 NULL,
3731 "not_null",
3733 NIL);
3734
3735 /*
3736 * Store the constraint in pg_constraint
3737 */
3738 ccoid =
3739 CreateConstraintEntry(constr->conname, /* Constraint Name */
3740 domainNamespace, /* namespace */
3741 CONSTRAINT_NOTNULL, /* Constraint Type */
3742 false, /* Is Deferrable */
3743 false, /* Is Deferred */
3744 true, /* Is Enforced */
3745 !constr->skip_validation, /* Is Validated */
3746 InvalidOid, /* no parent constraint */
3747 InvalidOid, /* not a relation constraint */
3748 NULL,
3749 0,
3750 0,
3751 domainOid, /* domain constraint */
3752 InvalidOid, /* no associated index */
3753 InvalidOid, /* Foreign key fields */
3754 NULL,
3755 NULL,
3756 NULL,
3757 NULL,
3758 0,
3759 ' ',
3760 ' ',
3761 NULL,
3762 0,
3763 ' ',
3764 NULL, /* not an exclusion constraint */
3765 NULL,
3766 NULL,
3767 true, /* is local */
3768 0, /* inhcount */
3769 false, /* connoinherit */
3770 false, /* conperiod */
3771 false); /* is_internal */
3772
3773 if (constrAddr)
3775}
3776
3777
3778/*
3779 * Execute ALTER TYPE RENAME
3780 */
3783{
3784 List *names = castNode(List, stmt->object);
3785 const char *newTypeName = stmt->newname;
3786 TypeName *typename;
3787 Oid typeOid;
3788 Relation rel;
3789 HeapTuple tup;
3791 ObjectAddress address;
3792
3793 /* Make a TypeName so we can use standard type lookup machinery */
3794 typename = makeTypeNameFromNameList(names);
3795 typeOid = typenameTypeId(NULL, typename);
3796
3797 /* Look up the type in the type table */
3799
3801 if (!HeapTupleIsValid(tup))
3802 elog(ERROR, "cache lookup failed for type %u", typeOid);
3804
3805 /* check permissions on type */
3806 if (!object_ownercheck(TypeRelationId, typeOid, GetUserId()))
3808
3809 /* ALTER DOMAIN used on a non-domain? */
3810 if (stmt->renameType == OBJECT_DOMAIN && typTup->typtype != TYPTYPE_DOMAIN)
3811 ereport(ERROR,
3813 errmsg("%s is not a domain",
3814 format_type_be(typeOid))));
3815
3816 /*
3817 * If it's a composite type, we need to check that it really is a
3818 * free-standing composite type, and not a table's rowtype. We want people
3819 * to use ALTER TABLE not ALTER TYPE for that case.
3820 */
3821 if (typTup->typtype == TYPTYPE_COMPOSITE &&
3823 ereport(ERROR,
3825 errmsg("%s is a table's row type",
3826 format_type_be(typeOid)),
3827 /* translator: %s is an SQL ALTER command */
3828 errhint("Use %s instead.",
3829 "ALTER TABLE")));
3830
3831 /* don't allow direct alteration of array types, either */
3833 ereport(ERROR,
3835 errmsg("cannot alter array type %s",
3836 format_type_be(typeOid)),
3837 errhint("You can alter type %s, which will alter the array type as well.",
3838 format_type_be(typTup->typelem))));
3839
3840 /* we do allow separate renaming of multirange types, though */
3841
3842 /*
3843 * If type is composite we need to rename associated pg_class entry too.
3844 * RenameRelationInternal will call RenameTypeInternal automatically.
3845 */
3846 if (typTup->typtype == TYPTYPE_COMPOSITE)
3847 RenameRelationInternal(typTup->typrelid, newTypeName, false, false);
3848 else
3850 typTup->typnamespace);
3851
3852 ObjectAddressSet(address, TypeRelationId, typeOid);
3853 /* Clean up */
3855
3856 return address;
3857}
3858
3859/*
3860 * Change the owner of a type.
3861 */
3864{
3865 TypeName *typename;
3866 Oid typeOid;
3867 Relation rel;
3868 HeapTuple tup;
3872 ObjectAddress address;
3873
3875
3876 /* Make a TypeName so we can use standard type lookup machinery */
3877 typename = makeTypeNameFromNameList(names);
3878
3879 /* Use LookupTypeName here so that shell types can be processed */
3880 tup = LookupTypeName(NULL, typename, NULL, false);
3881 if (tup == NULL)
3882 ereport(ERROR,
3884 errmsg("type \"%s\" does not exist",
3885 TypeNameToString(typename))));
3886 typeOid = typeTypeId(tup);
3887
3888 /* Copy the syscache entry so we can scribble on it below */
3891 tup = newtup;
3893
3894 /* Don't allow ALTER DOMAIN on a type */
3895 if (objecttype == OBJECT_DOMAIN && typTup->typtype != TYPTYPE_DOMAIN)
3896 ereport(ERROR,
3898 errmsg("%s is not a domain",
3899 format_type_be(typeOid))));
3900
3901 /*
3902 * If it's a composite type, we need to check that it really is a
3903 * free-standing composite type, and not a table's rowtype. We want people
3904 * to use ALTER TABLE not ALTER TYPE for that case.
3905 */
3906 if (typTup->typtype == TYPTYPE_COMPOSITE &&
3908 ereport(ERROR,
3910 errmsg("%s is a table's row type",
3911 format_type_be(typeOid)),
3912 /* translator: %s is an SQL ALTER command */
3913 errhint("Use %s instead.",
3914 "ALTER TABLE")));
3915
3916 /* don't allow direct alteration of array types, either */
3918 ereport(ERROR,
3920 errmsg("cannot alter array type %s",
3921 format_type_be(typeOid)),
3922 errhint("You can alter type %s, which will alter the array type as well.",
3923 format_type_be(typTup->typelem))));
3924
3925 /* don't allow direct alteration of multirange types, either */
3926 if (typTup->typtype == TYPTYPE_MULTIRANGE)
3927 {
3929
3930 /* We don't expect get_multirange_range to fail, but cope if so */
3931 ereport(ERROR,
3933 errmsg("cannot alter multirange type %s",
3934 format_type_be(typeOid)),
3936 errhint("You can alter type %s, which will alter the multirange type as well.",
3937 format_type_be(rangetype)) : 0));
3938 }
3939
3940 /*
3941 * If the new owner is the same as the existing owner, consider the
3942 * command to have succeeded. This is for dump restoration purposes.
3943 */
3944 if (typTup->typowner != newOwnerId)
3945 {
3946 /* Superusers can always do it */
3947 if (!superuser())
3948 {
3949 /* Otherwise, must be owner of the existing object */
3952
3953 /* Must be able to become new owner */
3955
3956 /* New owner must have CREATE privilege on namespace */
3958 newOwnerId,
3959 ACL_CREATE);
3960 if (aclresult != ACLCHECK_OK)
3962 get_namespace_name(typTup->typnamespace));
3963 }
3964
3965 AlterTypeOwner_oid(typeOid, newOwnerId, true);
3966 }
3967
3968 ObjectAddressSet(address, TypeRelationId, typeOid);
3969
3970 /* Clean up */
3972
3973 return address;
3974}
3975
3976/*
3977 * AlterTypeOwner_oid - change type owner unconditionally
3978 *
3979 * This function recurses to handle dependent types (arrays and multiranges).
3980 * It invokes any necessary access object hooks. If hasDependEntry is true,
3981 * this function modifies the pg_shdepend entry appropriately (this should be
3982 * passed as false only for table rowtypes and dependent types).
3983 *
3984 * This is used by ALTER TABLE/TYPE OWNER commands, as well as by REASSIGN
3985 * OWNED BY. It assumes the caller has done all needed checks.
3986 */
3987void
3989{
3990 Relation rel;
3991 HeapTuple tup;
3993
3995
3997 if (!HeapTupleIsValid(tup))
3998 elog(ERROR, "cache lookup failed for type %u", typeOid);
4000
4001 /*
4002 * If it's a composite type, invoke ATExecChangeOwner so that we fix up
4003 * the pg_class entry properly. That will call back to
4004 * AlterTypeOwnerInternal to take care of the pg_type entry(s).
4005 */
4006 if (typTup->typtype == TYPTYPE_COMPOSITE)
4008 else
4010
4011 /* Update owner dependency reference */
4012 if (hasDependEntry)
4014
4016
4019}
4020
4021/*
4022 * AlterTypeOwnerInternal - bare-bones type owner change.
4023 *
4024 * This routine simply modifies the owner of a pg_type entry, and recurses
4025 * to handle any dependent types.
4026 */
4027void
4029{
4030 Relation rel;
4031 HeapTuple tup;
4036 Acl *newAcl;
4038 bool isNull;
4039
4041
4043 if (!HeapTupleIsValid(tup))
4044 elog(ERROR, "cache lookup failed for type %u", typeOid);
4046
4047 memset(repl_null, false, sizeof(repl_null));
4048 memset(repl_repl, false, sizeof(repl_repl));
4049
4050 repl_repl[Anum_pg_type_typowner - 1] = true;
4052
4055 RelationGetDescr(rel),
4056 &isNull);
4057 /* Null ACLs do not require changes */
4058 if (!isNull)
4059 {
4061 typTup->typowner, newOwnerId);
4062 repl_repl[Anum_pg_type_typacl - 1] = true;
4064 }
4065
4067 repl_repl);
4068
4069 CatalogTupleUpdate(rel, &tup->t_self, tup);
4070
4071 /* If it has an array type, update that too */
4072 if (OidIsValid(typTup->typarray))
4074
4075 /* If it is a range type, update the associated multirange too */
4076 if (typTup->typtype == TYPTYPE_RANGE)
4077 {
4079
4081 ereport(ERROR,
4083 errmsg("could not find multirange type for data type %s",
4084 format_type_be(typeOid))));
4086 }
4087
4088 /* Clean up */
4090}
4091
4092/*
4093 * Execute ALTER TYPE SET SCHEMA
4094 */
4096AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype,
4097 Oid *oldschema)
4098{
4099 TypeName *typename;
4100 Oid typeOid;
4101 Oid nspOid;
4102 Oid oldNspOid;
4105
4106 /* Make a TypeName so we can use standard type lookup machinery */
4107 typename = makeTypeNameFromNameList(names);
4108 typeOid = typenameTypeId(NULL, typename);
4109
4110 /* Don't allow ALTER DOMAIN on a non-domain type */
4111 if (objecttype == OBJECT_DOMAIN && get_typtype(typeOid) != TYPTYPE_DOMAIN)
4112 ereport(ERROR,
4114 errmsg("%s is not a domain",
4115 format_type_be(typeOid))));
4116
4117 /* get schema OID and check its permissions */
4118 nspOid = LookupCreationNamespace(newschema);
4119
4123
4124 if (oldschema)
4126
4128
4129 return myself;
4130}
4131
4132/*
4133 * ALTER TYPE SET SCHEMA, where the caller has already looked up the OIDs
4134 * of the type and the target schema and checked the schema's privileges.
4135 *
4136 * If ignoreDependent is true, we silently ignore dependent types
4137 * (array types and table rowtypes) rather than raising errors.
4138 *
4139 * This entry point is exported for use by AlterObjectNamespace_oid,
4140 * which doesn't want errors when it passes OIDs of dependent types.
4141 *
4142 * Returns the type's old namespace OID, or InvalidOid if we did nothing.
4143 */
4144Oid
4147{
4148 Oid elemOid;
4149
4150 /* check permissions on type */
4151 if (!object_ownercheck(TypeRelationId, typeOid, GetUserId()))
4153
4154 /* don't allow direct alteration of array types */
4155 elemOid = get_element_type(typeOid);
4156 if (OidIsValid(elemOid) && get_array_type(elemOid) == typeOid)
4157 {
4158 if (ignoreDependent)
4159 return InvalidOid;
4160 ereport(ERROR,
4162 errmsg("cannot alter array type %s",
4163 format_type_be(typeOid)),
4164 errhint("You can alter type %s, which will alter the array type as well.",
4166 }
4167
4168 /* and do the work */
4169 return AlterTypeNamespaceInternal(typeOid, nspOid,
4170 false, /* isImplicitArray */
4171 ignoreDependent, /* ignoreDependent */
4172 true, /* errorOnTableType */
4173 objsMoved);
4174}
4175
4176/*
4177 * Move specified type to new namespace.
4178 *
4179 * Caller must have already checked privileges.
4180 *
4181 * The function automatically recurses to process the type's array type,
4182 * if any. isImplicitArray should be true only when doing this internal
4183 * recursion (outside callers must never try to move an array type directly).
4184 *
4185 * If ignoreDependent is true, we silently don't process table types.
4186 *
4187 * If errorOnTableType is true, the function errors out if the type is
4188 * a table type. ALTER TABLE has to be used to move a table to a new
4189 * namespace. (This flag is ignored if ignoreDependent is true.)
4190 *
4191 * We also do nothing if the type is already listed in *objsMoved.
4192 * After a successful move, we add the type to *objsMoved.
4193 *
4194 * Returns the type's old namespace OID, or InvalidOid if we did nothing.
4195 */
4196Oid
4198 bool isImplicitArray,
4199 bool ignoreDependent,
4200 bool errorOnTableType,
4202{
4203 Relation rel;
4204 HeapTuple tup;
4206 Oid oldNspOid;
4207 Oid arrayOid;
4208 bool isCompositeType;
4210
4211 /*
4212 * Make sure we haven't moved this object previously.
4213 */
4215 thisobj.objectId = typeOid;
4216 thisobj.objectSubId = 0;
4217
4219 return InvalidOid;
4220
4222
4224 if (!HeapTupleIsValid(tup))
4225 elog(ERROR, "cache lookup failed for type %u", typeOid);
4227
4228 oldNspOid = typform->typnamespace;
4229 arrayOid = typform->typarray;
4230
4231 /* If the type is already there, we scan skip these next few checks. */
4232 if (oldNspOid != nspOid)
4233 {
4234 /* common checks on switching namespaces */
4236
4237 /* check for duplicate name (more friendly than unique-index failure) */
4239 NameGetDatum(&typform->typname),
4241 ereport(ERROR,
4243 errmsg("type \"%s\" already exists in schema \"%s\"",
4244 NameStr(typform->typname),
4246 }
4247
4248 /* Detect whether type is a composite type (but not a table rowtype) */
4250 (typform->typtype == TYPTYPE_COMPOSITE &&
4252
4253 /* Enforce not-table-type if requested */
4254 if (typform->typtype == TYPTYPE_COMPOSITE && !isCompositeType)
4255 {
4256 if (ignoreDependent)
4257 {
4259 return InvalidOid;
4260 }
4261 if (errorOnTableType)
4262 ereport(ERROR,
4264 errmsg("%s is a table's row type",
4265 format_type_be(typeOid)),
4266 /* translator: %s is an SQL ALTER command */
4267 errhint("Use %s instead.", "ALTER TABLE")));
4268 }
4269
4270 if (oldNspOid != nspOid)
4271 {
4272 /* OK, modify the pg_type row */
4273
4274 /* tup is a copy, so we can scribble directly on it */
4275 typform->typnamespace = nspOid;
4276
4277 CatalogTupleUpdate(rel, &tup->t_self, tup);
4278 }
4279
4280 /*
4281 * Composite types have pg_class entries.
4282 *
4283 * We need to modify the pg_class tuple as well to reflect the change of
4284 * schema.
4285 */
4286 if (isCompositeType)
4287 {
4289
4291
4294 false, objsMoved);
4295
4297
4298 /*
4299 * Check for constraints associated with the composite type (we don't
4300 * currently support this, but probably will someday).
4301 */
4303 nspOid, false, objsMoved);
4304 }
4305 else
4306 {
4307 /* If it's a domain, it might have constraints */
4308 if (typform->typtype == TYPTYPE_DOMAIN)
4310 objsMoved);
4311 }
4312
4313 /*
4314 * Update dependency on schema, if any --- a table rowtype has not got
4315 * one, and neither does an implicit array.
4316 */
4317 if (oldNspOid != nspOid &&
4318 (isCompositeType || typform->typtype != TYPTYPE_COMPOSITE) &&
4322 elog(ERROR, "could not change schema dependency for type \"%s\"",
4323 format_type_be(typeOid));
4324
4326
4328
4330
4332
4333 /* Recursively alter the associated array type, if any */
4334 if (OidIsValid(arrayOid))
4336 true, /* isImplicitArray */
4337 false, /* ignoreDependent */
4338 true, /* errorOnTableType */
4339 objsMoved);
4340
4341 return oldNspOid;
4342}
4343
4344/*
4345 * AlterType
4346 * ALTER TYPE <type> SET (option = ...)
4347 *
4348 * NOTE: the set of changes that can be allowed here is constrained by many
4349 * non-obvious implementation restrictions. Tread carefully when considering
4350 * adding new flexibility.
4351 */
4354{
4355 ObjectAddress address;
4357 TypeName *typename;
4358 HeapTuple tup;
4359 Oid typeOid;
4361 bool requireSuper = false;
4363 ListCell *pl;
4364
4366
4367 /* Make a TypeName so we can use standard type lookup machinery */
4368 typename = makeTypeNameFromNameList(stmt->typeName);
4369 tup = typenameType(NULL, typename, NULL);
4370
4371 typeOid = typeTypeId(tup);
4373
4374 /* Process options */
4375 memset(&atparams, 0, sizeof(atparams));
4376 foreach(pl, stmt->options)
4377 {
4378 DefElem *defel = (DefElem *) lfirst(pl);
4379
4380 if (strcmp(defel->defname, "storage") == 0)
4381 {
4382 char *a = defGetString(defel);
4383
4384 if (pg_strcasecmp(a, "plain") == 0)
4385 atparams.storage = TYPSTORAGE_PLAIN;
4386 else if (pg_strcasecmp(a, "external") == 0)
4387 atparams.storage = TYPSTORAGE_EXTERNAL;
4388 else if (pg_strcasecmp(a, "extended") == 0)
4389 atparams.storage = TYPSTORAGE_EXTENDED;
4390 else if (pg_strcasecmp(a, "main") == 0)
4391 atparams.storage = TYPSTORAGE_MAIN;
4392 else
4393 ereport(ERROR,
4395 errmsg("storage \"%s\" not recognized", a)));
4396
4397 /*
4398 * Validate the storage request. If the type isn't varlena, it
4399 * certainly doesn't support non-PLAIN storage.
4400 */
4401 if (atparams.storage != TYPSTORAGE_PLAIN && typForm->typlen != -1)
4402 ereport(ERROR,
4404 errmsg("fixed-size types must have storage PLAIN")));
4405
4406 /*
4407 * Switching from PLAIN to non-PLAIN is allowed, but it requires
4408 * superuser, since we can't validate that the type's C functions
4409 * will support it. Switching from non-PLAIN to PLAIN is
4410 * disallowed outright, because it's not practical to ensure that
4411 * no tables have toasted values of the type. Switching among
4412 * different non-PLAIN settings is OK, since it just constitutes a
4413 * change in the strategy requested for columns created in the
4414 * future.
4415 */
4416 if (atparams.storage != TYPSTORAGE_PLAIN &&
4417 typForm->typstorage == TYPSTORAGE_PLAIN)
4418 requireSuper = true;
4419 else if (atparams.storage == TYPSTORAGE_PLAIN &&
4420 typForm->typstorage != TYPSTORAGE_PLAIN)
4421 ereport(ERROR,
4423 errmsg("cannot change type's storage to PLAIN")));
4424
4425 atparams.updateStorage = true;
4426 }
4427 else if (strcmp(defel->defname, "receive") == 0)
4428 {
4429 if (defel->arg != NULL)
4430 atparams.receiveOid =
4432 typeOid);
4433 else
4434 atparams.receiveOid = InvalidOid; /* NONE, remove function */
4435 atparams.updateReceive = true;
4436 /* Replacing an I/O function requires superuser. */
4437 requireSuper = true;
4438 }
4439 else if (strcmp(defel->defname, "send") == 0)
4440 {
4441 if (defel->arg != NULL)
4442 atparams.sendOid =
4444 typeOid);
4445 else
4446 atparams.sendOid = InvalidOid; /* NONE, remove function */
4447 atparams.updateSend = true;
4448 /* Replacing an I/O function requires superuser. */
4449 requireSuper = true;
4450 }
4451 else if (strcmp(defel->defname, "typmod_in") == 0)
4452 {
4453 if (defel->arg != NULL)
4454 atparams.typmodinOid =
4456 else
4457 atparams.typmodinOid = InvalidOid; /* NONE, remove function */
4458 atparams.updateTypmodin = true;
4459 /* Replacing an I/O function requires superuser. */
4460 requireSuper = true;
4461 }
4462 else if (strcmp(defel->defname, "typmod_out") == 0)
4463 {
4464 if (defel->arg != NULL)
4465 atparams.typmodoutOid =
4467 else
4468 atparams.typmodoutOid = InvalidOid; /* NONE, remove function */
4469 atparams.updateTypmodout = true;
4470 /* Replacing an I/O function requires superuser. */
4471 requireSuper = true;
4472 }
4473 else if (strcmp(defel->defname, "analyze") == 0)
4474 {
4475 if (defel->arg != NULL)
4476 atparams.analyzeOid =
4478 typeOid);
4479 else
4480 atparams.analyzeOid = InvalidOid; /* NONE, remove function */
4481 atparams.updateAnalyze = true;
4482 /* Replacing an analyze function requires superuser. */
4483 requireSuper = true;
4484 }
4485 else if (strcmp(defel->defname, "subscript") == 0)
4486 {
4487 if (defel->arg != NULL)
4488 atparams.subscriptOid =
4490 typeOid);
4491 else
4492 atparams.subscriptOid = InvalidOid; /* NONE, remove function */
4493 atparams.updateSubscript = true;
4494 /* Replacing a subscript function requires superuser. */
4495 requireSuper = true;
4496 }
4497
4498 /*
4499 * The rest of the options that CREATE accepts cannot be changed.
4500 * Check for them so that we can give a meaningful error message.
4501 */
4502 else if (strcmp(defel->defname, "input") == 0 ||
4503 strcmp(defel->defname, "output") == 0 ||
4504 strcmp(defel->defname, "internallength") == 0 ||
4505 strcmp(defel->defname, "passedbyvalue") == 0 ||
4506 strcmp(defel->defname, "alignment") == 0 ||
4507 strcmp(defel->defname, "like") == 0 ||
4508 strcmp(defel->defname, "category") == 0 ||
4509 strcmp(defel->defname, "preferred") == 0 ||
4510 strcmp(defel->defname, "default") == 0 ||
4511 strcmp(defel->defname, "element") == 0 ||
4512 strcmp(defel->defname, "delimiter") == 0 ||
4513 strcmp(defel->defname, "collatable") == 0)
4514 ereport(ERROR,
4516 errmsg("type attribute \"%s\" cannot be changed",
4517 defel->defname)));
4518 else
4519 ereport(ERROR,
4521 errmsg("type attribute \"%s\" not recognized",
4522 defel->defname)));
4523 }
4524
4525 /*
4526 * Permissions check. Require superuser if we decided the command
4527 * requires that, else must own the type.
4528 */
4529 if (requireSuper)
4530 {
4531 if (!superuser())
4532 ereport(ERROR,
4534 errmsg("must be superuser to alter a type")));
4535 }
4536 else
4537 {
4538 if (!object_ownercheck(TypeRelationId, typeOid, GetUserId()))
4540 }
4541
4542 /*
4543 * We disallow all forms of ALTER TYPE SET on types that aren't plain base
4544 * types. It would for example be highly unsafe, not to mention
4545 * pointless, to change the send/receive functions for a composite type.
4546 * Moreover, pg_dump has no support for changing these properties on
4547 * non-base types. We might weaken this someday, but not now.
4548 *
4549 * Note: if you weaken this enough to allow composite types, be sure to
4550 * adjust the GenerateTypeDependencies call in AlterTypeRecurse.
4551 */
4552 if (typForm->typtype != TYPTYPE_BASE)
4553 ereport(ERROR,
4555 errmsg("%s is not a base type",
4556 format_type_be(typeOid))));
4557
4558 /*
4559 * For the same reasons, don't allow direct alteration of array types.
4560 */
4562 ereport(ERROR,
4564 errmsg("%s is not a base type",
4565 format_type_be(typeOid))));
4566
4567 /* OK, recursively update this type and any arrays/domains over it */
4568 AlterTypeRecurse(typeOid, false, tup, catalog, &atparams);
4569
4570 /* Clean up */
4572
4574
4575 ObjectAddressSet(address, TypeRelationId, typeOid);
4576
4577 return address;
4578}
4579
4580/*
4581 * AlterTypeRecurse: one recursion step for AlterType()
4582 *
4583 * Apply the changes specified by "atparams" to the type identified by
4584 * "typeOid", whose existing pg_type tuple is "tup". If necessary,
4585 * recursively update its array type as well. Then search for any domains
4586 * over this type, and recursively apply (most of) the same changes to those
4587 * domains.
4588 *
4589 * We need this because the system generally assumes that a domain inherits
4590 * many properties from its base type. See DefineDomain() above for details
4591 * of what is inherited. Arrays inherit a smaller number of properties,
4592 * but not none.
4593 *
4594 * There's a race condition here, in that some other transaction could
4595 * concurrently add another domain atop this base type; we'd miss updating
4596 * that one. Hence, be wary of allowing ALTER TYPE to change properties for
4597 * which it'd be really fatal for a domain to be out of sync with its base
4598 * type (typlen, for example). In practice, races seem unlikely to be an
4599 * issue for plausible use-cases for ALTER TYPE. If one does happen, it could
4600 * be fixed by re-doing the same ALTER TYPE once all prior transactions have
4601 * committed.
4602 */
4603static void
4607{
4609 bool nulls[Natts_pg_type];
4610 bool replaces[Natts_pg_type];
4612 SysScanDesc scan;
4613 ScanKeyData key[1];
4615
4616 /* Since this function recurses, it could be driven to stack overflow */
4618
4619 /* Update the current type's tuple */
4620 memset(values, 0, sizeof(values));
4621 memset(nulls, 0, sizeof(nulls));
4622 memset(replaces, 0, sizeof(replaces));
4623
4624 if (atparams->updateStorage)
4625 {
4628 }
4629 if (atparams->updateReceive)
4630 {
4633 }
4634 if (atparams->updateSend)
4635 {
4636 replaces[Anum_pg_type_typsend - 1] = true;
4638 }
4639 if (atparams->updateTypmodin)
4640 {
4641 replaces[Anum_pg_type_typmodin - 1] = true;
4643 }
4644 if (atparams->updateTypmodout)
4645 {
4646 replaces[Anum_pg_type_typmodout - 1] = true;
4648 }
4649 if (atparams->updateAnalyze)
4650 {
4653 }
4654 if (atparams->updateSubscript)
4655 {
4658 }
4659
4661 values, nulls, replaces);
4662
4664
4665 /* Rebuild dependencies for this type */
4667 catalog,
4668 NULL, /* don't have defaultExpr handy */
4669 NULL, /* don't have typacl handy */
4670 0, /* we rejected composite types above */
4671 isImplicitArray, /* it might be an array */
4672 isImplicitArray, /* dependent iff it's array */
4673 false, /* don't touch extension membership */
4674 true);
4675
4677
4678 /*
4679 * Arrays inherit their base type's typmodin and typmodout, but none of
4680 * the other properties we're concerned with here. Recurse to the array
4681 * type if needed.
4682 */
4683 if (!isImplicitArray &&
4684 (atparams->updateTypmodin || atparams->updateTypmodout))
4685 {
4686 Oid arrtypoid = ((Form_pg_type) GETSTRUCT(newtup))->typarray;
4687
4688 if (OidIsValid(arrtypoid))
4689 {
4692
4695 elog(ERROR, "cache lookup failed for type %u", arrtypoid);
4696
4697 memset(&arrparams, 0, sizeof(arrparams));
4698 arrparams.updateTypmodin = atparams->updateTypmodin;
4699 arrparams.updateTypmodout = atparams->updateTypmodout;
4700 arrparams.typmodinOid = atparams->typmodinOid;
4701 arrparams.typmodoutOid = atparams->typmodoutOid;
4702
4704
4706 }
4707 }
4708
4709 /*
4710 * Now we need to recurse to domains. However, some properties are not
4711 * inherited by domains, so clear the update flags for those.
4712 */
4713 atparams->updateReceive = false; /* domains use F_DOMAIN_RECV */
4714 atparams->updateTypmodin = false; /* domains don't have typmods */
4715 atparams->updateTypmodout = false;
4716 atparams->updateSubscript = false; /* domains don't have subscriptors */
4717
4718 /* Skip the scan if nothing remains to be done */
4719 if (!(atparams->updateStorage ||
4720 atparams->updateSend ||
4721 atparams->updateAnalyze))
4722 return;
4723
4724 /* Search pg_type for possible domains over this type */
4725 ScanKeyInit(&key[0],
4728 ObjectIdGetDatum(typeOid));
4729
4730 scan = systable_beginscan(catalog, InvalidOid, false,
4731 NULL, 1, key);
4732
4733 while ((domainTup = systable_getnext(scan)) != NULL)
4734 {
4736
4737 /*
4738 * Shouldn't have a nonzero typbasetype in a non-domain, but let's
4739 * check
4740 */
4741 if (domainForm->typtype != TYPTYPE_DOMAIN)
4742 continue;
4743
4745 }
4746
4747 systable_endscan(scan);
4748}
Acl * aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
Definition acl.c:1120
void check_can_set_role(Oid member, Oid role)
Definition acl.c:5343
AclResult
Definition acl.h:182
@ ACLCHECK_OK
Definition acl.h:183
@ ACLCHECK_NOT_OWNER
Definition acl.h:185
#define DatumGetAclP(X)
Definition acl.h:120
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition aclchk.c:2654
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition aclchk.c:3854
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition aclchk.c:4108
void aclcheck_error_type(AclResult aclerr, Oid typeOid)
Definition aclchk.c:2973
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
static Datum values[MAXATTR]
Definition bootstrap.c:147
#define CStringGetTextDatum(s)
Definition builtins.h:98
#define TextDatumGetCString(d)
Definition builtins.h:99
#define NameStr(name)
Definition c.h:777
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:235
#define Assert(condition)
Definition c.h:885
Oid regproc
Definition c.h:675
int16_t int16
Definition c.h:553
int32_t int32
Definition c.h:554
#define lengthof(array)
Definition c.h:815
#define OidIsValid(objectId)
Definition c.h:800
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition catalog.c:448
TypeName * defGetTypeName(DefElem *def)
Definition define.c:270
int defGetTypeLength(DefElem *def)
Definition define.c:298
char * defGetString(DefElem *def)
Definition define.c:34
bool defGetBoolean(DefElem *def)
Definition define.c:93
List * defGetQualifiedName(DefElem *def)
Definition define.c:238
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition define.c:370
void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags)
Definition dependency.c:274
bool object_address_present(const ObjectAddress *object, const ObjectAddresses *addrs)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
ObjectAddresses * new_object_addresses(void)
void free_object_addresses(ObjectAddresses *addrs)
@ DEPENDENCY_INTERNAL
Definition dependency.h:35
int errcode(int sqlerrcode)
Definition elog.c:874
int errmsg(const char *fmt,...)
Definition elog.c:1093
int errhint(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:36
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define NOTICE
Definition elog.h:35
#define ereport(elevel,...)
Definition elog.h:150
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition execExpr.c:765
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
void FreeExecutorState(EState *estate)
Definition execUtils.c:192
EState * CreateExecutorState(void)
Definition execUtils.c:88
#define GetPerTupleExprContext(estate)
Definition executor.h:656
#define ResetExprContext(econtext)
Definition executor.h:650
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition executor.h:436
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
char * format_type_be(Oid type_oid)
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
bool IsBinaryUpgrade
Definition globals.c:121
Node * cookDefault(ParseState *pstate, Node *raw_default, Oid atttypid, int32 atttypmod, const char *attname, char attgenerated)
Definition heap.c:3323
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1210
HeapTuple heap_copytuple(HeapTuple tuple)
Definition heaptuple.c:778
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
static void * GETSTRUCT(const HeapTupleData *tuple)
#define stmt
#define storage
Oid GetDefaultOpClass(Oid type_id, Oid am_id)
Definition indexcmds.c:2368
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365
long val
Definition informix.c:689
void CacheInvalidateHeapTuple(Relation relation, HeapTuple tuple, HeapTuple newtuple)
Definition inval.c:1571
int a
Definition isn.c:73
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_concat(List *list1, const List *list2)
Definition list.c:561
int LOCKMODE
Definition lockdefs.h:26
#define NoLock
Definition lockdefs.h:34
#define AccessExclusiveLock
Definition lockdefs.h:43
#define AccessShareLock
Definition lockdefs.h:36
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
#define ShareLock
Definition lockdefs.h:40
#define RowExclusiveLock
Definition lockdefs.h:38
Oid get_element_type(Oid typid)
Definition lsyscache.c:2911
Oid get_opclass_input_type(Oid opclass)
Definition lsyscache.c:1314
Oid get_multirange_range(Oid multirangeOid)
Definition lsyscache.c:3635
bool get_typisdefined(Oid typid)
Definition lsyscache.c:2323
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition lsyscache.c:2421
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2153
Oid get_typcollation(Oid typid)
Definition lsyscache.c:3208
char func_volatile(Oid funcid)
Definition lsyscache.c:1930
char * get_func_name(Oid funcid)
Definition lsyscache.c:1758
Oid get_range_multirange(Oid rangeOid)
Definition lsyscache.c:3610
bool type_is_collatable(Oid typid)
Definition lsyscache.c:3233
int16 get_typlen(Oid typid)
Definition lsyscache.c:2347
char get_typtype(Oid typid)
Definition lsyscache.c:2781
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3518
Oid get_array_type(Oid typid)
Definition lsyscache.c:2939
Oid get_func_rettype(Oid funcid)
Definition lsyscache.c:1805
TypeName * makeTypeNameFromNameList(List *names)
Definition makefuncs.c:531
void pfree(void *pointer)
Definition mcxt.c:1616
Oid GetUserId(void)
Definition miscinit.c:469
Oid RangeVarGetAndCheckCreationNamespace(RangeVar *relation, LOCKMODE lockmode, Oid *existing_relation_id)
Definition namespace.c:740
char * NameListToString(const List *names)
Definition namespace.c:3666
Oid QualifiedNameGetCreationNamespace(const List *names, char **objname_p)
Definition namespace.c:3559
Oid LookupCreationNamespace(const char *nspname)
Definition namespace.c:3500
void RangeVarAdjustRelationPersistence(RangeVar *newRelation, Oid nspid)
Definition namespace.c:847
Oid get_collation_oid(List *collname, bool missing_ok)
Definition namespace.c:4043
void CheckSetNamespace(Oid oldNspOid, Oid nspOid)
Definition namespace.c:3531
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
#define nodeTag(nodeptr)
Definition nodes.h:139
#define makeNode(_type_)
Definition nodes.h:161
#define castNode(_type_, nodeptr)
Definition nodes.h:182
#define InvokeObjectPostAlterHook(classId, objectId, subId)
const ObjectAddress InvalidObjectAddress
#define ObjectAddressSet(addr, class_id, object_id)
oidvector * buildoidvector(const Oid *oids, int n)
Definition oid.c:87
Oid get_opclass_oid(Oid amID, List *opclassname, bool missing_ok)
char * nodeToString(const void *obj)
Definition outfuncs.c:802
bool IsBinaryCoercible(Oid srctype, Oid targettype)
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
void assign_expr_collations(ParseState *pstate, Node *expr)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition parse_expr.c:120
const char * func_signature_string(List *funcname, int nargs, List *argnames, const Oid *argtypes)
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool missing_ok)
static bool isCompositeType(Oid typid)
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
ParseState * make_parsestate(ParseState *parentParseState)
Definition parse_node.c:39
@ EXPR_KIND_DOMAIN_CHECK
Definition parse_node.h:69
char * TypeNameToString(const TypeName *typeName)
Definition parse_type.c:478
Type LookupTypeName(ParseState *pstate, const TypeName *typeName, int32 *typmod_p, bool missing_ok)
Definition parse_type.c:38
Type typenameType(ParseState *pstate, const TypeName *typeName, int32 *typmod_p)
Definition parse_type.c:264
Oid typeTypeId(Type tp)
Definition parse_type.c:590
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition parse_type.c:291
#define ACL_USAGE
Definition parsenodes.h:84
@ FUNC_PARAM_VARIADIC
@ CONSTR_ATTR_ENFORCED
@ CONSTR_FOREIGN
@ CONSTR_ATTR_DEFERRED
@ CONSTR_IDENTITY
@ CONSTR_UNIQUE
@ CONSTR_ATTR_NOT_DEFERRABLE
@ CONSTR_DEFAULT
@ CONSTR_NOTNULL
@ CONSTR_ATTR_IMMEDIATE
@ CONSTR_CHECK
@ CONSTR_NULL
@ CONSTR_GENERATED
@ CONSTR_EXCLUSION
@ CONSTR_ATTR_DEFERRABLE
@ CONSTR_ATTR_NOT_ENFORCED
@ CONSTR_PRIMARY
DropBehavior
@ DROP_RESTRICT
ObjectType
@ OBJECT_SCHEMA
@ OBJECT_DOMAIN
@ OBJECT_FUNCTION
#define ACL_EXECUTE
Definition parsenodes.h:83
#define ACL_CREATE
Definition parsenodes.h:85
int16 attnum
FormData_pg_attribute * Form_pg_attribute
ObjectAddress CastCreate(Oid sourcetypeid, Oid targettypeid, Oid funcid, Oid incastid, Oid outcastid, char castcontext, char castmethod, DependencyType behavior)
Definition pg_cast.c:49
Oid CreateConstraintEntry(const char *constraintName, Oid constraintNamespace, char constraintType, bool isDeferrable, bool isDeferred, bool isEnforced, bool isValidated, Oid parentConstrId, Oid relId, const int16 *constraintKey, int constraintNKeys, int constraintNTotalKeys, Oid domainId, Oid indexRelId, Oid foreignRelId, const int16 *foreignKey, const Oid *pfEqOp, const Oid *ppEqOp, const Oid *ffEqOp, int foreignNKeys, char foreignUpdateType, char foreignDeleteType, const int16 *fkDeleteSetCols, int numFkDeleteSetCols, char foreignMatchType, const Oid *exclOp, Node *conExpr, const char *conBin, bool conIsLocal, int16 conInhCount, bool conNoInherit, bool conPeriod, bool is_internal)
bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, const char *conname)
HeapTuple findDomainNotNullConstraint(Oid typid)
void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, Oid newNspId, bool isType, ObjectAddresses *objsMoved)
char * ChooseConstraintName(const char *name1, const char *name2, const char *label, Oid namespaceid, List *others)
@ CONSTRAINT_DOMAIN
END_CATALOG_STRUCT typedef FormData_pg_constraint * Form_pg_constraint
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition pg_depend.c:47
long changeDependencyFor(Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition pg_depend.c:459
END_CATALOG_STRUCT typedef FormData_pg_depend * Form_pg_depend
Definition pg_depend.h:76
void RenameEnumLabel(Oid enumTypeOid, const char *oldVal, const char *newVal)
Definition pg_enum.c:620
void EnumValuesDelete(Oid enumTypeOid)
Definition pg_enum.c:237
void AddEnumLabel(Oid enumTypeOid, const char *newVal, const char *neighbor, bool newValIsAfter, bool skipIfExists)
Definition pg_enum.c:305
void EnumValuesCreate(Oid enumTypeOid, List *vals)
Definition pg_enum.c:84
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define linitial(l)
Definition pg_list.h:178
ObjectAddress ProcedureCreate(const char *procedureName, Oid procNamespace, bool replace, bool returnsSet, Oid returnType, Oid proowner, Oid languageObjectId, Oid languageValidator, const char *prosrc, const char *probin, Node *prosqlbody, char prokind, bool security_definer, bool isLeakProof, bool isStrict, char volatility, char parallel, oidvector *parameterTypes, Datum allParameterTypes, Datum parameterModes, Datum parameterNames, List *parameterDefaults, Datum trftypes, List *trfoids, Datum proconfig, Oid prosupport, float4 procost, float4 prorows)
Definition pg_proc.c:99
int16 pronargs
Definition pg_proc.h:83
void RangeCreate(Oid rangeTypeOid, Oid rangeSubType, Oid rangeCollation, Oid rangeSubOpclass, RegProcedure rangeCanonical, RegProcedure rangeSubDiff, Oid multirangeTypeOid, RegProcedure rangeConstruct2, RegProcedure rangeConstruct3, RegProcedure mltrngConstruct0, RegProcedure mltrngConstruct1, RegProcedure mltrngConstruct2)
Definition pg_range.c:36
void RangeDelete(Oid rangeTypeOid)
Definition pg_range.c:120
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
void GenerateTypeDependencies(HeapTuple typeTuple, Relation typeCatalog, Node *defaultExpr, void *typacl, char relationKind, bool isImplicitArray, bool isDependentType, bool makeExtensionDep, bool rebuild)
Definition pg_type.c:555
void RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace)
Definition pg_type.c:763
ObjectAddress TypeCreate(Oid newTypeOid, const char *typeName, Oid typeNamespace, Oid relationOid, char relationKind, Oid ownerId, int16 internalSize, char typeType, char typeCategory, bool typePreferred, char typDelim, Oid inputProcedure, Oid outputProcedure, Oid receiveProcedure, Oid sendProcedure, Oid typmodinProcedure, Oid typmodoutProcedure, Oid analyzeProcedure, Oid subscriptProcedure, Oid elementType, bool isImplicitArray, Oid arrayType, Oid baseType, const char *defaultTypeValue, char *defaultTypeBin, bool passedByValue, char alignment, char storage, int32 typeMod, int32 typNDims, bool typeNotNull, Oid typeCollation)
Definition pg_type.c:195
bool moveArrayTypeName(Oid typeOid, const char *typeName, Oid typeNamespace)
Definition pg_type.c:903
ObjectAddress TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
Definition pg_type.c:57
char * makeMultirangeTypeName(const char *rangeTypeName, Oid typeNamespace)
Definition pg_type.c:948
char * makeArrayTypeName(const char *typeName, Oid typeNamespace)
Definition pg_type.c:838
END_CATALOG_STRUCT typedef FormData_pg_type * Form_pg_type
Definition pg_type.h:265
int pg_strcasecmp(const char *s1, const char *s2)
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
static Datum NameGetDatum(const NameData *X)
Definition postgres.h:403
uint64_t Datum
Definition postgres.h:70
static Datum CStringGetDatum(const char *X)
Definition postgres.h:380
static Datum CharGetDatum(char X)
Definition postgres.h:132
#define InvalidOid
unsigned int Oid
static int fb(int x)
@ ONCOMMIT_NOOP
Definition primnodes.h:59
void * stringToNode(const char *str)
Definition read.c:90
#define RelationGetRelid(relation)
Definition rel.h:514
#define RelationGetDescr(relation)
Definition rel.h:540
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:520
#define RelationGetRelationName(relation)
Definition rel.h:548
int errtablecol(Relation rel, int attnum)
Definition relcache.c:6061
char * deparse_expression(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit)
Definition ruleutils.c:3649
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
@ ForwardScanDirection
Definition sdir.h:28
Snapshot GetLatestSnapshot(void)
Definition snapmgr.c:354
void UnregisterSnapshot(Snapshot snapshot)
Definition snapmgr.c:866
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition snapmgr.c:824
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:47
void check_stack_depth(void)
Definition stack_depth.c:95
#define BTEqualStrategyNumber
Definition stratnum.h:31
#define ERRCODE_DUPLICATE_OBJECT
Definition streamutil.c:30
ParseLoc location
ConstrType contype
bool is_no_inherit
bool initially_valid
bool skip_validation
Node * raw_expr
char * conname
List * tableElts
OnCommitAction oncommit
List * options
bool if_not_exists
List * inhRelations
RangeVar * relation
char * tablespacename
List * constraints
Datum domainValue_datum
Definition execnodes.h:306
bool domainValue_isNull
Definition execnodes.h:308
Definition pg_list.h:54
Definition nodes.h:135
void * p_ref_hook_state
Definition parse_node.h:237
PreParseColumnRefHook p_pre_columnref_hook
Definition parse_node.h:233
List * p_rtable
Definition parse_node.h:192
char * relname
Definition primnodes.h:84
int * atts
Definition typecmds.c:83
Relation rel
Definition typecmds.c:81
int natts
Definition typecmds.c:82
Form_pg_class rd_rel
Definition rel.h:111
Definition c.h:757
bool superuser(void)
Definition superuser.c:47
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
Datum SysCacheGetAttrNotNull(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition syscache.c:625
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:220
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:595
#define SearchSysCacheCopy1(cacheId, key1)
Definition syscache.h:91
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition syscache.h:102
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition syscache.h:111
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition tableam.c:92
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1004
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition tableam.h:1039
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, ScanKeyData *key)
Definition tableam.h:896
void AlterRelationNamespaceInternal(Relation classRel, Oid relOid, Oid oldNspOid, Oid newNspOid, bool hasDependEntry, ObjectAddresses *objsMoved)
void ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing, LOCKMODE lockmode)
void find_composite_type_dependencies(Oid typeOid, Relation origRelation, const char *origTypeName)
Definition tablecmds.c:6975
ObjectAddress DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, ObjectAddress *typaddress, const char *queryString)
Definition tablecmds.c:769
void RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal, bool is_index)
Definition tablecmds.c:4278
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:160
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition tuptable.h:398
static bool slot_attisnull(TupleTableSlot *slot, int attnum)
Definition tuptable.h:384
ObjectAddress AlterDomainNotNull(List *names, bool notNull)
Definition typecmds.c:2768
static Oid findTypeReceiveFunction(List *procname, Oid typeOid)
Definition typecmds.c:2114
Oid AssignTypeMultirangeOid(void)
Definition typecmds.c:2506
Oid binary_upgrade_next_mrng_array_pg_type_oid
Definition typecmds.c:111
static void AlterTypeRecurse(Oid typeOid, bool isImplicitArray, HeapTuple tup, Relation catalog, AlterTypeRecurseParams *atparams)
Definition typecmds.c:4604
static void checkEnumOwner(HeapTuple tup)
Definition typecmds.c:1355
static Oid findTypeAnalyzeFunction(List *procname, Oid typeOid)
Definition typecmds.c:2271
static Oid findRangeSubOpclass(List *opcname, Oid subtype)
Definition typecmds.c:2345
Oid AlterTypeNamespace_oid(Oid typeOid, Oid nspOid, bool ignoreDependent, ObjectAddresses *objsMoved)
Definition typecmds.c:4145
Oid binary_upgrade_next_mrng_pg_type_oid
Definition typecmds.c:110
static void makeMultirangeConstructors(const char *name, Oid namespace, Oid multirangeOid, Oid rangeOid, Oid rangeArrayOid, Oid *mltrngConstruct0_p, Oid *mltrngConstruct1_p, Oid *mltrngConstruct2_p)
Definition typecmds.c:1869
ObjectAddress DefineType(ParseState *pstate, List *names, List *parameters)
Definition typecmds.c:154
static Oid findRangeSubtypeDiffFunction(List *procname, Oid subtype)
Definition typecmds.c:2425
static Oid findTypeOutputFunction(List *procname, Oid typeOid)
Definition typecmds.c:2079
ObjectAddress DefineEnum(CreateEnumStmt *stmt)
Definition typecmds.c:1183
static Oid findTypeSendFunction(List *procname, Oid typeOid)
Definition typecmds.c:2168
static char * domainAddCheckConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid, int typMod, Constraint *constr, const char *domainName, ObjectAddress *constrAddr)
Definition typecmds.c:3547
ObjectAddress AlterTypeOwner(List *names, Oid newOwnerId, ObjectType objecttype)
Definition typecmds.c:3863
static Oid findRangeCanonicalFunction(List *procname, Oid typeOid)
Definition typecmds.c:2384
ObjectAddress AlterDomainDropConstraint(List *names, const char *constrName, DropBehavior behavior, bool missing_ok)
Definition typecmds.c:2854
void AlterTypeOwnerInternal(Oid typeOid, Oid newOwnerId)
Definition typecmds.c:4028
ObjectAddress AlterEnum(AlterEnumStmt *stmt)
Definition typecmds.c:1307
ObjectAddress AlterDomainAddConstraint(List *names, Node *newConstraint, ObjectAddress *constrAddr)
Definition typecmds.c:2960
void RemoveTypeById(Oid typeOid)
Definition typecmds.c:659
ObjectAddress DefineDomain(ParseState *pstate, CreateDomainStmt *stmt)
Definition typecmds.c:699
static void makeRangeConstructors(const char *name, Oid namespace, Oid rangeOid, Oid subtype, Oid *rangeConstruct2_p, Oid *rangeConstruct3_p)
Definition typecmds.c:1788
ObjectAddress AlterDomainValidateConstraint(List *names, const char *constrName)
Definition typecmds.c:3060
static Oid findTypeSubscriptingFunction(List *procname, Oid typeOid)
Definition typecmds.c:2298
static Oid findTypeTypmodoutFunction(List *procname)
Definition typecmds.c:2237
static Oid findTypeTypmodinFunction(List *procname)
Definition typecmds.c:2203
ObjectAddress AlterDomainDefault(List *names, Node *defaultRaw)
Definition typecmds.c:2639
Oid binary_upgrade_next_array_pg_type_oid
Definition typecmds.c:109
ObjectAddress RenameType(RenameStmt *stmt)
Definition typecmds.c:3782
Oid AssignTypeArrayOid(void)
Definition typecmds.c:2473
static Oid findTypeInputFunction(List *procname, Oid typeOid)
Definition typecmds.c:2016
void checkDomainOwner(HeapTuple tup)
Definition typecmds.c:3527
void AlterTypeOwner_oid(Oid typeOid, Oid newOwnerId, bool hasDependEntry)
Definition typecmds.c:3988
ObjectAddress AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype, Oid *oldschema)
Definition typecmds.c:4096
static List * get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
Definition typecmds.c:3358
ObjectAddress DefineCompositeType(RangeVar *typevar, List *coldeflist)
Definition typecmds.c:2581
static void domainAddNotNullConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid, int typMod, Constraint *constr, const char *domainName, ObjectAddress *constrAddr)
Definition typecmds.c:3707
Oid AssignTypeMultirangeArrayOid(void)
Definition typecmds.c:2539
ObjectAddress AlterType(AlterTypeStmt *stmt)
Definition typecmds.c:4353
Oid AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid, bool isImplicitArray, bool ignoreDependent, bool errorOnTableType, ObjectAddresses *objsMoved)
Definition typecmds.c:4197
static void validateDomainNotNullConstraint(Oid domainoid)
Definition typecmds.c:3167
ObjectAddress DefineRange(ParseState *pstate, CreateRangeStmt *stmt)
Definition typecmds.c:1382
static void validateDomainCheckConstraint(Oid domainoid, const char *ccbin, LOCKMODE lockmode)
Definition typecmds.c:3239
static Node * replace_domain_constraint_value(ParseState *pstate, ColumnRef *cref)
Definition typecmds.c:3676
#define DEFAULT_TYPDELIM
Definition typecmds.h:22
#define strVal(v)
Definition value.h:82
bool contain_var_clause(Node *node)
Definition var.c:406
const char * name
void CommandCounterIncrement(void)
Definition xact.c:1101