]> granicus.if.org Git - postgresql/blob - src/backend/commands/typecmds.c
Make some spelling more consistent
[postgresql] / src / backend / commands / typecmds.c
1 /*-------------------------------------------------------------------------
2  *
3  * typecmds.c
4  *        Routines for SQL commands that manipulate types (and domains).
5  *
6  * Portions Copyright (c) 1996-2013, 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 "FooDefine" routines (in src/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/heapam.h"
36 #include "access/htup_details.h"
37 #include "access/xact.h"
38 #include "catalog/catalog.h"
39 #include "catalog/dependency.h"
40 #include "catalog/heap.h"
41 #include "catalog/indexing.h"
42 #include "catalog/pg_authid.h"
43 #include "catalog/pg_collation.h"
44 #include "catalog/pg_constraint.h"
45 #include "catalog/pg_depend.h"
46 #include "catalog/pg_enum.h"
47 #include "catalog/pg_language.h"
48 #include "catalog/pg_namespace.h"
49 #include "catalog/pg_proc.h"
50 #include "catalog/pg_proc_fn.h"
51 #include "catalog/pg_range.h"
52 #include "catalog/pg_type.h"
53 #include "catalog/pg_type_fn.h"
54 #include "commands/defrem.h"
55 #include "commands/tablecmds.h"
56 #include "commands/typecmds.h"
57 #include "executor/executor.h"
58 #include "miscadmin.h"
59 #include "nodes/makefuncs.h"
60 #include "optimizer/planner.h"
61 #include "optimizer/var.h"
62 #include "parser/parse_coerce.h"
63 #include "parser/parse_collate.h"
64 #include "parser/parse_expr.h"
65 #include "parser/parse_func.h"
66 #include "parser/parse_type.h"
67 #include "utils/acl.h"
68 #include "utils/builtins.h"
69 #include "utils/fmgroids.h"
70 #include "utils/lsyscache.h"
71 #include "utils/memutils.h"
72 #include "utils/rel.h"
73 #include "utils/syscache.h"
74 #include "utils/tqual.h"
75
76
77 /* result structure for get_rels_with_domain() */
78 typedef struct
79 {
80         Relation        rel;                    /* opened and locked relation */
81         int                     natts;                  /* number of attributes of interest */
82         int                *atts;                       /* attribute numbers */
83         /* atts[] is of allocated length RelationGetNumberOfAttributes(rel) */
84 } RelToCheck;
85
86 /* Potentially set by contrib/pg_upgrade_support functions */
87 Oid                     binary_upgrade_next_array_pg_type_oid = InvalidOid;
88
89 static void makeRangeConstructors(const char *name, Oid namespace,
90                                           Oid rangeOid, Oid subtype);
91 static Oid      findTypeInputFunction(List *procname, Oid typeOid);
92 static Oid      findTypeOutputFunction(List *procname, Oid typeOid);
93 static Oid      findTypeReceiveFunction(List *procname, Oid typeOid);
94 static Oid      findTypeSendFunction(List *procname, Oid typeOid);
95 static Oid      findTypeTypmodinFunction(List *procname);
96 static Oid      findTypeTypmodoutFunction(List *procname);
97 static Oid      findTypeAnalyzeFunction(List *procname, Oid typeOid);
98 static Oid      findRangeSubOpclass(List *opcname, Oid subtype);
99 static Oid      findRangeCanonicalFunction(List *procname, Oid typeOid);
100 static Oid      findRangeSubtypeDiffFunction(List *procname, Oid subtype);
101 static void validateDomainConstraint(Oid domainoid, char *ccbin);
102 static List *get_rels_with_domain(Oid domainOid, LOCKMODE lockmode);
103 static void checkEnumOwner(HeapTuple tup);
104 static char *domainAddConstraint(Oid domainOid, Oid domainNamespace,
105                                         Oid baseTypeOid,
106                                         int typMod, Constraint *constr,
107                                         char *domainName);
108
109
110 /*
111  * DefineType
112  *              Registers a new base type.
113  */
114 Oid
115 DefineType(List *names, List *parameters)
116 {
117         char       *typeName;
118         Oid                     typeNamespace;
119         int16           internalLength = -1;    /* default: variable-length */
120         List       *inputName = NIL;
121         List       *outputName = NIL;
122         List       *receiveName = NIL;
123         List       *sendName = NIL;
124         List       *typmodinName = NIL;
125         List       *typmodoutName = NIL;
126         List       *analyzeName = NIL;
127         char            category = TYPCATEGORY_USER;
128         bool            preferred = false;
129         char            delimiter = DEFAULT_TYPDELIM;
130         Oid                     elemType = InvalidOid;
131         char       *defaultValue = NULL;
132         bool            byValue = false;
133         char            alignment = 'i';        /* default alignment */
134         char            storage = 'p';  /* default TOAST storage method */
135         Oid                     collation = InvalidOid;
136         DefElem    *likeTypeEl = NULL;
137         DefElem    *internalLengthEl = NULL;
138         DefElem    *inputNameEl = NULL;
139         DefElem    *outputNameEl = NULL;
140         DefElem    *receiveNameEl = NULL;
141         DefElem    *sendNameEl = NULL;
142         DefElem    *typmodinNameEl = NULL;
143         DefElem    *typmodoutNameEl = NULL;
144         DefElem    *analyzeNameEl = NULL;
145         DefElem    *categoryEl = NULL;
146         DefElem    *preferredEl = NULL;
147         DefElem    *delimiterEl = NULL;
148         DefElem    *elemTypeEl = NULL;
149         DefElem    *defaultValueEl = NULL;
150         DefElem    *byValueEl = NULL;
151         DefElem    *alignmentEl = NULL;
152         DefElem    *storageEl = NULL;
153         DefElem    *collatableEl = NULL;
154         Oid                     inputOid;
155         Oid                     outputOid;
156         Oid                     receiveOid = InvalidOid;
157         Oid                     sendOid = InvalidOid;
158         Oid                     typmodinOid = InvalidOid;
159         Oid                     typmodoutOid = InvalidOid;
160         Oid                     analyzeOid = InvalidOid;
161         char       *array_type;
162         Oid                     array_oid;
163         Oid                     typoid;
164         Oid                     resulttype;
165         ListCell   *pl;
166
167         /*
168          * As of Postgres 8.4, we require superuser privilege to create a base
169          * type.  This is simple paranoia: there are too many ways to mess up the
170          * system with an incorrect type definition (for instance, representation
171          * parameters that don't match what the C code expects).  In practice it
172          * takes superuser privilege to create the I/O functions, and so the
173          * former requirement that you own the I/O functions pretty much forced
174          * superuserness anyway.  We're just making doubly sure here.
175          *
176          * XXX re-enable NOT_USED code sections below if you remove this test.
177          */
178         if (!superuser())
179                 ereport(ERROR,
180                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
181                                  errmsg("must be superuser to create a base type")));
182
183         /* Convert list of names to a name and namespace */
184         typeNamespace = QualifiedNameGetCreationNamespace(names, &typeName);
185
186 #ifdef NOT_USED
187         /* XXX this is unnecessary given the superuser check above */
188         /* Check we have creation rights in target namespace */
189         aclresult = pg_namespace_aclcheck(typeNamespace, GetUserId(), ACL_CREATE);
190         if (aclresult != ACLCHECK_OK)
191                 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
192                                            get_namespace_name(typeNamespace));
193 #endif
194
195         /*
196          * Look to see if type already exists (presumably as a shell; if not,
197          * TypeCreate will complain).
198          */
199         typoid = GetSysCacheOid2(TYPENAMENSP,
200                                                          CStringGetDatum(typeName),
201                                                          ObjectIdGetDatum(typeNamespace));
202
203         /*
204          * If it's not a shell, see if it's an autogenerated array type, and if so
205          * rename it out of the way.
206          */
207         if (OidIsValid(typoid) && get_typisdefined(typoid))
208         {
209                 if (moveArrayTypeName(typoid, typeName, typeNamespace))
210                         typoid = InvalidOid;
211         }
212
213         /*
214          * If it doesn't exist, create it as a shell, so that the OID is known for
215          * use in the I/O function definitions.
216          */
217         if (!OidIsValid(typoid))
218         {
219                 typoid = TypeShellMake(typeName, typeNamespace, GetUserId());
220                 /* Make new shell type visible for modification below */
221                 CommandCounterIncrement();
222
223                 /*
224                  * If the command was a parameterless CREATE TYPE, we're done ---
225                  * creating the shell type was all we're supposed to do.
226                  */
227                 if (parameters == NIL)
228                         return InvalidOid;
229         }
230         else
231         {
232                 /* Complain if dummy CREATE TYPE and entry already exists */
233                 if (parameters == NIL)
234                         ereport(ERROR,
235                                         (errcode(ERRCODE_DUPLICATE_OBJECT),
236                                          errmsg("type \"%s\" already exists", typeName)));
237         }
238
239         /* Extract the parameters from the parameter list */
240         foreach(pl, parameters)
241         {
242                 DefElem    *defel = (DefElem *) lfirst(pl);
243                 DefElem   **defelp;
244
245                 if (pg_strcasecmp(defel->defname, "like") == 0)
246                         defelp = &likeTypeEl;
247                 else if (pg_strcasecmp(defel->defname, "internallength") == 0)
248                         defelp = &internalLengthEl;
249                 else if (pg_strcasecmp(defel->defname, "input") == 0)
250                         defelp = &inputNameEl;
251                 else if (pg_strcasecmp(defel->defname, "output") == 0)
252                         defelp = &outputNameEl;
253                 else if (pg_strcasecmp(defel->defname, "receive") == 0)
254                         defelp = &receiveNameEl;
255                 else if (pg_strcasecmp(defel->defname, "send") == 0)
256                         defelp = &sendNameEl;
257                 else if (pg_strcasecmp(defel->defname, "typmod_in") == 0)
258                         defelp = &typmodinNameEl;
259                 else if (pg_strcasecmp(defel->defname, "typmod_out") == 0)
260                         defelp = &typmodoutNameEl;
261                 else if (pg_strcasecmp(defel->defname, "analyze") == 0 ||
262                                  pg_strcasecmp(defel->defname, "analyse") == 0)
263                         defelp = &analyzeNameEl;
264                 else if (pg_strcasecmp(defel->defname, "category") == 0)
265                         defelp = &categoryEl;
266                 else if (pg_strcasecmp(defel->defname, "preferred") == 0)
267                         defelp = &preferredEl;
268                 else if (pg_strcasecmp(defel->defname, "delimiter") == 0)
269                         defelp = &delimiterEl;
270                 else if (pg_strcasecmp(defel->defname, "element") == 0)
271                         defelp = &elemTypeEl;
272                 else if (pg_strcasecmp(defel->defname, "default") == 0)
273                         defelp = &defaultValueEl;
274                 else if (pg_strcasecmp(defel->defname, "passedbyvalue") == 0)
275                         defelp = &byValueEl;
276                 else if (pg_strcasecmp(defel->defname, "alignment") == 0)
277                         defelp = &alignmentEl;
278                 else if (pg_strcasecmp(defel->defname, "storage") == 0)
279                         defelp = &storageEl;
280                 else if (pg_strcasecmp(defel->defname, "collatable") == 0)
281                         defelp = &collatableEl;
282                 else
283                 {
284                         /* WARNING, not ERROR, for historical backwards-compatibility */
285                         ereport(WARNING,
286                                         (errcode(ERRCODE_SYNTAX_ERROR),
287                                          errmsg("type attribute \"%s\" not recognized",
288                                                         defel->defname)));
289                         continue;
290                 }
291                 if (*defelp != NULL)
292                         ereport(ERROR,
293                                         (errcode(ERRCODE_SYNTAX_ERROR),
294                                          errmsg("conflicting or redundant options")));
295                 *defelp = defel;
296         }
297
298         /*
299          * Now interpret the options; we do this separately so that LIKE can be
300          * overridden by other options regardless of the ordering in the parameter
301          * list.
302          */
303         if (likeTypeEl)
304         {
305                 Type            likeType;
306                 Form_pg_type likeForm;
307
308                 likeType = typenameType(NULL, defGetTypeName(likeTypeEl), NULL);
309                 likeForm = (Form_pg_type) GETSTRUCT(likeType);
310                 internalLength = likeForm->typlen;
311                 byValue = likeForm->typbyval;
312                 alignment = likeForm->typalign;
313                 storage = likeForm->typstorage;
314                 ReleaseSysCache(likeType);
315         }
316         if (internalLengthEl)
317                 internalLength = defGetTypeLength(internalLengthEl);
318         if (inputNameEl)
319                 inputName = defGetQualifiedName(inputNameEl);
320         if (outputNameEl)
321                 outputName = defGetQualifiedName(outputNameEl);
322         if (receiveNameEl)
323                 receiveName = defGetQualifiedName(receiveNameEl);
324         if (sendNameEl)
325                 sendName = defGetQualifiedName(sendNameEl);
326         if (typmodinNameEl)
327                 typmodinName = defGetQualifiedName(typmodinNameEl);
328         if (typmodoutNameEl)
329                 typmodoutName = defGetQualifiedName(typmodoutNameEl);
330         if (analyzeNameEl)
331                 analyzeName = defGetQualifiedName(analyzeNameEl);
332         if (categoryEl)
333         {
334                 char       *p = defGetString(categoryEl);
335
336                 category = p[0];
337                 /* restrict to non-control ASCII */
338                 if (category < 32 || category > 126)
339                         ereport(ERROR,
340                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
341                                  errmsg("invalid type category \"%s\": must be simple ASCII",
342                                                 p)));
343         }
344         if (preferredEl)
345                 preferred = defGetBoolean(preferredEl);
346         if (delimiterEl)
347         {
348                 char       *p = defGetString(delimiterEl);
349
350                 delimiter = p[0];
351                 /* XXX shouldn't we restrict the delimiter? */
352         }
353         if (elemTypeEl)
354         {
355                 elemType = typenameTypeId(NULL, defGetTypeName(elemTypeEl));
356                 /* disallow arrays of pseudotypes */
357                 if (get_typtype(elemType) == TYPTYPE_PSEUDO)
358                         ereport(ERROR,
359                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
360                                          errmsg("array element type cannot be %s",
361                                                         format_type_be(elemType))));
362         }
363         if (defaultValueEl)
364                 defaultValue = defGetString(defaultValueEl);
365         if (byValueEl)
366                 byValue = defGetBoolean(byValueEl);
367         if (alignmentEl)
368         {
369                 char       *a = defGetString(alignmentEl);
370
371                 /*
372                  * Note: if argument was an unquoted identifier, parser will have
373                  * applied translations to it, so be prepared to recognize translated
374                  * type names as well as the nominal form.
375                  */
376                 if (pg_strcasecmp(a, "double") == 0 ||
377                         pg_strcasecmp(a, "float8") == 0 ||
378                         pg_strcasecmp(a, "pg_catalog.float8") == 0)
379                         alignment = 'd';
380                 else if (pg_strcasecmp(a, "int4") == 0 ||
381                                  pg_strcasecmp(a, "pg_catalog.int4") == 0)
382                         alignment = 'i';
383                 else if (pg_strcasecmp(a, "int2") == 0 ||
384                                  pg_strcasecmp(a, "pg_catalog.int2") == 0)
385                         alignment = 's';
386                 else if (pg_strcasecmp(a, "char") == 0 ||
387                                  pg_strcasecmp(a, "pg_catalog.bpchar") == 0)
388                         alignment = 'c';
389                 else
390                         ereport(ERROR,
391                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
392                                          errmsg("alignment \"%s\" not recognized", a)));
393         }
394         if (storageEl)
395         {
396                 char       *a = defGetString(storageEl);
397
398                 if (pg_strcasecmp(a, "plain") == 0)
399                         storage = 'p';
400                 else if (pg_strcasecmp(a, "external") == 0)
401                         storage = 'e';
402                 else if (pg_strcasecmp(a, "extended") == 0)
403                         storage = 'x';
404                 else if (pg_strcasecmp(a, "main") == 0)
405                         storage = 'm';
406                 else
407                         ereport(ERROR,
408                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
409                                          errmsg("storage \"%s\" not recognized", a)));
410         }
411         if (collatableEl)
412                 collation = defGetBoolean(collatableEl) ? DEFAULT_COLLATION_OID : InvalidOid;
413
414         /*
415          * make sure we have our required definitions
416          */
417         if (inputName == NIL)
418                 ereport(ERROR,
419                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
420                                  errmsg("type input function must be specified")));
421         if (outputName == NIL)
422                 ereport(ERROR,
423                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
424                                  errmsg("type output function must be specified")));
425
426         if (typmodinName == NIL && typmodoutName != NIL)
427                 ereport(ERROR,
428                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
429                                  errmsg("type modifier output function is useless without a type modifier input function")));
430
431         /*
432          * Convert I/O proc names to OIDs
433          */
434         inputOid = findTypeInputFunction(inputName, typoid);
435         outputOid = findTypeOutputFunction(outputName, typoid);
436         if (receiveName)
437                 receiveOid = findTypeReceiveFunction(receiveName, typoid);
438         if (sendName)
439                 sendOid = findTypeSendFunction(sendName, typoid);
440
441         /*
442          * Verify that I/O procs return the expected thing.  If we see OPAQUE,
443          * complain and change it to the correct type-safe choice.
444          */
445         resulttype = get_func_rettype(inputOid);
446         if (resulttype != typoid)
447         {
448                 if (resulttype == OPAQUEOID)
449                 {
450                         /* backwards-compatibility hack */
451                         ereport(WARNING,
452                                         (errmsg("changing return type of function %s from \"opaque\" to %s",
453                                                         NameListToString(inputName), typeName)));
454                         SetFunctionReturnType(inputOid, typoid);
455                 }
456                 else
457                         ereport(ERROR,
458                                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
459                                          errmsg("type input function %s must return type %s",
460                                                         NameListToString(inputName), typeName)));
461         }
462         resulttype = get_func_rettype(outputOid);
463         if (resulttype != CSTRINGOID)
464         {
465                 if (resulttype == OPAQUEOID)
466                 {
467                         /* backwards-compatibility hack */
468                         ereport(WARNING,
469                                         (errmsg("changing return type of function %s from \"opaque\" to \"cstring\"",
470                                                         NameListToString(outputName))));
471                         SetFunctionReturnType(outputOid, CSTRINGOID);
472                 }
473                 else
474                         ereport(ERROR,
475                                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
476                            errmsg("type output function %s must return type \"cstring\"",
477                                           NameListToString(outputName))));
478         }
479         if (receiveOid)
480         {
481                 resulttype = get_func_rettype(receiveOid);
482                 if (resulttype != typoid)
483                         ereport(ERROR,
484                                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
485                                          errmsg("type receive function %s must return type %s",
486                                                         NameListToString(receiveName), typeName)));
487         }
488         if (sendOid)
489         {
490                 resulttype = get_func_rettype(sendOid);
491                 if (resulttype != BYTEAOID)
492                         ereport(ERROR,
493                                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
494                                    errmsg("type send function %s must return type \"bytea\"",
495                                                   NameListToString(sendName))));
496         }
497
498         /*
499          * Convert typmodin/out function proc names to OIDs.
500          */
501         if (typmodinName)
502                 typmodinOid = findTypeTypmodinFunction(typmodinName);
503         if (typmodoutName)
504                 typmodoutOid = findTypeTypmodoutFunction(typmodoutName);
505
506         /*
507          * Convert analysis function proc name to an OID. If no analysis function
508          * is specified, we'll use zero to select the built-in default algorithm.
509          */
510         if (analyzeName)
511                 analyzeOid = findTypeAnalyzeFunction(analyzeName, typoid);
512
513         /*
514          * Check permissions on functions.      We choose to require the creator/owner
515          * of a type to also own the underlying functions.      Since creating a type
516          * is tantamount to granting public execute access on the functions, the
517          * minimum sane check would be for execute-with-grant-option.  But we
518          * don't have a way to make the type go away if the grant option is
519          * revoked, so ownership seems better.
520          */
521 #ifdef NOT_USED
522         /* XXX this is unnecessary given the superuser check above */
523         if (inputOid && !pg_proc_ownercheck(inputOid, GetUserId()))
524                 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
525                                            NameListToString(inputName));
526         if (outputOid && !pg_proc_ownercheck(outputOid, GetUserId()))
527                 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
528                                            NameListToString(outputName));
529         if (receiveOid && !pg_proc_ownercheck(receiveOid, GetUserId()))
530                 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
531                                            NameListToString(receiveName));
532         if (sendOid && !pg_proc_ownercheck(sendOid, GetUserId()))
533                 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
534                                            NameListToString(sendName));
535         if (typmodinOid && !pg_proc_ownercheck(typmodinOid, GetUserId()))
536                 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
537                                            NameListToString(typmodinName));
538         if (typmodoutOid && !pg_proc_ownercheck(typmodoutOid, GetUserId()))
539                 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
540                                            NameListToString(typmodoutName));
541         if (analyzeOid && !pg_proc_ownercheck(analyzeOid, GetUserId()))
542                 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
543                                            NameListToString(analyzeName));
544 #endif
545
546         array_oid = AssignTypeArrayOid();
547
548         /*
549          * now have TypeCreate do all the real work.
550          *
551          * Note: the pg_type.oid is stored in user tables as array elements (base
552          * types) in ArrayType and in composite types in DatumTupleFields.      This
553          * oid must be preserved by binary upgrades.
554          */
555         typoid =
556                 TypeCreate(InvalidOid,  /* no predetermined type OID */
557                                    typeName,    /* type name */
558                                    typeNamespace,               /* namespace */
559                                    InvalidOid,  /* relation oid (n/a here) */
560                                    0,                   /* relation kind (ditto) */
561                                    GetUserId(), /* owner's ID */
562                                    internalLength,              /* internal size */
563                                    TYPTYPE_BASE,        /* type-type (base type) */
564                                    category,    /* type-category */
565                                    preferred,   /* is it a preferred type? */
566                                    delimiter,   /* array element delimiter */
567                                    inputOid,    /* input procedure */
568                                    outputOid,   /* output procedure */
569                                    receiveOid,  /* receive procedure */
570                                    sendOid,             /* send procedure */
571                                    typmodinOid, /* typmodin procedure */
572                                    typmodoutOid,        /* typmodout procedure */
573                                    analyzeOid,  /* analyze procedure */
574                                    elemType,    /* element type ID */
575                                    false,               /* this is not an array type */
576                                    array_oid,   /* array type we are about to create */
577                                    InvalidOid,  /* base type ID (only for domains) */
578                                    defaultValue,        /* default type value */
579                                    NULL,                /* no binary form available */
580                                    byValue,             /* passed by value */
581                                    alignment,   /* required alignment */
582                                    storage,             /* TOAST strategy */
583                                    -1,                  /* typMod (Domains only) */
584                                    0,                   /* Array Dimensions of typbasetype */
585                                    false,               /* Type NOT NULL */
586                                    collation);  /* type's collation */
587
588         /*
589          * Create the array type that goes with it.
590          */
591         array_type = makeArrayTypeName(typeName, typeNamespace);
592
593         /* alignment must be 'i' or 'd' for arrays */
594         alignment = (alignment == 'd') ? 'd' : 'i';
595
596         typoid = TypeCreate(array_oid,          /* force assignment of this type OID */
597                                                 array_type,             /* type name */
598                                                 typeNamespace,  /* namespace */
599                                                 InvalidOid,             /* relation oid (n/a here) */
600                                                 0,                              /* relation kind (ditto) */
601                                                 GetUserId(),            /* owner's ID */
602                                                 -1,                             /* internal size (always varlena) */
603                                                 TYPTYPE_BASE,   /* type-type (base type) */
604                                                 TYPCATEGORY_ARRAY,              /* type-category (array) */
605                                                 false,                  /* array types are never preferred */
606                                                 delimiter,              /* array element delimiter */
607                                                 F_ARRAY_IN,             /* input procedure */
608                                                 F_ARRAY_OUT,            /* output procedure */
609                                                 F_ARRAY_RECV,   /* receive procedure */
610                                                 F_ARRAY_SEND,   /* send procedure */
611                                                 typmodinOid,            /* typmodin procedure */
612                                                 typmodoutOid,   /* typmodout procedure */
613                                                 F_ARRAY_TYPANALYZE,             /* analyze procedure */
614                                                 typoid,                 /* element type ID */
615                                                 true,                   /* yes this is an array type */
616                                                 InvalidOid,             /* no further array type */
617                                                 InvalidOid,             /* base type ID */
618                                                 NULL,                   /* never a default type value */
619                                                 NULL,                   /* binary default isn't sent either */
620                                                 false,                  /* never passed by value */
621                                                 alignment,              /* see above */
622                                                 'x',                            /* ARRAY is always toastable */
623                                                 -1,                             /* typMod (Domains only) */
624                                                 0,                              /* Array dimensions of typbasetype */
625                                                 false,                  /* Type NOT NULL */
626                                                 collation);             /* type's collation */
627
628         pfree(array_type);
629
630         return typoid;
631 }
632
633 /*
634  * Guts of type deletion.
635  */
636 void
637 RemoveTypeById(Oid typeOid)
638 {
639         Relation        relation;
640         HeapTuple       tup;
641
642         relation = heap_open(TypeRelationId, RowExclusiveLock);
643
644         tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeOid));
645         if (!HeapTupleIsValid(tup))
646                 elog(ERROR, "cache lookup failed for type %u", typeOid);
647
648         simple_heap_delete(relation, &tup->t_self);
649
650         /*
651          * If it is an enum, delete the pg_enum entries too; we don't bother with
652          * making dependency entries for those, so it has to be done "by hand"
653          * here.
654          */
655         if (((Form_pg_type) GETSTRUCT(tup))->typtype == TYPTYPE_ENUM)
656                 EnumValuesDelete(typeOid);
657
658         /*
659          * If it is a range type, delete the pg_range entry too; we don't bother
660          * with making a dependency entry for that, so it has to be done "by hand"
661          * here.
662          */
663         if (((Form_pg_type) GETSTRUCT(tup))->typtype == TYPTYPE_RANGE)
664                 RangeDelete(typeOid);
665
666         ReleaseSysCache(tup);
667
668         heap_close(relation, RowExclusiveLock);
669 }
670
671
672 /*
673  * DefineDomain
674  *              Registers a new domain.
675  */
676 Oid
677 DefineDomain(CreateDomainStmt *stmt)
678 {
679         char       *domainName;
680         Oid                     domainNamespace;
681         AclResult       aclresult;
682         int16           internalLength;
683         Oid                     inputProcedure;
684         Oid                     outputProcedure;
685         Oid                     receiveProcedure;
686         Oid                     sendProcedure;
687         Oid                     analyzeProcedure;
688         bool            byValue;
689         char            category;
690         char            delimiter;
691         char            alignment;
692         char            storage;
693         char            typtype;
694         Datum           datum;
695         bool            isnull;
696         char       *defaultValue = NULL;
697         char       *defaultValueBin = NULL;
698         bool            saw_default = false;
699         bool            typNotNull = false;
700         bool            nullDefined = false;
701         int32           typNDims = list_length(stmt->typeName->arrayBounds);
702         HeapTuple       typeTup;
703         List       *schema = stmt->constraints;
704         ListCell   *listptr;
705         Oid                     basetypeoid;
706         Oid                     domainoid;
707         Oid                     old_type_oid;
708         Oid                     domaincoll;
709         Form_pg_type baseType;
710         int32           basetypeMod;
711         Oid                     baseColl;
712
713         /* Convert list of names to a name and namespace */
714         domainNamespace = QualifiedNameGetCreationNamespace(stmt->domainname,
715                                                                                                                 &domainName);
716
717         /* Check we have creation rights in target namespace */
718         aclresult = pg_namespace_aclcheck(domainNamespace, GetUserId(),
719                                                                           ACL_CREATE);
720         if (aclresult != ACLCHECK_OK)
721                 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
722                                            get_namespace_name(domainNamespace));
723
724         /*
725          * Check for collision with an existing type name.      If there is one and
726          * it's an autogenerated array, we can rename it out of the way.
727          */
728         old_type_oid = GetSysCacheOid2(TYPENAMENSP,
729                                                                    CStringGetDatum(domainName),
730                                                                    ObjectIdGetDatum(domainNamespace));
731         if (OidIsValid(old_type_oid))
732         {
733                 if (!moveArrayTypeName(old_type_oid, domainName, domainNamespace))
734                         ereport(ERROR,
735                                         (errcode(ERRCODE_DUPLICATE_OBJECT),
736                                          errmsg("type \"%s\" already exists", domainName)));
737         }
738
739         /*
740          * Look up the base type.
741          */
742         typeTup = typenameType(NULL, stmt->typeName, &basetypeMod);
743         baseType = (Form_pg_type) GETSTRUCT(typeTup);
744         basetypeoid = HeapTupleGetOid(typeTup);
745
746         /*
747          * Base type must be a plain base type, another domain, an enum or a range
748          * type. Domains over pseudotypes would create a security hole.  Domains
749          * over composite types might be made to work in the future, but not
750          * today.
751          */
752         typtype = baseType->typtype;
753         if (typtype != TYPTYPE_BASE &&
754                 typtype != TYPTYPE_DOMAIN &&
755                 typtype != TYPTYPE_ENUM &&
756                 typtype != TYPTYPE_RANGE)
757                 ereport(ERROR,
758                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
759                                  errmsg("\"%s\" is not a valid base type for a domain",
760                                                 TypeNameToString(stmt->typeName))));
761
762         aclresult = pg_type_aclcheck(basetypeoid, GetUserId(), ACL_USAGE);
763         if (aclresult != ACLCHECK_OK)
764                 aclcheck_error_type(aclresult, basetypeoid);
765
766         /*
767          * Identify the collation if any
768          */
769         baseColl = baseType->typcollation;
770         if (stmt->collClause)
771                 domaincoll = get_collation_oid(stmt->collClause->collname, false);
772         else
773                 domaincoll = baseColl;
774
775         /* Complain if COLLATE is applied to an uncollatable type */
776         if (OidIsValid(domaincoll) && !OidIsValid(baseColl))
777                 ereport(ERROR,
778                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
779                                  errmsg("collations are not supported by type %s",
780                                                 format_type_be(basetypeoid))));
781
782         /* passed by value */
783         byValue = baseType->typbyval;
784
785         /* Required Alignment */
786         alignment = baseType->typalign;
787
788         /* TOAST Strategy */
789         storage = baseType->typstorage;
790
791         /* Storage Length */
792         internalLength = baseType->typlen;
793
794         /* Type Category */
795         category = baseType->typcategory;
796
797         /* Array element Delimiter */
798         delimiter = baseType->typdelim;
799
800         /* I/O Functions */
801         inputProcedure = F_DOMAIN_IN;
802         outputProcedure = baseType->typoutput;
803         receiveProcedure = F_DOMAIN_RECV;
804         sendProcedure = baseType->typsend;
805
806         /* Domains never accept typmods, so no typmodin/typmodout needed */
807
808         /* Analysis function */
809         analyzeProcedure = baseType->typanalyze;
810
811         /* Inherited default value */
812         datum = SysCacheGetAttr(TYPEOID, typeTup,
813                                                         Anum_pg_type_typdefault, &isnull);
814         if (!isnull)
815                 defaultValue = TextDatumGetCString(datum);
816
817         /* Inherited default binary value */
818         datum = SysCacheGetAttr(TYPEOID, typeTup,
819                                                         Anum_pg_type_typdefaultbin, &isnull);
820         if (!isnull)
821                 defaultValueBin = TextDatumGetCString(datum);
822
823         /*
824          * Run through constraints manually to avoid the additional processing
825          * conducted by DefineRelation() and friends.
826          */
827         foreach(listptr, schema)
828         {
829                 Constraint *constr = lfirst(listptr);
830
831                 if (!IsA(constr, Constraint))
832                         elog(ERROR, "unrecognized node type: %d",
833                                  (int) nodeTag(constr));
834                 switch (constr->contype)
835                 {
836                         case CONSTR_DEFAULT:
837
838                                 /*
839                                  * The inherited default value may be overridden by the user
840                                  * with the DEFAULT <expr> clause ... but only once.
841                                  */
842                                 if (saw_default)
843                                         ereport(ERROR,
844                                                         (errcode(ERRCODE_SYNTAX_ERROR),
845                                                          errmsg("multiple default expressions")));
846                                 saw_default = true;
847
848                                 if (constr->raw_expr)
849                                 {
850                                         ParseState *pstate;
851                                         Node       *defaultExpr;
852
853                                         /* Create a dummy ParseState for transformExpr */
854                                         pstate = make_parsestate(NULL);
855
856                                         /*
857                                          * Cook the constr->raw_expr into an expression. Note:
858                                          * name is strictly for error message
859                                          */
860                                         defaultExpr = cookDefault(pstate, constr->raw_expr,
861                                                                                           basetypeoid,
862                                                                                           basetypeMod,
863                                                                                           domainName);
864
865                                         /*
866                                          * If the expression is just a NULL constant, we treat it
867                                          * like not having a default.
868                                          *
869                                          * Note that if the basetype is another domain, we'll see
870                                          * a CoerceToDomain expr here and not discard the default.
871                                          * This is critical because the domain default needs to be
872                                          * retained to override any default that the base domain
873                                          * might have.
874                                          */
875                                         if (defaultExpr == NULL ||
876                                                 (IsA(defaultExpr, Const) &&
877                                                  ((Const *) defaultExpr)->constisnull))
878                                         {
879                                                 defaultValue = NULL;
880                                                 defaultValueBin = NULL;
881                                         }
882                                         else
883                                         {
884                                                 /*
885                                                  * Expression must be stored as a nodeToString result,
886                                                  * but we also require a valid textual representation
887                                                  * (mainly to make life easier for pg_dump).
888                                                  */
889                                                 defaultValue =
890                                                         deparse_expression(defaultExpr,
891                                                                                            NIL, false, false);
892                                                 defaultValueBin = nodeToString(defaultExpr);
893                                         }
894                                 }
895                                 else
896                                 {
897                                         /* No default (can this still happen?) */
898                                         defaultValue = NULL;
899                                         defaultValueBin = NULL;
900                                 }
901                                 break;
902
903                         case CONSTR_NOTNULL:
904                                 if (nullDefined && !typNotNull)
905                                         ereport(ERROR,
906                                                         (errcode(ERRCODE_SYNTAX_ERROR),
907                                                    errmsg("conflicting NULL/NOT NULL constraints")));
908                                 typNotNull = true;
909                                 nullDefined = true;
910                                 break;
911
912                         case CONSTR_NULL:
913                                 if (nullDefined && typNotNull)
914                                         ereport(ERROR,
915                                                         (errcode(ERRCODE_SYNTAX_ERROR),
916                                                    errmsg("conflicting NULL/NOT NULL constraints")));
917                                 typNotNull = false;
918                                 nullDefined = true;
919                                 break;
920
921                         case CONSTR_CHECK:
922
923                                 /*
924                                  * Check constraints are handled after domain creation, as
925                                  * they require the Oid of the domain; at this point we can
926                                  * only check that they're not marked NO INHERIT, because
927                                  * that would be bogus.
928                                  */
929                                 if (constr->is_no_inherit)
930                                         ereport(ERROR,
931                                                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
932                                                          errmsg("check constraints for domains cannot be marked NO INHERIT")));
933                                 break;
934
935                                 /*
936                                  * All else are error cases
937                                  */
938                         case CONSTR_UNIQUE:
939                                 ereport(ERROR,
940                                                 (errcode(ERRCODE_SYNTAX_ERROR),
941                                          errmsg("unique constraints not possible for domains")));
942                                 break;
943
944                         case CONSTR_PRIMARY:
945                                 ereport(ERROR,
946                                                 (errcode(ERRCODE_SYNTAX_ERROR),
947                                 errmsg("primary key constraints not possible for domains")));
948                                 break;
949
950                         case CONSTR_EXCLUSION:
951                                 ereport(ERROR,
952                                                 (errcode(ERRCODE_SYNTAX_ERROR),
953                                   errmsg("exclusion constraints not possible for domains")));
954                                 break;
955
956                         case CONSTR_FOREIGN:
957                                 ereport(ERROR,
958                                                 (errcode(ERRCODE_SYNTAX_ERROR),
959                                 errmsg("foreign key constraints not possible for domains")));
960                                 break;
961
962                         case CONSTR_ATTR_DEFERRABLE:
963                         case CONSTR_ATTR_NOT_DEFERRABLE:
964                         case CONSTR_ATTR_DEFERRED:
965                         case CONSTR_ATTR_IMMEDIATE:
966                                 ereport(ERROR,
967                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
968                                                  errmsg("specifying constraint deferrability not supported for domains")));
969                                 break;
970
971                         default:
972                                 elog(ERROR, "unrecognized constraint subtype: %d",
973                                          (int) constr->contype);
974                                 break;
975                 }
976         }
977
978         /*
979          * Have TypeCreate do all the real work.
980          */
981         domainoid =
982                 TypeCreate(InvalidOid,  /* no predetermined type OID */
983                                    domainName,  /* type name */
984                                    domainNamespace,             /* namespace */
985                                    InvalidOid,  /* relation oid (n/a here) */
986                                    0,                   /* relation kind (ditto) */
987                                    GetUserId(), /* owner's ID */
988                                    internalLength,              /* internal size */
989                                    TYPTYPE_DOMAIN,              /* type-type (domain type) */
990                                    category,    /* type-category */
991                                    false,               /* domain types are never preferred */
992                                    delimiter,   /* array element delimiter */
993                                    inputProcedure,              /* input procedure */
994                                    outputProcedure,             /* output procedure */
995                                    receiveProcedure,    /* receive procedure */
996                                    sendProcedure,               /* send procedure */
997                                    InvalidOid,  /* typmodin procedure - none */
998                                    InvalidOid,  /* typmodout procedure - none */
999                                    analyzeProcedure,    /* analyze procedure */
1000                                    InvalidOid,  /* no array element type */
1001                                    false,               /* this isn't an array */
1002                                    InvalidOid,  /* no arrays for domains (yet) */
1003                                    basetypeoid, /* base type ID */
1004                                    defaultValue,        /* default type value (text) */
1005                                    defaultValueBin,             /* default type value (binary) */
1006                                    byValue,             /* passed by value */
1007                                    alignment,   /* required alignment */
1008                                    storage,             /* TOAST strategy */
1009                                    basetypeMod, /* typeMod value */
1010                                    typNDims,    /* Array dimensions for base type */
1011                                    typNotNull,  /* Type NOT NULL */
1012                                    domaincoll); /* type's collation */
1013
1014         /*
1015          * Process constraints which refer to the domain ID returned by TypeCreate
1016          */
1017         foreach(listptr, schema)
1018         {
1019                 Constraint *constr = lfirst(listptr);
1020
1021                 /* it must be a Constraint, per check above */
1022
1023                 switch (constr->contype)
1024                 {
1025                         case CONSTR_CHECK:
1026                                 domainAddConstraint(domainoid, domainNamespace,
1027                                                                         basetypeoid, basetypeMod,
1028                                                                         constr, domainName);
1029                                 break;
1030
1031                                 /* Other constraint types were fully processed above */
1032
1033                         default:
1034                                 break;
1035                 }
1036
1037                 /* CCI so we can detect duplicate constraint names */
1038                 CommandCounterIncrement();
1039         }
1040
1041         /*
1042          * Now we can clean up.
1043          */
1044         ReleaseSysCache(typeTup);
1045
1046         return domainoid;
1047 }
1048
1049
1050 /*
1051  * DefineEnum
1052  *              Registers a new enum.
1053  */
1054 Oid
1055 DefineEnum(CreateEnumStmt *stmt)
1056 {
1057         char       *enumName;
1058         char       *enumArrayName;
1059         Oid                     enumNamespace;
1060         Oid                     enumTypeOid;
1061         AclResult       aclresult;
1062         Oid                     old_type_oid;
1063         Oid                     enumArrayOid;
1064
1065         /* Convert list of names to a name and namespace */
1066         enumNamespace = QualifiedNameGetCreationNamespace(stmt->typeName,
1067                                                                                                           &enumName);
1068
1069         /* Check we have creation rights in target namespace */
1070         aclresult = pg_namespace_aclcheck(enumNamespace, GetUserId(), ACL_CREATE);
1071         if (aclresult != ACLCHECK_OK)
1072                 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
1073                                            get_namespace_name(enumNamespace));
1074
1075         /*
1076          * Check for collision with an existing type name.      If there is one and
1077          * it's an autogenerated array, we can rename it out of the way.
1078          */
1079         old_type_oid = GetSysCacheOid2(TYPENAMENSP,
1080                                                                    CStringGetDatum(enumName),
1081                                                                    ObjectIdGetDatum(enumNamespace));
1082         if (OidIsValid(old_type_oid))
1083         {
1084                 if (!moveArrayTypeName(old_type_oid, enumName, enumNamespace))
1085                         ereport(ERROR,
1086                                         (errcode(ERRCODE_DUPLICATE_OBJECT),
1087                                          errmsg("type \"%s\" already exists", enumName)));
1088         }
1089
1090         enumArrayOid = AssignTypeArrayOid();
1091
1092         /* Create the pg_type entry */
1093         enumTypeOid =
1094                 TypeCreate(InvalidOid,  /* no predetermined type OID */
1095                                    enumName,    /* type name */
1096                                    enumNamespace,               /* namespace */
1097                                    InvalidOid,  /* relation oid (n/a here) */
1098                                    0,                   /* relation kind (ditto) */
1099                                    GetUserId(), /* owner's ID */
1100                                    sizeof(Oid), /* internal size */
1101                                    TYPTYPE_ENUM,        /* type-type (enum type) */
1102                                    TYPCATEGORY_ENUM,    /* type-category (enum type) */
1103                                    false,               /* enum types are never preferred */
1104                                    DEFAULT_TYPDELIM,    /* array element delimiter */
1105                                    F_ENUM_IN,   /* input procedure */
1106                                    F_ENUM_OUT,  /* output procedure */
1107                                    F_ENUM_RECV, /* receive procedure */
1108                                    F_ENUM_SEND, /* send procedure */
1109                                    InvalidOid,  /* typmodin procedure - none */
1110                                    InvalidOid,  /* typmodout procedure - none */
1111                                    InvalidOid,  /* analyze procedure - default */
1112                                    InvalidOid,  /* element type ID */
1113                                    false,               /* this is not an array type */
1114                                    enumArrayOid,        /* array type we are about to create */
1115                                    InvalidOid,  /* base type ID (only for domains) */
1116                                    NULL,                /* never a default type value */
1117                                    NULL,                /* binary default isn't sent either */
1118                                    true,                /* always passed by value */
1119                                    'i',                 /* int alignment */
1120                                    'p',                 /* TOAST strategy always plain */
1121                                    -1,                  /* typMod (Domains only) */
1122                                    0,                   /* Array dimensions of typbasetype */
1123                                    false,               /* Type NOT NULL */
1124                                    InvalidOid); /* type's collation */
1125
1126         /* Enter the enum's values into pg_enum */
1127         EnumValuesCreate(enumTypeOid, stmt->vals);
1128
1129         /*
1130          * Create the array type that goes with it.
1131          */
1132         enumArrayName = makeArrayTypeName(enumName, enumNamespace);
1133
1134         TypeCreate(enumArrayOid,        /* force assignment of this type OID */
1135                            enumArrayName,       /* type name */
1136                            enumNamespace,       /* namespace */
1137                            InvalidOid,          /* relation oid (n/a here) */
1138                            0,                           /* relation kind (ditto) */
1139                            GetUserId(),         /* owner's ID */
1140                            -1,                          /* internal size (always varlena) */
1141                            TYPTYPE_BASE,        /* type-type (base type) */
1142                            TYPCATEGORY_ARRAY,           /* type-category (array) */
1143                            false,                       /* array types are never preferred */
1144                            DEFAULT_TYPDELIM,    /* array element delimiter */
1145                            F_ARRAY_IN,          /* input procedure */
1146                            F_ARRAY_OUT,         /* output procedure */
1147                            F_ARRAY_RECV,        /* receive procedure */
1148                            F_ARRAY_SEND,        /* send procedure */
1149                            InvalidOid,          /* typmodin procedure - none */
1150                            InvalidOid,          /* typmodout procedure - none */
1151                            F_ARRAY_TYPANALYZE,          /* analyze procedure */
1152                            enumTypeOid,         /* element type ID */
1153                            true,                        /* yes this is an array type */
1154                            InvalidOid,          /* no further array type */
1155                            InvalidOid,          /* base type ID */
1156                            NULL,                        /* never a default type value */
1157                            NULL,                        /* binary default isn't sent either */
1158                            false,                       /* never passed by value */
1159                            'i',                         /* enums have align i, so do their arrays */
1160                            'x',                         /* ARRAY is always toastable */
1161                            -1,                          /* typMod (Domains only) */
1162                            0,                           /* Array dimensions of typbasetype */
1163                            false,                       /* Type NOT NULL */
1164                            InvalidOid);         /* type's collation */
1165
1166         pfree(enumArrayName);
1167
1168         return enumTypeOid;
1169 }
1170
1171 /*
1172  * AlterEnum
1173  *              Adds a new label to an existing enum.
1174  */
1175 Oid
1176 AlterEnum(AlterEnumStmt *stmt, bool isTopLevel)
1177 {
1178         Oid                     enum_type_oid;
1179         TypeName   *typename;
1180         HeapTuple       tup;
1181
1182         /* Make a TypeName so we can use standard type lookup machinery */
1183         typename = makeTypeNameFromNameList(stmt->typeName);
1184         enum_type_oid = typenameTypeId(NULL, typename);
1185
1186         tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(enum_type_oid));
1187         if (!HeapTupleIsValid(tup))
1188                 elog(ERROR, "cache lookup failed for type %u", enum_type_oid);
1189
1190         /*
1191          * Ordinarily we disallow adding values within transaction blocks, because
1192          * we can't cope with enum OID values getting into indexes and then having
1193          * their defining pg_enum entries go away.  However, it's okay if the enum
1194          * type was created in the current transaction, since then there can be
1195          * no such indexes that wouldn't themselves go away on rollback.  (We
1196          * support this case because pg_dump --binary-upgrade needs it.)  We test
1197          * this by seeing if the pg_type row has xmin == current XID and is not
1198          * HEAP_UPDATED.  If it is HEAP_UPDATED, we can't be sure whether the
1199          * type was created or only modified in this xact.  So we are disallowing
1200          * some cases that could theoretically be safe; but fortunately pg_dump
1201          * only needs the simplest case.
1202          */
1203         if (HeapTupleHeaderGetXmin(tup->t_data) == GetCurrentTransactionId() &&
1204                 !(tup->t_data->t_infomask & HEAP_UPDATED))
1205                 /* safe to do inside transaction block */ ;
1206         else
1207                 PreventTransactionChain(isTopLevel, "ALTER TYPE ... ADD");
1208
1209         /* Check it's an enum and check user has permission to ALTER the enum */
1210         checkEnumOwner(tup);
1211
1212         /* Add the new label */
1213         AddEnumLabel(enum_type_oid, stmt->newVal,
1214                                  stmt->newValNeighbor, stmt->newValIsAfter,
1215                                  stmt->skipIfExists);
1216
1217         ReleaseSysCache(tup);
1218
1219         return enum_type_oid;
1220 }
1221
1222
1223 /*
1224  * checkEnumOwner
1225  *
1226  * Check that the type is actually an enum and that the current user
1227  * has permission to do ALTER TYPE on it.  Throw an error if not.
1228  */
1229 static void
1230 checkEnumOwner(HeapTuple tup)
1231 {
1232         Form_pg_type typTup = (Form_pg_type) GETSTRUCT(tup);
1233
1234         /* Check that this is actually an enum */
1235         if (typTup->typtype != TYPTYPE_ENUM)
1236                 ereport(ERROR,
1237                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1238                                  errmsg("%s is not an enum",
1239                                                 format_type_be(HeapTupleGetOid(tup)))));
1240
1241         /* Permission check: must own type */
1242         if (!pg_type_ownercheck(HeapTupleGetOid(tup), GetUserId()))
1243                 aclcheck_error_type(ACLCHECK_NOT_OWNER, HeapTupleGetOid(tup));
1244 }
1245
1246
1247 /*
1248  * DefineRange
1249  *              Registers a new range type.
1250  */
1251 Oid
1252 DefineRange(CreateRangeStmt *stmt)
1253 {
1254         char       *typeName;
1255         Oid                     typeNamespace;
1256         Oid                     typoid;
1257         char       *rangeArrayName;
1258         Oid                     rangeArrayOid;
1259         Oid                     rangeSubtype = InvalidOid;
1260         List       *rangeSubOpclassName = NIL;
1261         List       *rangeCollationName = NIL;
1262         List       *rangeCanonicalName = NIL;
1263         List       *rangeSubtypeDiffName = NIL;
1264         Oid                     rangeSubOpclass;
1265         Oid                     rangeCollation;
1266         regproc         rangeCanonical;
1267         regproc         rangeSubtypeDiff;
1268         int16           subtyplen;
1269         bool            subtypbyval;
1270         char            subtypalign;
1271         char            alignment;
1272         AclResult       aclresult;
1273         ListCell   *lc;
1274
1275         /* Convert list of names to a name and namespace */
1276         typeNamespace = QualifiedNameGetCreationNamespace(stmt->typeName,
1277                                                                                                           &typeName);
1278
1279         /* Check we have creation rights in target namespace */
1280         aclresult = pg_namespace_aclcheck(typeNamespace, GetUserId(), ACL_CREATE);
1281         if (aclresult != ACLCHECK_OK)
1282                 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
1283                                            get_namespace_name(typeNamespace));
1284
1285         /*
1286          * Look to see if type already exists.
1287          */
1288         typoid = GetSysCacheOid2(TYPENAMENSP,
1289                                                          CStringGetDatum(typeName),
1290                                                          ObjectIdGetDatum(typeNamespace));
1291
1292         /*
1293          * If it's not a shell, see if it's an autogenerated array type, and if so
1294          * rename it out of the way.
1295          */
1296         if (OidIsValid(typoid) && get_typisdefined(typoid))
1297         {
1298                 if (moveArrayTypeName(typoid, typeName, typeNamespace))
1299                         typoid = InvalidOid;
1300                 else
1301                         ereport(ERROR,
1302                                         (errcode(ERRCODE_DUPLICATE_OBJECT),
1303                                          errmsg("type \"%s\" already exists", typeName)));
1304         }
1305
1306         /*
1307          * If it doesn't exist, create it as a shell, so that the OID is known for
1308          * use in the range function definitions.
1309          */
1310         if (!OidIsValid(typoid))
1311         {
1312                 typoid = TypeShellMake(typeName, typeNamespace, GetUserId());
1313                 /* Make new shell type visible for modification below */
1314                 CommandCounterIncrement();
1315         }
1316
1317         /* Extract the parameters from the parameter list */
1318         foreach(lc, stmt->params)
1319         {
1320                 DefElem    *defel = (DefElem *) lfirst(lc);
1321
1322                 if (pg_strcasecmp(defel->defname, "subtype") == 0)
1323                 {
1324                         if (OidIsValid(rangeSubtype))
1325                                 ereport(ERROR,
1326                                                 (errcode(ERRCODE_SYNTAX_ERROR),
1327                                                  errmsg("conflicting or redundant options")));
1328                         /* we can look up the subtype name immediately */
1329                         rangeSubtype = typenameTypeId(NULL, defGetTypeName(defel));
1330                 }
1331                 else if (pg_strcasecmp(defel->defname, "subtype_opclass") == 0)
1332                 {
1333                         if (rangeSubOpclassName != NIL)
1334                                 ereport(ERROR,
1335                                                 (errcode(ERRCODE_SYNTAX_ERROR),
1336                                                  errmsg("conflicting or redundant options")));
1337                         rangeSubOpclassName = defGetQualifiedName(defel);
1338                 }
1339                 else if (pg_strcasecmp(defel->defname, "collation") == 0)
1340                 {
1341                         if (rangeCollationName != NIL)
1342                                 ereport(ERROR,
1343                                                 (errcode(ERRCODE_SYNTAX_ERROR),
1344                                                  errmsg("conflicting or redundant options")));
1345                         rangeCollationName = defGetQualifiedName(defel);
1346                 }
1347                 else if (pg_strcasecmp(defel->defname, "canonical") == 0)
1348                 {
1349                         if (rangeCanonicalName != NIL)
1350                                 ereport(ERROR,
1351                                                 (errcode(ERRCODE_SYNTAX_ERROR),
1352                                                  errmsg("conflicting or redundant options")));
1353                         rangeCanonicalName = defGetQualifiedName(defel);
1354                 }
1355                 else if (pg_strcasecmp(defel->defname, "subtype_diff") == 0)
1356                 {
1357                         if (rangeSubtypeDiffName != NIL)
1358                                 ereport(ERROR,
1359                                                 (errcode(ERRCODE_SYNTAX_ERROR),
1360                                                  errmsg("conflicting or redundant options")));
1361                         rangeSubtypeDiffName = defGetQualifiedName(defel);
1362                 }
1363                 else
1364                         ereport(ERROR,
1365                                         (errcode(ERRCODE_SYNTAX_ERROR),
1366                                          errmsg("type attribute \"%s\" not recognized",
1367                                                         defel->defname)));
1368         }
1369
1370         /* Must have a subtype */
1371         if (!OidIsValid(rangeSubtype))
1372                 ereport(ERROR,
1373                                 (errcode(ERRCODE_SYNTAX_ERROR),
1374                                  errmsg("type attribute \"subtype\" is required")));
1375         /* disallow ranges of pseudotypes */
1376         if (get_typtype(rangeSubtype) == TYPTYPE_PSEUDO)
1377                 ereport(ERROR,
1378                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1379                                  errmsg("range subtype cannot be %s",
1380                                                 format_type_be(rangeSubtype))));
1381
1382         /* Identify subopclass */
1383         rangeSubOpclass = findRangeSubOpclass(rangeSubOpclassName, rangeSubtype);
1384
1385         /* Identify collation to use, if any */
1386         if (type_is_collatable(rangeSubtype))
1387         {
1388                 if (rangeCollationName != NIL)
1389                         rangeCollation = get_collation_oid(rangeCollationName, false);
1390                 else
1391                         rangeCollation = get_typcollation(rangeSubtype);
1392         }
1393         else
1394         {
1395                 if (rangeCollationName != NIL)
1396                         ereport(ERROR,
1397                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1398                                          errmsg("range collation specified but subtype does not support collation")));
1399                 rangeCollation = InvalidOid;
1400         }
1401
1402         /* Identify support functions, if provided */
1403         if (rangeCanonicalName != NIL)
1404                 rangeCanonical = findRangeCanonicalFunction(rangeCanonicalName,
1405                                                                                                         typoid);
1406         else
1407                 rangeCanonical = InvalidOid;
1408
1409         if (rangeSubtypeDiffName != NIL)
1410                 rangeSubtypeDiff = findRangeSubtypeDiffFunction(rangeSubtypeDiffName,
1411                                                                                                                 rangeSubtype);
1412         else
1413                 rangeSubtypeDiff = InvalidOid;
1414
1415         get_typlenbyvalalign(rangeSubtype,
1416                                                  &subtyplen, &subtypbyval, &subtypalign);
1417
1418         /* alignment must be 'i' or 'd' for ranges */
1419         alignment = (subtypalign == 'd') ? 'd' : 'i';
1420
1421         /* Allocate OID for array type */
1422         rangeArrayOid = AssignTypeArrayOid();
1423
1424         /* Create the pg_type entry */
1425         typoid =
1426                 TypeCreate(InvalidOid,  /* no predetermined type OID */
1427                                    typeName,    /* type name */
1428                                    typeNamespace,               /* namespace */
1429                                    InvalidOid,  /* relation oid (n/a here) */
1430                                    0,                   /* relation kind (ditto) */
1431                                    GetUserId(), /* owner's ID */
1432                                    -1,                  /* internal size (always varlena) */
1433                                    TYPTYPE_RANGE,               /* type-type (range type) */
1434                                    TYPCATEGORY_RANGE,   /* type-category (range type) */
1435                                    false,               /* range types are never preferred */
1436                                    DEFAULT_TYPDELIM,    /* array element delimiter */
1437                                    F_RANGE_IN,  /* input procedure */
1438                                    F_RANGE_OUT, /* output procedure */
1439                                    F_RANGE_RECV,        /* receive procedure */
1440                                    F_RANGE_SEND,        /* send procedure */
1441                                    InvalidOid,  /* typmodin procedure - none */
1442                                    InvalidOid,  /* typmodout procedure - none */
1443                                    F_RANGE_TYPANALYZE,  /* analyze procedure */
1444                                    InvalidOid,  /* element type ID - none */
1445                                    false,               /* this is not an array type */
1446                                    rangeArrayOid,               /* array type we are about to create */
1447                                    InvalidOid,  /* base type ID (only for domains) */
1448                                    NULL,                /* never a default type value */
1449                                    NULL,                /* no binary form available either */
1450                                    false,               /* never passed by value */
1451                                    alignment,   /* alignment */
1452                                    'x',                 /* TOAST strategy (always extended) */
1453                                    -1,                  /* typMod (Domains only) */
1454                                    0,                   /* Array dimensions of typbasetype */
1455                                    false,               /* Type NOT NULL */
1456                                    InvalidOid); /* type's collation (ranges never have one) */
1457
1458         /* Create the entry in pg_range */
1459         RangeCreate(typoid, rangeSubtype, rangeCollation, rangeSubOpclass,
1460                                 rangeCanonical, rangeSubtypeDiff);
1461
1462         /*
1463          * Create the array type that goes with it.
1464          */
1465         rangeArrayName = makeArrayTypeName(typeName, typeNamespace);
1466
1467         TypeCreate(rangeArrayOid,       /* force assignment of this type OID */
1468                            rangeArrayName,      /* type name */
1469                            typeNamespace,       /* namespace */
1470                            InvalidOid,          /* relation oid (n/a here) */
1471                            0,                           /* relation kind (ditto) */
1472                            GetUserId(),         /* owner's ID */
1473                            -1,                          /* internal size (always varlena) */
1474                            TYPTYPE_BASE,        /* type-type (base type) */
1475                            TYPCATEGORY_ARRAY,           /* type-category (array) */
1476                            false,                       /* array types are never preferred */
1477                            DEFAULT_TYPDELIM,    /* array element delimiter */
1478                            F_ARRAY_IN,          /* input procedure */
1479                            F_ARRAY_OUT,         /* output procedure */
1480                            F_ARRAY_RECV,        /* receive procedure */
1481                            F_ARRAY_SEND,        /* send procedure */
1482                            InvalidOid,          /* typmodin procedure - none */
1483                            InvalidOid,          /* typmodout procedure - none */
1484                            F_ARRAY_TYPANALYZE,          /* analyze procedure */
1485                            typoid,                      /* element type ID */
1486                            true,                        /* yes this is an array type */
1487                            InvalidOid,          /* no further array type */
1488                            InvalidOid,          /* base type ID */
1489                            NULL,                        /* never a default type value */
1490                            NULL,                        /* binary default isn't sent either */
1491                            false,                       /* never passed by value */
1492                            alignment,           /* alignment - same as range's */
1493                            'x',                         /* ARRAY is always toastable */
1494                            -1,                          /* typMod (Domains only) */
1495                            0,                           /* Array dimensions of typbasetype */
1496                            false,                       /* Type NOT NULL */
1497                            InvalidOid);         /* typcollation */
1498
1499         pfree(rangeArrayName);
1500
1501         /* And create the constructor functions for this range type */
1502         makeRangeConstructors(typeName, typeNamespace, typoid, rangeSubtype);
1503
1504         return typoid;
1505 }
1506
1507 /*
1508  * Because there may exist several range types over the same subtype, the
1509  * range type can't be uniquely determined from the subtype.  So it's
1510  * impossible to define a polymorphic constructor; we have to generate new
1511  * constructor functions explicitly for each range type.
1512  *
1513  * We actually define 4 functions, with 0 through 3 arguments.  This is just
1514  * to offer more convenience for the user.
1515  */
1516 static void
1517 makeRangeConstructors(const char *name, Oid namespace,
1518                                           Oid rangeOid, Oid subtype)
1519 {
1520         static const char *const prosrc[2] = {"range_constructor2",
1521         "range_constructor3"};
1522         static const int pronargs[2] = {2, 3};
1523
1524         Oid                     constructorArgTypes[3];
1525         ObjectAddress myself,
1526                                 referenced;
1527         int                     i;
1528
1529         constructorArgTypes[0] = subtype;
1530         constructorArgTypes[1] = subtype;
1531         constructorArgTypes[2] = TEXTOID;
1532
1533         referenced.classId = TypeRelationId;
1534         referenced.objectId = rangeOid;
1535         referenced.objectSubId = 0;
1536
1537         for (i = 0; i < lengthof(prosrc); i++)
1538         {
1539                 oidvector  *constructorArgTypesVector;
1540                 Oid                     procOid;
1541
1542                 constructorArgTypesVector = buildoidvector(constructorArgTypes,
1543                                                                                                    pronargs[i]);
1544
1545                 procOid = ProcedureCreate(name, /* name: same as range type */
1546                                                                   namespace,    /* namespace */
1547                                                                   false,                /* replace */
1548                                                                   false,                /* returns set */
1549                                                                   rangeOid,             /* return type */
1550                                                                   BOOTSTRAP_SUPERUSERID,                /* proowner */
1551                                                                   INTERNALlanguageId,   /* language */
1552                                                                   F_FMGR_INTERNAL_VALIDATOR,    /* language validator */
1553                                                                   prosrc[i],    /* prosrc */
1554                                                                   NULL, /* probin */
1555                                                                   false,                /* isAgg */
1556                                                                   false,                /* isWindowFunc */
1557                                                                   false,                /* security_definer */
1558                                                                   false,                /* leakproof */
1559                                                                   false,                /* isStrict */
1560                                                                   PROVOLATILE_IMMUTABLE,                /* volatility */
1561                                                                   constructorArgTypesVector,    /* parameterTypes */
1562                                                                   PointerGetDatum(NULL),                /* allParameterTypes */
1563                                                                   PointerGetDatum(NULL),                /* parameterModes */
1564                                                                   PointerGetDatum(NULL),                /* parameterNames */
1565                                                                   NIL,  /* parameterDefaults */
1566                                                                   PointerGetDatum(NULL),                /* proconfig */
1567                                                                   1.0,  /* procost */
1568                                                                   0.0); /* prorows */
1569
1570                 /*
1571                  * Make the constructors internally-dependent on the range type so
1572                  * that they go away silently when the type is dropped.  Note that
1573                  * pg_dump depends on this choice to avoid dumping the constructors.
1574                  */
1575                 myself.classId = ProcedureRelationId;
1576                 myself.objectId = procOid;
1577                 myself.objectSubId = 0;
1578
1579                 recordDependencyOn(&myself, &referenced, DEPENDENCY_INTERNAL);
1580         }
1581 }
1582
1583
1584 /*
1585  * Find suitable I/O functions for a type.
1586  *
1587  * typeOid is the type's OID (which will already exist, if only as a shell
1588  * type).
1589  */
1590
1591 static Oid
1592 findTypeInputFunction(List *procname, Oid typeOid)
1593 {
1594         Oid                     argList[3];
1595         Oid                     procOid;
1596
1597         /*
1598          * Input functions can take a single argument of type CSTRING, or three
1599          * arguments (string, typioparam OID, typmod).
1600          *
1601          * For backwards compatibility we allow OPAQUE in place of CSTRING; if we
1602          * see this, we issue a warning and fix up the pg_proc entry.
1603          */
1604         argList[0] = CSTRINGOID;
1605
1606         procOid = LookupFuncName(procname, 1, argList, true);
1607         if (OidIsValid(procOid))
1608                 return procOid;
1609
1610         argList[1] = OIDOID;
1611         argList[2] = INT4OID;
1612
1613         procOid = LookupFuncName(procname, 3, argList, true);
1614         if (OidIsValid(procOid))
1615                 return procOid;
1616
1617         /* No luck, try it with OPAQUE */
1618         argList[0] = OPAQUEOID;
1619
1620         procOid = LookupFuncName(procname, 1, argList, true);
1621
1622         if (!OidIsValid(procOid))
1623         {
1624                 argList[1] = OIDOID;
1625                 argList[2] = INT4OID;
1626
1627                 procOid = LookupFuncName(procname, 3, argList, true);
1628         }
1629
1630         if (OidIsValid(procOid))
1631         {
1632                 /* Found, but must complain and fix the pg_proc entry */
1633                 ereport(WARNING,
1634                                 (errmsg("changing argument type of function %s from \"opaque\" to \"cstring\"",
1635                                                 NameListToString(procname))));
1636                 SetFunctionArgType(procOid, 0, CSTRINGOID);
1637
1638                 /*
1639                  * Need CommandCounterIncrement since DefineType will likely try to
1640                  * alter the pg_proc tuple again.
1641                  */
1642                 CommandCounterIncrement();
1643
1644                 return procOid;
1645         }
1646
1647         /* Use CSTRING (preferred) in the error message */
1648         argList[0] = CSTRINGOID;
1649
1650         ereport(ERROR,
1651                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
1652                          errmsg("function %s does not exist",
1653                                         func_signature_string(procname, 1, NIL, argList))));
1654
1655         return InvalidOid;                      /* keep compiler quiet */
1656 }
1657
1658 static Oid
1659 findTypeOutputFunction(List *procname, Oid typeOid)
1660 {
1661         Oid                     argList[1];
1662         Oid                     procOid;
1663
1664         /*
1665          * Output functions can take a single argument of the type.
1666          *
1667          * For backwards compatibility we allow OPAQUE in place of the actual type
1668          * name; if we see this, we issue a warning and fix up the pg_proc entry.
1669          */
1670         argList[0] = typeOid;
1671
1672         procOid = LookupFuncName(procname, 1, argList, true);
1673         if (OidIsValid(procOid))
1674                 return procOid;
1675
1676         /* No luck, try it with OPAQUE */
1677         argList[0] = OPAQUEOID;
1678
1679         procOid = LookupFuncName(procname, 1, argList, true);
1680
1681         if (OidIsValid(procOid))
1682         {
1683                 /* Found, but must complain and fix the pg_proc entry */
1684                 ereport(WARNING,
1685                 (errmsg("changing argument type of function %s from \"opaque\" to %s",
1686                                 NameListToString(procname), format_type_be(typeOid))));
1687                 SetFunctionArgType(procOid, 0, typeOid);
1688
1689                 /*
1690                  * Need CommandCounterIncrement since DefineType will likely try to
1691                  * alter the pg_proc tuple again.
1692                  */
1693                 CommandCounterIncrement();
1694
1695                 return procOid;
1696         }
1697
1698         /* Use type name, not OPAQUE, in the failure message. */
1699         argList[0] = typeOid;
1700
1701         ereport(ERROR,
1702                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
1703                          errmsg("function %s does not exist",
1704                                         func_signature_string(procname, 1, NIL, argList))));
1705
1706         return InvalidOid;                      /* keep compiler quiet */
1707 }
1708
1709 static Oid
1710 findTypeReceiveFunction(List *procname, Oid typeOid)
1711 {
1712         Oid                     argList[3];
1713         Oid                     procOid;
1714
1715         /*
1716          * Receive functions can take a single argument of type INTERNAL, or three
1717          * arguments (internal, typioparam OID, typmod).
1718          */
1719         argList[0] = INTERNALOID;
1720
1721         procOid = LookupFuncName(procname, 1, argList, true);
1722         if (OidIsValid(procOid))
1723                 return procOid;
1724
1725         argList[1] = OIDOID;
1726         argList[2] = INT4OID;
1727
1728         procOid = LookupFuncName(procname, 3, argList, true);
1729         if (OidIsValid(procOid))
1730                 return procOid;
1731
1732         ereport(ERROR,
1733                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
1734                          errmsg("function %s does not exist",
1735                                         func_signature_string(procname, 1, NIL, argList))));
1736
1737         return InvalidOid;                      /* keep compiler quiet */
1738 }
1739
1740 static Oid
1741 findTypeSendFunction(List *procname, Oid typeOid)
1742 {
1743         Oid                     argList[1];
1744         Oid                     procOid;
1745
1746         /*
1747          * Send functions can take a single argument of the type.
1748          */
1749         argList[0] = typeOid;
1750
1751         procOid = LookupFuncName(procname, 1, argList, true);
1752         if (OidIsValid(procOid))
1753                 return procOid;
1754
1755         ereport(ERROR,
1756                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
1757                          errmsg("function %s does not exist",
1758                                         func_signature_string(procname, 1, NIL, argList))));
1759
1760         return InvalidOid;                      /* keep compiler quiet */
1761 }
1762
1763 static Oid
1764 findTypeTypmodinFunction(List *procname)
1765 {
1766         Oid                     argList[1];
1767         Oid                     procOid;
1768
1769         /*
1770          * typmodin functions always take one cstring[] argument and return int4.
1771          */
1772         argList[0] = CSTRINGARRAYOID;
1773
1774         procOid = LookupFuncName(procname, 1, argList, true);
1775         if (!OidIsValid(procOid))
1776                 ereport(ERROR,
1777                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1778                                  errmsg("function %s does not exist",
1779                                                 func_signature_string(procname, 1, NIL, argList))));
1780
1781         if (get_func_rettype(procOid) != INT4OID)
1782                 ereport(ERROR,
1783                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1784                                  errmsg("typmod_in function %s must return type \"integer\"",
1785                                                 NameListToString(procname))));
1786
1787         return procOid;
1788 }
1789
1790 static Oid
1791 findTypeTypmodoutFunction(List *procname)
1792 {
1793         Oid                     argList[1];
1794         Oid                     procOid;
1795
1796         /*
1797          * typmodout functions always take one int4 argument and return cstring.
1798          */
1799         argList[0] = INT4OID;
1800
1801         procOid = LookupFuncName(procname, 1, argList, true);
1802         if (!OidIsValid(procOid))
1803                 ereport(ERROR,
1804                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1805                                  errmsg("function %s does not exist",
1806                                                 func_signature_string(procname, 1, NIL, argList))));
1807
1808         if (get_func_rettype(procOid) != CSTRINGOID)
1809                 ereport(ERROR,
1810                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1811                                  errmsg("typmod_out function %s must return type \"cstring\"",
1812                                                 NameListToString(procname))));
1813
1814         return procOid;
1815 }
1816
1817 static Oid
1818 findTypeAnalyzeFunction(List *procname, Oid typeOid)
1819 {
1820         Oid                     argList[1];
1821         Oid                     procOid;
1822
1823         /*
1824          * Analyze functions always take one INTERNAL argument and return bool.
1825          */
1826         argList[0] = INTERNALOID;
1827
1828         procOid = LookupFuncName(procname, 1, argList, true);
1829         if (!OidIsValid(procOid))
1830                 ereport(ERROR,
1831                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1832                                  errmsg("function %s does not exist",
1833                                                 func_signature_string(procname, 1, NIL, argList))));
1834
1835         if (get_func_rettype(procOid) != BOOLOID)
1836                 ereport(ERROR,
1837                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1838                           errmsg("type analyze function %s must return type \"boolean\"",
1839                                          NameListToString(procname))));
1840
1841         return procOid;
1842 }
1843
1844 /*
1845  * Find suitable support functions and opclasses for a range type.
1846  */
1847
1848 /*
1849  * Find named btree opclass for subtype, or default btree opclass if
1850  * opcname is NIL.
1851  */
1852 static Oid
1853 findRangeSubOpclass(List *opcname, Oid subtype)
1854 {
1855         Oid                     opcid;
1856         Oid                     opInputType;
1857
1858         if (opcname != NIL)
1859         {
1860                 opcid = get_opclass_oid(BTREE_AM_OID, opcname, false);
1861
1862                 /*
1863                  * Verify that the operator class accepts this datatype. Note we will
1864                  * accept binary compatibility.
1865                  */
1866                 opInputType = get_opclass_input_type(opcid);
1867                 if (!IsBinaryCoercible(subtype, opInputType))
1868                         ereport(ERROR,
1869                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1870                                  errmsg("operator class \"%s\" does not accept data type %s",
1871                                                 NameListToString(opcname),
1872                                                 format_type_be(subtype))));
1873         }
1874         else
1875         {
1876                 opcid = GetDefaultOpClass(subtype, BTREE_AM_OID);
1877                 if (!OidIsValid(opcid))
1878                 {
1879                         /* We spell the error message identically to GetIndexOpClass */
1880                         ereport(ERROR,
1881                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
1882                                          errmsg("data type %s has no default operator class for access method \"%s\"",
1883                                                         format_type_be(subtype), "btree"),
1884                                          errhint("You must specify an operator class for the range type or define a default operator class for the subtype.")));
1885                 }
1886         }
1887
1888         return opcid;
1889 }
1890
1891 static Oid
1892 findRangeCanonicalFunction(List *procname, Oid typeOid)
1893 {
1894         Oid                     argList[1];
1895         Oid                     procOid;
1896         AclResult       aclresult;
1897
1898         /*
1899          * Range canonical functions must take and return the range type, and must
1900          * be immutable.
1901          */
1902         argList[0] = typeOid;
1903
1904         procOid = LookupFuncName(procname, 1, argList, true);
1905
1906         if (!OidIsValid(procOid))
1907                 ereport(ERROR,
1908                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1909                                  errmsg("function %s does not exist",
1910                                                 func_signature_string(procname, 1, NIL, argList))));
1911
1912         if (get_func_rettype(procOid) != typeOid)
1913                 ereport(ERROR,
1914                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1915                                  errmsg("range canonical function %s must return range type",
1916                                                 func_signature_string(procname, 1, NIL, argList))));
1917
1918         if (func_volatile(procOid) != PROVOLATILE_IMMUTABLE)
1919                 ereport(ERROR,
1920                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1921                                  errmsg("range canonical function %s must be immutable",
1922                                                 func_signature_string(procname, 1, NIL, argList))));
1923
1924         /* Also, range type's creator must have permission to call function */
1925         aclresult = pg_proc_aclcheck(procOid, GetUserId(), ACL_EXECUTE);
1926         if (aclresult != ACLCHECK_OK)
1927                 aclcheck_error(aclresult, ACL_KIND_PROC, get_func_name(procOid));
1928
1929         return procOid;
1930 }
1931
1932 static Oid
1933 findRangeSubtypeDiffFunction(List *procname, Oid subtype)
1934 {
1935         Oid                     argList[2];
1936         Oid                     procOid;
1937         AclResult       aclresult;
1938
1939         /*
1940          * Range subtype diff functions must take two arguments of the subtype,
1941          * must return float8, and must be immutable.
1942          */
1943         argList[0] = subtype;
1944         argList[1] = subtype;
1945
1946         procOid = LookupFuncName(procname, 2, argList, true);
1947
1948         if (!OidIsValid(procOid))
1949                 ereport(ERROR,
1950                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1951                                  errmsg("function %s does not exist",
1952                                                 func_signature_string(procname, 2, NIL, argList))));
1953
1954         if (get_func_rettype(procOid) != FLOAT8OID)
1955                 ereport(ERROR,
1956                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1957                                  errmsg("range subtype diff function %s must return type double precision",
1958                                                 func_signature_string(procname, 2, NIL, argList))));
1959
1960         if (func_volatile(procOid) != PROVOLATILE_IMMUTABLE)
1961                 ereport(ERROR,
1962                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1963                                  errmsg("range subtype diff function %s must be immutable",
1964                                                 func_signature_string(procname, 2, NIL, argList))));
1965
1966         /* Also, range type's creator must have permission to call function */
1967         aclresult = pg_proc_aclcheck(procOid, GetUserId(), ACL_EXECUTE);
1968         if (aclresult != ACLCHECK_OK)
1969                 aclcheck_error(aclresult, ACL_KIND_PROC, get_func_name(procOid));
1970
1971         return procOid;
1972 }
1973
1974 /*
1975  *      AssignTypeArrayOid
1976  *
1977  *      Pre-assign the type's array OID for use in pg_type.typarray
1978  */
1979 Oid
1980 AssignTypeArrayOid(void)
1981 {
1982         Oid                     type_array_oid;
1983
1984         /* Use binary-upgrade override for pg_type.typarray, if supplied. */
1985         if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_array_pg_type_oid))
1986         {
1987                 type_array_oid = binary_upgrade_next_array_pg_type_oid;
1988                 binary_upgrade_next_array_pg_type_oid = InvalidOid;
1989         }
1990         else
1991         {
1992                 Relation        pg_type = heap_open(TypeRelationId, AccessShareLock);
1993
1994                 type_array_oid = GetNewOid(pg_type);
1995                 heap_close(pg_type, AccessShareLock);
1996         }
1997
1998         return type_array_oid;
1999 }
2000
2001
2002 /*-------------------------------------------------------------------
2003  * DefineCompositeType
2004  *
2005  * Create a Composite Type relation.
2006  * `DefineRelation' does all the work, we just provide the correct
2007  * arguments!
2008  *
2009  * If the relation already exists, then 'DefineRelation' will abort
2010  * the xact...
2011  *
2012  * DefineCompositeType returns relid for use when creating
2013  * an implicit composite type during function creation
2014  *-------------------------------------------------------------------
2015  */
2016 Oid
2017 DefineCompositeType(RangeVar *typevar, List *coldeflist)
2018 {
2019         CreateStmt *createStmt = makeNode(CreateStmt);
2020         Oid                     old_type_oid;
2021         Oid                     typeNamespace;
2022         Oid                     relid;
2023
2024         /*
2025          * now set the parameters for keys/inheritance etc. All of these are
2026          * uninteresting for composite types...
2027          */
2028         createStmt->relation = typevar;
2029         createStmt->tableElts = coldeflist;
2030         createStmt->inhRelations = NIL;
2031         createStmt->constraints = NIL;
2032         createStmt->options = list_make1(defWithOids(false));
2033         createStmt->oncommit = ONCOMMIT_NOOP;
2034         createStmt->tablespacename = NULL;
2035         createStmt->if_not_exists = false;
2036
2037         /*
2038          * Check for collision with an existing type name. If there is one and
2039          * it's an autogenerated array, we can rename it out of the way.  This
2040          * check is here mainly to get a better error message about a "type"
2041          * instead of below about a "relation".
2042          */
2043         typeNamespace = RangeVarGetAndCheckCreationNamespace(createStmt->relation,
2044                                                                                                                  NoLock, NULL);
2045         RangeVarAdjustRelationPersistence(createStmt->relation, typeNamespace);
2046         old_type_oid =
2047                 GetSysCacheOid2(TYPENAMENSP,
2048                                                 CStringGetDatum(createStmt->relation->relname),
2049                                                 ObjectIdGetDatum(typeNamespace));
2050         if (OidIsValid(old_type_oid))
2051         {
2052                 if (!moveArrayTypeName(old_type_oid, createStmt->relation->relname, typeNamespace))
2053                         ereport(ERROR,
2054                                         (errcode(ERRCODE_DUPLICATE_OBJECT),
2055                                          errmsg("type \"%s\" already exists", createStmt->relation->relname)));
2056         }
2057
2058         /*
2059          * Finally create the relation.  This also creates the type.
2060          */
2061         relid = DefineRelation(createStmt, RELKIND_COMPOSITE_TYPE, InvalidOid);
2062         Assert(relid != InvalidOid);
2063         return relid;
2064 }
2065
2066 /*
2067  * AlterDomainDefault
2068  *
2069  * Routine implementing ALTER DOMAIN SET/DROP DEFAULT statements.
2070  */
2071 Oid
2072 AlterDomainDefault(List *names, Node *defaultRaw)
2073 {
2074         TypeName   *typename;
2075         Oid                     domainoid;
2076         HeapTuple       tup;
2077         ParseState *pstate;
2078         Relation        rel;
2079         char       *defaultValue;
2080         Node       *defaultExpr = NULL;         /* NULL if no default specified */
2081         Datum           new_record[Natts_pg_type];
2082         bool            new_record_nulls[Natts_pg_type];
2083         bool            new_record_repl[Natts_pg_type];
2084         HeapTuple       newtuple;
2085         Form_pg_type typTup;
2086
2087         /* Make a TypeName so we can use standard type lookup machinery */
2088         typename = makeTypeNameFromNameList(names);
2089         domainoid = typenameTypeId(NULL, typename);
2090
2091         /* Look up the domain in the type table */
2092         rel = heap_open(TypeRelationId, RowExclusiveLock);
2093
2094         tup = SearchSysCacheCopy1(TYPEOID, ObjectIdGetDatum(domainoid));
2095         if (!HeapTupleIsValid(tup))
2096                 elog(ERROR, "cache lookup failed for type %u", domainoid);
2097         typTup = (Form_pg_type) GETSTRUCT(tup);
2098
2099         /* Check it's a domain and check user has permission for ALTER DOMAIN */
2100         checkDomainOwner(tup);
2101
2102         /* Setup new tuple */
2103         MemSet(new_record, (Datum) 0, sizeof(new_record));
2104         MemSet(new_record_nulls, false, sizeof(new_record_nulls));
2105         MemSet(new_record_repl, false, sizeof(new_record_repl));
2106
2107         /* Store the new default into the tuple */
2108         if (defaultRaw)
2109         {
2110                 /* Create a dummy ParseState for transformExpr */
2111                 pstate = make_parsestate(NULL);
2112
2113                 /*
2114                  * Cook the colDef->raw_expr into an expression. Note: Name is
2115                  * strictly for error message
2116                  */
2117                 defaultExpr = cookDefault(pstate, defaultRaw,
2118                                                                   typTup->typbasetype,
2119                                                                   typTup->typtypmod,
2120                                                                   NameStr(typTup->typname));
2121
2122                 /*
2123                  * If the expression is just a NULL constant, we treat the command
2124                  * like ALTER ... DROP DEFAULT.  (But see note for same test in
2125                  * DefineDomain.)
2126                  */
2127                 if (defaultExpr == NULL ||
2128                         (IsA(defaultExpr, Const) &&((Const *) defaultExpr)->constisnull))
2129                 {
2130                         /* Default is NULL, drop it */
2131                         new_record_nulls[Anum_pg_type_typdefaultbin - 1] = true;
2132                         new_record_repl[Anum_pg_type_typdefaultbin - 1] = true;
2133                         new_record_nulls[Anum_pg_type_typdefault - 1] = true;
2134                         new_record_repl[Anum_pg_type_typdefault - 1] = true;
2135                 }
2136                 else
2137                 {
2138                         /*
2139                          * Expression must be stored as a nodeToString result, but we also
2140                          * require a valid textual representation (mainly to make life
2141                          * easier for pg_dump).
2142                          */
2143                         defaultValue = deparse_expression(defaultExpr,
2144                                                                                           NIL, false, false);
2145
2146                         /*
2147                          * Form an updated tuple with the new default and write it back.
2148                          */
2149                         new_record[Anum_pg_type_typdefaultbin - 1] = CStringGetTextDatum(nodeToString(defaultExpr));
2150
2151                         new_record_repl[Anum_pg_type_typdefaultbin - 1] = true;
2152                         new_record[Anum_pg_type_typdefault - 1] = CStringGetTextDatum(defaultValue);
2153                         new_record_repl[Anum_pg_type_typdefault - 1] = true;
2154                 }
2155         }
2156         else
2157         {
2158                 /* ALTER ... DROP DEFAULT */
2159                 new_record_nulls[Anum_pg_type_typdefaultbin - 1] = true;
2160                 new_record_repl[Anum_pg_type_typdefaultbin - 1] = true;
2161                 new_record_nulls[Anum_pg_type_typdefault - 1] = true;
2162                 new_record_repl[Anum_pg_type_typdefault - 1] = true;
2163         }
2164
2165         newtuple = heap_modify_tuple(tup, RelationGetDescr(rel),
2166                                                                  new_record, new_record_nulls,
2167                                                                  new_record_repl);
2168
2169         simple_heap_update(rel, &tup->t_self, newtuple);
2170
2171         CatalogUpdateIndexes(rel, newtuple);
2172
2173         /* Rebuild dependencies */
2174         GenerateTypeDependencies(typTup->typnamespace,
2175                                                          domainoid,
2176                                                          InvalidOid,            /* typrelid is n/a */
2177                                                          0, /* relation kind is n/a */
2178                                                          typTup->typowner,
2179                                                          typTup->typinput,
2180                                                          typTup->typoutput,
2181                                                          typTup->typreceive,
2182                                                          typTup->typsend,
2183                                                          typTup->typmodin,
2184                                                          typTup->typmodout,
2185                                                          typTup->typanalyze,
2186                                                          InvalidOid,
2187                                                          false,         /* a domain isn't an implicit array */
2188                                                          typTup->typbasetype,
2189                                                          typTup->typcollation,
2190                                                          defaultExpr,
2191                                                          true);         /* Rebuild is true */
2192
2193         /* Clean up */
2194         heap_close(rel, NoLock);
2195         heap_freetuple(newtuple);
2196
2197         return domainoid;
2198 }
2199
2200 /*
2201  * AlterDomainNotNull
2202  *
2203  * Routine implementing ALTER DOMAIN SET/DROP NOT NULL statements.
2204  */
2205 Oid
2206 AlterDomainNotNull(List *names, bool notNull)
2207 {
2208         TypeName   *typename;
2209         Oid                     domainoid;
2210         Relation        typrel;
2211         HeapTuple       tup;
2212         Form_pg_type typTup;
2213
2214         /* Make a TypeName so we can use standard type lookup machinery */
2215         typename = makeTypeNameFromNameList(names);
2216         domainoid = typenameTypeId(NULL, typename);
2217
2218         /* Look up the domain in the type table */
2219         typrel = heap_open(TypeRelationId, RowExclusiveLock);
2220
2221         tup = SearchSysCacheCopy1(TYPEOID, ObjectIdGetDatum(domainoid));
2222         if (!HeapTupleIsValid(tup))
2223                 elog(ERROR, "cache lookup failed for type %u", domainoid);
2224         typTup = (Form_pg_type) GETSTRUCT(tup);
2225
2226         /* Check it's a domain and check user has permission for ALTER DOMAIN */
2227         checkDomainOwner(tup);
2228
2229         /* Is the domain already set to the desired constraint? */
2230         if (typTup->typnotnull == notNull)
2231         {
2232                 heap_close(typrel, RowExclusiveLock);
2233                 return InvalidOid;
2234         }
2235
2236         /* Adding a NOT NULL constraint requires checking existing columns */
2237         if (notNull)
2238         {
2239                 List       *rels;
2240                 ListCell   *rt;
2241
2242                 /* Fetch relation list with attributes based on this domain */
2243                 /* ShareLock is sufficient to prevent concurrent data changes */
2244
2245                 rels = get_rels_with_domain(domainoid, ShareLock);
2246
2247                 foreach(rt, rels)
2248                 {
2249                         RelToCheck *rtc = (RelToCheck *) lfirst(rt);
2250                         Relation        testrel = rtc->rel;
2251                         TupleDesc       tupdesc = RelationGetDescr(testrel);
2252                         HeapScanDesc scan;
2253                         HeapTuple       tuple;
2254
2255                         /* Scan all tuples in this relation */
2256                         scan = heap_beginscan(testrel, SnapshotNow, 0, NULL);
2257                         while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
2258                         {
2259                                 int                     i;
2260
2261                                 /* Test attributes that are of the domain */
2262                                 for (i = 0; i < rtc->natts; i++)
2263                                 {
2264                                         int                     attnum = rtc->atts[i];
2265
2266                                         if (heap_attisnull(tuple, attnum))
2267                                                 ereport(ERROR,
2268                                                                 (errcode(ERRCODE_NOT_NULL_VIOLATION),
2269                                                                  errmsg("column \"%s\" of table \"%s\" contains null values",
2270                                                                 NameStr(tupdesc->attrs[attnum - 1]->attname),
2271                                                                                 RelationGetRelationName(testrel))));
2272                                 }
2273                         }
2274                         heap_endscan(scan);
2275
2276                         /* Close each rel after processing, but keep lock */
2277                         heap_close(testrel, NoLock);
2278                 }
2279         }
2280
2281         /*
2282          * Okay to update pg_type row.  We can scribble on typTup because it's a
2283          * copy.
2284          */
2285         typTup->typnotnull = notNull;
2286
2287         simple_heap_update(typrel, &tup->t_self, tup);
2288
2289         CatalogUpdateIndexes(typrel, tup);
2290
2291         /* Clean up */
2292         heap_freetuple(tup);
2293         heap_close(typrel, RowExclusiveLock);
2294
2295         return domainoid;
2296 }
2297
2298 /*
2299  * AlterDomainDropConstraint
2300  *
2301  * Implements the ALTER DOMAIN DROP CONSTRAINT statement
2302  */
2303 Oid
2304 AlterDomainDropConstraint(List *names, const char *constrName,
2305                                                   DropBehavior behavior, bool missing_ok)
2306 {
2307         TypeName   *typename;
2308         Oid                     domainoid;
2309         HeapTuple       tup;
2310         Relation        rel;
2311         Relation        conrel;
2312         SysScanDesc conscan;
2313         ScanKeyData key[1];
2314         HeapTuple       contup;
2315         bool            found = false;
2316
2317         /* Make a TypeName so we can use standard type lookup machinery */
2318         typename = makeTypeNameFromNameList(names);
2319         domainoid = typenameTypeId(NULL, typename);
2320
2321         /* Look up the domain in the type table */
2322         rel = heap_open(TypeRelationId, RowExclusiveLock);
2323
2324         tup = SearchSysCacheCopy1(TYPEOID, ObjectIdGetDatum(domainoid));
2325         if (!HeapTupleIsValid(tup))
2326                 elog(ERROR, "cache lookup failed for type %u", domainoid);
2327
2328         /* Check it's a domain and check user has permission for ALTER DOMAIN */
2329         checkDomainOwner(tup);
2330
2331         /* Grab an appropriate lock on the pg_constraint relation */
2332         conrel = heap_open(ConstraintRelationId, RowExclusiveLock);
2333
2334         /* Use the index to scan only constraints of the target relation */
2335         ScanKeyInit(&key[0],
2336                                 Anum_pg_constraint_contypid,
2337                                 BTEqualStrategyNumber, F_OIDEQ,
2338                                 ObjectIdGetDatum(HeapTupleGetOid(tup)));
2339
2340         conscan = systable_beginscan(conrel, ConstraintTypidIndexId, true,
2341                                                                  SnapshotNow, 1, key);
2342
2343         /*
2344          * Scan over the result set, removing any matching entries.
2345          */
2346         while ((contup = systable_getnext(conscan)) != NULL)
2347         {
2348                 Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(contup);
2349
2350                 if (strcmp(NameStr(con->conname), constrName) == 0)
2351                 {
2352                         ObjectAddress conobj;
2353
2354                         conobj.classId = ConstraintRelationId;
2355                         conobj.objectId = HeapTupleGetOid(contup);
2356                         conobj.objectSubId = 0;
2357
2358                         performDeletion(&conobj, behavior, 0);
2359                         found = true;
2360                 }
2361         }
2362         /* Clean up after the scan */
2363         systable_endscan(conscan);
2364         heap_close(conrel, RowExclusiveLock);
2365
2366         heap_close(rel, NoLock);
2367
2368         if (!found)
2369         {
2370                 if (!missing_ok)
2371                         ereport(ERROR,
2372                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
2373                                   errmsg("constraint \"%s\" of domain \"%s\" does not exist",
2374                                                  constrName, TypeNameToString(typename))));
2375                 else
2376                         ereport(NOTICE,
2377                                         (errmsg("constraint \"%s\" of domain \"%s\" does not exist, skipping",
2378                                                         constrName, TypeNameToString(typename))));
2379         }
2380
2381         return domainoid;
2382 }
2383
2384 /*
2385  * AlterDomainAddConstraint
2386  *
2387  * Implements the ALTER DOMAIN .. ADD CONSTRAINT statement.
2388  */
2389 Oid
2390 AlterDomainAddConstraint(List *names, Node *newConstraint)
2391 {
2392         TypeName   *typename;
2393         Oid                     domainoid;
2394         Relation        typrel;
2395         HeapTuple       tup;
2396         Form_pg_type typTup;
2397         Constraint *constr;
2398         char       *ccbin;
2399
2400         /* Make a TypeName so we can use standard type lookup machinery */
2401         typename = makeTypeNameFromNameList(names);
2402         domainoid = typenameTypeId(NULL, typename);
2403
2404         /* Look up the domain in the type table */
2405         typrel = heap_open(TypeRelationId, RowExclusiveLock);
2406
2407         tup = SearchSysCacheCopy1(TYPEOID, ObjectIdGetDatum(domainoid));
2408         if (!HeapTupleIsValid(tup))
2409                 elog(ERROR, "cache lookup failed for type %u", domainoid);
2410         typTup = (Form_pg_type) GETSTRUCT(tup);
2411
2412         /* Check it's a domain and check user has permission for ALTER DOMAIN */
2413         checkDomainOwner(tup);
2414
2415         if (!IsA(newConstraint, Constraint))
2416                 elog(ERROR, "unrecognized node type: %d",
2417                          (int) nodeTag(newConstraint));
2418
2419         constr = (Constraint *) newConstraint;
2420
2421         switch (constr->contype)
2422         {
2423                 case CONSTR_CHECK:
2424                         /* processed below */
2425                         break;
2426
2427                 case CONSTR_UNIQUE:
2428                         ereport(ERROR,
2429                                         (errcode(ERRCODE_SYNTAX_ERROR),
2430                                          errmsg("unique constraints not possible for domains")));
2431                         break;
2432
2433                 case CONSTR_PRIMARY:
2434                         ereport(ERROR,
2435                                         (errcode(ERRCODE_SYNTAX_ERROR),
2436                                 errmsg("primary key constraints not possible for domains")));
2437                         break;
2438
2439                 case CONSTR_EXCLUSION:
2440                         ereport(ERROR,
2441                                         (errcode(ERRCODE_SYNTAX_ERROR),
2442                                   errmsg("exclusion constraints not possible for domains")));
2443                         break;
2444
2445                 case CONSTR_FOREIGN:
2446                         ereport(ERROR,
2447                                         (errcode(ERRCODE_SYNTAX_ERROR),
2448                                 errmsg("foreign key constraints not possible for domains")));
2449                         break;
2450
2451                 case CONSTR_ATTR_DEFERRABLE:
2452                 case CONSTR_ATTR_NOT_DEFERRABLE:
2453                 case CONSTR_ATTR_DEFERRED:
2454                 case CONSTR_ATTR_IMMEDIATE:
2455                         ereport(ERROR,
2456                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2457                                          errmsg("specifying constraint deferrability not supported for domains")));
2458                         break;
2459
2460                 default:
2461                         elog(ERROR, "unrecognized constraint subtype: %d",
2462                                  (int) constr->contype);
2463                         break;
2464         }
2465
2466         /*
2467          * Since all other constraint types throw errors, this must be a check
2468          * constraint.  First, process the constraint expression and add an entry
2469          * to pg_constraint.
2470          */
2471
2472         ccbin = domainAddConstraint(HeapTupleGetOid(tup), typTup->typnamespace,
2473                                                                 typTup->typbasetype, typTup->typtypmod,
2474                                                                 constr, NameStr(typTup->typname));
2475
2476         /*
2477          * If requested to validate the constraint, test all values stored in the
2478          * attributes based on the domain the constraint is being added to.
2479          */
2480         if (!constr->skip_validation)
2481                 validateDomainConstraint(domainoid, ccbin);
2482
2483         /* Clean up */
2484         heap_close(typrel, RowExclusiveLock);
2485
2486         return domainoid;
2487 }
2488
2489 /*
2490  * AlterDomainValidateConstraint
2491  *
2492  * Implements the ALTER DOMAIN .. VALIDATE CONSTRAINT statement.
2493  */
2494 Oid
2495 AlterDomainValidateConstraint(List *names, char *constrName)
2496 {
2497         TypeName   *typename;
2498         Oid                     domainoid;
2499         Relation        typrel;
2500         Relation        conrel;
2501         HeapTuple       tup;
2502         Form_pg_constraint con = NULL;
2503         Form_pg_constraint copy_con;
2504         char       *conbin;
2505         SysScanDesc scan;
2506         Datum           val;
2507         bool            found = false;
2508         bool            isnull;
2509         HeapTuple       tuple;
2510         HeapTuple       copyTuple;
2511         ScanKeyData key;
2512
2513         /* Make a TypeName so we can use standard type lookup machinery */
2514         typename = makeTypeNameFromNameList(names);
2515         domainoid = typenameTypeId(NULL, typename);
2516
2517         /* Look up the domain in the type table */
2518         typrel = heap_open(TypeRelationId, AccessShareLock);
2519
2520         tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(domainoid));
2521         if (!HeapTupleIsValid(tup))
2522                 elog(ERROR, "cache lookup failed for type %u", domainoid);
2523
2524         /* Check it's a domain and check user has permission for ALTER DOMAIN */
2525         checkDomainOwner(tup);
2526
2527         /*
2528          * Find and check the target constraint
2529          */
2530         conrel = heap_open(ConstraintRelationId, RowExclusiveLock);
2531         ScanKeyInit(&key,
2532                                 Anum_pg_constraint_contypid,
2533                                 BTEqualStrategyNumber, F_OIDEQ,
2534                                 ObjectIdGetDatum(domainoid));
2535         scan = systable_beginscan(conrel, ConstraintTypidIndexId,
2536                                                           true, SnapshotNow, 1, &key);
2537
2538         while (HeapTupleIsValid(tuple = systable_getnext(scan)))
2539         {
2540                 con = (Form_pg_constraint) GETSTRUCT(tuple);
2541                 if (strcmp(NameStr(con->conname), constrName) == 0)
2542                 {
2543                         found = true;
2544                         break;
2545                 }
2546         }
2547
2548         if (!found)
2549                 ereport(ERROR,
2550                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
2551                                  errmsg("constraint \"%s\" of domain \"%s\" does not exist",
2552                                                 constrName, TypeNameToString(typename))));
2553
2554         if (con->contype != CONSTRAINT_CHECK)
2555                 ereport(ERROR,
2556                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2557                 errmsg("constraint \"%s\" of domain \"%s\" is not a check constraint",
2558                            constrName, TypeNameToString(typename))));
2559
2560         val = SysCacheGetAttr(CONSTROID, tuple,
2561                                                   Anum_pg_constraint_conbin,
2562                                                   &isnull);
2563         if (isnull)
2564                 elog(ERROR, "null conbin for constraint %u",
2565                          HeapTupleGetOid(tuple));
2566         conbin = TextDatumGetCString(val);
2567
2568         validateDomainConstraint(domainoid, conbin);
2569
2570         /*
2571          * Now update the catalog, while we have the door open.
2572          */
2573         copyTuple = heap_copytuple(tuple);
2574         copy_con = (Form_pg_constraint) GETSTRUCT(copyTuple);
2575         copy_con->convalidated = true;
2576         simple_heap_update(conrel, &copyTuple->t_self, copyTuple);
2577         CatalogUpdateIndexes(conrel, copyTuple);
2578         heap_freetuple(copyTuple);
2579
2580         systable_endscan(scan);
2581
2582         heap_close(typrel, AccessShareLock);
2583         heap_close(conrel, RowExclusiveLock);
2584
2585         ReleaseSysCache(tup);
2586
2587         return domainoid;
2588 }
2589
2590 static void
2591 validateDomainConstraint(Oid domainoid, char *ccbin)
2592 {
2593         Expr       *expr = (Expr *) stringToNode(ccbin);
2594         List       *rels;
2595         ListCell   *rt;
2596         EState     *estate;
2597         ExprContext *econtext;
2598         ExprState  *exprstate;
2599
2600         /* Need an EState to run ExecEvalExpr */
2601         estate = CreateExecutorState();
2602         econtext = GetPerTupleExprContext(estate);
2603
2604         /* build execution state for expr */
2605         exprstate = ExecPrepareExpr(expr, estate);
2606
2607         /* Fetch relation list with attributes based on this domain */
2608         /* ShareLock is sufficient to prevent concurrent data changes */
2609
2610         rels = get_rels_with_domain(domainoid, ShareLock);
2611
2612         foreach(rt, rels)
2613         {
2614                 RelToCheck *rtc = (RelToCheck *) lfirst(rt);
2615                 Relation        testrel = rtc->rel;
2616                 TupleDesc       tupdesc = RelationGetDescr(testrel);
2617                 HeapScanDesc scan;
2618                 HeapTuple       tuple;
2619
2620                 /* Scan all tuples in this relation */
2621                 scan = heap_beginscan(testrel, SnapshotNow, 0, NULL);
2622                 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
2623                 {
2624                         int                     i;
2625
2626                         /* Test attributes that are of the domain */
2627                         for (i = 0; i < rtc->natts; i++)
2628                         {
2629                                 int                     attnum = rtc->atts[i];
2630                                 Datum           d;
2631                                 bool            isNull;
2632                                 Datum           conResult;
2633
2634                                 d = heap_getattr(tuple, attnum, tupdesc, &isNull);
2635
2636                                 econtext->domainValue_datum = d;
2637                                 econtext->domainValue_isNull = isNull;
2638
2639                                 conResult = ExecEvalExprSwitchContext(exprstate,
2640                                                                                                           econtext,
2641                                                                                                           &isNull, NULL);
2642
2643                                 if (!isNull && !DatumGetBool(conResult))
2644                                         ereport(ERROR,
2645                                                         (errcode(ERRCODE_CHECK_VIOLATION),
2646                                                          errmsg("column \"%s\" of table \"%s\" contains values that violate the new constraint",
2647                                                                 NameStr(tupdesc->attrs[attnum - 1]->attname),
2648                                                                         RelationGetRelationName(testrel))));
2649                         }
2650
2651                         ResetExprContext(econtext);
2652                 }
2653                 heap_endscan(scan);
2654
2655                 /* Hold relation lock till commit (XXX bad for concurrency) */
2656                 heap_close(testrel, NoLock);
2657         }
2658
2659         FreeExecutorState(estate);
2660 }
2661
2662 /*
2663  * get_rels_with_domain
2664  *
2665  * Fetch all relations / attributes which are using the domain
2666  *
2667  * The result is a list of RelToCheck structs, one for each distinct
2668  * relation, each containing one or more attribute numbers that are of
2669  * the domain type.  We have opened each rel and acquired the specified lock
2670  * type on it.
2671  *
2672  * We support nested domains by including attributes that are of derived
2673  * domain types.  Current callers do not need to distinguish between attributes
2674  * that are of exactly the given domain and those that are of derived domains.
2675  *
2676  * XXX this is completely broken because there is no way to lock the domain
2677  * to prevent columns from being added or dropped while our command runs.
2678  * We can partially protect against column drops by locking relations as we
2679  * come across them, but there is still a race condition (the window between
2680  * seeing a pg_depend entry and acquiring lock on the relation it references).
2681  * Also, holding locks on all these relations simultaneously creates a non-
2682  * trivial risk of deadlock.  We can minimize but not eliminate the deadlock
2683  * risk by using the weakest suitable lock (ShareLock for most callers).
2684  *
2685  * XXX the API for this is not sufficient to support checking domain values
2686  * that are inside composite types or arrays.  Currently we just error out
2687  * if a composite type containing the target domain is stored anywhere.
2688  * There are not currently arrays of domains; if there were, we could take
2689  * the same approach, but it'd be nicer to fix it properly.
2690  *
2691  * Generally used for retrieving a list of tests when adding
2692  * new constraints to a domain.
2693  */
2694 static List *
2695 get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
2696 {
2697         List       *result = NIL;
2698         Relation        depRel;
2699         ScanKeyData key[2];
2700         SysScanDesc depScan;
2701         HeapTuple       depTup;
2702
2703         Assert(lockmode != NoLock);
2704
2705         /*
2706          * We scan pg_depend to find those things that depend on the domain. (We
2707          * assume we can ignore refobjsubid for a domain.)
2708          */
2709         depRel = heap_open(DependRelationId, AccessShareLock);
2710
2711         ScanKeyInit(&key[0],
2712                                 Anum_pg_depend_refclassid,
2713                                 BTEqualStrategyNumber, F_OIDEQ,
2714                                 ObjectIdGetDatum(TypeRelationId));
2715         ScanKeyInit(&key[1],
2716                                 Anum_pg_depend_refobjid,
2717                                 BTEqualStrategyNumber, F_OIDEQ,
2718                                 ObjectIdGetDatum(domainOid));
2719
2720         depScan = systable_beginscan(depRel, DependReferenceIndexId, true,
2721                                                                  SnapshotNow, 2, key);
2722
2723         while (HeapTupleIsValid(depTup = systable_getnext(depScan)))
2724         {
2725                 Form_pg_depend pg_depend = (Form_pg_depend) GETSTRUCT(depTup);
2726                 RelToCheck *rtc = NULL;
2727                 ListCell   *rellist;
2728                 Form_pg_attribute pg_att;
2729                 int                     ptr;
2730
2731                 /* Check for directly dependent types --- must be domains */
2732                 if (pg_depend->classid == TypeRelationId)
2733                 {
2734                         Assert(get_typtype(pg_depend->objid) == TYPTYPE_DOMAIN);
2735
2736                         /*
2737                          * Recursively add dependent columns to the output list.  This is
2738                          * a bit inefficient since we may fail to combine RelToCheck
2739                          * entries when attributes of the same rel have different derived
2740                          * domain types, but it's probably not worth improving.
2741                          */
2742                         result = list_concat(result,
2743                                                                  get_rels_with_domain(pg_depend->objid,
2744                                                                                                           lockmode));
2745                         continue;
2746                 }
2747
2748                 /* Else, ignore dependees that aren't user columns of relations */
2749                 /* (we assume system columns are never of domain types) */
2750                 if (pg_depend->classid != RelationRelationId ||
2751                         pg_depend->objsubid <= 0)
2752                         continue;
2753
2754                 /* See if we already have an entry for this relation */
2755                 foreach(rellist, result)
2756                 {
2757                         RelToCheck *rt = (RelToCheck *) lfirst(rellist);
2758
2759                         if (RelationGetRelid(rt->rel) == pg_depend->objid)
2760                         {
2761                                 rtc = rt;
2762                                 break;
2763                         }
2764                 }
2765
2766                 if (rtc == NULL)
2767                 {
2768                         /* First attribute found for this relation */
2769                         Relation        rel;
2770
2771                         /* Acquire requested lock on relation */
2772                         rel = relation_open(pg_depend->objid, lockmode);
2773
2774                         /*
2775                          * Check to see if rowtype is stored anyplace as a composite-type
2776                          * column; if so we have to fail, for now anyway.
2777                          */
2778                         if (OidIsValid(rel->rd_rel->reltype))
2779                                 find_composite_type_dependencies(rel->rd_rel->reltype,
2780                                                                                                  NULL,
2781                                                                                                  format_type_be(domainOid));
2782
2783                         /* Otherwise we can ignore views, composite types, etc */
2784                         if (rel->rd_rel->relkind != RELKIND_RELATION)
2785                         {
2786                                 relation_close(rel, lockmode);
2787                                 continue;
2788                         }
2789
2790                         /* Build the RelToCheck entry with enough space for all atts */
2791                         rtc = (RelToCheck *) palloc(sizeof(RelToCheck));
2792                         rtc->rel = rel;
2793                         rtc->natts = 0;
2794                         rtc->atts = (int *) palloc(sizeof(int) * RelationGetNumberOfAttributes(rel));
2795                         result = lcons(rtc, result);
2796                 }
2797
2798                 /*
2799                  * Confirm column has not been dropped, and is of the expected type.
2800                  * This defends against an ALTER DROP COLUMN occurring just before we
2801                  * acquired lock ... but if the whole table were dropped, we'd still
2802                  * have a problem.
2803                  */
2804                 if (pg_depend->objsubid > RelationGetNumberOfAttributes(rtc->rel))
2805                         continue;
2806                 pg_att = rtc->rel->rd_att->attrs[pg_depend->objsubid - 1];
2807                 if (pg_att->attisdropped || pg_att->atttypid != domainOid)
2808                         continue;
2809
2810                 /*
2811                  * Okay, add column to result.  We store the columns in column-number
2812                  * order; this is just a hack to improve predictability of regression
2813                  * test output ...
2814                  */
2815                 Assert(rtc->natts < RelationGetNumberOfAttributes(rtc->rel));
2816
2817                 ptr = rtc->natts++;
2818                 while (ptr > 0 && rtc->atts[ptr - 1] > pg_depend->objsubid)
2819                 {
2820                         rtc->atts[ptr] = rtc->atts[ptr - 1];
2821                         ptr--;
2822                 }
2823                 rtc->atts[ptr] = pg_depend->objsubid;
2824         }
2825
2826         systable_endscan(depScan);
2827
2828         relation_close(depRel, AccessShareLock);
2829
2830         return result;
2831 }
2832
2833 /*
2834  * checkDomainOwner
2835  *
2836  * Check that the type is actually a domain and that the current user
2837  * has permission to do ALTER DOMAIN on it.  Throw an error if not.
2838  */
2839 void
2840 checkDomainOwner(HeapTuple tup)
2841 {
2842         Form_pg_type typTup = (Form_pg_type) GETSTRUCT(tup);
2843
2844         /* Check that this is actually a domain */
2845         if (typTup->typtype != TYPTYPE_DOMAIN)
2846                 ereport(ERROR,
2847                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2848                                  errmsg("%s is not a domain",
2849                                                 format_type_be(HeapTupleGetOid(tup)))));
2850
2851         /* Permission check: must own type */
2852         if (!pg_type_ownercheck(HeapTupleGetOid(tup), GetUserId()))
2853                 aclcheck_error_type(ACLCHECK_NOT_OWNER, HeapTupleGetOid(tup));
2854 }
2855
2856 /*
2857  * domainAddConstraint - code shared between CREATE and ALTER DOMAIN
2858  */
2859 static char *
2860 domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid,
2861                                         int typMod, Constraint *constr,
2862                                         char *domainName)
2863 {
2864         Node       *expr;
2865         char       *ccsrc;
2866         char       *ccbin;
2867         ParseState *pstate;
2868         CoerceToDomainValue *domVal;
2869
2870         /*
2871          * Assign or validate constraint name
2872          */
2873         if (constr->conname)
2874         {
2875                 if (ConstraintNameIsUsed(CONSTRAINT_DOMAIN,
2876                                                                  domainOid,
2877                                                                  domainNamespace,
2878                                                                  constr->conname))
2879                         ereport(ERROR,
2880                                         (errcode(ERRCODE_DUPLICATE_OBJECT),
2881                                  errmsg("constraint \"%s\" for domain \"%s\" already exists",
2882                                                 constr->conname, domainName)));
2883         }
2884         else
2885                 constr->conname = ChooseConstraintName(domainName,
2886                                                                                            NULL,
2887                                                                                            "check",
2888                                                                                            domainNamespace,
2889                                                                                            NIL);
2890
2891         /*
2892          * Convert the A_EXPR in raw_expr into an EXPR
2893          */
2894         pstate = make_parsestate(NULL);
2895
2896         /*
2897          * Set up a CoerceToDomainValue to represent the occurrence of VALUE in
2898          * the expression.      Note that it will appear to have the type of the base
2899          * type, not the domain.  This seems correct since within the check
2900          * expression, we should not assume the input value can be considered a
2901          * member of the domain.
2902          */
2903         domVal = makeNode(CoerceToDomainValue);
2904         domVal->typeId = baseTypeOid;
2905         domVal->typeMod = typMod;
2906         domVal->collation = get_typcollation(baseTypeOid);
2907         domVal->location = -1;          /* will be set when/if used */
2908
2909         pstate->p_value_substitute = (Node *) domVal;
2910
2911         expr = transformExpr(pstate, constr->raw_expr, EXPR_KIND_DOMAIN_CHECK);
2912
2913         /*
2914          * Make sure it yields a boolean result.
2915          */
2916         expr = coerce_to_boolean(pstate, expr, "CHECK");
2917
2918         /*
2919          * Fix up collation information.
2920          */
2921         assign_expr_collations(pstate, expr);
2922
2923         /*
2924          * Domains don't allow variables (this is probably dead code now that
2925          * add_missing_from is history, but let's be sure).
2926          */
2927         if (list_length(pstate->p_rtable) != 0 ||
2928                 contain_var_clause(expr))
2929                 ereport(ERROR,
2930                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2931                   errmsg("cannot use table references in domain check constraint")));
2932
2933         /*
2934          * Convert to string form for storage.
2935          */
2936         ccbin = nodeToString(expr);
2937
2938         /*
2939          * Deparse it to produce text for consrc.
2940          */
2941         ccsrc = deparse_expression(expr,
2942                                                            NIL, false, false);
2943
2944         /*
2945          * Store the constraint in pg_constraint
2946          */
2947         CreateConstraintEntry(constr->conname,          /* Constraint Name */
2948                                                   domainNamespace,              /* namespace */
2949                                                   CONSTRAINT_CHECK,             /* Constraint Type */
2950                                                   false,        /* Is Deferrable */
2951                                                   false,        /* Is Deferred */
2952                                                   !constr->skip_validation,             /* Is Validated */
2953                                                   InvalidOid,   /* not a relation constraint */
2954                                                   NULL,
2955                                                   0,
2956                                                   domainOid,    /* domain constraint */
2957                                                   InvalidOid,   /* no associated index */
2958                                                   InvalidOid,   /* Foreign key fields */
2959                                                   NULL,
2960                                                   NULL,
2961                                                   NULL,
2962                                                   NULL,
2963                                                   0,
2964                                                   ' ',
2965                                                   ' ',
2966                                                   ' ',
2967                                                   NULL, /* not an exclusion constraint */
2968                                                   expr, /* Tree form of check constraint */
2969                                                   ccbin,        /* Binary form of check constraint */
2970                                                   ccsrc,        /* Source form of check constraint */
2971                                                   true, /* is local */
2972                                                   0,    /* inhcount */
2973                                                   false);               /* connoinherit */
2974
2975         /*
2976          * Return the compiled constraint expression so the calling routine can
2977          * perform any additional required tests.
2978          */
2979         return ccbin;
2980 }
2981
2982 /*
2983  * GetDomainConstraints - get a list of the current constraints of domain
2984  *
2985  * Returns a possibly-empty list of DomainConstraintState nodes.
2986  *
2987  * This is called by the executor during plan startup for a CoerceToDomain
2988  * expression node.  The given constraints will be checked for each value
2989  * passed through the node.
2990  *
2991  * We allow this to be called for non-domain types, in which case the result
2992  * is always NIL.
2993  */
2994 List *
2995 GetDomainConstraints(Oid typeOid)
2996 {
2997         List       *result = NIL;
2998         bool            notNull = false;
2999         Relation        conRel;
3000
3001         conRel = heap_open(ConstraintRelationId, AccessShareLock);
3002
3003         for (;;)
3004         {
3005                 HeapTuple       tup;
3006                 HeapTuple       conTup;
3007                 Form_pg_type typTup;
3008                 ScanKeyData key[1];
3009                 SysScanDesc scan;
3010
3011                 tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeOid));
3012                 if (!HeapTupleIsValid(tup))
3013                         elog(ERROR, "cache lookup failed for type %u", typeOid);
3014                 typTup = (Form_pg_type) GETSTRUCT(tup);
3015
3016                 if (typTup->typtype != TYPTYPE_DOMAIN)
3017                 {
3018                         /* Not a domain, so done */
3019                         ReleaseSysCache(tup);
3020                         break;
3021                 }
3022
3023                 /* Test for NOT NULL Constraint */
3024                 if (typTup->typnotnull)
3025                         notNull = true;
3026
3027                 /* Look for CHECK Constraints on this domain */
3028                 ScanKeyInit(&key[0],
3029                                         Anum_pg_constraint_contypid,
3030                                         BTEqualStrategyNumber, F_OIDEQ,
3031                                         ObjectIdGetDatum(typeOid));
3032
3033                 scan = systable_beginscan(conRel, ConstraintTypidIndexId, true,
3034                                                                   SnapshotNow, 1, key);
3035
3036                 while (HeapTupleIsValid(conTup = systable_getnext(scan)))
3037                 {
3038                         Form_pg_constraint c = (Form_pg_constraint) GETSTRUCT(conTup);
3039                         Datum           val;
3040                         bool            isNull;
3041                         Expr       *check_expr;
3042                         DomainConstraintState *r;
3043
3044                         /* Ignore non-CHECK constraints (presently, shouldn't be any) */
3045                         if (c->contype != CONSTRAINT_CHECK)
3046                                 continue;
3047
3048                         /*
3049                          * Not expecting conbin to be NULL, but we'll test for it anyway
3050                          */
3051                         val = fastgetattr(conTup, Anum_pg_constraint_conbin,
3052                                                           conRel->rd_att, &isNull);
3053                         if (isNull)
3054                                 elog(ERROR, "domain \"%s\" constraint \"%s\" has NULL conbin",
3055                                          NameStr(typTup->typname), NameStr(c->conname));
3056
3057                         check_expr = (Expr *) stringToNode(TextDatumGetCString(val));
3058
3059                         /* ExecInitExpr assumes we've planned the expression */
3060                         check_expr = expression_planner(check_expr);
3061
3062                         r = makeNode(DomainConstraintState);
3063                         r->constrainttype = DOM_CONSTRAINT_CHECK;
3064                         r->name = pstrdup(NameStr(c->conname));
3065                         r->check_expr = ExecInitExpr(check_expr, NULL);
3066
3067                         /*
3068                          * use lcons() here because constraints of lower domains should be
3069                          * applied earlier.
3070                          */
3071                         result = lcons(r, result);
3072                 }
3073
3074                 systable_endscan(scan);
3075
3076                 /* loop to next domain in stack */
3077                 typeOid = typTup->typbasetype;
3078                 ReleaseSysCache(tup);
3079         }
3080
3081         heap_close(conRel, AccessShareLock);
3082
3083         /*
3084          * Only need to add one NOT NULL check regardless of how many domains in
3085          * the stack request it.
3086          */
3087         if (notNull)
3088         {
3089                 DomainConstraintState *r = makeNode(DomainConstraintState);
3090
3091                 r->constrainttype = DOM_CONSTRAINT_NOTNULL;
3092                 r->name = pstrdup("NOT NULL");
3093                 r->check_expr = NULL;
3094
3095                 /* lcons to apply the nullness check FIRST */
3096                 result = lcons(r, result);
3097         }
3098
3099         return result;
3100 }
3101
3102
3103 /*
3104  * Execute ALTER TYPE RENAME
3105  */
3106 Oid
3107 RenameType(RenameStmt *stmt)
3108 {
3109         List       *names = stmt->object;
3110         const char *newTypeName = stmt->newname;
3111         TypeName   *typename;
3112         Oid                     typeOid;
3113         Relation        rel;
3114         HeapTuple       tup;
3115         Form_pg_type typTup;
3116
3117         /* Make a TypeName so we can use standard type lookup machinery */
3118         typename = makeTypeNameFromNameList(names);
3119         typeOid = typenameTypeId(NULL, typename);
3120
3121         /* Look up the type in the type table */
3122         rel = heap_open(TypeRelationId, RowExclusiveLock);
3123
3124         tup = SearchSysCacheCopy1(TYPEOID, ObjectIdGetDatum(typeOid));
3125         if (!HeapTupleIsValid(tup))
3126                 elog(ERROR, "cache lookup failed for type %u", typeOid);
3127         typTup = (Form_pg_type) GETSTRUCT(tup);
3128
3129         /* check permissions on type */
3130         if (!pg_type_ownercheck(typeOid, GetUserId()))
3131                 aclcheck_error_type(ACLCHECK_NOT_OWNER, typeOid);
3132
3133         /* ALTER DOMAIN used on a non-domain? */
3134         if (stmt->renameType == OBJECT_DOMAIN && typTup->typtype != TYPTYPE_DOMAIN)
3135                 ereport(ERROR,
3136                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3137                                  errmsg("\"%s\" is not a domain",
3138                                                 format_type_be(typeOid))));
3139
3140         /*
3141          * If it's a composite type, we need to check that it really is a
3142          * free-standing composite type, and not a table's rowtype. We want people
3143          * to use ALTER TABLE not ALTER TYPE for that case.
3144          */
3145         if (typTup->typtype == TYPTYPE_COMPOSITE &&
3146                 get_rel_relkind(typTup->typrelid) != RELKIND_COMPOSITE_TYPE)
3147                 ereport(ERROR,
3148                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3149                                  errmsg("%s is a table's row type",
3150                                                 format_type_be(typeOid)),
3151                                  errhint("Use ALTER TABLE instead.")));
3152
3153         /* don't allow direct alteration of array types, either */
3154         if (OidIsValid(typTup->typelem) &&
3155                 get_array_type(typTup->typelem) == typeOid)
3156                 ereport(ERROR,
3157                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3158                                  errmsg("cannot alter array type %s",
3159                                                 format_type_be(typeOid)),
3160                                  errhint("You can alter type %s, which will alter the array type as well.",
3161                                                  format_type_be(typTup->typelem))));
3162
3163         /*
3164          * If type is composite we need to rename associated pg_class entry too.
3165          * RenameRelationInternal will call RenameTypeInternal automatically.
3166          */
3167         if (typTup->typtype == TYPTYPE_COMPOSITE)
3168                 RenameRelationInternal(typTup->typrelid, newTypeName);
3169         else
3170                 RenameTypeInternal(typeOid, newTypeName,
3171                                                    typTup->typnamespace);
3172
3173         /* Clean up */
3174         heap_close(rel, RowExclusiveLock);
3175
3176         return typeOid;
3177 }
3178
3179 /*
3180  * Change the owner of a type.
3181  */
3182 Oid
3183 AlterTypeOwner(List *names, Oid newOwnerId, ObjectType objecttype)
3184 {
3185         TypeName   *typename;
3186         Oid                     typeOid;
3187         Relation        rel;
3188         HeapTuple       tup;
3189         HeapTuple       newtup;
3190         Form_pg_type typTup;
3191         AclResult       aclresult;
3192
3193         rel = heap_open(TypeRelationId, RowExclusiveLock);
3194
3195         /* Make a TypeName so we can use standard type lookup machinery */
3196         typename = makeTypeNameFromNameList(names);
3197
3198         /* Use LookupTypeName here so that shell types can be processed */
3199         tup = LookupTypeName(NULL, typename, NULL);
3200         if (tup == NULL)
3201                 ereport(ERROR,
3202                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
3203                                  errmsg("type \"%s\" does not exist",
3204                                                 TypeNameToString(typename))));
3205         typeOid = typeTypeId(tup);
3206
3207         /* Copy the syscache entry so we can scribble on it below */
3208         newtup = heap_copytuple(tup);
3209         ReleaseSysCache(tup);
3210         tup = newtup;
3211         typTup = (Form_pg_type) GETSTRUCT(tup);
3212
3213         /* Don't allow ALTER DOMAIN on a type */
3214         if (objecttype == OBJECT_DOMAIN && typTup->typtype != TYPTYPE_DOMAIN)
3215                 ereport(ERROR,
3216                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3217                                  errmsg("%s is not a domain",
3218                                                 format_type_be(typeOid))));
3219
3220         /*
3221          * If it's a composite type, we need to check that it really is a
3222          * free-standing composite type, and not a table's rowtype. We want people
3223          * to use ALTER TABLE not ALTER TYPE for that case.
3224          */
3225         if (typTup->typtype == TYPTYPE_COMPOSITE &&
3226                 get_rel_relkind(typTup->typrelid) != RELKIND_COMPOSITE_TYPE)
3227                 ereport(ERROR,
3228                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3229                                  errmsg("%s is a table's row type",
3230                                                 format_type_be(typeOid)),
3231                                  errhint("Use ALTER TABLE instead.")));
3232
3233         /* don't allow direct alteration of array types, either */
3234         if (OidIsValid(typTup->typelem) &&
3235                 get_array_type(typTup->typelem) == typeOid)
3236                 ereport(ERROR,
3237                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3238                                  errmsg("cannot alter array type %s",
3239                                                 format_type_be(typeOid)),
3240                                  errhint("You can alter type %s, which will alter the array type as well.",
3241                                                  format_type_be(typTup->typelem))));
3242
3243         /*
3244          * If the new owner is the same as the existing owner, consider the
3245          * command to have succeeded.  This is for dump restoration purposes.
3246          */
3247         if (typTup->typowner != newOwnerId)
3248         {
3249                 /* Superusers can always do it */
3250                 if (!superuser())
3251                 {
3252                         /* Otherwise, must be owner of the existing object */
3253                         if (!pg_type_ownercheck(HeapTupleGetOid(tup), GetUserId()))
3254                                 aclcheck_error_type(ACLCHECK_NOT_OWNER, HeapTupleGetOid(tup));
3255
3256                         /* Must be able to become new owner */
3257                         check_is_member_of_role(GetUserId(), newOwnerId);
3258
3259                         /* New owner must have CREATE privilege on namespace */
3260                         aclresult = pg_namespace_aclcheck(typTup->typnamespace,
3261                                                                                           newOwnerId,
3262                                                                                           ACL_CREATE);
3263                         if (aclresult != ACLCHECK_OK)
3264                                 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
3265                                                            get_namespace_name(typTup->typnamespace));
3266                 }
3267
3268                 /*
3269                  * If it's a composite type, invoke ATExecChangeOwner so that we fix
3270                  * up the pg_class entry properly.      That will call back to
3271                  * AlterTypeOwnerInternal to take care of the pg_type entry(s).
3272                  */
3273                 if (typTup->typtype == TYPTYPE_COMPOSITE)
3274                         ATExecChangeOwner(typTup->typrelid, newOwnerId, true, AccessExclusiveLock);
3275                 else
3276                 {
3277                         /*
3278                          * We can just apply the modification directly.
3279                          *
3280                          * okay to scribble on typTup because it's a copy
3281                          */
3282                         typTup->typowner = newOwnerId;
3283
3284                         simple_heap_update(rel, &tup->t_self, tup);
3285
3286                         CatalogUpdateIndexes(rel, tup);
3287
3288                         /* Update owner dependency reference */
3289                         changeDependencyOnOwner(TypeRelationId, typeOid, newOwnerId);
3290
3291                         /* If it has an array type, update that too */
3292                         if (OidIsValid(typTup->typarray))
3293                                 AlterTypeOwnerInternal(typTup->typarray, newOwnerId, false);
3294                 }
3295         }
3296
3297         /* Clean up */
3298         heap_close(rel, RowExclusiveLock);
3299
3300         return typeOid;
3301 }
3302
3303 /*
3304  * AlterTypeOwnerInternal - change type owner unconditionally
3305  *
3306  * This is currently only used to propagate ALTER TABLE/TYPE OWNER to a
3307  * table's rowtype or an array type, and to implement REASSIGN OWNED BY.
3308  * It assumes the caller has done all needed checks.  The function will
3309  * automatically recurse to an array type if the type has one.
3310  *
3311  * hasDependEntry should be TRUE if type is expected to have a pg_shdepend
3312  * entry (ie, it's not a table rowtype nor an array type).
3313  */
3314 void
3315 AlterTypeOwnerInternal(Oid typeOid, Oid newOwnerId,
3316                                            bool hasDependEntry)
3317 {
3318         Relation        rel;
3319         HeapTuple       tup;
3320         Form_pg_type typTup;
3321
3322         rel = heap_open(TypeRelationId, RowExclusiveLock);
3323
3324         tup = SearchSysCacheCopy1(TYPEOID, ObjectIdGetDatum(typeOid));
3325         if (!HeapTupleIsValid(tup))
3326                 elog(ERROR, "cache lookup failed for type %u", typeOid);
3327         typTup = (Form_pg_type) GETSTRUCT(tup);
3328
3329         /*
3330          * Modify the owner --- okay to scribble on typTup because it's a copy
3331          */
3332         typTup->typowner = newOwnerId;
3333
3334         simple_heap_update(rel, &tup->t_self, tup);
3335
3336         CatalogUpdateIndexes(rel, tup);
3337
3338         /* Update owner dependency reference, if it has one */
3339         if (hasDependEntry)
3340                 changeDependencyOnOwner(TypeRelationId, typeOid, newOwnerId);
3341
3342         /* If it has an array type, update that too */
3343         if (OidIsValid(typTup->typarray))
3344                 AlterTypeOwnerInternal(typTup->typarray, newOwnerId, false);
3345
3346         /* Clean up */
3347         heap_close(rel, RowExclusiveLock);
3348 }
3349
3350 /*
3351  * Execute ALTER TYPE SET SCHEMA
3352  */
3353 Oid
3354 AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype)
3355 {
3356         TypeName   *typename;
3357         Oid                     typeOid;
3358         Oid                     nspOid;
3359         ObjectAddresses *objsMoved;
3360
3361         /* Make a TypeName so we can use standard type lookup machinery */
3362         typename = makeTypeNameFromNameList(names);
3363         typeOid = typenameTypeId(NULL, typename);
3364
3365         /* Don't allow ALTER DOMAIN on a type */
3366         if (objecttype == OBJECT_DOMAIN && get_typtype(typeOid) != TYPTYPE_DOMAIN)
3367                 ereport(ERROR,
3368                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3369                                  errmsg("%s is not a domain",
3370                                                 format_type_be(typeOid))));
3371
3372         /* get schema OID and check its permissions */
3373         nspOid = LookupCreationNamespace(newschema);
3374
3375         objsMoved = new_object_addresses();
3376         AlterTypeNamespace_oid(typeOid, nspOid, objsMoved);
3377         free_object_addresses(objsMoved);
3378
3379         return typeOid;
3380 }
3381
3382 Oid
3383 AlterTypeNamespace_oid(Oid typeOid, Oid nspOid, ObjectAddresses *objsMoved)
3384 {
3385         Oid                     elemOid;
3386
3387         /* check permissions on type */
3388         if (!pg_type_ownercheck(typeOid, GetUserId()))
3389                 aclcheck_error_type(ACLCHECK_NOT_OWNER, typeOid);
3390
3391         /* don't allow direct alteration of array types */
3392         elemOid = get_element_type(typeOid);
3393         if (OidIsValid(elemOid) && get_array_type(elemOid) == typeOid)
3394                 ereport(ERROR,
3395                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3396                                  errmsg("cannot alter array type %s",
3397                                                 format_type_be(typeOid)),
3398                                  errhint("You can alter type %s, which will alter the array type as well.",
3399                                                  format_type_be(elemOid))));
3400
3401         /* and do the work */
3402         return AlterTypeNamespaceInternal(typeOid, nspOid, false, true, objsMoved);
3403 }
3404
3405 /*
3406  * Move specified type to new namespace.
3407  *
3408  * Caller must have already checked privileges.
3409  *
3410  * The function automatically recurses to process the type's array type,
3411  * if any.      isImplicitArray should be TRUE only when doing this internal
3412  * recursion (outside callers must never try to move an array type directly).
3413  *
3414  * If errorOnTableType is TRUE, the function errors out if the type is
3415  * a table type.  ALTER TABLE has to be used to move a table to a new
3416  * namespace.
3417  *
3418  * Returns the type's old namespace OID.
3419  */
3420 Oid
3421 AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
3422                                                    bool isImplicitArray,
3423                                                    bool errorOnTableType,
3424                                                    ObjectAddresses *objsMoved)
3425 {
3426         Relation        rel;
3427         HeapTuple       tup;
3428         Form_pg_type typform;
3429         Oid                     oldNspOid;
3430         Oid                     arrayOid;
3431         bool            isCompositeType;
3432         ObjectAddress thisobj;
3433
3434         /*
3435          * Make sure we haven't moved this object previously.
3436          */
3437         thisobj.classId = TypeRelationId;
3438         thisobj.objectId = typeOid;
3439         thisobj.objectSubId = 0;
3440
3441         if (object_address_present(&thisobj, objsMoved))
3442                 return InvalidOid;
3443
3444         rel = heap_open(TypeRelationId, RowExclusiveLock);
3445
3446         tup = SearchSysCacheCopy1(TYPEOID, ObjectIdGetDatum(typeOid));
3447         if (!HeapTupleIsValid(tup))
3448                 elog(ERROR, "cache lookup failed for type %u", typeOid);
3449         typform = (Form_pg_type) GETSTRUCT(tup);
3450
3451         oldNspOid = typform->typnamespace;
3452         arrayOid = typform->typarray;
3453
3454         /* common checks on switching namespaces */
3455         CheckSetNamespace(oldNspOid, nspOid, TypeRelationId, typeOid);
3456
3457         /* check for duplicate name (more friendly than unique-index failure) */
3458         if (SearchSysCacheExists2(TYPENAMENSP,
3459                                                           CStringGetDatum(NameStr(typform->typname)),
3460                                                           ObjectIdGetDatum(nspOid)))
3461                 ereport(ERROR,
3462                                 (errcode(ERRCODE_DUPLICATE_OBJECT),
3463                                  errmsg("type \"%s\" already exists in schema \"%s\"",
3464                                                 NameStr(typform->typname),
3465                                                 get_namespace_name(nspOid))));
3466
3467         /* Detect whether type is a composite type (but not a table rowtype) */
3468         isCompositeType =
3469                 (typform->typtype == TYPTYPE_COMPOSITE &&
3470                  get_rel_relkind(typform->typrelid) == RELKIND_COMPOSITE_TYPE);
3471
3472         /* Enforce not-table-type if requested */
3473         if (typform->typtype == TYPTYPE_COMPOSITE && !isCompositeType &&
3474                 errorOnTableType)
3475                 ereport(ERROR,
3476                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3477                                  errmsg("%s is a table's row type",
3478                                                 format_type_be(typeOid)),
3479                                  errhint("Use ALTER TABLE instead.")));
3480
3481         /* OK, modify the pg_type row */
3482
3483         /* tup is a copy, so we can scribble directly on it */
3484         typform->typnamespace = nspOid;
3485
3486         simple_heap_update(rel, &tup->t_self, tup);
3487         CatalogUpdateIndexes(rel, tup);
3488
3489         /*
3490          * Composite types have pg_class entries.
3491          *
3492          * We need to modify the pg_class tuple as well to reflect the change of
3493          * schema.
3494          */
3495         if (isCompositeType)
3496         {
3497                 Relation        classRel;
3498
3499                 classRel = heap_open(RelationRelationId, RowExclusiveLock);
3500
3501                 AlterRelationNamespaceInternal(classRel, typform->typrelid,
3502                                                                            oldNspOid, nspOid,
3503                                                                            false, objsMoved);
3504
3505                 heap_close(classRel, RowExclusiveLock);
3506
3507                 /*
3508                  * Check for constraints associated with the composite type (we don't
3509                  * currently support this, but probably will someday).
3510                  */
3511                 AlterConstraintNamespaces(typform->typrelid, oldNspOid,
3512                                                                   nspOid, false, objsMoved);
3513         }
3514         else
3515         {
3516                 /* If it's a domain, it might have constraints */
3517                 if (typform->typtype == TYPTYPE_DOMAIN)
3518                         AlterConstraintNamespaces(typeOid, oldNspOid, nspOid, true,
3519                                                                           objsMoved);
3520         }
3521
3522         /*
3523          * Update dependency on schema, if any --- a table rowtype has not got
3524          * one, and neither does an implicit array.
3525          */
3526         if ((isCompositeType || typform->typtype != TYPTYPE_COMPOSITE) &&
3527                 !isImplicitArray)
3528                 if (changeDependencyFor(TypeRelationId, typeOid,
3529                                                                 NamespaceRelationId, oldNspOid, nspOid) != 1)
3530                         elog(ERROR, "failed to change schema dependency for type %s",
3531                                  format_type_be(typeOid));
3532
3533         heap_freetuple(tup);
3534
3535         heap_close(rel, RowExclusiveLock);
3536
3537         add_exact_object_address(&thisobj, objsMoved);
3538
3539         /* Recursively alter the associated array type, if any */
3540         if (OidIsValid(arrayOid))
3541                 AlterTypeNamespaceInternal(arrayOid, nspOid, true, true, objsMoved);
3542
3543         return oldNspOid;
3544 }