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