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