Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * parse_utilcmd.c
4 : * Perform parse analysis work for various utility commands
5 : *
6 : * Formerly we did this work during parse_analyze_*() in analyze.c. However
7 : * that is fairly unsafe in the presence of querytree caching, since any
8 : * database state that we depend on in making the transformations might be
9 : * obsolete by the time the utility command is executed; and utility commands
10 : * have no infrastructure for holding locks or rechecking plan validity.
11 : * Hence these functions are now called at the start of execution of their
12 : * respective utility commands.
13 : *
14 : *
15 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
16 : * Portions Copyright (c) 1994, Regents of the University of California
17 : *
18 : * src/backend/parser/parse_utilcmd.c
19 : *
20 : *-------------------------------------------------------------------------
21 : */
22 :
23 : #include "postgres.h"
24 :
25 : #include "access/amapi.h"
26 : #include "access/attmap.h"
27 : #include "access/htup_details.h"
28 : #include "access/relation.h"
29 : #include "access/reloptions.h"
30 : #include "access/table.h"
31 : #include "access/toast_compression.h"
32 : #include "catalog/dependency.h"
33 : #include "catalog/heap.h"
34 : #include "catalog/index.h"
35 : #include "catalog/namespace.h"
36 : #include "catalog/partition.h"
37 : #include "catalog/pg_am.h"
38 : #include "catalog/pg_collation.h"
39 : #include "catalog/pg_constraint.h"
40 : #include "catalog/pg_opclass.h"
41 : #include "catalog/pg_operator.h"
42 : #include "catalog/pg_statistic_ext.h"
43 : #include "catalog/pg_type.h"
44 : #include "commands/comment.h"
45 : #include "commands/defrem.h"
46 : #include "commands/sequence.h"
47 : #include "commands/tablecmds.h"
48 : #include "commands/tablespace.h"
49 : #include "miscadmin.h"
50 : #include "nodes/makefuncs.h"
51 : #include "nodes/nodeFuncs.h"
52 : #include "optimizer/optimizer.h"
53 : #include "parser/analyze.h"
54 : #include "parser/parse_clause.h"
55 : #include "parser/parse_coerce.h"
56 : #include "parser/parse_collate.h"
57 : #include "parser/parse_expr.h"
58 : #include "parser/parse_relation.h"
59 : #include "parser/parse_target.h"
60 : #include "parser/parse_type.h"
61 : #include "parser/parse_utilcmd.h"
62 : #include "parser/parser.h"
63 : #include "partitioning/partbounds.h"
64 : #include "partitioning/partdesc.h"
65 : #include "rewrite/rewriteManip.h"
66 : #include "utils/acl.h"
67 : #include "utils/builtins.h"
68 : #include "utils/lsyscache.h"
69 : #include "utils/partcache.h"
70 : #include "utils/rel.h"
71 : #include "utils/ruleutils.h"
72 : #include "utils/syscache.h"
73 : #include "utils/typcache.h"
74 :
75 :
76 : /* State shared by transformCreateStmt and its subroutines */
77 : typedef struct
78 : {
79 : ParseState *pstate; /* overall parser state */
80 : const char *stmtType; /* "CREATE [FOREIGN] TABLE" or "ALTER TABLE" */
81 : RangeVar *relation; /* relation to create */
82 : Relation rel; /* opened/locked rel, if ALTER */
83 : List *inhRelations; /* relations to inherit from */
84 : bool isforeign; /* true if CREATE/ALTER FOREIGN TABLE */
85 : bool isalter; /* true if altering existing table */
86 : List *columns; /* ColumnDef items */
87 : List *ckconstraints; /* CHECK constraints */
88 : List *nnconstraints; /* NOT NULL constraints */
89 : List *fkconstraints; /* FOREIGN KEY constraints */
90 : List *ixconstraints; /* index-creating constraints */
91 : List *likeclauses; /* LIKE clauses that need post-processing */
92 : List *blist; /* "before list" of things to do before
93 : * creating the table */
94 : List *alist; /* "after list" of things to do after creating
95 : * the table */
96 : IndexStmt *pkey; /* PRIMARY KEY index, if any */
97 : bool ispartitioned; /* true if table is partitioned */
98 : PartitionBoundSpec *partbound; /* transformed FOR VALUES */
99 : bool ofType; /* true if statement contains OF typename */
100 : } CreateStmtContext;
101 :
102 : /* State shared by transformCreateSchemaStmtElements and its subroutines */
103 : typedef struct
104 : {
105 : const char *schemaname; /* name of schema */
106 : List *sequences; /* CREATE SEQUENCE items */
107 : List *tables; /* CREATE TABLE items */
108 : List *views; /* CREATE VIEW items */
109 : List *indexes; /* CREATE INDEX items */
110 : List *triggers; /* CREATE TRIGGER items */
111 : List *grants; /* GRANT items */
112 : } CreateSchemaStmtContext;
113 :
114 :
115 : static void transformColumnDefinition(CreateStmtContext *cxt,
116 : ColumnDef *column);
117 : static void transformTableConstraint(CreateStmtContext *cxt,
118 : Constraint *constraint);
119 : static void transformTableLikeClause(CreateStmtContext *cxt,
120 : TableLikeClause *table_like_clause);
121 : static void transformOfType(CreateStmtContext *cxt,
122 : TypeName *ofTypename);
123 : static CreateStatsStmt *generateClonedExtStatsStmt(RangeVar *heapRel,
124 : Oid heapRelid,
125 : Oid source_statsid,
126 : const AttrMap *attmap);
127 : static List *get_collation(Oid collation, Oid actual_datatype);
128 : static List *get_opclass(Oid opclass, Oid actual_datatype);
129 : static void transformIndexConstraints(CreateStmtContext *cxt);
130 : static IndexStmt *transformIndexConstraint(Constraint *constraint,
131 : CreateStmtContext *cxt);
132 : static void transformFKConstraints(CreateStmtContext *cxt,
133 : bool skipValidation,
134 : bool isAddConstraint);
135 : static void transformCheckConstraints(CreateStmtContext *cxt,
136 : bool skipValidation);
137 : static void transformConstraintAttrs(CreateStmtContext *cxt,
138 : List *constraintList);
139 : static void transformColumnType(CreateStmtContext *cxt, ColumnDef *column);
140 : static void setSchemaName(const char *context_schema, char **stmt_schema_name);
141 : static void transformPartitionCmd(CreateStmtContext *cxt, PartitionBoundSpec *bound);
142 : static List *transformPartitionRangeBounds(ParseState *pstate, List *blist,
143 : Relation parent);
144 : static void validateInfiniteBounds(ParseState *pstate, List *blist);
145 : static Const *transformPartitionBoundValue(ParseState *pstate, Node *val,
146 : const char *colName, Oid colType, int32 colTypmod,
147 : Oid partCollation);
148 :
149 :
150 : /*
151 : * transformCreateStmt -
152 : * parse analysis for CREATE TABLE
153 : *
154 : * Returns a List of utility commands to be done in sequence. One of these
155 : * will be the transformed CreateStmt, but there may be additional actions
156 : * to be done before and after the actual DefineRelation() call.
157 : * In addition to normal utility commands such as AlterTableStmt and
158 : * IndexStmt, the result list may contain TableLikeClause(s), representing
159 : * the need to perform additional parse analysis after DefineRelation().
160 : *
161 : * SQL allows constraints to be scattered all over, so thumb through
162 : * the columns and collect all constraints into one place.
163 : * If there are any implied indices (e.g. UNIQUE or PRIMARY KEY)
164 : * then expand those into multiple IndexStmt blocks.
165 : * - thomas 1997-12-02
166 : */
167 : List *
168 25252 : transformCreateStmt(CreateStmt *stmt, const char *queryString)
169 : {
170 : ParseState *pstate;
171 : CreateStmtContext cxt;
172 : List *result;
173 : List *save_alist;
174 : ListCell *elements;
175 : Oid namespaceid;
176 : Oid existing_relid;
177 : ParseCallbackState pcbstate;
178 :
179 : /* Set up pstate */
180 25252 : pstate = make_parsestate(NULL);
181 25252 : pstate->p_sourcetext = queryString;
182 :
183 : /*
184 : * Look up the creation namespace. This also checks permissions on the
185 : * target namespace, locks it against concurrent drops, checks for a
186 : * preexisting relation in that namespace with the same name, and updates
187 : * stmt->relation->relpersistence if the selected namespace is temporary.
188 : */
189 25252 : setup_parser_errposition_callback(&pcbstate, pstate,
190 25252 : stmt->relation->location);
191 : namespaceid =
192 25252 : RangeVarGetAndCheckCreationNamespace(stmt->relation, NoLock,
193 : &existing_relid);
194 25244 : cancel_parser_errposition_callback(&pcbstate);
195 :
196 : /*
197 : * If the relation already exists and the user specified "IF NOT EXISTS",
198 : * bail out with a NOTICE.
199 : */
200 25244 : if (stmt->if_not_exists && OidIsValid(existing_relid))
201 : {
202 : /*
203 : * If we are in an extension script, insist that the pre-existing
204 : * object be a member of the extension, to avoid security risks.
205 : */
206 : ObjectAddress address;
207 :
208 6 : ObjectAddressSet(address, RelationRelationId, existing_relid);
209 6 : checkMembershipInCurrentExtension(&address);
210 :
211 : /* OK to skip */
212 5 : ereport(NOTICE,
213 : (errcode(ERRCODE_DUPLICATE_TABLE),
214 : errmsg("relation \"%s\" already exists, skipping",
215 : stmt->relation->relname)));
216 5 : return NIL;
217 : }
218 :
219 : /*
220 : * If the target relation name isn't schema-qualified, make it so. This
221 : * prevents some corner cases in which added-on rewritten commands might
222 : * think they should apply to other relations that have the same name and
223 : * are earlier in the search path. But a local temp table is effectively
224 : * specified to be in pg_temp, so no need for anything extra in that case.
225 : */
226 25238 : if (stmt->relation->schemaname == NULL
227 23321 : && stmt->relation->relpersistence != RELPERSISTENCE_TEMP)
228 21642 : stmt->relation->schemaname = get_namespace_name(namespaceid);
229 :
230 : /* Set up CreateStmtContext */
231 25238 : cxt.pstate = pstate;
232 25238 : if (IsA(stmt, CreateForeignTableStmt))
233 : {
234 291 : cxt.stmtType = "CREATE FOREIGN TABLE";
235 291 : cxt.isforeign = true;
236 : }
237 : else
238 : {
239 24947 : cxt.stmtType = "CREATE TABLE";
240 24947 : cxt.isforeign = false;
241 : }
242 25238 : cxt.relation = stmt->relation;
243 25238 : cxt.rel = NULL;
244 25238 : cxt.inhRelations = stmt->inhRelations;
245 25238 : cxt.isalter = false;
246 25238 : cxt.columns = NIL;
247 25238 : cxt.ckconstraints = NIL;
248 25238 : cxt.nnconstraints = NIL;
249 25238 : cxt.fkconstraints = NIL;
250 25238 : cxt.ixconstraints = NIL;
251 25238 : cxt.likeclauses = NIL;
252 25238 : cxt.blist = NIL;
253 25238 : cxt.alist = NIL;
254 25238 : cxt.pkey = NULL;
255 25238 : cxt.ispartitioned = stmt->partspec != NULL;
256 25238 : cxt.partbound = stmt->partbound;
257 25238 : cxt.ofType = (stmt->ofTypename != NULL);
258 :
259 : Assert(!stmt->ofTypename || !stmt->inhRelations); /* grammar enforces */
260 :
261 25238 : if (stmt->ofTypename)
262 81 : transformOfType(&cxt, stmt->ofTypename);
263 :
264 25226 : if (stmt->partspec)
265 : {
266 3566 : if (stmt->inhRelations && !stmt->partbound)
267 4 : ereport(ERROR,
268 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
269 : errmsg("cannot create partitioned table as inheritance child")));
270 : }
271 :
272 : /*
273 : * Run through each primary element in the table creation clause. Separate
274 : * column defs from constraints, and do preliminary analysis.
275 : */
276 69614 : foreach(elements, stmt->tableElts)
277 : {
278 44553 : Node *element = lfirst(elements);
279 :
280 44553 : switch (nodeTag(element))
281 : {
282 42196 : case T_ColumnDef:
283 42196 : transformColumnDefinition(&cxt, (ColumnDef *) element);
284 42051 : break;
285 :
286 1841 : case T_Constraint:
287 1841 : transformTableConstraint(&cxt, (Constraint *) element);
288 1833 : break;
289 :
290 516 : case T_TableLikeClause:
291 516 : transformTableLikeClause(&cxt, (TableLikeClause *) element);
292 508 : break;
293 :
294 0 : default:
295 0 : elog(ERROR, "unrecognized node type: %d",
296 : (int) nodeTag(element));
297 : break;
298 : }
299 : }
300 :
301 : /*
302 : * Transfer anything we already have in cxt.alist into save_alist, to keep
303 : * it separate from the output of transformIndexConstraints. (This may
304 : * not be necessary anymore, but we'll keep doing it to preserve the
305 : * historical order of execution of the alist commands.)
306 : */
307 25061 : save_alist = cxt.alist;
308 25061 : cxt.alist = NIL;
309 :
310 : Assert(stmt->constraints == NIL);
311 :
312 : /*
313 : * Before processing index constraints, which could include a primary key,
314 : * we must scan all not-null constraints to propagate the is_not_null flag
315 : * to each corresponding ColumnDef. This is necessary because table-level
316 : * not-null constraints have not been marked in each ColumnDef, and the PK
317 : * processing code needs to know whether one constraint has already been
318 : * declared in order not to declare a redundant one.
319 : */
320 57372 : foreach_node(Constraint, nn, cxt.nnconstraints)
321 : {
322 7250 : char *colname = strVal(linitial(nn->keys));
323 :
324 17003 : foreach_node(ColumnDef, cd, cxt.columns)
325 : {
326 : /* not our column? */
327 9736 : if (strcmp(cd->colname, colname) != 0)
328 2503 : continue;
329 : /* Already marked not-null? Nothing to do */
330 7233 : if (cd->is_not_null)
331 6901 : break;
332 : /* Bingo, we're done for this constraint */
333 332 : cd->is_not_null = true;
334 332 : break;
335 : }
336 : }
337 :
338 : /*
339 : * Postprocess constraints that give rise to index definitions.
340 : */
341 25061 : transformIndexConstraints(&cxt);
342 :
343 : /*
344 : * Re-consideration of LIKE clauses should happen after creation of
345 : * indexes, but before creation of foreign keys. This order is critical
346 : * because a LIKE clause may attempt to create a primary key. If there's
347 : * also a pkey in the main CREATE TABLE list, creation of that will not
348 : * check for a duplicate at runtime (since index_check_primary_key()
349 : * expects that we rejected dups here). Creation of the LIKE-generated
350 : * pkey behaves like ALTER TABLE ADD, so it will check, but obviously that
351 : * only works if it happens second. On the other hand, we want to make
352 : * pkeys before foreign key constraints, in case the user tries to make a
353 : * self-referential FK.
354 : */
355 25033 : cxt.alist = list_concat(cxt.alist, cxt.likeclauses);
356 :
357 : /*
358 : * Postprocess foreign-key constraints.
359 : */
360 25033 : transformFKConstraints(&cxt, true, false);
361 :
362 : /*
363 : * Postprocess check constraints.
364 : *
365 : * For regular tables all constraints can be marked valid immediately,
366 : * because the table is new therefore empty. Not so for foreign tables.
367 : */
368 25033 : transformCheckConstraints(&cxt, !cxt.isforeign);
369 :
370 : /*
371 : * Output results.
372 : */
373 25033 : stmt->tableElts = cxt.columns;
374 25033 : stmt->constraints = cxt.ckconstraints;
375 25033 : stmt->nnconstraints = cxt.nnconstraints;
376 :
377 25033 : result = lappend(cxt.blist, stmt);
378 25033 : result = list_concat(result, cxt.alist);
379 25033 : result = list_concat(result, save_alist);
380 :
381 25033 : return result;
382 : }
383 :
384 : /*
385 : * generateSerialExtraStmts
386 : * Generate CREATE SEQUENCE and ALTER SEQUENCE ... OWNED BY statements
387 : * to create the sequence for a serial or identity column.
388 : *
389 : * This includes determining the name the sequence will have. The caller
390 : * can ask to get back the name components by passing non-null pointers
391 : * for snamespace_p and sname_p.
392 : */
393 : static void
394 875 : generateSerialExtraStmts(CreateStmtContext *cxt, ColumnDef *column,
395 : Oid seqtypid, List *seqoptions,
396 : bool for_identity, bool col_exists,
397 : char **snamespace_p, char **sname_p)
398 : {
399 : ListCell *option;
400 875 : DefElem *nameEl = NULL;
401 875 : DefElem *loggedEl = NULL;
402 : Oid snamespaceid;
403 : char *snamespace;
404 : char *sname;
405 : char seqpersistence;
406 : CreateSeqStmt *seqstmt;
407 : AlterSeqStmt *altseqstmt;
408 : List *attnamelist;
409 :
410 : /* Make a copy of this as we may end up modifying it in the code below */
411 875 : seqoptions = list_copy(seqoptions);
412 :
413 : /*
414 : * Check for non-SQL-standard options (not supported within CREATE
415 : * SEQUENCE, because they'd be redundant), and remove them from the
416 : * seqoptions list if found.
417 : */
418 1081 : foreach(option, seqoptions)
419 : {
420 206 : DefElem *defel = lfirst_node(DefElem, option);
421 :
422 206 : if (strcmp(defel->defname, "sequence_name") == 0)
423 : {
424 22 : if (nameEl)
425 0 : errorConflictingDefElem(defel, cxt->pstate);
426 22 : nameEl = defel;
427 22 : seqoptions = foreach_delete_current(seqoptions, option);
428 : }
429 184 : else if (strcmp(defel->defname, "logged") == 0 ||
430 183 : strcmp(defel->defname, "unlogged") == 0)
431 : {
432 2 : if (loggedEl)
433 0 : errorConflictingDefElem(defel, cxt->pstate);
434 2 : loggedEl = defel;
435 2 : seqoptions = foreach_delete_current(seqoptions, option);
436 : }
437 : }
438 :
439 : /*
440 : * Determine namespace and name to use for the sequence.
441 : */
442 875 : if (nameEl)
443 : {
444 : /* Use specified name */
445 22 : RangeVar *rv = makeRangeVarFromNameList(castNode(List, nameEl->arg));
446 :
447 22 : snamespace = rv->schemaname;
448 22 : if (!snamespace)
449 : {
450 : /* Given unqualified SEQUENCE NAME, select namespace */
451 0 : if (cxt->rel)
452 0 : snamespaceid = RelationGetNamespace(cxt->rel);
453 : else
454 0 : snamespaceid = RangeVarGetCreationNamespace(cxt->relation);
455 0 : snamespace = get_namespace_name(snamespaceid);
456 : }
457 22 : sname = rv->relname;
458 : }
459 : else
460 : {
461 : /*
462 : * Generate a name.
463 : *
464 : * Although we use ChooseRelationName, it's not guaranteed that the
465 : * selected sequence name won't conflict; given sufficiently long
466 : * field names, two different serial columns in the same table could
467 : * be assigned the same sequence name, and we'd not notice since we
468 : * aren't creating the sequence quite yet. In practice this seems
469 : * quite unlikely to be a problem, especially since few people would
470 : * need two serial columns in one table.
471 : */
472 853 : if (cxt->rel)
473 137 : snamespaceid = RelationGetNamespace(cxt->rel);
474 : else
475 : {
476 716 : snamespaceid = RangeVarGetCreationNamespace(cxt->relation);
477 716 : RangeVarAdjustRelationPersistence(cxt->relation, snamespaceid);
478 : }
479 853 : snamespace = get_namespace_name(snamespaceid);
480 853 : sname = ChooseRelationName(cxt->relation->relname,
481 853 : column->colname,
482 : "seq",
483 : snamespaceid,
484 : false);
485 : }
486 :
487 875 : ereport(DEBUG1,
488 : (errmsg_internal("%s will create implicit sequence \"%s\" for serial column \"%s.%s\"",
489 : cxt->stmtType, sname,
490 : cxt->relation->relname, column->colname)));
491 :
492 : /*
493 : * Determine the persistence of the sequence. By default we copy the
494 : * persistence of the table, but if LOGGED or UNLOGGED was specified, use
495 : * that (as long as the table isn't TEMP).
496 : *
497 : * For CREATE TABLE, we get the persistence from cxt->relation, which
498 : * comes from the CreateStmt in progress. For ALTER TABLE, the parser
499 : * won't set cxt->relation->relpersistence, but we have cxt->rel as the
500 : * existing table, so we copy the persistence from there.
501 : */
502 875 : seqpersistence = cxt->rel ? cxt->rel->rd_rel->relpersistence : cxt->relation->relpersistence;
503 875 : if (loggedEl)
504 : {
505 2 : if (seqpersistence == RELPERSISTENCE_TEMP)
506 0 : ereport(ERROR,
507 : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
508 : errmsg("cannot set logged status of a temporary sequence"),
509 : parser_errposition(cxt->pstate, loggedEl->location)));
510 2 : else if (strcmp(loggedEl->defname, "logged") == 0)
511 1 : seqpersistence = RELPERSISTENCE_PERMANENT;
512 : else
513 1 : seqpersistence = RELPERSISTENCE_UNLOGGED;
514 : }
515 :
516 : /*
517 : * Build a CREATE SEQUENCE command to create the sequence object, and add
518 : * it to the list of things to be done before this CREATE/ALTER TABLE.
519 : */
520 875 : seqstmt = makeNode(CreateSeqStmt);
521 875 : seqstmt->for_identity = for_identity;
522 875 : seqstmt->sequence = makeRangeVar(snamespace, sname, -1);
523 875 : seqstmt->sequence->relpersistence = seqpersistence;
524 875 : seqstmt->options = seqoptions;
525 :
526 : /*
527 : * If a sequence data type was specified, add it to the options. Prepend
528 : * to the list rather than append; in case a user supplied their own AS
529 : * clause, the "redundant options" error will point to their occurrence,
530 : * not our synthetic one.
531 : */
532 875 : if (seqtypid)
533 867 : seqstmt->options = lcons(makeDefElem("as",
534 867 : (Node *) makeTypeNameFromOid(seqtypid, -1),
535 : -1),
536 : seqstmt->options);
537 :
538 : /*
539 : * If this is ALTER ADD COLUMN, make sure the sequence will be owned by
540 : * the table's owner. The current user might be someone else (perhaps a
541 : * superuser, or someone who's only a member of the owning role), but the
542 : * SEQUENCE OWNED BY mechanisms will bleat unless table and sequence have
543 : * exactly the same owning role.
544 : */
545 875 : if (cxt->rel)
546 159 : seqstmt->ownerId = cxt->rel->rd_rel->relowner;
547 : else
548 716 : seqstmt->ownerId = InvalidOid;
549 :
550 875 : cxt->blist = lappend(cxt->blist, seqstmt);
551 :
552 : /*
553 : * Store the identity sequence name that we decided on. ALTER TABLE ...
554 : * ADD COLUMN ... IDENTITY needs this so that it can fill the new column
555 : * with values from the sequence, while the association of the sequence
556 : * with the table is not set until after the ALTER TABLE.
557 : */
558 875 : column->identitySequence = seqstmt->sequence;
559 :
560 : /*
561 : * Build an ALTER SEQUENCE ... OWNED BY command to mark the sequence as
562 : * owned by this column, and add it to the appropriate list of things to
563 : * be done along with this CREATE/ALTER TABLE. In a CREATE or ALTER ADD
564 : * COLUMN, it must be done after the statement because we don't know the
565 : * column's attnum yet. But if we do have the attnum (in AT_AddIdentity),
566 : * we can do the marking immediately, which improves some ALTER TABLE
567 : * behaviors.
568 : */
569 875 : altseqstmt = makeNode(AlterSeqStmt);
570 875 : altseqstmt->sequence = makeRangeVar(snamespace, sname, -1);
571 875 : attnamelist = list_make3(makeString(snamespace),
572 : makeString(cxt->relation->relname),
573 : makeString(column->colname));
574 875 : altseqstmt->options = list_make1(makeDefElem("owned_by",
575 : (Node *) attnamelist, -1));
576 875 : altseqstmt->for_identity = for_identity;
577 :
578 875 : if (col_exists)
579 103 : cxt->blist = lappend(cxt->blist, altseqstmt);
580 : else
581 772 : cxt->alist = lappend(cxt->alist, altseqstmt);
582 :
583 875 : if (snamespace_p)
584 544 : *snamespace_p = snamespace;
585 875 : if (sname_p)
586 544 : *sname_p = sname;
587 875 : }
588 :
589 : /*
590 : * transformColumnDefinition -
591 : * transform a single ColumnDef within CREATE TABLE
592 : * Also used in ALTER TABLE ADD COLUMN
593 : */
594 : static void
595 43672 : transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column)
596 : {
597 : bool is_serial;
598 : bool saw_nullable;
599 : bool saw_default;
600 : bool saw_identity;
601 : bool saw_generated;
602 43672 : bool need_notnull = false;
603 43672 : bool disallow_noinherit_notnull = false;
604 43672 : Constraint *notnull_constraint = NULL;
605 :
606 43672 : cxt->columns = lappend(cxt->columns, column);
607 :
608 : /* Check for SERIAL pseudo-types */
609 43672 : is_serial = false;
610 43672 : if (column->typeName
611 43452 : && list_length(column->typeName->names) == 1
612 18229 : && !column->typeName->pct_type)
613 : {
614 18229 : char *typname = strVal(linitial(column->typeName->names));
615 :
616 18229 : if (strcmp(typname, "smallserial") == 0 ||
617 18224 : strcmp(typname, "serial2") == 0)
618 : {
619 9 : is_serial = true;
620 9 : column->typeName->names = NIL;
621 9 : column->typeName->typeOid = INT2OID;
622 : }
623 18220 : else if (strcmp(typname, "serial") == 0 ||
624 17703 : strcmp(typname, "serial4") == 0)
625 : {
626 517 : is_serial = true;
627 517 : column->typeName->names = NIL;
628 517 : column->typeName->typeOid = INT4OID;
629 : }
630 17703 : else if (strcmp(typname, "bigserial") == 0 ||
631 17693 : strcmp(typname, "serial8") == 0)
632 : {
633 18 : is_serial = true;
634 18 : column->typeName->names = NIL;
635 18 : column->typeName->typeOid = INT8OID;
636 : }
637 :
638 : /*
639 : * We have to reject "serial[]" explicitly, because once we've set
640 : * typeid, LookupTypeName won't notice arrayBounds. We don't need any
641 : * special coding for serial(typmod) though.
642 : */
643 18229 : if (is_serial && column->typeName->arrayBounds != NIL)
644 0 : ereport(ERROR,
645 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
646 : errmsg("array of serial is not implemented"),
647 : parser_errposition(cxt->pstate,
648 : column->typeName->location)));
649 : }
650 :
651 : /* Do necessary work on the column type declaration */
652 43672 : if (column->typeName)
653 43452 : transformColumnType(cxt, column);
654 :
655 : /* Special actions for SERIAL pseudo-types */
656 43647 : if (is_serial)
657 : {
658 : char *snamespace;
659 : char *sname;
660 : char *qstring;
661 : A_Const *snamenode;
662 : TypeCast *castnode;
663 : FuncCall *funccallnode;
664 : Constraint *constraint;
665 :
666 544 : generateSerialExtraStmts(cxt, column,
667 544 : column->typeName->typeOid, NIL,
668 : false, false,
669 : &snamespace, &sname);
670 :
671 : /*
672 : * Create appropriate constraints for SERIAL. We do this in full,
673 : * rather than shortcutting, so that we will detect any conflicting
674 : * constraints the user wrote (like a different DEFAULT).
675 : *
676 : * Create an expression tree representing the function call
677 : * nextval('sequencename'). We cannot reduce the raw tree to cooked
678 : * form until after the sequence is created, but there's no need to do
679 : * so.
680 : */
681 544 : qstring = quote_qualified_identifier(snamespace, sname);
682 544 : snamenode = makeNode(A_Const);
683 544 : snamenode->val.node.type = T_String;
684 544 : snamenode->val.sval.sval = qstring;
685 544 : snamenode->location = -1;
686 544 : castnode = makeNode(TypeCast);
687 544 : castnode->typeName = SystemTypeName("regclass");
688 544 : castnode->arg = (Node *) snamenode;
689 544 : castnode->location = -1;
690 544 : funccallnode = makeFuncCall(SystemFuncName("nextval"),
691 544 : list_make1(castnode),
692 : COERCE_EXPLICIT_CALL,
693 : -1);
694 544 : constraint = makeNode(Constraint);
695 544 : constraint->contype = CONSTR_DEFAULT;
696 544 : constraint->location = -1;
697 544 : constraint->raw_expr = (Node *) funccallnode;
698 544 : constraint->cooked_expr = NULL;
699 544 : column->constraints = lappend(column->constraints, constraint);
700 :
701 : /* have a not-null constraint added later */
702 544 : need_notnull = true;
703 544 : disallow_noinherit_notnull = true;
704 : }
705 :
706 : /* Process column constraints, if any... */
707 43647 : transformConstraintAttrs(cxt, column->constraints);
708 :
709 : /*
710 : * First, scan the column's constraints to see if a not-null constraint
711 : * that we add must be prevented from being NO INHERIT. This should be
712 : * enforced only for PRIMARY KEY, not IDENTITY or SERIAL. However, if the
713 : * not-null constraint is specified as a table constraint rather than as a
714 : * column constraint, AddRelationNotNullConstraints would raise an error
715 : * if a NO INHERIT mismatch is found. To avoid inconsistently disallowing
716 : * it in the table constraint case but not the column constraint case, we
717 : * disallow it here as well. Maybe AddRelationNotNullConstraints can be
718 : * improved someday, so that it doesn't complain, and then we can remove
719 : * the restriction for SERIAL and IDENTITY here as well.
720 : */
721 43631 : if (!disallow_noinherit_notnull)
722 : {
723 97057 : foreach_node(Constraint, constraint, column->constraints)
724 : {
725 10883 : switch (constraint->contype)
726 : {
727 3464 : case CONSTR_IDENTITY:
728 : case CONSTR_PRIMARY:
729 3464 : disallow_noinherit_notnull = true;
730 3464 : break;
731 7419 : default:
732 7419 : break;
733 : }
734 : }
735 : }
736 :
737 : /* Now scan them again to do full processing */
738 43631 : saw_nullable = false;
739 43631 : saw_default = false;
740 43631 : saw_identity = false;
741 43631 : saw_generated = false;
742 :
743 98826 : foreach_node(Constraint, constraint, column->constraints)
744 : {
745 11780 : switch (constraint->contype)
746 : {
747 15 : case CONSTR_NULL:
748 15 : if ((saw_nullable && column->is_not_null) || need_notnull)
749 4 : ereport(ERROR,
750 : (errcode(ERRCODE_SYNTAX_ERROR),
751 : errmsg("conflicting NULL/NOT NULL declarations for column \"%s\" of table \"%s\"",
752 : column->colname, cxt->relation->relname),
753 : parser_errposition(cxt->pstate,
754 : constraint->location)));
755 11 : column->is_not_null = false;
756 11 : saw_nullable = true;
757 11 : break;
758 :
759 3839 : case CONSTR_NOTNULL:
760 3839 : if (cxt->ispartitioned && constraint->is_no_inherit)
761 4 : ereport(ERROR,
762 : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
763 : errmsg("not-null constraints on partitioned tables cannot be NO INHERIT"));
764 :
765 : /* Disallow conflicting [NOT] NULL markings */
766 3835 : if (saw_nullable && !column->is_not_null)
767 0 : ereport(ERROR,
768 : (errcode(ERRCODE_SYNTAX_ERROR),
769 : errmsg("conflicting NULL/NOT NULL declarations for column \"%s\" of table \"%s\"",
770 : column->colname, cxt->relation->relname),
771 : parser_errposition(cxt->pstate,
772 : constraint->location)));
773 :
774 3835 : if (disallow_noinherit_notnull && constraint->is_no_inherit)
775 20 : ereport(ERROR,
776 : errcode(ERRCODE_SYNTAX_ERROR),
777 : errmsg("conflicting NO INHERIT declarations for not-null constraints on column \"%s\"",
778 : column->colname));
779 :
780 : /*
781 : * If this is the first time we see this column being marked
782 : * not-null, add the constraint entry and keep track of it.
783 : * Also, remove previous markings that we need one.
784 : *
785 : * If this is a redundant not-null specification, just check
786 : * that it doesn't conflict with what was specified earlier.
787 : *
788 : * Any conflicts with table constraints will be further
789 : * checked in AddRelationNotNullConstraints().
790 : */
791 3815 : if (!column->is_not_null)
792 : {
793 3799 : column->is_not_null = true;
794 3799 : saw_nullable = true;
795 3799 : need_notnull = false;
796 :
797 3799 : constraint->keys = list_make1(makeString(column->colname));
798 3799 : notnull_constraint = constraint;
799 3799 : cxt->nnconstraints = lappend(cxt->nnconstraints, constraint);
800 : }
801 16 : else if (notnull_constraint)
802 : {
803 16 : if (constraint->conname &&
804 12 : notnull_constraint->conname &&
805 8 : strcmp(notnull_constraint->conname, constraint->conname) != 0)
806 4 : elog(ERROR, "conflicting not-null constraint names \"%s\" and \"%s\"",
807 : notnull_constraint->conname, constraint->conname);
808 :
809 12 : if (notnull_constraint->is_no_inherit != constraint->is_no_inherit)
810 0 : ereport(ERROR,
811 : errcode(ERRCODE_SYNTAX_ERROR),
812 : errmsg("conflicting NO INHERIT declarations for not-null constraints on column \"%s\"",
813 : column->colname));
814 :
815 12 : if (!notnull_constraint->conname && constraint->conname)
816 4 : notnull_constraint->conname = constraint->conname;
817 : }
818 :
819 3811 : break;
820 :
821 1644 : case CONSTR_DEFAULT:
822 1644 : if (saw_default)
823 0 : ereport(ERROR,
824 : (errcode(ERRCODE_SYNTAX_ERROR),
825 : errmsg("multiple default values specified for column \"%s\" of table \"%s\"",
826 : column->colname, cxt->relation->relname),
827 : parser_errposition(cxt->pstate,
828 : constraint->location)));
829 1644 : column->raw_default = constraint->raw_expr;
830 : Assert(constraint->cooked_expr == NULL);
831 1644 : saw_default = true;
832 1644 : break;
833 :
834 244 : case CONSTR_IDENTITY:
835 : {
836 : Type ctype;
837 : Oid typeOid;
838 :
839 244 : if (cxt->ofType)
840 4 : ereport(ERROR,
841 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
842 : errmsg("identity columns are not supported on typed tables")));
843 240 : if (cxt->partbound)
844 16 : ereport(ERROR,
845 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
846 : errmsg("identity columns are not supported on partitions")));
847 :
848 224 : ctype = typenameType(cxt->pstate, column->typeName, NULL);
849 224 : typeOid = ((Form_pg_type) GETSTRUCT(ctype))->oid;
850 224 : ReleaseSysCache(ctype);
851 :
852 224 : if (saw_identity)
853 4 : ereport(ERROR,
854 : (errcode(ERRCODE_SYNTAX_ERROR),
855 : errmsg("multiple identity specifications for column \"%s\" of table \"%s\"",
856 : column->colname, cxt->relation->relname),
857 : parser_errposition(cxt->pstate,
858 : constraint->location)));
859 :
860 220 : generateSerialExtraStmts(cxt, column,
861 : typeOid, constraint->options,
862 : true, false,
863 : NULL, NULL);
864 :
865 220 : column->identity = constraint->generated_when;
866 220 : saw_identity = true;
867 :
868 : /*
869 : * Identity columns are always NOT NULL, but we may have a
870 : * constraint already.
871 : */
872 220 : if (!saw_nullable)
873 204 : need_notnull = true;
874 16 : else if (!column->is_not_null)
875 4 : ereport(ERROR,
876 : (errcode(ERRCODE_SYNTAX_ERROR),
877 : errmsg("conflicting NULL/NOT NULL declarations for column \"%s\" of table \"%s\"",
878 : column->colname, cxt->relation->relname),
879 : parser_errposition(cxt->pstate,
880 : constraint->location)));
881 216 : break;
882 : }
883 :
884 1233 : case CONSTR_GENERATED:
885 1233 : if (cxt->ofType)
886 8 : ereport(ERROR,
887 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
888 : errmsg("generated columns are not supported on typed tables")));
889 1225 : if (saw_generated)
890 8 : ereport(ERROR,
891 : (errcode(ERRCODE_SYNTAX_ERROR),
892 : errmsg("multiple generation clauses specified for column \"%s\" of table \"%s\"",
893 : column->colname, cxt->relation->relname),
894 : parser_errposition(cxt->pstate,
895 : constraint->location)));
896 1217 : column->generated = constraint->generated_kind;
897 1217 : column->raw_default = constraint->raw_expr;
898 : Assert(constraint->cooked_expr == NULL);
899 1217 : saw_generated = true;
900 1217 : break;
901 :
902 340 : case CONSTR_CHECK:
903 340 : cxt->ckconstraints = lappend(cxt->ckconstraints, constraint);
904 340 : break;
905 :
906 3496 : case CONSTR_PRIMARY:
907 3496 : if (saw_nullable && !column->is_not_null)
908 0 : ereport(ERROR,
909 : (errcode(ERRCODE_SYNTAX_ERROR),
910 : errmsg("conflicting NULL/NOT NULL declarations for column \"%s\" of table \"%s\"",
911 : column->colname, cxt->relation->relname),
912 : parser_errposition(cxt->pstate,
913 : constraint->location)));
914 3496 : need_notnull = true;
915 :
916 3496 : if (cxt->isforeign)
917 4 : ereport(ERROR,
918 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
919 : errmsg("primary key constraints are not supported on foreign tables"),
920 : parser_errposition(cxt->pstate,
921 : constraint->location)));
922 : pg_fallthrough;
923 :
924 : case CONSTR_UNIQUE:
925 3733 : if (cxt->isforeign)
926 0 : ereport(ERROR,
927 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
928 : errmsg("unique constraints are not supported on foreign tables"),
929 : parser_errposition(cxt->pstate,
930 : constraint->location)));
931 3733 : if (constraint->keys == NIL)
932 3733 : constraint->keys = list_make1(makeString(column->colname));
933 3733 : cxt->ixconstraints = lappend(cxt->ixconstraints, constraint);
934 3733 : break;
935 :
936 0 : case CONSTR_EXCLUSION:
937 : /* grammar does not allow EXCLUDE as a column constraint */
938 0 : elog(ERROR, "column exclusion constraints are not supported");
939 : break;
940 :
941 568 : case CONSTR_FOREIGN:
942 568 : if (cxt->isforeign)
943 4 : ereport(ERROR,
944 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
945 : errmsg("foreign key constraints are not supported on foreign tables"),
946 : parser_errposition(cxt->pstate,
947 : constraint->location)));
948 :
949 : /*
950 : * Fill in the current attribute's name and throw it into the
951 : * list of FK constraints to be processed later.
952 : */
953 564 : constraint->fk_attrs = list_make1(makeString(column->colname));
954 564 : cxt->fkconstraints = lappend(cxt->fkconstraints, constraint);
955 564 : break;
956 :
957 160 : case CONSTR_ATTR_DEFERRABLE:
958 : case CONSTR_ATTR_NOT_DEFERRABLE:
959 : case CONSTR_ATTR_DEFERRED:
960 : case CONSTR_ATTR_IMMEDIATE:
961 : case CONSTR_ATTR_ENFORCED:
962 : case CONSTR_ATTR_NOT_ENFORCED:
963 : /* transformConstraintAttrs took care of these */
964 160 : break;
965 :
966 0 : default:
967 0 : elog(ERROR, "unrecognized constraint type: %d",
968 : constraint->contype);
969 : break;
970 : }
971 :
972 11696 : if (saw_default && saw_identity)
973 8 : ereport(ERROR,
974 : (errcode(ERRCODE_SYNTAX_ERROR),
975 : errmsg("both default and identity specified for column \"%s\" of table \"%s\"",
976 : column->colname, cxt->relation->relname),
977 : parser_errposition(cxt->pstate,
978 : constraint->location)));
979 :
980 11688 : if (saw_default && saw_generated)
981 8 : ereport(ERROR,
982 : (errcode(ERRCODE_SYNTAX_ERROR),
983 : errmsg("both default and generation expression specified for column \"%s\" of table \"%s\"",
984 : column->colname, cxt->relation->relname),
985 : parser_errposition(cxt->pstate,
986 : constraint->location)));
987 :
988 11680 : if (saw_identity && saw_generated)
989 8 : ereport(ERROR,
990 : (errcode(ERRCODE_SYNTAX_ERROR),
991 : errmsg("both identity and generation expression specified for column \"%s\" of table \"%s\"",
992 : column->colname, cxt->relation->relname),
993 : parser_errposition(cxt->pstate,
994 : constraint->location)));
995 : }
996 :
997 : /*
998 : * If we need a not-null constraint for PRIMARY KEY, SERIAL or IDENTITY,
999 : * and one was not explicitly specified, add one now.
1000 : */
1001 43523 : if (need_notnull && !(saw_nullable && column->is_not_null))
1002 : {
1003 3291 : column->is_not_null = true;
1004 3291 : notnull_constraint = makeNotNullConstraint(makeString(column->colname));
1005 3291 : cxt->nnconstraints = lappend(cxt->nnconstraints, notnull_constraint);
1006 : }
1007 :
1008 : /*
1009 : * If needed, generate ALTER FOREIGN TABLE ALTER COLUMN statement to add
1010 : * per-column foreign data wrapper options to this column after creation.
1011 : */
1012 43523 : if (column->fdwoptions != NIL)
1013 : {
1014 : AlterTableStmt *stmt;
1015 : AlterTableCmd *cmd;
1016 :
1017 85 : cmd = makeNode(AlterTableCmd);
1018 85 : cmd->subtype = AT_AlterColumnGenericOptions;
1019 85 : cmd->name = column->colname;
1020 85 : cmd->def = (Node *) column->fdwoptions;
1021 85 : cmd->behavior = DROP_RESTRICT;
1022 85 : cmd->missing_ok = false;
1023 :
1024 85 : stmt = makeNode(AlterTableStmt);
1025 85 : stmt->relation = cxt->relation;
1026 85 : stmt->cmds = NIL;
1027 85 : stmt->objtype = OBJECT_FOREIGN_TABLE;
1028 85 : stmt->cmds = lappend(stmt->cmds, cmd);
1029 :
1030 85 : cxt->alist = lappend(cxt->alist, stmt);
1031 : }
1032 43523 : }
1033 :
1034 : /*
1035 : * transformTableConstraint
1036 : * transform a Constraint node within CREATE TABLE or ALTER TABLE
1037 : */
1038 : static void
1039 11749 : transformTableConstraint(CreateStmtContext *cxt, Constraint *constraint)
1040 : {
1041 11749 : switch (constraint->contype)
1042 : {
1043 4669 : case CONSTR_PRIMARY:
1044 4669 : if (cxt->isforeign)
1045 4 : ereport(ERROR,
1046 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1047 : errmsg("primary key constraints are not supported on foreign tables"),
1048 : parser_errposition(cxt->pstate,
1049 : constraint->location)));
1050 4665 : cxt->ixconstraints = lappend(cxt->ixconstraints, constraint);
1051 4665 : break;
1052 :
1053 3014 : case CONSTR_UNIQUE:
1054 3014 : if (cxt->isforeign)
1055 4 : ereport(ERROR,
1056 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1057 : errmsg("unique constraints are not supported on foreign tables"),
1058 : parser_errposition(cxt->pstate,
1059 : constraint->location)));
1060 3010 : cxt->ixconstraints = lappend(cxt->ixconstraints, constraint);
1061 3010 : break;
1062 :
1063 153 : case CONSTR_EXCLUSION:
1064 153 : if (cxt->isforeign)
1065 0 : ereport(ERROR,
1066 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1067 : errmsg("exclusion constraints are not supported on foreign tables"),
1068 : parser_errposition(cxt->pstate,
1069 : constraint->location)));
1070 153 : cxt->ixconstraints = lappend(cxt->ixconstraints, constraint);
1071 153 : break;
1072 :
1073 1013 : case CONSTR_CHECK:
1074 1013 : cxt->ckconstraints = lappend(cxt->ckconstraints, constraint);
1075 1013 : break;
1076 :
1077 732 : case CONSTR_NOTNULL:
1078 732 : if (cxt->ispartitioned && constraint->is_no_inherit)
1079 4 : ereport(ERROR,
1080 : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1081 : errmsg("not-null constraints on partitioned tables cannot be NO INHERIT"));
1082 :
1083 728 : cxt->nnconstraints = lappend(cxt->nnconstraints, constraint);
1084 728 : break;
1085 :
1086 2168 : case CONSTR_FOREIGN:
1087 2168 : if (cxt->isforeign)
1088 0 : ereport(ERROR,
1089 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1090 : errmsg("foreign key constraints are not supported on foreign tables"),
1091 : parser_errposition(cxt->pstate,
1092 : constraint->location)));
1093 2168 : cxt->fkconstraints = lappend(cxt->fkconstraints, constraint);
1094 2168 : break;
1095 :
1096 0 : case CONSTR_NULL:
1097 : case CONSTR_DEFAULT:
1098 : case CONSTR_ATTR_DEFERRABLE:
1099 : case CONSTR_ATTR_NOT_DEFERRABLE:
1100 : case CONSTR_ATTR_DEFERRED:
1101 : case CONSTR_ATTR_IMMEDIATE:
1102 : case CONSTR_ATTR_ENFORCED:
1103 : case CONSTR_ATTR_NOT_ENFORCED:
1104 0 : elog(ERROR, "invalid context for constraint type %d",
1105 : constraint->contype);
1106 : break;
1107 :
1108 0 : default:
1109 0 : elog(ERROR, "unrecognized constraint type: %d",
1110 : constraint->contype);
1111 : break;
1112 : }
1113 11737 : }
1114 :
1115 : /*
1116 : * transformTableLikeClause
1117 : *
1118 : * Change the LIKE <srctable> portion of a CREATE TABLE statement into
1119 : * column definitions that recreate the user defined column portions of
1120 : * <srctable>. Also, if there are any LIKE options that we can't fully
1121 : * process at this point, add the TableLikeClause to cxt->likeclauses, which
1122 : * will cause utility.c to call expandTableLikeClause() after the new
1123 : * table has been created.
1124 : *
1125 : * Some options are ignored. For example, as foreign tables have no storage,
1126 : * these INCLUDING options have no effect: STORAGE, COMPRESSION, IDENTITY
1127 : * and INDEXES. Similarly, INCLUDING INDEXES is ignored from a view.
1128 : */
1129 : static void
1130 516 : transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_clause)
1131 : {
1132 : AttrNumber parent_attno;
1133 : Relation relation;
1134 : TupleDesc tupleDesc;
1135 : AclResult aclresult;
1136 : char *comment;
1137 : ParseCallbackState pcbstate;
1138 :
1139 516 : setup_parser_errposition_callback(&pcbstate, cxt->pstate,
1140 516 : table_like_clause->relation->location);
1141 :
1142 : /* Open the relation referenced by the LIKE clause */
1143 516 : relation = relation_openrv(table_like_clause->relation, AccessShareLock);
1144 :
1145 512 : if (relation->rd_rel->relkind != RELKIND_RELATION &&
1146 254 : relation->rd_rel->relkind != RELKIND_VIEW &&
1147 246 : relation->rd_rel->relkind != RELKIND_MATVIEW &&
1148 246 : relation->rd_rel->relkind != RELKIND_COMPOSITE_TYPE &&
1149 242 : relation->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
1150 242 : relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1151 4 : ereport(ERROR,
1152 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1153 : errmsg("relation \"%s\" is invalid in LIKE clause",
1154 : RelationGetRelationName(relation)),
1155 : errdetail_relkind_not_supported(relation->rd_rel->relkind)));
1156 :
1157 508 : cancel_parser_errposition_callback(&pcbstate);
1158 :
1159 : /*
1160 : * Check for privileges
1161 : */
1162 508 : if (relation->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
1163 : {
1164 4 : aclresult = object_aclcheck(TypeRelationId, relation->rd_rel->reltype, GetUserId(),
1165 : ACL_USAGE);
1166 4 : if (aclresult != ACLCHECK_OK)
1167 0 : aclcheck_error(aclresult, OBJECT_TYPE,
1168 0 : RelationGetRelationName(relation));
1169 : }
1170 : else
1171 : {
1172 504 : aclresult = pg_class_aclcheck(RelationGetRelid(relation), GetUserId(),
1173 : ACL_SELECT);
1174 504 : if (aclresult != ACLCHECK_OK)
1175 0 : aclcheck_error(aclresult, get_relkind_objtype(relation->rd_rel->relkind),
1176 0 : RelationGetRelationName(relation));
1177 : }
1178 :
1179 508 : tupleDesc = RelationGetDescr(relation);
1180 :
1181 : /*
1182 : * Insert the copied attributes into the cxt for the new table definition.
1183 : * We must do this now so that they appear in the table in the relative
1184 : * position where the LIKE clause is, as required by SQL99.
1185 : */
1186 1641 : for (parent_attno = 1; parent_attno <= tupleDesc->natts;
1187 1133 : parent_attno++)
1188 : {
1189 1133 : Form_pg_attribute attribute = TupleDescAttr(tupleDesc,
1190 : parent_attno - 1);
1191 : ColumnDef *def;
1192 :
1193 : /*
1194 : * Ignore dropped columns in the parent.
1195 : */
1196 1133 : if (attribute->attisdropped)
1197 16 : continue;
1198 :
1199 : /*
1200 : * Create a new column definition
1201 : */
1202 1117 : def = makeColumnDef(NameStr(attribute->attname), attribute->atttypid,
1203 : attribute->atttypmod, attribute->attcollation);
1204 :
1205 : /*
1206 : * Add to column list
1207 : */
1208 1117 : cxt->columns = lappend(cxt->columns, def);
1209 :
1210 : /*
1211 : * Although we don't transfer the column's default/generation
1212 : * expression now, we need to mark it GENERATED if appropriate.
1213 : */
1214 1117 : if (attribute->atthasdef && attribute->attgenerated &&
1215 52 : (table_like_clause->options & CREATE_TABLE_LIKE_GENERATED))
1216 32 : def->generated = attribute->attgenerated;
1217 :
1218 : /*
1219 : * Copy identity if requested
1220 : */
1221 1117 : if (attribute->attidentity &&
1222 28 : (table_like_clause->options & CREATE_TABLE_LIKE_IDENTITY) &&
1223 12 : !cxt->isforeign)
1224 : {
1225 : Oid seq_relid;
1226 : List *seq_options;
1227 :
1228 : /*
1229 : * find sequence owned by old column; extract sequence parameters;
1230 : * build new create sequence command
1231 : */
1232 8 : seq_relid = getIdentitySequence(relation, attribute->attnum, false);
1233 8 : seq_options = sequence_options(seq_relid);
1234 8 : generateSerialExtraStmts(cxt, def,
1235 : InvalidOid, seq_options,
1236 : true, false,
1237 : NULL, NULL);
1238 8 : def->identity = attribute->attidentity;
1239 : }
1240 :
1241 : /* Likewise, copy storage if requested */
1242 1117 : if ((table_like_clause->options & CREATE_TABLE_LIKE_STORAGE) &&
1243 130 : !cxt->isforeign)
1244 110 : def->storage = attribute->attstorage;
1245 : else
1246 1007 : def->storage = 0;
1247 :
1248 : /* Likewise, copy compression if requested */
1249 1117 : if ((table_like_clause->options & CREATE_TABLE_LIKE_COMPRESSION) != 0 &&
1250 100 : CompressionMethodIsValid(attribute->attcompression) &&
1251 8 : !cxt->isforeign)
1252 4 : def->compression =
1253 4 : pstrdup(GetCompressionMethodName(attribute->attcompression));
1254 : else
1255 1113 : def->compression = NULL;
1256 :
1257 : /* Likewise, copy comment if requested */
1258 1253 : if ((table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS) &&
1259 136 : (comment = GetComment(attribute->attrelid,
1260 : RelationRelationId,
1261 136 : attribute->attnum)) != NULL)
1262 : {
1263 56 : CommentStmt *stmt = makeNode(CommentStmt);
1264 :
1265 56 : stmt->objtype = OBJECT_COLUMN;
1266 56 : stmt->object = (Node *) list_make3(makeString(cxt->relation->schemaname),
1267 : makeString(cxt->relation->relname),
1268 : makeString(def->colname));
1269 56 : stmt->comment = comment;
1270 :
1271 56 : cxt->alist = lappend(cxt->alist, stmt);
1272 : }
1273 : }
1274 :
1275 : /*
1276 : * Reproduce not-null constraints, if any, by copying them. We do this
1277 : * regardless of options given.
1278 : */
1279 508 : if (tupleDesc->constr && tupleDesc->constr->has_not_null)
1280 : {
1281 : List *lst;
1282 :
1283 226 : lst = RelationGetNotNullConstraints(RelationGetRelid(relation), false,
1284 : true);
1285 226 : cxt->nnconstraints = list_concat(cxt->nnconstraints, lst);
1286 :
1287 : /* Copy comments on not-null constraints */
1288 226 : if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
1289 : {
1290 148 : foreach_node(Constraint, nnconstr, lst)
1291 : {
1292 60 : if ((comment = GetComment(get_relation_constraint_oid(RelationGetRelid(relation),
1293 60 : nnconstr->conname, false),
1294 : ConstraintRelationId,
1295 : 0)) != NULL)
1296 : {
1297 20 : CommentStmt *stmt = makeNode(CommentStmt);
1298 :
1299 20 : stmt->objtype = OBJECT_TABCONSTRAINT;
1300 20 : stmt->object = (Node *) list_make3(makeString(cxt->relation->schemaname),
1301 : makeString(cxt->relation->relname),
1302 : makeString(nnconstr->conname));
1303 20 : stmt->comment = comment;
1304 20 : cxt->alist = lappend(cxt->alist, stmt);
1305 : }
1306 : }
1307 : }
1308 : }
1309 :
1310 : /*
1311 : * We cannot yet deal with defaults, CHECK constraints, indexes, or
1312 : * statistics, since we don't yet know what column numbers the copied
1313 : * columns will have in the finished table. If any of those options are
1314 : * specified, add the LIKE clause to cxt->likeclauses so that
1315 : * expandTableLikeClause will be called after we do know that.
1316 : *
1317 : * In order for this to work, we remember the relation OID so that
1318 : * expandTableLikeClause is certain to open the same table.
1319 : */
1320 508 : if (table_like_clause->options &
1321 : (CREATE_TABLE_LIKE_DEFAULTS |
1322 : CREATE_TABLE_LIKE_GENERATED |
1323 : CREATE_TABLE_LIKE_CONSTRAINTS |
1324 : CREATE_TABLE_LIKE_INDEXES |
1325 : CREATE_TABLE_LIKE_STATISTICS))
1326 : {
1327 129 : table_like_clause->relationOid = RelationGetRelid(relation);
1328 129 : cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
1329 : }
1330 :
1331 : /*
1332 : * Close the parent rel, but keep our AccessShareLock on it until xact
1333 : * commit. That will prevent someone else from deleting or ALTERing the
1334 : * parent before we can run expandTableLikeClause.
1335 : */
1336 508 : table_close(relation, NoLock);
1337 508 : }
1338 :
1339 : /*
1340 : * expandTableLikeClause
1341 : *
1342 : * Process LIKE options that require knowing the final column numbers
1343 : * assigned to the new table's columns. This executes after we have
1344 : * run DefineRelation for the new table. It returns a list of utility
1345 : * commands that should be run to generate indexes etc.
1346 : */
1347 : List *
1348 129 : expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
1349 : {
1350 129 : List *result = NIL;
1351 129 : List *atsubcmds = NIL;
1352 : AttrNumber parent_attno;
1353 : Relation relation;
1354 : Relation childrel;
1355 : TupleDesc tupleDesc;
1356 : TupleConstr *constr;
1357 : AttrMap *attmap;
1358 : char *comment;
1359 :
1360 : /*
1361 : * Open the relation referenced by the LIKE clause. We should still have
1362 : * the table lock obtained by transformTableLikeClause (and this'll throw
1363 : * an assertion failure if not). Hence, no need to recheck privileges
1364 : * etc. We must open the rel by OID not name, to be sure we get the same
1365 : * table.
1366 : */
1367 129 : if (!OidIsValid(table_like_clause->relationOid))
1368 0 : elog(ERROR, "expandTableLikeClause called on untransformed LIKE clause");
1369 :
1370 129 : relation = relation_open(table_like_clause->relationOid, NoLock);
1371 :
1372 129 : tupleDesc = RelationGetDescr(relation);
1373 129 : constr = tupleDesc->constr;
1374 :
1375 : /*
1376 : * Open the newly-created child relation; we have lock on that too.
1377 : */
1378 129 : childrel = relation_openrv(heapRel, NoLock);
1379 :
1380 : /*
1381 : * Construct a map from the LIKE relation's attnos to the child rel's.
1382 : * This re-checks type match etc, although it shouldn't be possible to
1383 : * have a failure since both tables are locked.
1384 : */
1385 129 : attmap = build_attrmap_by_name(RelationGetDescr(childrel),
1386 : tupleDesc,
1387 : false);
1388 :
1389 : /*
1390 : * Process defaults, if required.
1391 : */
1392 129 : if ((table_like_clause->options &
1393 69 : (CREATE_TABLE_LIKE_DEFAULTS | CREATE_TABLE_LIKE_GENERATED)) &&
1394 : constr != NULL)
1395 : {
1396 231 : for (parent_attno = 1; parent_attno <= tupleDesc->natts;
1397 170 : parent_attno++)
1398 : {
1399 170 : Form_pg_attribute attribute = TupleDescAttr(tupleDesc,
1400 : parent_attno - 1);
1401 :
1402 : /*
1403 : * Ignore dropped columns in the parent.
1404 : */
1405 170 : if (attribute->attisdropped)
1406 8 : continue;
1407 :
1408 : /*
1409 : * Copy default, if present and it should be copied. We have
1410 : * separate options for plain default expressions and GENERATED
1411 : * defaults.
1412 : */
1413 223 : if (attribute->atthasdef &&
1414 61 : (attribute->attgenerated ?
1415 36 : (table_like_clause->options & CREATE_TABLE_LIKE_GENERATED) :
1416 25 : (table_like_clause->options & CREATE_TABLE_LIKE_DEFAULTS)))
1417 : {
1418 : Node *this_default;
1419 : AlterTableCmd *atsubcmd;
1420 : bool found_whole_row;
1421 :
1422 53 : this_default = TupleDescGetDefault(tupleDesc, parent_attno);
1423 53 : if (this_default == NULL)
1424 0 : elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
1425 : parent_attno, RelationGetRelationName(relation));
1426 :
1427 53 : atsubcmd = makeNode(AlterTableCmd);
1428 53 : atsubcmd->subtype = AT_CookedColumnDefault;
1429 53 : atsubcmd->num = attmap->attnums[parent_attno - 1];
1430 53 : atsubcmd->def = map_variable_attnos(this_default,
1431 : 1, 0,
1432 : attmap,
1433 : InvalidOid,
1434 : &found_whole_row);
1435 :
1436 : /*
1437 : * Prevent this for the same reason as for constraints below.
1438 : * Note that defaults cannot contain any vars, so it's OK that
1439 : * the error message refers to generated columns.
1440 : */
1441 53 : if (found_whole_row)
1442 0 : ereport(ERROR,
1443 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1444 : errmsg("cannot convert whole-row table reference"),
1445 : errdetail("Generation expression for column \"%s\" contains a whole-row reference to table \"%s\".",
1446 : NameStr(attribute->attname),
1447 : RelationGetRelationName(relation))));
1448 :
1449 53 : atsubcmds = lappend(atsubcmds, atsubcmd);
1450 : }
1451 : }
1452 : }
1453 :
1454 : /*
1455 : * Copy CHECK constraints if requested, being careful to adjust attribute
1456 : * numbers so they match the child.
1457 : */
1458 129 : if ((table_like_clause->options & CREATE_TABLE_LIKE_CONSTRAINTS) &&
1459 : constr != NULL)
1460 : {
1461 : int ccnum;
1462 :
1463 168 : for (ccnum = 0; ccnum < constr->num_check; ccnum++)
1464 : {
1465 100 : char *ccname = constr->check[ccnum].ccname;
1466 100 : char *ccbin = constr->check[ccnum].ccbin;
1467 100 : bool ccenforced = constr->check[ccnum].ccenforced;
1468 100 : bool ccnoinherit = constr->check[ccnum].ccnoinherit;
1469 : Node *ccbin_node;
1470 : bool found_whole_row;
1471 : Constraint *n;
1472 : AlterTableCmd *atsubcmd;
1473 :
1474 100 : ccbin_node = map_variable_attnos(stringToNode(ccbin),
1475 : 1, 0,
1476 : attmap,
1477 : InvalidOid, &found_whole_row);
1478 :
1479 : /*
1480 : * We reject whole-row variables because the whole point of LIKE
1481 : * is that the new table's rowtype might later diverge from the
1482 : * parent's. So, while translation might be possible right now,
1483 : * it wouldn't be possible to guarantee it would work in future.
1484 : */
1485 100 : if (found_whole_row)
1486 0 : ereport(ERROR,
1487 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1488 : errmsg("cannot convert whole-row table reference"),
1489 : errdetail("Constraint \"%s\" contains a whole-row reference to table \"%s\".",
1490 : ccname,
1491 : RelationGetRelationName(relation))));
1492 :
1493 100 : n = makeNode(Constraint);
1494 100 : n->contype = CONSTR_CHECK;
1495 100 : n->conname = pstrdup(ccname);
1496 100 : n->location = -1;
1497 100 : n->is_enforced = ccenforced;
1498 100 : n->initially_valid = ccenforced; /* sic */
1499 100 : n->is_no_inherit = ccnoinherit;
1500 100 : n->raw_expr = NULL;
1501 100 : n->cooked_expr = nodeToString(ccbin_node);
1502 :
1503 : /* We can skip validation, since the new table should be empty. */
1504 100 : n->skip_validation = true;
1505 :
1506 100 : atsubcmd = makeNode(AlterTableCmd);
1507 100 : atsubcmd->subtype = AT_AddConstraint;
1508 100 : atsubcmd->def = (Node *) n;
1509 100 : atsubcmds = lappend(atsubcmds, atsubcmd);
1510 :
1511 : /* Copy comment on constraint */
1512 176 : if ((table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS) &&
1513 76 : (comment = GetComment(get_relation_constraint_oid(RelationGetRelid(relation),
1514 76 : n->conname, false),
1515 : ConstraintRelationId,
1516 : 0)) != NULL)
1517 : {
1518 20 : CommentStmt *stmt = makeNode(CommentStmt);
1519 :
1520 20 : stmt->objtype = OBJECT_TABCONSTRAINT;
1521 20 : stmt->object = (Node *) list_make3(makeString(heapRel->schemaname),
1522 : makeString(heapRel->relname),
1523 : makeString(n->conname));
1524 20 : stmt->comment = comment;
1525 :
1526 20 : result = lappend(result, stmt);
1527 : }
1528 : }
1529 : }
1530 :
1531 : /*
1532 : * If we generated any ALTER TABLE actions above, wrap them into a single
1533 : * ALTER TABLE command. Stick it at the front of the result, so it runs
1534 : * before any CommentStmts we made above.
1535 : */
1536 129 : if (atsubcmds)
1537 : {
1538 89 : AlterTableStmt *atcmd = makeNode(AlterTableStmt);
1539 :
1540 89 : atcmd->relation = copyObject(heapRel);
1541 89 : atcmd->cmds = atsubcmds;
1542 89 : atcmd->objtype = OBJECT_TABLE;
1543 89 : atcmd->missing_ok = false;
1544 89 : result = lcons(atcmd, result);
1545 : }
1546 :
1547 : /*
1548 : * Process indexes if required.
1549 : */
1550 129 : if ((table_like_clause->options & CREATE_TABLE_LIKE_INDEXES) &&
1551 73 : relation->rd_rel->relhasindex &&
1552 57 : childrel->rd_rel->relkind != RELKIND_FOREIGN_TABLE)
1553 : {
1554 : List *parent_indexes;
1555 : ListCell *l;
1556 :
1557 53 : parent_indexes = RelationGetIndexList(relation);
1558 :
1559 139 : foreach(l, parent_indexes)
1560 : {
1561 86 : Oid parent_index_oid = lfirst_oid(l);
1562 : Relation parent_index;
1563 : IndexStmt *index_stmt;
1564 :
1565 86 : parent_index = index_open(parent_index_oid, AccessShareLock);
1566 :
1567 : /* Build CREATE INDEX statement to recreate the parent_index */
1568 86 : index_stmt = generateClonedIndexStmt(heapRel,
1569 : parent_index,
1570 : attmap,
1571 : NULL);
1572 :
1573 : /* Copy comment on index, if requested */
1574 86 : if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
1575 : {
1576 48 : comment = GetComment(parent_index_oid, RelationRelationId, 0);
1577 :
1578 : /*
1579 : * We make use of IndexStmt's idxcomment option, so as not to
1580 : * need to know now what name the index will have.
1581 : */
1582 48 : index_stmt->idxcomment = comment;
1583 : }
1584 :
1585 86 : result = lappend(result, index_stmt);
1586 :
1587 86 : index_close(parent_index, AccessShareLock);
1588 : }
1589 : }
1590 :
1591 : /*
1592 : * Process extended statistics if required.
1593 : */
1594 129 : if (table_like_clause->options & CREATE_TABLE_LIKE_STATISTICS)
1595 : {
1596 : List *parent_extstats;
1597 : ListCell *l;
1598 :
1599 40 : parent_extstats = RelationGetStatExtList(relation);
1600 :
1601 72 : foreach(l, parent_extstats)
1602 : {
1603 32 : Oid parent_stat_oid = lfirst_oid(l);
1604 : CreateStatsStmt *stats_stmt;
1605 :
1606 32 : stats_stmt = generateClonedExtStatsStmt(heapRel,
1607 : RelationGetRelid(childrel),
1608 : parent_stat_oid,
1609 : attmap);
1610 :
1611 : /* Copy comment on statistics object, if requested */
1612 32 : if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
1613 : {
1614 32 : comment = GetComment(parent_stat_oid, StatisticExtRelationId, 0);
1615 :
1616 : /*
1617 : * We make use of CreateStatsStmt's stxcomment option, so as
1618 : * not to need to know now what name the statistics will have.
1619 : */
1620 32 : stats_stmt->stxcomment = comment;
1621 : }
1622 :
1623 32 : result = lappend(result, stats_stmt);
1624 : }
1625 :
1626 40 : list_free(parent_extstats);
1627 : }
1628 :
1629 : /* Done with child rel */
1630 129 : table_close(childrel, NoLock);
1631 :
1632 : /*
1633 : * Close the parent rel, but keep our AccessShareLock on it until xact
1634 : * commit. That will prevent someone else from deleting or ALTERing the
1635 : * parent before the child is committed.
1636 : */
1637 129 : table_close(relation, NoLock);
1638 :
1639 129 : return result;
1640 : }
1641 :
1642 : static void
1643 81 : transformOfType(CreateStmtContext *cxt, TypeName *ofTypename)
1644 : {
1645 : HeapTuple tuple;
1646 : TupleDesc tupdesc;
1647 : int i;
1648 : Oid ofTypeId;
1649 :
1650 : Assert(ofTypename);
1651 :
1652 81 : tuple = typenameType(cxt->pstate, ofTypename, NULL);
1653 77 : check_of_type(tuple);
1654 69 : ofTypeId = ((Form_pg_type) GETSTRUCT(tuple))->oid;
1655 69 : ofTypename->typeOid = ofTypeId; /* cached for later */
1656 :
1657 69 : tupdesc = lookup_rowtype_tupdesc(ofTypeId, -1);
1658 207 : for (i = 0; i < tupdesc->natts; i++)
1659 : {
1660 138 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
1661 : ColumnDef *n;
1662 :
1663 138 : if (attr->attisdropped)
1664 0 : continue;
1665 :
1666 138 : n = makeColumnDef(NameStr(attr->attname), attr->atttypid,
1667 : attr->atttypmod, attr->attcollation);
1668 138 : n->is_from_type = true;
1669 :
1670 138 : cxt->columns = lappend(cxt->columns, n);
1671 : }
1672 69 : ReleaseTupleDesc(tupdesc);
1673 :
1674 69 : ReleaseSysCache(tuple);
1675 69 : }
1676 :
1677 : /*
1678 : * Generate an IndexStmt node using information from an already existing index
1679 : * "source_idx".
1680 : *
1681 : * heapRel is stored into the IndexStmt's relation field, but we don't use it
1682 : * otherwise; some callers pass NULL, if they don't need it to be valid.
1683 : * (The target relation might not exist yet, so we mustn't try to access it.)
1684 : *
1685 : * Attribute numbers in expression Vars are adjusted according to attmap.
1686 : *
1687 : * If constraintOid isn't NULL, we store the OID of any constraint associated
1688 : * with the index there.
1689 : *
1690 : * Unlike transformIndexConstraint, we don't make any effort to force primary
1691 : * key columns to be not-null. The larger cloning process this is part of
1692 : * should have cloned their not-null status separately (and DefineIndex will
1693 : * complain if that fails to happen).
1694 : */
1695 : IndexStmt *
1696 2130 : generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx,
1697 : const AttrMap *attmap,
1698 : Oid *constraintOid)
1699 : {
1700 2130 : Oid source_relid = RelationGetRelid(source_idx);
1701 : HeapTuple ht_idxrel;
1702 : HeapTuple ht_idx;
1703 : HeapTuple ht_am;
1704 : Form_pg_class idxrelrec;
1705 : Form_pg_index idxrec;
1706 : Form_pg_am amrec;
1707 : oidvector *indcollation;
1708 : oidvector *indclass;
1709 : IndexStmt *index;
1710 : List *indexprs;
1711 : ListCell *indexpr_item;
1712 : Oid indrelid;
1713 : int keyno;
1714 : Oid keycoltype;
1715 : Datum datum;
1716 : bool isnull;
1717 :
1718 2130 : if (constraintOid)
1719 1378 : *constraintOid = InvalidOid;
1720 :
1721 : /*
1722 : * Fetch pg_class tuple of source index. We can't use the copy in the
1723 : * relcache entry because it doesn't include optional fields.
1724 : */
1725 2130 : ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(source_relid));
1726 2130 : if (!HeapTupleIsValid(ht_idxrel))
1727 0 : elog(ERROR, "cache lookup failed for relation %u", source_relid);
1728 2130 : idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
1729 :
1730 : /* Fetch pg_index tuple for source index from relcache entry */
1731 2130 : ht_idx = source_idx->rd_indextuple;
1732 2130 : idxrec = (Form_pg_index) GETSTRUCT(ht_idx);
1733 2130 : indrelid = idxrec->indrelid;
1734 :
1735 : /* Fetch the pg_am tuple of the index' access method */
1736 2130 : ht_am = SearchSysCache1(AMOID, ObjectIdGetDatum(idxrelrec->relam));
1737 2130 : if (!HeapTupleIsValid(ht_am))
1738 0 : elog(ERROR, "cache lookup failed for access method %u",
1739 : idxrelrec->relam);
1740 2130 : amrec = (Form_pg_am) GETSTRUCT(ht_am);
1741 :
1742 : /* Extract indcollation from the pg_index tuple */
1743 2130 : datum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
1744 : Anum_pg_index_indcollation);
1745 2130 : indcollation = (oidvector *) DatumGetPointer(datum);
1746 :
1747 : /* Extract indclass from the pg_index tuple */
1748 2130 : datum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx, Anum_pg_index_indclass);
1749 2130 : indclass = (oidvector *) DatumGetPointer(datum);
1750 :
1751 : /* Begin building the IndexStmt */
1752 2130 : index = makeNode(IndexStmt);
1753 2130 : index->relation = heapRel;
1754 2130 : index->accessMethod = pstrdup(NameStr(amrec->amname));
1755 2130 : if (OidIsValid(idxrelrec->reltablespace))
1756 58 : index->tableSpace = get_tablespace_name(idxrelrec->reltablespace);
1757 : else
1758 2072 : index->tableSpace = NULL;
1759 2130 : index->excludeOpNames = NIL;
1760 2130 : index->idxcomment = NULL;
1761 2130 : index->indexOid = InvalidOid;
1762 2130 : index->oldNumber = InvalidRelFileNumber;
1763 2130 : index->oldCreateSubid = InvalidSubTransactionId;
1764 2130 : index->oldFirstRelfilelocatorSubid = InvalidSubTransactionId;
1765 2130 : index->unique = idxrec->indisunique;
1766 2130 : index->nulls_not_distinct = idxrec->indnullsnotdistinct;
1767 2130 : index->primary = idxrec->indisprimary;
1768 2130 : index->iswithoutoverlaps = (idxrec->indisprimary || idxrec->indisunique) && idxrec->indisexclusion;
1769 2130 : index->transformed = true; /* don't need transformIndexStmt */
1770 2130 : index->concurrent = false;
1771 2130 : index->if_not_exists = false;
1772 2130 : index->reset_default_tblspc = false;
1773 :
1774 : /*
1775 : * We don't try to preserve the name of the source index; instead, just
1776 : * let DefineIndex() choose a reasonable name. (If we tried to preserve
1777 : * the name, we'd get duplicate-relation-name failures unless the source
1778 : * table was in a different schema.)
1779 : */
1780 2130 : index->idxname = NULL;
1781 :
1782 : /*
1783 : * If the index is marked PRIMARY or has an exclusion condition, it's
1784 : * certainly from a constraint; else, if it's not marked UNIQUE, it
1785 : * certainly isn't. If it is or might be from a constraint, we have to
1786 : * fetch the pg_constraint record.
1787 : */
1788 2130 : if (index->primary || index->unique || idxrec->indisexclusion)
1789 1134 : {
1790 1134 : Oid constraintId = get_index_constraint(source_relid);
1791 :
1792 1134 : if (OidIsValid(constraintId))
1793 : {
1794 : HeapTuple ht_constr;
1795 : Form_pg_constraint conrec;
1796 :
1797 1101 : if (constraintOid)
1798 980 : *constraintOid = constraintId;
1799 :
1800 1101 : ht_constr = SearchSysCache1(CONSTROID,
1801 : ObjectIdGetDatum(constraintId));
1802 1101 : if (!HeapTupleIsValid(ht_constr))
1803 0 : elog(ERROR, "cache lookup failed for constraint %u",
1804 : constraintId);
1805 1101 : conrec = (Form_pg_constraint) GETSTRUCT(ht_constr);
1806 :
1807 1101 : index->isconstraint = true;
1808 1101 : index->deferrable = conrec->condeferrable;
1809 1101 : index->initdeferred = conrec->condeferred;
1810 :
1811 : /* If it's an exclusion constraint, we need the operator names */
1812 1101 : if (idxrec->indisexclusion)
1813 : {
1814 : Datum *elems;
1815 : int nElems;
1816 : int i;
1817 :
1818 : Assert(conrec->contype == CONSTRAINT_EXCLUSION ||
1819 : (index->iswithoutoverlaps &&
1820 : (conrec->contype == CONSTRAINT_PRIMARY || conrec->contype == CONSTRAINT_UNIQUE)));
1821 : /* Extract operator OIDs from the pg_constraint tuple */
1822 55 : datum = SysCacheGetAttrNotNull(CONSTROID, ht_constr,
1823 : Anum_pg_constraint_conexclop);
1824 55 : deconstruct_array_builtin(DatumGetArrayTypeP(datum), OIDOID, &elems, NULL, &nElems);
1825 :
1826 164 : for (i = 0; i < nElems; i++)
1827 : {
1828 109 : Oid operid = DatumGetObjectId(elems[i]);
1829 : HeapTuple opertup;
1830 : Form_pg_operator operform;
1831 : char *oprname;
1832 : char *nspname;
1833 : List *namelist;
1834 :
1835 109 : opertup = SearchSysCache1(OPEROID,
1836 : ObjectIdGetDatum(operid));
1837 109 : if (!HeapTupleIsValid(opertup))
1838 0 : elog(ERROR, "cache lookup failed for operator %u",
1839 : operid);
1840 109 : operform = (Form_pg_operator) GETSTRUCT(opertup);
1841 109 : oprname = pstrdup(NameStr(operform->oprname));
1842 : /* For simplicity we always schema-qualify the op name */
1843 109 : nspname = get_namespace_name(operform->oprnamespace);
1844 109 : namelist = list_make2(makeString(nspname),
1845 : makeString(oprname));
1846 109 : index->excludeOpNames = lappend(index->excludeOpNames,
1847 : namelist);
1848 109 : ReleaseSysCache(opertup);
1849 : }
1850 : }
1851 :
1852 1101 : ReleaseSysCache(ht_constr);
1853 : }
1854 : else
1855 33 : index->isconstraint = false;
1856 : }
1857 : else
1858 996 : index->isconstraint = false;
1859 :
1860 : /* Get the index expressions, if any */
1861 2130 : datum = SysCacheGetAttr(INDEXRELID, ht_idx,
1862 : Anum_pg_index_indexprs, &isnull);
1863 2130 : if (!isnull)
1864 : {
1865 : char *exprsString;
1866 :
1867 122 : exprsString = TextDatumGetCString(datum);
1868 122 : indexprs = (List *) stringToNode(exprsString);
1869 : }
1870 : else
1871 2008 : indexprs = NIL;
1872 :
1873 : /* Build the list of IndexElem */
1874 2130 : index->indexParams = NIL;
1875 2130 : index->indexIncludingParams = NIL;
1876 :
1877 2130 : indexpr_item = list_head(indexprs);
1878 4615 : for (keyno = 0; keyno < idxrec->indnkeyatts; keyno++)
1879 : {
1880 : IndexElem *iparam;
1881 2485 : AttrNumber attnum = idxrec->indkey.values[keyno];
1882 2485 : Form_pg_attribute attr = TupleDescAttr(RelationGetDescr(source_idx),
1883 : keyno);
1884 2485 : int16 opt = source_idx->rd_indoption[keyno];
1885 :
1886 2485 : iparam = makeNode(IndexElem);
1887 :
1888 2485 : if (AttributeNumberIsValid(attnum))
1889 : {
1890 : /* Simple index column */
1891 : char *attname;
1892 :
1893 2363 : attname = get_attname(indrelid, attnum, false);
1894 2363 : keycoltype = get_atttype(indrelid, attnum);
1895 :
1896 2363 : iparam->name = attname;
1897 2363 : iparam->expr = NULL;
1898 : }
1899 : else
1900 : {
1901 : /* Expressional index */
1902 : Node *indexkey;
1903 : bool found_whole_row;
1904 :
1905 122 : if (indexpr_item == NULL)
1906 0 : elog(ERROR, "too few entries in indexprs list");
1907 122 : indexkey = (Node *) lfirst(indexpr_item);
1908 122 : indexpr_item = lnext(indexprs, indexpr_item);
1909 :
1910 : /* Adjust Vars to match new table's column numbering */
1911 122 : indexkey = map_variable_attnos(indexkey,
1912 : 1, 0,
1913 : attmap,
1914 : InvalidOid, &found_whole_row);
1915 :
1916 : /* As in expandTableLikeClause, reject whole-row variables */
1917 122 : if (found_whole_row)
1918 0 : ereport(ERROR,
1919 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1920 : errmsg("cannot convert whole-row table reference"),
1921 : errdetail("Index \"%s\" contains a whole-row table reference.",
1922 : RelationGetRelationName(source_idx))));
1923 :
1924 122 : iparam->name = NULL;
1925 122 : iparam->expr = indexkey;
1926 :
1927 122 : keycoltype = exprType(indexkey);
1928 : }
1929 :
1930 : /* Copy the original index column name */
1931 2485 : iparam->indexcolname = pstrdup(NameStr(attr->attname));
1932 :
1933 : /* Add the collation name, if non-default */
1934 2485 : iparam->collation = get_collation(indcollation->values[keyno], keycoltype);
1935 :
1936 : /* Add the operator class name, if non-default */
1937 2485 : iparam->opclass = get_opclass(indclass->values[keyno], keycoltype);
1938 2485 : iparam->opclassopts =
1939 2485 : untransformRelOptions(get_attoptions(source_relid, keyno + 1));
1940 :
1941 2485 : iparam->ordering = SORTBY_DEFAULT;
1942 2485 : iparam->nulls_ordering = SORTBY_NULLS_DEFAULT;
1943 :
1944 : /* Adjust options if necessary */
1945 2485 : if (source_idx->rd_indam->amcanorder)
1946 : {
1947 : /*
1948 : * If it supports sort ordering, copy DESC and NULLS opts. Don't
1949 : * set non-default settings unnecessarily, though, so as to
1950 : * improve the chance of recognizing equivalence to constraint
1951 : * indexes.
1952 : */
1953 2359 : if (opt & INDOPTION_DESC)
1954 : {
1955 0 : iparam->ordering = SORTBY_DESC;
1956 0 : if ((opt & INDOPTION_NULLS_FIRST) == 0)
1957 0 : iparam->nulls_ordering = SORTBY_NULLS_LAST;
1958 : }
1959 : else
1960 : {
1961 2359 : if (opt & INDOPTION_NULLS_FIRST)
1962 0 : iparam->nulls_ordering = SORTBY_NULLS_FIRST;
1963 : }
1964 : }
1965 :
1966 2485 : iparam->location = -1;
1967 :
1968 2485 : index->indexParams = lappend(index->indexParams, iparam);
1969 : }
1970 :
1971 : /* Handle included columns separately */
1972 2142 : for (keyno = idxrec->indnkeyatts; keyno < idxrec->indnatts; keyno++)
1973 : {
1974 : IndexElem *iparam;
1975 12 : AttrNumber attnum = idxrec->indkey.values[keyno];
1976 12 : Form_pg_attribute attr = TupleDescAttr(RelationGetDescr(source_idx),
1977 : keyno);
1978 :
1979 12 : iparam = makeNode(IndexElem);
1980 :
1981 12 : if (AttributeNumberIsValid(attnum))
1982 : {
1983 : /* Simple index column */
1984 : char *attname;
1985 :
1986 12 : attname = get_attname(indrelid, attnum, false);
1987 :
1988 12 : iparam->name = attname;
1989 12 : iparam->expr = NULL;
1990 : }
1991 : else
1992 0 : ereport(ERROR,
1993 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1994 : errmsg("expressions are not supported in included columns")));
1995 :
1996 : /* Copy the original index column name */
1997 12 : iparam->indexcolname = pstrdup(NameStr(attr->attname));
1998 :
1999 12 : iparam->location = -1;
2000 :
2001 12 : index->indexIncludingParams = lappend(index->indexIncludingParams, iparam);
2002 : }
2003 : /* Copy reloptions if any */
2004 2130 : datum = SysCacheGetAttr(RELOID, ht_idxrel,
2005 : Anum_pg_class_reloptions, &isnull);
2006 2130 : if (!isnull)
2007 0 : index->options = untransformRelOptions(datum);
2008 :
2009 : /* If it's a partial index, decompile and append the predicate */
2010 2130 : datum = SysCacheGetAttr(INDEXRELID, ht_idx,
2011 : Anum_pg_index_indpred, &isnull);
2012 2130 : if (!isnull)
2013 : {
2014 : char *pred_str;
2015 : Node *pred_tree;
2016 : bool found_whole_row;
2017 :
2018 : /* Convert text string to node tree */
2019 20 : pred_str = TextDatumGetCString(datum);
2020 20 : pred_tree = (Node *) stringToNode(pred_str);
2021 :
2022 : /* Adjust Vars to match new table's column numbering */
2023 20 : pred_tree = map_variable_attnos(pred_tree,
2024 : 1, 0,
2025 : attmap,
2026 : InvalidOid, &found_whole_row);
2027 :
2028 : /* As in expandTableLikeClause, reject whole-row variables */
2029 20 : if (found_whole_row)
2030 0 : ereport(ERROR,
2031 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2032 : errmsg("cannot convert whole-row table reference"),
2033 : errdetail("Index \"%s\" contains a whole-row table reference.",
2034 : RelationGetRelationName(source_idx))));
2035 :
2036 20 : index->whereClause = pred_tree;
2037 : }
2038 :
2039 : /* Clean up */
2040 2130 : ReleaseSysCache(ht_idxrel);
2041 2130 : ReleaseSysCache(ht_am);
2042 :
2043 2130 : return index;
2044 : }
2045 :
2046 : /*
2047 : * Generate a CreateStatsStmt node using information from an already existing
2048 : * extended statistic "source_statsid", for the rel identified by heapRel and
2049 : * heapRelid.
2050 : *
2051 : * Attribute numbers in expression Vars are adjusted according to attmap.
2052 : */
2053 : static CreateStatsStmt *
2054 32 : generateClonedExtStatsStmt(RangeVar *heapRel, Oid heapRelid,
2055 : Oid source_statsid, const AttrMap *attmap)
2056 : {
2057 : HeapTuple ht_stats;
2058 : Form_pg_statistic_ext statsrec;
2059 : CreateStatsStmt *stats;
2060 32 : List *stat_types = NIL;
2061 32 : List *def_names = NIL;
2062 : bool isnull;
2063 : Datum datum;
2064 : ArrayType *arr;
2065 : char *enabled;
2066 : int i;
2067 :
2068 : Assert(OidIsValid(heapRelid));
2069 : Assert(heapRel != NULL);
2070 :
2071 : /*
2072 : * Fetch pg_statistic_ext tuple of source statistics object.
2073 : */
2074 32 : ht_stats = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(source_statsid));
2075 32 : if (!HeapTupleIsValid(ht_stats))
2076 0 : elog(ERROR, "cache lookup failed for statistics object %u", source_statsid);
2077 32 : statsrec = (Form_pg_statistic_ext) GETSTRUCT(ht_stats);
2078 :
2079 : /* Determine which statistics types exist */
2080 32 : datum = SysCacheGetAttrNotNull(STATEXTOID, ht_stats,
2081 : Anum_pg_statistic_ext_stxkind);
2082 32 : arr = DatumGetArrayTypeP(datum);
2083 32 : if (ARR_NDIM(arr) != 1 ||
2084 32 : ARR_HASNULL(arr) ||
2085 32 : ARR_ELEMTYPE(arr) != CHAROID)
2086 0 : elog(ERROR, "stxkind is not a 1-D char array");
2087 32 : enabled = (char *) ARR_DATA_PTR(arr);
2088 96 : for (i = 0; i < ARR_DIMS(arr)[0]; i++)
2089 : {
2090 64 : if (enabled[i] == STATS_EXT_NDISTINCT)
2091 16 : stat_types = lappend(stat_types, makeString("ndistinct"));
2092 48 : else if (enabled[i] == STATS_EXT_DEPENDENCIES)
2093 16 : stat_types = lappend(stat_types, makeString("dependencies"));
2094 32 : else if (enabled[i] == STATS_EXT_MCV)
2095 16 : stat_types = lappend(stat_types, makeString("mcv"));
2096 16 : else if (enabled[i] == STATS_EXT_EXPRESSIONS)
2097 : /* expression stats are not exposed to users */
2098 16 : continue;
2099 : else
2100 0 : elog(ERROR, "unrecognized statistics kind %c", enabled[i]);
2101 : }
2102 :
2103 : /* Determine which columns the statistics are on */
2104 64 : for (i = 0; i < statsrec->stxkeys.dim1; i++)
2105 : {
2106 32 : StatsElem *selem = makeNode(StatsElem);
2107 32 : AttrNumber attnum = statsrec->stxkeys.values[i];
2108 :
2109 32 : selem->name = get_attname(heapRelid, attnum, false);
2110 32 : selem->expr = NULL;
2111 :
2112 32 : def_names = lappend(def_names, selem);
2113 : }
2114 :
2115 : /*
2116 : * Now handle expressions, if there are any. The order (with respect to
2117 : * regular attributes) does not really matter for extended stats, so we
2118 : * simply append them after simple column references.
2119 : *
2120 : * XXX Some places during build/estimation treat expressions as if they
2121 : * are before attributes, but for the CREATE command that's entirely
2122 : * irrelevant.
2123 : */
2124 32 : datum = SysCacheGetAttr(STATEXTOID, ht_stats,
2125 : Anum_pg_statistic_ext_stxexprs, &isnull);
2126 :
2127 32 : if (!isnull)
2128 : {
2129 : ListCell *lc;
2130 16 : List *exprs = NIL;
2131 : char *exprsString;
2132 :
2133 16 : exprsString = TextDatumGetCString(datum);
2134 16 : exprs = (List *) stringToNode(exprsString);
2135 :
2136 32 : foreach(lc, exprs)
2137 : {
2138 16 : Node *expr = (Node *) lfirst(lc);
2139 16 : StatsElem *selem = makeNode(StatsElem);
2140 : bool found_whole_row;
2141 :
2142 : /* Adjust Vars to match new table's column numbering */
2143 16 : expr = map_variable_attnos(expr,
2144 : 1, 0,
2145 : attmap,
2146 : InvalidOid,
2147 : &found_whole_row);
2148 :
2149 16 : selem->name = NULL;
2150 16 : selem->expr = expr;
2151 :
2152 16 : def_names = lappend(def_names, selem);
2153 : }
2154 :
2155 16 : pfree(exprsString);
2156 : }
2157 :
2158 : /* finally, build the output node */
2159 32 : stats = makeNode(CreateStatsStmt);
2160 32 : stats->defnames = NULL;
2161 32 : stats->stat_types = stat_types;
2162 32 : stats->exprs = def_names;
2163 32 : stats->relations = list_make1(heapRel);
2164 32 : stats->stxcomment = NULL;
2165 32 : stats->transformed = true; /* don't need transformStatsStmt again */
2166 32 : stats->if_not_exists = false;
2167 :
2168 : /* Clean up */
2169 32 : ReleaseSysCache(ht_stats);
2170 :
2171 32 : return stats;
2172 : }
2173 :
2174 : /*
2175 : * get_collation - fetch qualified name of a collation
2176 : *
2177 : * If collation is InvalidOid or is the default for the given actual_datatype,
2178 : * then the return value is NIL.
2179 : */
2180 : static List *
2181 2485 : get_collation(Oid collation, Oid actual_datatype)
2182 : {
2183 : List *result;
2184 : HeapTuple ht_coll;
2185 : Form_pg_collation coll_rec;
2186 : char *nsp_name;
2187 : char *coll_name;
2188 :
2189 2485 : if (!OidIsValid(collation))
2190 2214 : return NIL; /* easy case */
2191 271 : if (collation == get_typcollation(actual_datatype))
2192 257 : return NIL; /* just let it default */
2193 :
2194 14 : ht_coll = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation));
2195 14 : if (!HeapTupleIsValid(ht_coll))
2196 0 : elog(ERROR, "cache lookup failed for collation %u", collation);
2197 14 : coll_rec = (Form_pg_collation) GETSTRUCT(ht_coll);
2198 :
2199 : /* For simplicity, we always schema-qualify the name */
2200 14 : nsp_name = get_namespace_name(coll_rec->collnamespace);
2201 14 : coll_name = pstrdup(NameStr(coll_rec->collname));
2202 14 : result = list_make2(makeString(nsp_name), makeString(coll_name));
2203 :
2204 14 : ReleaseSysCache(ht_coll);
2205 14 : return result;
2206 : }
2207 :
2208 : /*
2209 : * get_opclass - fetch qualified name of an index operator class
2210 : *
2211 : * If the opclass is the default for the given actual_datatype, then
2212 : * the return value is NIL.
2213 : */
2214 : static List *
2215 2485 : get_opclass(Oid opclass, Oid actual_datatype)
2216 : {
2217 2485 : List *result = NIL;
2218 : HeapTuple ht_opc;
2219 : Form_pg_opclass opc_rec;
2220 :
2221 2485 : ht_opc = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
2222 2485 : if (!HeapTupleIsValid(ht_opc))
2223 0 : elog(ERROR, "cache lookup failed for opclass %u", opclass);
2224 2485 : opc_rec = (Form_pg_opclass) GETSTRUCT(ht_opc);
2225 :
2226 2485 : if (GetDefaultOpClass(actual_datatype, opc_rec->opcmethod) != opclass)
2227 : {
2228 : /* For simplicity, we always schema-qualify the name */
2229 16 : char *nsp_name = get_namespace_name(opc_rec->opcnamespace);
2230 16 : char *opc_name = pstrdup(NameStr(opc_rec->opcname));
2231 :
2232 16 : result = list_make2(makeString(nsp_name), makeString(opc_name));
2233 : }
2234 :
2235 2485 : ReleaseSysCache(ht_opc);
2236 2485 : return result;
2237 : }
2238 :
2239 :
2240 : /*
2241 : * transformIndexConstraints
2242 : * Handle UNIQUE, PRIMARY KEY, EXCLUDE constraints, which create indexes.
2243 : * We also merge in any index definitions arising from
2244 : * LIKE ... INCLUDING INDEXES.
2245 : */
2246 : static void
2247 39970 : transformIndexConstraints(CreateStmtContext *cxt)
2248 : {
2249 : IndexStmt *index;
2250 39970 : List *indexlist = NIL;
2251 39970 : List *finalindexlist = NIL;
2252 : ListCell *lc;
2253 :
2254 : /*
2255 : * Run through the constraints that need to generate an index, and do so.
2256 : *
2257 : * For PRIMARY KEY, this queues not-null constraints for each column, if
2258 : * needed.
2259 : */
2260 51459 : foreach(lc, cxt->ixconstraints)
2261 : {
2262 11533 : Constraint *constraint = lfirst_node(Constraint, lc);
2263 :
2264 : Assert(constraint->contype == CONSTR_PRIMARY ||
2265 : constraint->contype == CONSTR_UNIQUE ||
2266 : constraint->contype == CONSTR_EXCLUSION);
2267 :
2268 11533 : index = transformIndexConstraint(constraint, cxt);
2269 :
2270 11489 : indexlist = lappend(indexlist, index);
2271 : }
2272 :
2273 : /*
2274 : * Scan the index list and remove any redundant index specifications. This
2275 : * can happen if, for instance, the user writes UNIQUE PRIMARY KEY. A
2276 : * strict reading of SQL would suggest raising an error instead, but that
2277 : * strikes me as too anal-retentive. - tgl 2001-02-14
2278 : *
2279 : * XXX in ALTER TABLE case, it'd be nice to look for duplicate
2280 : * pre-existing indexes, too.
2281 : */
2282 39926 : if (cxt->pkey != NULL)
2283 : {
2284 : /* Make sure we keep the PKEY index in preference to others... */
2285 8109 : finalindexlist = list_make1(cxt->pkey);
2286 : }
2287 :
2288 51415 : foreach(lc, indexlist)
2289 : {
2290 11489 : bool keep = true;
2291 : ListCell *k;
2292 :
2293 11489 : index = lfirst(lc);
2294 :
2295 : /* if it's pkey, it's already in finalindexlist */
2296 11489 : if (index == cxt->pkey)
2297 8109 : continue;
2298 :
2299 3504 : foreach(k, finalindexlist)
2300 : {
2301 124 : IndexStmt *priorindex = lfirst(k);
2302 :
2303 128 : if (equal(index->indexParams, priorindex->indexParams) &&
2304 8 : equal(index->indexIncludingParams, priorindex->indexIncludingParams) &&
2305 8 : equal(index->whereClause, priorindex->whereClause) &&
2306 4 : equal(index->excludeOpNames, priorindex->excludeOpNames) &&
2307 4 : strcmp(index->accessMethod, priorindex->accessMethod) == 0 &&
2308 4 : index->nulls_not_distinct == priorindex->nulls_not_distinct &&
2309 4 : index->deferrable == priorindex->deferrable &&
2310 0 : index->initdeferred == priorindex->initdeferred)
2311 : {
2312 0 : priorindex->unique |= index->unique;
2313 :
2314 : /*
2315 : * If the prior index is as yet unnamed, and this one is
2316 : * named, then transfer the name to the prior index. This
2317 : * ensures that if we have named and unnamed constraints,
2318 : * we'll use (at least one of) the names for the index.
2319 : */
2320 0 : if (priorindex->idxname == NULL)
2321 0 : priorindex->idxname = index->idxname;
2322 0 : keep = false;
2323 0 : break;
2324 : }
2325 : }
2326 :
2327 3380 : if (keep)
2328 3380 : finalindexlist = lappend(finalindexlist, index);
2329 : }
2330 :
2331 : /*
2332 : * Now append all the IndexStmts to cxt->alist.
2333 : */
2334 39926 : cxt->alist = list_concat(cxt->alist, finalindexlist);
2335 39926 : }
2336 :
2337 : /*
2338 : * transformIndexConstraint
2339 : * Transform one UNIQUE, PRIMARY KEY, or EXCLUDE constraint for
2340 : * transformIndexConstraints. An IndexStmt is returned.
2341 : *
2342 : * For a PRIMARY KEY constraint, we additionally create not-null constraints
2343 : * for columns that don't already have them.
2344 : */
2345 : static IndexStmt *
2346 11533 : transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
2347 : {
2348 : IndexStmt *index;
2349 : ListCell *lc;
2350 :
2351 11533 : index = makeNode(IndexStmt);
2352 :
2353 11533 : index->unique = (constraint->contype != CONSTR_EXCLUSION);
2354 11533 : index->primary = (constraint->contype == CONSTR_PRIMARY);
2355 11533 : if (index->primary)
2356 : {
2357 8129 : if (cxt->pkey != NULL)
2358 0 : ereport(ERROR,
2359 : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
2360 : errmsg("multiple primary keys for table \"%s\" are not allowed",
2361 : cxt->relation->relname),
2362 : parser_errposition(cxt->pstate, constraint->location)));
2363 8129 : cxt->pkey = index;
2364 :
2365 : /*
2366 : * In ALTER TABLE case, a primary index might already exist, but
2367 : * DefineIndex will check for it.
2368 : */
2369 : }
2370 11533 : index->nulls_not_distinct = constraint->nulls_not_distinct;
2371 11533 : index->isconstraint = true;
2372 11533 : index->iswithoutoverlaps = constraint->without_overlaps;
2373 11533 : index->deferrable = constraint->deferrable;
2374 11533 : index->initdeferred = constraint->initdeferred;
2375 :
2376 11533 : if (constraint->conname != NULL)
2377 944 : index->idxname = pstrdup(constraint->conname);
2378 : else
2379 10589 : index->idxname = NULL; /* DefineIndex will choose name */
2380 :
2381 11533 : index->relation = cxt->relation;
2382 11533 : index->accessMethod = constraint->access_method ? constraint->access_method : DEFAULT_INDEX_TYPE;
2383 11533 : index->options = constraint->options;
2384 11533 : index->tableSpace = constraint->indexspace;
2385 11533 : index->whereClause = constraint->where_clause;
2386 11533 : index->indexParams = NIL;
2387 11533 : index->indexIncludingParams = NIL;
2388 11533 : index->excludeOpNames = NIL;
2389 11533 : index->idxcomment = NULL;
2390 11533 : index->indexOid = InvalidOid;
2391 11533 : index->oldNumber = InvalidRelFileNumber;
2392 11533 : index->oldCreateSubid = InvalidSubTransactionId;
2393 11533 : index->oldFirstRelfilelocatorSubid = InvalidSubTransactionId;
2394 11533 : index->transformed = false;
2395 11533 : index->concurrent = false;
2396 11533 : index->if_not_exists = false;
2397 11533 : index->reset_default_tblspc = constraint->reset_default_tblspc;
2398 :
2399 : /*
2400 : * If it's ALTER TABLE ADD CONSTRAINT USING INDEX, look up the index and
2401 : * verify it's usable, then extract the implied column name list. (We
2402 : * will not actually need the column name list at runtime, but we need it
2403 : * now to check for duplicate column entries below.)
2404 : */
2405 11533 : if (constraint->indexname != NULL)
2406 : {
2407 5949 : char *index_name = constraint->indexname;
2408 5949 : Relation heap_rel = cxt->rel;
2409 : Oid index_oid;
2410 : Relation index_rel;
2411 : Form_pg_index index_form;
2412 : oidvector *indclass;
2413 : Datum indclassDatum;
2414 : int i;
2415 :
2416 : /* Grammar should not allow this with explicit column list */
2417 : Assert(constraint->keys == NIL);
2418 :
2419 : /* Grammar should only allow PRIMARY and UNIQUE constraints */
2420 : Assert(constraint->contype == CONSTR_PRIMARY ||
2421 : constraint->contype == CONSTR_UNIQUE);
2422 :
2423 : /* Must be ALTER, not CREATE, but grammar doesn't enforce that */
2424 5949 : if (!cxt->isalter)
2425 0 : ereport(ERROR,
2426 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2427 : errmsg("cannot use an existing index in CREATE TABLE"),
2428 : parser_errposition(cxt->pstate, constraint->location)));
2429 :
2430 : /* Look for the index in the same schema as the table */
2431 5949 : index_oid = get_relname_relid(index_name, RelationGetNamespace(heap_rel));
2432 :
2433 5949 : if (!OidIsValid(index_oid))
2434 0 : ereport(ERROR,
2435 : (errcode(ERRCODE_UNDEFINED_OBJECT),
2436 : errmsg("index \"%s\" does not exist", index_name),
2437 : parser_errposition(cxt->pstate, constraint->location)));
2438 :
2439 : /* Open the index (this will throw an error if it is not an index) */
2440 5949 : index_rel = index_open(index_oid, AccessShareLock);
2441 5949 : index_form = index_rel->rd_index;
2442 :
2443 : /* Check that it does not have an associated constraint already */
2444 5949 : if (OidIsValid(get_index_constraint(index_oid)))
2445 0 : ereport(ERROR,
2446 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2447 : errmsg("index \"%s\" is already associated with a constraint",
2448 : index_name),
2449 : parser_errposition(cxt->pstate, constraint->location)));
2450 :
2451 : /* Perform validity checks on the index */
2452 5949 : if (index_form->indrelid != RelationGetRelid(heap_rel))
2453 0 : ereport(ERROR,
2454 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2455 : errmsg("index \"%s\" does not belong to table \"%s\"",
2456 : index_name, RelationGetRelationName(heap_rel)),
2457 : parser_errposition(cxt->pstate, constraint->location)));
2458 :
2459 5949 : if (!index_form->indisvalid)
2460 0 : ereport(ERROR,
2461 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2462 : errmsg("index \"%s\" is not valid", index_name),
2463 : parser_errposition(cxt->pstate, constraint->location)));
2464 :
2465 : /*
2466 : * Today we forbid non-unique indexes, but we could permit GiST
2467 : * indexes whose last entry is a range type and use that to create a
2468 : * WITHOUT OVERLAPS constraint (i.e. a temporal constraint).
2469 : */
2470 5949 : if (!index_form->indisunique)
2471 8 : ereport(ERROR,
2472 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2473 : errmsg("\"%s\" is not a unique index", index_name),
2474 : errdetail("Cannot create a primary key or unique constraint using such an index."),
2475 : parser_errposition(cxt->pstate, constraint->location)));
2476 :
2477 5941 : if (RelationGetIndexExpressions(index_rel) != NIL)
2478 0 : ereport(ERROR,
2479 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2480 : errmsg("index \"%s\" contains expressions", index_name),
2481 : errdetail("Cannot create a primary key or unique constraint using such an index."),
2482 : parser_errposition(cxt->pstate, constraint->location)));
2483 :
2484 5941 : if (RelationGetIndexPredicate(index_rel) != NIL)
2485 0 : ereport(ERROR,
2486 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2487 : errmsg("\"%s\" is a partial index", index_name),
2488 : errdetail("Cannot create a primary key or unique constraint using such an index."),
2489 : parser_errposition(cxt->pstate, constraint->location)));
2490 :
2491 : /*
2492 : * It's probably unsafe to change a deferred index to non-deferred. (A
2493 : * non-constraint index couldn't be deferred anyway, so this case
2494 : * should never occur; no need to sweat, but let's check it.)
2495 : */
2496 5941 : if (!index_form->indimmediate && !constraint->deferrable)
2497 0 : ereport(ERROR,
2498 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2499 : errmsg("\"%s\" is a deferrable index", index_name),
2500 : errdetail("Cannot create a non-deferrable constraint using a deferrable index."),
2501 : parser_errposition(cxt->pstate, constraint->location)));
2502 :
2503 : /*
2504 : * Insist on it being a btree. We must have an index that exactly
2505 : * matches what you'd get from plain ADD CONSTRAINT syntax, else dump
2506 : * and reload will produce a different index (breaking pg_upgrade in
2507 : * particular).
2508 : */
2509 5941 : if (index_rel->rd_rel->relam != get_index_am_oid(DEFAULT_INDEX_TYPE, false))
2510 0 : ereport(ERROR,
2511 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2512 : errmsg("index \"%s\" is not a btree", index_name),
2513 : parser_errposition(cxt->pstate, constraint->location)));
2514 :
2515 : /* Must get indclass the hard way */
2516 5941 : indclassDatum = SysCacheGetAttrNotNull(INDEXRELID,
2517 5941 : index_rel->rd_indextuple,
2518 : Anum_pg_index_indclass);
2519 5941 : indclass = (oidvector *) DatumGetPointer(indclassDatum);
2520 :
2521 15642 : for (i = 0; i < index_form->indnatts; i++)
2522 : {
2523 9709 : int16 attnum = index_form->indkey.values[i];
2524 : const FormData_pg_attribute *attform;
2525 : char *attname;
2526 : Oid defopclass;
2527 :
2528 : /*
2529 : * We shouldn't see attnum == 0 here, since we already rejected
2530 : * expression indexes. If we do, SystemAttributeDefinition will
2531 : * throw an error.
2532 : */
2533 9709 : if (attnum > 0)
2534 : {
2535 : Assert(attnum <= heap_rel->rd_att->natts);
2536 9709 : attform = TupleDescAttr(heap_rel->rd_att, attnum - 1);
2537 : }
2538 : else
2539 0 : attform = SystemAttributeDefinition(attnum);
2540 9709 : attname = pstrdup(NameStr(attform->attname));
2541 :
2542 9709 : if (i < index_form->indnkeyatts)
2543 : {
2544 : /*
2545 : * Insist on default opclass, collation, and sort options.
2546 : * While the index would still work as a constraint with
2547 : * non-default settings, it might not provide exactly the same
2548 : * uniqueness semantics as you'd get from a normally-created
2549 : * constraint; and there's also the dump/reload problem
2550 : * mentioned above.
2551 : */
2552 : Datum attoptions =
2553 9689 : get_attoptions(RelationGetRelid(index_rel), i + 1);
2554 :
2555 9689 : defopclass = GetDefaultOpClass(attform->atttypid,
2556 9689 : index_rel->rd_rel->relam);
2557 9689 : if (indclass->values[i] != defopclass ||
2558 9689 : attform->attcollation != index_rel->rd_indcollation[i] ||
2559 9685 : attoptions != (Datum) 0 ||
2560 9685 : index_rel->rd_indoption[i] != 0)
2561 8 : ereport(ERROR,
2562 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2563 : errmsg("index \"%s\" column number %d does not have default sorting behavior", index_name, i + 1),
2564 : errdetail("Cannot create a primary key or unique constraint using such an index."),
2565 : parser_errposition(cxt->pstate, constraint->location)));
2566 :
2567 : /* If a PK, ensure the columns get not null constraints */
2568 9681 : if (constraint->contype == CONSTR_PRIMARY)
2569 4319 : cxt->nnconstraints =
2570 4319 : lappend(cxt->nnconstraints,
2571 4319 : makeNotNullConstraint(makeString(attname)));
2572 :
2573 9681 : constraint->keys = lappend(constraint->keys, makeString(attname));
2574 : }
2575 : else
2576 20 : constraint->including = lappend(constraint->including, makeString(attname));
2577 : }
2578 :
2579 : /* Close the index relation but keep the lock */
2580 5933 : index_close(index_rel, NoLock);
2581 :
2582 5933 : index->indexOid = index_oid;
2583 : }
2584 :
2585 : /*
2586 : * If it's an EXCLUDE constraint, the grammar returns a list of pairs of
2587 : * IndexElems and operator names. We have to break that apart into
2588 : * separate lists.
2589 : */
2590 11517 : if (constraint->contype == CONSTR_EXCLUSION)
2591 : {
2592 375 : foreach(lc, constraint->exclusions)
2593 : {
2594 222 : List *pair = (List *) lfirst(lc);
2595 : IndexElem *elem;
2596 : List *opname;
2597 :
2598 : Assert(list_length(pair) == 2);
2599 222 : elem = linitial_node(IndexElem, pair);
2600 222 : opname = lsecond_node(List, pair);
2601 :
2602 222 : index->indexParams = lappend(index->indexParams, elem);
2603 222 : index->excludeOpNames = lappend(index->excludeOpNames, opname);
2604 : }
2605 : }
2606 :
2607 : /*
2608 : * For UNIQUE and PRIMARY KEY, we just have a list of column names.
2609 : *
2610 : * Make sure referenced keys exist. If we are making a PRIMARY KEY index,
2611 : * also make sure they are not-null. For WITHOUT OVERLAPS constraints, we
2612 : * make sure the last part is a range or multirange.
2613 : */
2614 : else
2615 : {
2616 27428 : foreach(lc, constraint->keys)
2617 : {
2618 16084 : char *key = strVal(lfirst(lc));
2619 16084 : bool found = false;
2620 16084 : ColumnDef *column = NULL;
2621 : ListCell *columns;
2622 : IndexElem *iparam;
2623 16084 : Oid typid = InvalidOid;
2624 :
2625 : /* Make sure referenced column exists. */
2626 17193 : foreach(columns, cxt->columns)
2627 : {
2628 6380 : column = lfirst_node(ColumnDef, columns);
2629 6380 : if (strcmp(column->colname, key) == 0)
2630 : {
2631 5271 : found = true;
2632 5271 : break;
2633 : }
2634 : }
2635 16084 : if (!found)
2636 10813 : column = NULL;
2637 :
2638 16084 : if (found)
2639 : {
2640 : /*
2641 : * column is defined in the new table. For CREATE TABLE with
2642 : * a PRIMARY KEY, we can apply the not-null constraint cheaply
2643 : * here. If the not-null constraint already exists, we can
2644 : * (albeit not so cheaply) verify that it's not a NO INHERIT
2645 : * constraint.
2646 : *
2647 : * Note that ALTER TABLE never needs either check, because
2648 : * those constraints have already been added by
2649 : * ATPrepAddPrimaryKey.
2650 : */
2651 5271 : if (constraint->contype == CONSTR_PRIMARY &&
2652 4753 : !cxt->isalter)
2653 : {
2654 4734 : if (column->is_not_null)
2655 : {
2656 7451 : foreach_node(Constraint, nn, cxt->nnconstraints)
2657 : {
2658 3775 : if (strcmp(strVal(linitial(nn->keys)), key) == 0)
2659 : {
2660 3680 : if (nn->is_no_inherit)
2661 4 : ereport(ERROR,
2662 : errcode(ERRCODE_SYNTAX_ERROR),
2663 : errmsg("conflicting NO INHERIT declaration for not-null constraint on column \"%s\"",
2664 : key));
2665 3676 : break;
2666 : }
2667 : }
2668 : }
2669 : else
2670 : {
2671 1054 : column->is_not_null = true;
2672 1054 : cxt->nnconstraints =
2673 1054 : lappend(cxt->nnconstraints,
2674 1054 : makeNotNullConstraint(makeString(key)));
2675 : }
2676 : }
2677 19 : else if (constraint->contype == CONSTR_PRIMARY)
2678 : Assert(column->is_not_null);
2679 : }
2680 10813 : else if (SystemAttributeByName(key) != NULL)
2681 : {
2682 : /*
2683 : * column will be a system column in the new table, so accept
2684 : * it. System columns can't ever be null, so no need to worry
2685 : * about PRIMARY/NOT NULL constraint.
2686 : */
2687 0 : found = true;
2688 : }
2689 10813 : else if (cxt->inhRelations)
2690 : {
2691 : /* try inherited tables */
2692 : ListCell *inher;
2693 :
2694 64 : foreach(inher, cxt->inhRelations)
2695 : {
2696 64 : RangeVar *inh = lfirst_node(RangeVar, inher);
2697 : Relation rel;
2698 : int count;
2699 :
2700 64 : rel = table_openrv(inh, AccessShareLock);
2701 : /* check user requested inheritance from valid relkind */
2702 64 : if (rel->rd_rel->relkind != RELKIND_RELATION &&
2703 0 : rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
2704 0 : rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
2705 0 : ereport(ERROR,
2706 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2707 : errmsg("inherited relation \"%s\" is not a table or foreign table",
2708 : inh->relname)));
2709 68 : for (count = 0; count < rel->rd_att->natts; count++)
2710 : {
2711 68 : Form_pg_attribute inhattr = TupleDescAttr(rel->rd_att,
2712 : count);
2713 68 : char *inhname = NameStr(inhattr->attname);
2714 :
2715 68 : if (inhattr->attisdropped)
2716 0 : continue;
2717 68 : if (strcmp(key, inhname) == 0)
2718 : {
2719 64 : found = true;
2720 64 : typid = inhattr->atttypid;
2721 :
2722 64 : if (constraint->contype == CONSTR_PRIMARY)
2723 56 : cxt->nnconstraints =
2724 56 : lappend(cxt->nnconstraints,
2725 56 : makeNotNullConstraint(makeString(pstrdup(inhname))));
2726 64 : break;
2727 : }
2728 : }
2729 64 : table_close(rel, NoLock);
2730 64 : if (found)
2731 64 : break;
2732 : }
2733 : }
2734 :
2735 : /*
2736 : * In the ALTER TABLE case, don't complain about index keys not
2737 : * created in the command; they may well exist already.
2738 : * DefineIndex will complain about them if not.
2739 : */
2740 16080 : if (!found && !cxt->isalter)
2741 8 : ereport(ERROR,
2742 : (errcode(ERRCODE_UNDEFINED_COLUMN),
2743 : errmsg("column \"%s\" named in key does not exist", key),
2744 : parser_errposition(cxt->pstate, constraint->location)));
2745 :
2746 : /* Check for PRIMARY KEY(foo, foo) */
2747 22278 : foreach(columns, index->indexParams)
2748 : {
2749 6206 : iparam = (IndexElem *) lfirst(columns);
2750 6206 : if (iparam->name && strcmp(key, iparam->name) == 0)
2751 : {
2752 0 : if (index->primary)
2753 0 : ereport(ERROR,
2754 : (errcode(ERRCODE_DUPLICATE_COLUMN),
2755 : errmsg("column \"%s\" appears twice in primary key constraint",
2756 : key),
2757 : parser_errposition(cxt->pstate, constraint->location)));
2758 : else
2759 0 : ereport(ERROR,
2760 : (errcode(ERRCODE_DUPLICATE_COLUMN),
2761 : errmsg("column \"%s\" appears twice in unique constraint",
2762 : key),
2763 : parser_errposition(cxt->pstate, constraint->location)));
2764 : }
2765 : }
2766 :
2767 : /*
2768 : * The WITHOUT OVERLAPS part (if any) must be a range or
2769 : * multirange type.
2770 : */
2771 16072 : if (constraint->without_overlaps && lc == list_last_cell(constraint->keys))
2772 : {
2773 370 : if (!found && cxt->isalter)
2774 : {
2775 : /*
2776 : * Look up the column type on existing table. If we can't
2777 : * find it, let things fail in DefineIndex.
2778 : */
2779 108 : Relation rel = cxt->rel;
2780 :
2781 218 : for (int i = 0; i < rel->rd_att->natts; i++)
2782 : {
2783 218 : Form_pg_attribute attr = TupleDescAttr(rel->rd_att, i);
2784 : const char *attname;
2785 :
2786 218 : if (attr->attisdropped)
2787 0 : break;
2788 :
2789 218 : attname = NameStr(attr->attname);
2790 218 : if (strcmp(attname, key) == 0)
2791 : {
2792 108 : found = true;
2793 108 : typid = attr->atttypid;
2794 108 : break;
2795 : }
2796 : }
2797 : }
2798 370 : if (found)
2799 : {
2800 370 : if (!OidIsValid(typid) && column)
2801 258 : typid = typenameTypeId(NULL, column->typeName);
2802 :
2803 370 : if (!OidIsValid(typid) || !(type_is_range(typid) || type_is_multirange(typid)))
2804 8 : ereport(ERROR,
2805 : (errcode(ERRCODE_DATATYPE_MISMATCH),
2806 : errmsg("column \"%s\" in WITHOUT OVERLAPS is not a range or multirange type", key),
2807 : parser_errposition(cxt->pstate, constraint->location)));
2808 : }
2809 : }
2810 :
2811 : /* OK, add it to the index definition */
2812 16064 : iparam = makeNode(IndexElem);
2813 16064 : iparam->name = pstrdup(key);
2814 16064 : iparam->expr = NULL;
2815 16064 : iparam->indexcolname = NULL;
2816 16064 : iparam->collation = NIL;
2817 16064 : iparam->opclass = NIL;
2818 16064 : iparam->opclassopts = NIL;
2819 16064 : iparam->ordering = SORTBY_DEFAULT;
2820 16064 : iparam->nulls_ordering = SORTBY_NULLS_DEFAULT;
2821 16064 : iparam->location = -1;
2822 16064 : index->indexParams = lappend(index->indexParams, iparam);
2823 : }
2824 :
2825 11344 : if (constraint->without_overlaps)
2826 : {
2827 : /*
2828 : * This enforces that there is at least one equality column
2829 : * besides the WITHOUT OVERLAPS columns. This is per SQL
2830 : * standard. XXX Do we need this?
2831 : */
2832 362 : if (list_length(constraint->keys) < 2)
2833 8 : ereport(ERROR,
2834 : errcode(ERRCODE_SYNTAX_ERROR),
2835 : errmsg("constraint using WITHOUT OVERLAPS needs at least two columns"));
2836 :
2837 : /* WITHOUT OVERLAPS requires a GiST index */
2838 354 : index->accessMethod = "gist";
2839 : }
2840 :
2841 : }
2842 :
2843 : /*
2844 : * Add included columns to index definition. This is much like the
2845 : * simple-column-name-list code above, except that we don't worry about
2846 : * NOT NULL marking; included columns in a primary key should not be
2847 : * forced NOT NULL. We don't complain about duplicate columns, either,
2848 : * though maybe we should?
2849 : */
2850 11681 : foreach(lc, constraint->including)
2851 : {
2852 192 : char *key = strVal(lfirst(lc));
2853 192 : bool found = false;
2854 192 : ColumnDef *column = NULL;
2855 : ListCell *columns;
2856 : IndexElem *iparam;
2857 :
2858 417 : foreach(columns, cxt->columns)
2859 : {
2860 341 : column = lfirst_node(ColumnDef, columns);
2861 341 : if (strcmp(column->colname, key) == 0)
2862 : {
2863 116 : found = true;
2864 116 : break;
2865 : }
2866 : }
2867 :
2868 192 : if (!found)
2869 : {
2870 76 : if (SystemAttributeByName(key) != NULL)
2871 : {
2872 : /*
2873 : * column will be a system column in the new table, so accept
2874 : * it.
2875 : */
2876 0 : found = true;
2877 : }
2878 76 : else if (cxt->inhRelations)
2879 : {
2880 : /* try inherited tables */
2881 : ListCell *inher;
2882 :
2883 0 : foreach(inher, cxt->inhRelations)
2884 : {
2885 0 : RangeVar *inh = lfirst_node(RangeVar, inher);
2886 : Relation rel;
2887 : int count;
2888 :
2889 0 : rel = table_openrv(inh, AccessShareLock);
2890 : /* check user requested inheritance from valid relkind */
2891 0 : if (rel->rd_rel->relkind != RELKIND_RELATION &&
2892 0 : rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
2893 0 : rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
2894 0 : ereport(ERROR,
2895 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2896 : errmsg("inherited relation \"%s\" is not a table or foreign table",
2897 : inh->relname)));
2898 0 : for (count = 0; count < rel->rd_att->natts; count++)
2899 : {
2900 0 : Form_pg_attribute inhattr = TupleDescAttr(rel->rd_att,
2901 : count);
2902 0 : char *inhname = NameStr(inhattr->attname);
2903 :
2904 0 : if (inhattr->attisdropped)
2905 0 : continue;
2906 0 : if (strcmp(key, inhname) == 0)
2907 : {
2908 0 : found = true;
2909 0 : break;
2910 : }
2911 : }
2912 0 : table_close(rel, NoLock);
2913 0 : if (found)
2914 0 : break;
2915 : }
2916 : }
2917 : }
2918 :
2919 : /*
2920 : * In the ALTER TABLE case, don't complain about index keys not
2921 : * created in the command; they may well exist already. DefineIndex
2922 : * will complain about them if not.
2923 : */
2924 192 : if (!found && !cxt->isalter)
2925 0 : ereport(ERROR,
2926 : (errcode(ERRCODE_UNDEFINED_COLUMN),
2927 : errmsg("column \"%s\" named in key does not exist", key),
2928 : parser_errposition(cxt->pstate, constraint->location)));
2929 :
2930 : /* OK, add it to the index definition */
2931 192 : iparam = makeNode(IndexElem);
2932 192 : iparam->name = pstrdup(key);
2933 192 : iparam->expr = NULL;
2934 192 : iparam->indexcolname = NULL;
2935 192 : iparam->collation = NIL;
2936 192 : iparam->opclass = NIL;
2937 192 : iparam->opclassopts = NIL;
2938 192 : iparam->location = -1;
2939 192 : index->indexIncludingParams = lappend(index->indexIncludingParams, iparam);
2940 : }
2941 :
2942 11489 : return index;
2943 : }
2944 :
2945 : /*
2946 : * transformCheckConstraints
2947 : * handle CHECK constraints
2948 : *
2949 : * Right now, there's nothing to do here when called from ALTER TABLE,
2950 : * but the other constraint-transformation functions are called in both
2951 : * the CREATE TABLE and ALTER TABLE paths, so do the same here, and just
2952 : * don't do anything if we're not authorized to skip validation.
2953 : */
2954 : static void
2955 39926 : transformCheckConstraints(CreateStmtContext *cxt, bool skipValidation)
2956 : {
2957 : ListCell *ckclist;
2958 :
2959 39926 : if (cxt->ckconstraints == NIL)
2960 38680 : return;
2961 :
2962 : /*
2963 : * When creating a new table (but not a foreign table), we can safely skip
2964 : * the validation of check constraints and mark them as valid based on the
2965 : * constraint enforcement flag, since NOT ENFORCED constraints must always
2966 : * be marked as NOT VALID. (This will override any user-supplied NOT VALID
2967 : * flag.)
2968 : */
2969 1246 : if (skipValidation)
2970 : {
2971 1082 : foreach(ckclist, cxt->ckconstraints)
2972 : {
2973 592 : Constraint *constraint = (Constraint *) lfirst(ckclist);
2974 :
2975 592 : constraint->skip_validation = true;
2976 592 : constraint->initially_valid = constraint->is_enforced;
2977 : }
2978 : }
2979 : }
2980 :
2981 : /*
2982 : * transformFKConstraints
2983 : * handle FOREIGN KEY constraints
2984 : */
2985 : static void
2986 39926 : transformFKConstraints(CreateStmtContext *cxt,
2987 : bool skipValidation, bool isAddConstraint)
2988 : {
2989 : ListCell *fkclist;
2990 :
2991 39926 : if (cxt->fkconstraints == NIL)
2992 37266 : return;
2993 :
2994 : /*
2995 : * If CREATE TABLE or adding a column with NULL default, we can safely
2996 : * skip validation of FK constraints, and mark them as valid based on the
2997 : * constraint enforcement flag, since NOT ENFORCED constraints must always
2998 : * be marked as NOT VALID. (This will override any user-supplied NOT VALID
2999 : * flag.)
3000 : */
3001 2660 : if (skipValidation)
3002 : {
3003 1922 : foreach(fkclist, cxt->fkconstraints)
3004 : {
3005 997 : Constraint *constraint = (Constraint *) lfirst(fkclist);
3006 :
3007 997 : constraint->skip_validation = true;
3008 997 : constraint->initially_valid = constraint->is_enforced;
3009 : }
3010 : }
3011 :
3012 : /*
3013 : * For CREATE TABLE or ALTER TABLE ADD COLUMN, gin up an ALTER TABLE ADD
3014 : * CONSTRAINT command to execute after the basic command is complete. (If
3015 : * called from ADD CONSTRAINT, that routine will add the FK constraints to
3016 : * its own subcommand list.)
3017 : *
3018 : * Note: the ADD CONSTRAINT command must also execute after any index
3019 : * creation commands. Thus, this should run after
3020 : * transformIndexConstraints, so that the CREATE INDEX commands are
3021 : * already in cxt->alist. See also the handling of cxt->likeclauses.
3022 : */
3023 2660 : if (!isAddConstraint)
3024 : {
3025 921 : AlterTableStmt *alterstmt = makeNode(AlterTableStmt);
3026 :
3027 921 : alterstmt->relation = cxt->relation;
3028 921 : alterstmt->cmds = NIL;
3029 921 : alterstmt->objtype = OBJECT_TABLE;
3030 :
3031 1914 : foreach(fkclist, cxt->fkconstraints)
3032 : {
3033 993 : Constraint *constraint = (Constraint *) lfirst(fkclist);
3034 993 : AlterTableCmd *altercmd = makeNode(AlterTableCmd);
3035 :
3036 993 : altercmd->subtype = AT_AddConstraint;
3037 993 : altercmd->name = NULL;
3038 993 : altercmd->def = (Node *) constraint;
3039 993 : alterstmt->cmds = lappend(alterstmt->cmds, altercmd);
3040 : }
3041 :
3042 921 : cxt->alist = lappend(cxt->alist, alterstmt);
3043 : }
3044 : }
3045 :
3046 : /*
3047 : * transformIndexStmt - parse analysis for CREATE INDEX and ALTER TABLE
3048 : *
3049 : * Note: this is a no-op for an index not using either index expressions or
3050 : * a predicate expression. There are several code paths that create indexes
3051 : * without bothering to call this, because they know they don't have any
3052 : * such expressions to deal with.
3053 : *
3054 : * To avoid race conditions, it's important that this function rely only on
3055 : * the passed-in relid (and not on stmt->relation) to determine the target
3056 : * relation.
3057 : */
3058 : IndexStmt *
3059 15814 : transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString)
3060 : {
3061 : ParseState *pstate;
3062 : ParseNamespaceItem *nsitem;
3063 : ListCell *l;
3064 : Relation rel;
3065 :
3066 : /* Nothing to do if statement already transformed. */
3067 15814 : if (stmt->transformed)
3068 86 : return stmt;
3069 :
3070 : /* Set up pstate */
3071 15728 : pstate = make_parsestate(NULL);
3072 15728 : pstate->p_sourcetext = queryString;
3073 :
3074 : /*
3075 : * Put the parent table into the rtable so that the expressions can refer
3076 : * to its fields without qualification. Caller is responsible for locking
3077 : * relation, but we still need to open it.
3078 : */
3079 15728 : rel = relation_open(relid, NoLock);
3080 15728 : nsitem = addRangeTableEntryForRelation(pstate, rel,
3081 : AccessShareLock,
3082 : NULL, false, true);
3083 :
3084 : /* no to join list, yes to namespaces */
3085 15728 : addNSItemToQuery(pstate, nsitem, false, true, true);
3086 :
3087 : /* take care of the where clause */
3088 15728 : if (stmt->whereClause)
3089 : {
3090 262 : stmt->whereClause = transformWhereClause(pstate,
3091 : stmt->whereClause,
3092 : EXPR_KIND_INDEX_PREDICATE,
3093 : "WHERE");
3094 : /* we have to fix its collations too */
3095 262 : assign_expr_collations(pstate, stmt->whereClause);
3096 : }
3097 :
3098 : /* take care of any index expressions */
3099 37298 : foreach(l, stmt->indexParams)
3100 : {
3101 21594 : IndexElem *ielem = (IndexElem *) lfirst(l);
3102 :
3103 21594 : if (ielem->expr)
3104 : {
3105 : /* Extract preliminary index col name before transforming expr */
3106 747 : if (ielem->indexcolname == NULL)
3107 747 : ielem->indexcolname = FigureIndexColname(ielem->expr);
3108 :
3109 : /* Now do parse transformation of the expression */
3110 747 : ielem->expr = transformExpr(pstate, ielem->expr,
3111 : EXPR_KIND_INDEX_EXPRESSION);
3112 :
3113 : /* We have to fix its collations too */
3114 723 : assign_expr_collations(pstate, ielem->expr);
3115 :
3116 : /*
3117 : * transformExpr() should have already rejected subqueries,
3118 : * aggregates, window functions, and SRFs, based on the EXPR_KIND_
3119 : * for an index expression.
3120 : *
3121 : * DefineIndex() will make more checks.
3122 : */
3123 : }
3124 : }
3125 :
3126 : /*
3127 : * Check that only the base rel is mentioned. (This should be dead code
3128 : * now that add_missing_from is history.)
3129 : */
3130 15704 : if (list_length(pstate->p_rtable) != 1)
3131 0 : ereport(ERROR,
3132 : (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3133 : errmsg("index expressions and predicates can refer only to the table being indexed")));
3134 :
3135 15704 : free_parsestate(pstate);
3136 :
3137 : /* Close relation */
3138 15704 : table_close(rel, NoLock);
3139 :
3140 : /* Mark statement as successfully transformed */
3141 15704 : stmt->transformed = true;
3142 :
3143 15704 : return stmt;
3144 : }
3145 :
3146 : /*
3147 : * transformStatsStmt - parse analysis for CREATE STATISTICS
3148 : *
3149 : * To avoid race conditions, it's important that this function relies only on
3150 : * the passed-in relid (and not on stmt->relation) to determine the target
3151 : * relation.
3152 : */
3153 : CreateStatsStmt *
3154 648 : transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString)
3155 : {
3156 : ParseState *pstate;
3157 : ParseNamespaceItem *nsitem;
3158 : ListCell *l;
3159 : Relation rel;
3160 :
3161 : /* Nothing to do if statement already transformed. */
3162 648 : if (stmt->transformed)
3163 32 : return stmt;
3164 :
3165 : /* Set up pstate */
3166 616 : pstate = make_parsestate(NULL);
3167 616 : pstate->p_sourcetext = queryString;
3168 :
3169 : /*
3170 : * Put the parent table into the rtable so that the expressions can refer
3171 : * to its fields without qualification. Caller is responsible for locking
3172 : * relation, but we still need to open it.
3173 : */
3174 616 : rel = relation_open(relid, NoLock);
3175 616 : nsitem = addRangeTableEntryForRelation(pstate, rel,
3176 : AccessShareLock,
3177 : NULL, false, true);
3178 :
3179 : /* no to join list, yes to namespaces */
3180 616 : addNSItemToQuery(pstate, nsitem, false, true, true);
3181 :
3182 : /* take care of any expressions */
3183 2108 : foreach(l, stmt->exprs)
3184 : {
3185 1492 : StatsElem *selem = (StatsElem *) lfirst(l);
3186 :
3187 1492 : if (selem->expr)
3188 : {
3189 : /* Now do parse transformation of the expression */
3190 419 : selem->expr = transformExpr(pstate, selem->expr,
3191 : EXPR_KIND_STATS_EXPRESSION);
3192 :
3193 : /* We have to fix its collations too */
3194 419 : assign_expr_collations(pstate, selem->expr);
3195 : }
3196 : }
3197 :
3198 : /*
3199 : * Check that only the base rel is mentioned. (This should be dead code
3200 : * now that add_missing_from is history.)
3201 : */
3202 616 : if (list_length(pstate->p_rtable) != 1)
3203 0 : ereport(ERROR,
3204 : (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3205 : errmsg("statistics expressions can refer only to the table being referenced")));
3206 :
3207 616 : free_parsestate(pstate);
3208 :
3209 : /* Close relation */
3210 616 : table_close(rel, NoLock);
3211 :
3212 : /* Mark statement as successfully transformed */
3213 616 : stmt->transformed = true;
3214 :
3215 616 : return stmt;
3216 : }
3217 :
3218 :
3219 : /*
3220 : * transformRuleStmt -
3221 : * transform a CREATE RULE Statement. The action is a list of parse
3222 : * trees which is transformed into a list of query trees, and we also
3223 : * transform the WHERE clause if any.
3224 : *
3225 : * actions and whereClause are output parameters that receive the
3226 : * transformed results.
3227 : */
3228 : void
3229 690 : transformRuleStmt(RuleStmt *stmt, const char *queryString,
3230 : List **actions, Node **whereClause)
3231 : {
3232 : Relation rel;
3233 : ParseState *pstate;
3234 : ParseNamespaceItem *oldnsitem;
3235 : ParseNamespaceItem *newnsitem;
3236 :
3237 : /*
3238 : * To avoid deadlock, make sure the first thing we do is grab
3239 : * AccessExclusiveLock on the target relation. This will be needed by
3240 : * DefineQueryRewrite(), and we don't want to grab a lesser lock
3241 : * beforehand.
3242 : */
3243 690 : rel = table_openrv(stmt->relation, AccessExclusiveLock);
3244 :
3245 690 : if (rel->rd_rel->relkind == RELKIND_MATVIEW)
3246 0 : ereport(ERROR,
3247 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3248 : errmsg("rules on materialized views are not supported")));
3249 :
3250 : /* Set up pstate */
3251 690 : pstate = make_parsestate(NULL);
3252 690 : pstate->p_sourcetext = queryString;
3253 :
3254 : /*
3255 : * NOTE: 'OLD' must always have a varno equal to 1 and 'NEW' equal to 2.
3256 : * Set up their ParseNamespaceItems in the main pstate for use in parsing
3257 : * the rule qualification.
3258 : */
3259 690 : oldnsitem = addRangeTableEntryForRelation(pstate, rel,
3260 : AccessShareLock,
3261 : makeAlias("old", NIL),
3262 : false, false);
3263 690 : newnsitem = addRangeTableEntryForRelation(pstate, rel,
3264 : AccessShareLock,
3265 : makeAlias("new", NIL),
3266 : false, false);
3267 :
3268 : /*
3269 : * They must be in the namespace too for lookup purposes, but only add the
3270 : * one(s) that are relevant for the current kind of rule. In an UPDATE
3271 : * rule, quals must refer to OLD.field or NEW.field to be unambiguous, but
3272 : * there's no need to be so picky for INSERT & DELETE. We do not add them
3273 : * to the joinlist.
3274 : */
3275 690 : switch (stmt->event)
3276 : {
3277 12 : case CMD_SELECT:
3278 12 : addNSItemToQuery(pstate, oldnsitem, false, true, true);
3279 12 : break;
3280 254 : case CMD_UPDATE:
3281 254 : addNSItemToQuery(pstate, oldnsitem, false, true, true);
3282 254 : addNSItemToQuery(pstate, newnsitem, false, true, true);
3283 254 : break;
3284 318 : case CMD_INSERT:
3285 318 : addNSItemToQuery(pstate, newnsitem, false, true, true);
3286 318 : break;
3287 106 : case CMD_DELETE:
3288 106 : addNSItemToQuery(pstate, oldnsitem, false, true, true);
3289 106 : break;
3290 0 : default:
3291 0 : elog(ERROR, "unrecognized event type: %d",
3292 : (int) stmt->event);
3293 : break;
3294 : }
3295 :
3296 : /* take care of the where clause */
3297 690 : *whereClause = transformWhereClause(pstate,
3298 : stmt->whereClause,
3299 : EXPR_KIND_WHERE,
3300 : "WHERE");
3301 : /* we have to fix its collations too */
3302 690 : assign_expr_collations(pstate, *whereClause);
3303 :
3304 : /* this is probably dead code without add_missing_from: */
3305 690 : if (list_length(pstate->p_rtable) != 2) /* naughty, naughty... */
3306 0 : ereport(ERROR,
3307 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3308 : errmsg("rule WHERE condition cannot contain references to other relations")));
3309 :
3310 : /*
3311 : * 'instead nothing' rules with a qualification need a query rangetable so
3312 : * the rewrite handler can add the negated rule qualification to the
3313 : * original query. We create a query with the new command type CMD_NOTHING
3314 : * here that is treated specially by the rewrite system.
3315 : */
3316 690 : if (stmt->actions == NIL)
3317 : {
3318 90 : Query *nothing_qry = makeNode(Query);
3319 :
3320 90 : nothing_qry->commandType = CMD_NOTHING;
3321 90 : nothing_qry->rtable = pstate->p_rtable;
3322 90 : nothing_qry->rteperminfos = pstate->p_rteperminfos;
3323 90 : nothing_qry->jointree = makeFromExpr(NIL, NULL); /* no join wanted */
3324 :
3325 90 : *actions = list_make1(nothing_qry);
3326 : }
3327 : else
3328 : {
3329 : ListCell *l;
3330 600 : List *newactions = NIL;
3331 :
3332 : /*
3333 : * transform each statement, like parse_sub_analyze()
3334 : */
3335 1218 : foreach(l, stmt->actions)
3336 : {
3337 630 : Node *action = (Node *) lfirst(l);
3338 630 : ParseState *sub_pstate = make_parsestate(NULL);
3339 : Query *sub_qry,
3340 : *top_subqry;
3341 : bool has_old,
3342 : has_new;
3343 :
3344 : /*
3345 : * Since outer ParseState isn't parent of inner, have to pass down
3346 : * the query text by hand.
3347 : */
3348 630 : sub_pstate->p_sourcetext = queryString;
3349 :
3350 : /*
3351 : * Set up OLD/NEW in the rtable for this statement. The entries
3352 : * are added only to relnamespace, not varnamespace, because we
3353 : * don't want them to be referred to by unqualified field names
3354 : * nor "*" in the rule actions. We decide later whether to put
3355 : * them in the joinlist.
3356 : */
3357 630 : oldnsitem = addRangeTableEntryForRelation(sub_pstate, rel,
3358 : AccessShareLock,
3359 : makeAlias("old", NIL),
3360 : false, false);
3361 630 : newnsitem = addRangeTableEntryForRelation(sub_pstate, rel,
3362 : AccessShareLock,
3363 : makeAlias("new", NIL),
3364 : false, false);
3365 630 : addNSItemToQuery(sub_pstate, oldnsitem, false, true, false);
3366 630 : addNSItemToQuery(sub_pstate, newnsitem, false, true, false);
3367 :
3368 : /* Transform the rule action statement */
3369 630 : top_subqry = transformStmt(sub_pstate, action);
3370 :
3371 : /*
3372 : * We cannot support utility-statement actions (eg NOTIFY) with
3373 : * nonempty rule WHERE conditions, because there's no way to make
3374 : * the utility action execute conditionally.
3375 : */
3376 622 : if (top_subqry->commandType == CMD_UTILITY &&
3377 26 : *whereClause != NULL)
3378 0 : ereport(ERROR,
3379 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3380 : errmsg("rules with WHERE conditions can only have SELECT, INSERT, UPDATE, or DELETE actions")));
3381 :
3382 : /*
3383 : * If the action is INSERT...SELECT, OLD/NEW have been pushed down
3384 : * into the SELECT, and that's what we need to look at. (Ugly
3385 : * kluge ... try to fix this when we redesign querytrees.)
3386 : */
3387 622 : sub_qry = getInsertSelectQuery(top_subqry, NULL);
3388 :
3389 : /*
3390 : * If the sub_qry is a setop, we cannot attach any qualifications
3391 : * to it, because the planner won't notice them. This could
3392 : * perhaps be relaxed someday, but for now, we may as well reject
3393 : * such a rule immediately.
3394 : */
3395 622 : if (sub_qry->setOperations != NULL && *whereClause != NULL)
3396 0 : ereport(ERROR,
3397 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3398 : errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
3399 :
3400 : /*
3401 : * Validate action's use of OLD/NEW, qual too
3402 : */
3403 622 : has_old =
3404 1006 : rangeTableEntry_used((Node *) sub_qry, PRS2_OLD_VARNO, 0) ||
3405 384 : rangeTableEntry_used(*whereClause, PRS2_OLD_VARNO, 0);
3406 622 : has_new =
3407 837 : rangeTableEntry_used((Node *) sub_qry, PRS2_NEW_VARNO, 0) ||
3408 215 : rangeTableEntry_used(*whereClause, PRS2_NEW_VARNO, 0);
3409 :
3410 622 : switch (stmt->event)
3411 : {
3412 12 : case CMD_SELECT:
3413 12 : if (has_old)
3414 0 : ereport(ERROR,
3415 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3416 : errmsg("ON SELECT rule cannot use OLD")));
3417 12 : if (has_new)
3418 0 : ereport(ERROR,
3419 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3420 : errmsg("ON SELECT rule cannot use NEW")));
3421 12 : break;
3422 210 : case CMD_UPDATE:
3423 : /* both are OK */
3424 210 : break;
3425 291 : case CMD_INSERT:
3426 291 : if (has_old)
3427 0 : ereport(ERROR,
3428 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3429 : errmsg("ON INSERT rule cannot use OLD")));
3430 291 : break;
3431 109 : case CMD_DELETE:
3432 109 : if (has_new)
3433 0 : ereport(ERROR,
3434 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3435 : errmsg("ON DELETE rule cannot use NEW")));
3436 109 : break;
3437 0 : default:
3438 0 : elog(ERROR, "unrecognized event type: %d",
3439 : (int) stmt->event);
3440 : break;
3441 : }
3442 :
3443 : /*
3444 : * OLD/NEW are not allowed in WITH queries, because they would
3445 : * amount to outer references for the WITH, which we disallow.
3446 : * However, they were already in the outer rangetable when we
3447 : * analyzed the query, so we have to check.
3448 : *
3449 : * Note that in the INSERT...SELECT case, we need to examine the
3450 : * CTE lists of both top_subqry and sub_qry.
3451 : *
3452 : * Note that we aren't digging into the body of the query looking
3453 : * for WITHs in nested sub-SELECTs. A WITH down there can
3454 : * legitimately refer to OLD/NEW, because it'd be an
3455 : * indirect-correlated outer reference.
3456 : */
3457 622 : if (rangeTableEntry_used((Node *) top_subqry->cteList,
3458 618 : PRS2_OLD_VARNO, 0) ||
3459 618 : rangeTableEntry_used((Node *) sub_qry->cteList,
3460 : PRS2_OLD_VARNO, 0))
3461 4 : ereport(ERROR,
3462 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3463 : errmsg("cannot refer to OLD within WITH query")));
3464 618 : if (rangeTableEntry_used((Node *) top_subqry->cteList,
3465 618 : PRS2_NEW_VARNO, 0) ||
3466 618 : rangeTableEntry_used((Node *) sub_qry->cteList,
3467 : PRS2_NEW_VARNO, 0))
3468 0 : ereport(ERROR,
3469 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3470 : errmsg("cannot refer to NEW within WITH query")));
3471 :
3472 : /*
3473 : * For efficiency's sake, add OLD to the rule action's jointree
3474 : * only if it was actually referenced in the statement or qual.
3475 : *
3476 : * For INSERT, NEW is not really a relation (only a reference to
3477 : * the to-be-inserted tuple) and should never be added to the
3478 : * jointree.
3479 : *
3480 : * For UPDATE, we treat NEW as being another kind of reference to
3481 : * OLD, because it represents references to *transformed* tuples
3482 : * of the existing relation. It would be wrong to enter NEW
3483 : * separately in the jointree, since that would cause a double
3484 : * join of the updated relation. It's also wrong to fail to make
3485 : * a jointree entry if only NEW and not OLD is mentioned.
3486 : */
3487 618 : if (has_old || (has_new && stmt->event == CMD_UPDATE))
3488 : {
3489 : RangeTblRef *rtr;
3490 :
3491 : /*
3492 : * If sub_qry is a setop, manipulating its jointree will do no
3493 : * good at all, because the jointree is dummy. (This should be
3494 : * a can't-happen case because of prior tests.)
3495 : */
3496 265 : if (sub_qry->setOperations != NULL)
3497 0 : ereport(ERROR,
3498 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3499 : errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
3500 : /* hackishly add OLD to the already-built FROM clause */
3501 265 : rtr = makeNode(RangeTblRef);
3502 265 : rtr->rtindex = oldnsitem->p_rtindex;
3503 265 : sub_qry->jointree->fromlist =
3504 265 : lappend(sub_qry->jointree->fromlist, rtr);
3505 : }
3506 :
3507 618 : newactions = lappend(newactions, top_subqry);
3508 :
3509 618 : free_parsestate(sub_pstate);
3510 : }
3511 :
3512 588 : *actions = newactions;
3513 : }
3514 :
3515 678 : free_parsestate(pstate);
3516 :
3517 : /* Close relation, but keep the exclusive lock */
3518 678 : table_close(rel, NoLock);
3519 678 : }
3520 :
3521 :
3522 : /*
3523 : * checkPartition
3524 : * Check whether partRelOid is a leaf partition of the parent table (rel).
3525 : * isMerge: true indicates the operation is "ALTER TABLE ... MERGE PARTITIONS";
3526 : * false indicates the operation is "ALTER TABLE ... SPLIT PARTITION".
3527 : */
3528 : static void
3529 580 : checkPartition(Relation rel, Oid partRelOid, bool isMerge)
3530 : {
3531 : Relation partRel;
3532 :
3533 580 : partRel = table_open(partRelOid, NoLock);
3534 :
3535 580 : if (partRel->rd_rel->relkind != RELKIND_RELATION)
3536 4 : ereport(ERROR,
3537 : errcode(ERRCODE_WRONG_OBJECT_TYPE),
3538 : errmsg("\"%s\" is not a table", RelationGetRelationName(partRel)),
3539 : isMerge
3540 : ? errhint("ALTER TABLE ... MERGE PARTITIONS can only merge partitions don't have sub-partitions")
3541 : : errhint("ALTER TABLE ... SPLIT PARTITION can only split partitions don't have sub-partitions"));
3542 :
3543 576 : if (!partRel->rd_rel->relispartition)
3544 12 : ereport(ERROR,
3545 : errcode(ERRCODE_WRONG_OBJECT_TYPE),
3546 : errmsg("\"%s\" is not a partition of partitioned table \"%s\"",
3547 : RelationGetRelationName(partRel), RelationGetRelationName(rel)),
3548 : isMerge
3549 : ? errhint("ALTER TABLE ... MERGE PARTITIONS can only merge partitions don't have sub-partitions")
3550 : : errhint("ALTER TABLE ... SPLIT PARTITION can only split partitions don't have sub-partitions"));
3551 :
3552 564 : if (get_partition_parent(partRelOid, false) != RelationGetRelid(rel))
3553 12 : ereport(ERROR,
3554 : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3555 : errmsg("relation \"%s\" is not a partition of relation \"%s\"",
3556 : RelationGetRelationName(partRel), RelationGetRelationName(rel)),
3557 : isMerge
3558 : ? errhint("ALTER TABLE ... MERGE PARTITIONS can only merge partitions don't have sub-partitions")
3559 : : errhint("ALTER TABLE ... SPLIT PARTITION can only split partitions don't have sub-partitions"));
3560 :
3561 552 : table_close(partRel, NoLock);
3562 552 : }
3563 :
3564 : /*
3565 : * transformPartitionCmdForSplit -
3566 : * analyze the ALTER TABLE ... SPLIT PARTITION command
3567 : *
3568 : * For each new partition, sps->bound is set to the transformed value of bound.
3569 : * Does checks for bounds of new partitions.
3570 : */
3571 : static void
3572 240 : transformPartitionCmdForSplit(CreateStmtContext *cxt, PartitionCmd *partcmd)
3573 : {
3574 240 : Relation parent = cxt->rel;
3575 : PartitionKey key;
3576 : char strategy;
3577 : Oid splitPartOid;
3578 : Oid defaultPartOid;
3579 240 : int default_index = -1;
3580 : bool isSplitPartDefault;
3581 : ListCell *listptr,
3582 : *listptr2;
3583 : List *splitlist;
3584 :
3585 240 : splitlist = partcmd->partlist;
3586 240 : key = RelationGetPartitionKey(parent);
3587 240 : strategy = get_partition_strategy(key);
3588 240 : defaultPartOid = get_default_oid_from_partdesc(RelationGetPartitionDesc(parent, true));
3589 :
3590 : /* Transform partition bounds for all partitions in the list: */
3591 1164 : foreach_node(SinglePartitionSpec, sps, splitlist)
3592 : {
3593 692 : cxt->partbound = NULL;
3594 692 : transformPartitionCmd(cxt, sps->bound);
3595 : /* Assign the transformed value of the partition bound. */
3596 688 : sps->bound = cxt->partbound;
3597 : }
3598 :
3599 : /*
3600 : * Open and lock the partition, check ownership along the way. We need to
3601 : * use AccessExclusiveLock here because this split partition will be
3602 : * detached, then dropped in ATExecSplitPartition.
3603 : */
3604 236 : splitPartOid = RangeVarGetRelidExtended(partcmd->name, AccessExclusiveLock,
3605 : 0, RangeVarCallbackOwnsRelation,
3606 : NULL);
3607 :
3608 228 : checkPartition(parent, splitPartOid, false);
3609 :
3610 224 : switch (strategy)
3611 : {
3612 220 : case PARTITION_STRATEGY_LIST:
3613 : case PARTITION_STRATEGY_RANGE:
3614 : {
3615 1084 : foreach_node(SinglePartitionSpec, sps, splitlist)
3616 : {
3617 652 : if (sps->bound->is_default)
3618 : {
3619 56 : if (default_index != -1)
3620 4 : ereport(ERROR,
3621 : errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3622 : errmsg("DEFAULT partition should be one"),
3623 : parser_errposition(cxt->pstate, sps->name->location));
3624 :
3625 52 : default_index = foreach_current_index(sps);
3626 : }
3627 : }
3628 : }
3629 216 : break;
3630 :
3631 4 : case PARTITION_STRATEGY_HASH:
3632 4 : ereport(ERROR,
3633 : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3634 : errmsg("partition of hash-partitioned table cannot be split"));
3635 : break;
3636 :
3637 0 : default:
3638 0 : elog(ERROR, "unexpected partition strategy: %d",
3639 : (int) key->strategy);
3640 : break;
3641 : }
3642 :
3643 : /* isSplitPartDefault: is the being split partition a DEFAULT partition? */
3644 216 : isSplitPartDefault = (defaultPartOid == splitPartOid);
3645 :
3646 216 : if (isSplitPartDefault && default_index == -1)
3647 4 : ereport(ERROR,
3648 : errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3649 : errmsg("can not split DEFAULT partition \"%s\"",
3650 : get_rel_name(splitPartOid)),
3651 : errhint("To split DEFAULT partition one of the new partition msut be DEFAULT"),
3652 : parser_errposition(cxt->pstate, ((SinglePartitionSpec *) linitial(splitlist))->name->location));
3653 :
3654 : /*
3655 : * If the partition being split is not the DEFAULT partition, but the
3656 : * DEFAULT partition exists, then none of the resulting split partitions
3657 : * can be the DEFAULT.
3658 : */
3659 212 : if (!isSplitPartDefault && (default_index != -1) && OidIsValid(defaultPartOid))
3660 : {
3661 : SinglePartitionSpec *spsDef =
3662 4 : (SinglePartitionSpec *) list_nth(splitlist, default_index);
3663 :
3664 4 : ereport(ERROR,
3665 : errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3666 : errmsg("can not split non-DEFAULT partition \"%s\"",
3667 : get_rel_name(splitPartOid)),
3668 : errmsg("new partition cannot be DEFAULT because DEFAULT partition \"%s\" already exists",
3669 : get_rel_name(defaultPartOid)),
3670 : parser_errposition(cxt->pstate, spsDef->name->location));
3671 : }
3672 :
3673 776 : foreach(listptr, splitlist)
3674 : {
3675 : Oid nspid;
3676 580 : SinglePartitionSpec *sps = (SinglePartitionSpec *) lfirst(listptr);
3677 580 : RangeVar *name = sps->name;
3678 :
3679 580 : nspid = RangeVarGetCreationNamespace(sps->name);
3680 :
3681 : /* Partitions in the list should have different names. */
3682 1180 : for_each_cell(listptr2, splitlist, lnext(splitlist, listptr))
3683 : {
3684 : Oid nspid2;
3685 612 : SinglePartitionSpec *sps2 = (SinglePartitionSpec *) lfirst(listptr2);
3686 612 : RangeVar *name2 = sps2->name;
3687 :
3688 612 : if (equal(name, name2))
3689 8 : ereport(ERROR,
3690 : errcode(ERRCODE_DUPLICATE_TABLE),
3691 : errmsg("partition with name \"%s\" is already used", name->relname),
3692 : parser_errposition(cxt->pstate, name2->location));
3693 :
3694 604 : nspid2 = RangeVarGetCreationNamespace(sps2->name);
3695 :
3696 604 : if (nspid2 == nspid && strcmp(name->relname, name2->relname) == 0)
3697 4 : ereport(ERROR,
3698 : errcode(ERRCODE_DUPLICATE_TABLE),
3699 : errmsg("partition with name \"%s\" is already used", name->relname),
3700 : parser_errposition(cxt->pstate, name2->location));
3701 : }
3702 : }
3703 :
3704 : /* Then we should check partitions with transformed bounds. */
3705 196 : check_partitions_for_split(parent, splitPartOid, splitlist, cxt->pstate);
3706 128 : }
3707 :
3708 :
3709 : /*
3710 : * transformPartitionCmdForMerge -
3711 : * analyze the ALTER TABLE ... MERGE PARTITIONS command
3712 : *
3713 : * Does simple checks for merged partitions. Calculates bound of the resulting
3714 : * partition.
3715 : */
3716 : static void
3717 168 : transformPartitionCmdForMerge(CreateStmtContext *cxt, PartitionCmd *partcmd)
3718 : {
3719 : Oid defaultPartOid;
3720 : Oid partOid;
3721 168 : Relation parent = cxt->rel;
3722 : PartitionKey key;
3723 : char strategy;
3724 : ListCell *listptr,
3725 : *listptr2;
3726 168 : bool isDefaultPart = false;
3727 168 : List *partOids = NIL;
3728 :
3729 168 : key = RelationGetPartitionKey(parent);
3730 168 : strategy = get_partition_strategy(key);
3731 :
3732 168 : if (strategy == PARTITION_STRATEGY_HASH)
3733 4 : ereport(ERROR,
3734 : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3735 : errmsg("partition of hash-partitioned table cannot be merged"));
3736 :
3737 : /* Does the partitioned table (parent) have a default partition? */
3738 164 : defaultPartOid = get_default_oid_from_partdesc(RelationGetPartitionDesc(parent, true));
3739 :
3740 492 : foreach(listptr, partcmd->partlist)
3741 : {
3742 368 : RangeVar *name = (RangeVar *) lfirst(listptr);
3743 :
3744 : /* Partitions in the list should have different names. */
3745 644 : for_each_cell(listptr2, partcmd->partlist, lnext(partcmd->partlist, listptr))
3746 : {
3747 280 : RangeVar *name2 = (RangeVar *) lfirst(listptr2);
3748 :
3749 280 : if (equal(name, name2))
3750 4 : ereport(ERROR,
3751 : errcode(ERRCODE_DUPLICATE_TABLE),
3752 : errmsg("partition with name \"%s\" is already used", name->relname),
3753 : parser_errposition(cxt->pstate, name2->location));
3754 : }
3755 :
3756 : /*
3757 : * Search the DEFAULT partition in the list. Open and lock partitions
3758 : * before calculating the boundary for resulting partition, we also
3759 : * check for ownership along the way. We need to use
3760 : * AccessExclusiveLock here, because these merged partitions will be
3761 : * detached and then dropped in ATExecMergePartitions.
3762 : */
3763 364 : partOid = RangeVarGetRelidExtended(name, AccessExclusiveLock, 0,
3764 : RangeVarCallbackOwnsRelation,
3765 : NULL);
3766 : /* Is the current partition a DEFAULT partition? */
3767 356 : if (partOid == defaultPartOid)
3768 4 : isDefaultPart = true;
3769 :
3770 : /*
3771 : * Extended check because the same partition can have different names
3772 : * (for example, "part_name" and "public.part_name").
3773 : */
3774 600 : foreach(listptr2, partOids)
3775 : {
3776 248 : Oid curOid = lfirst_oid(listptr2);
3777 :
3778 248 : if (curOid == partOid)
3779 4 : ereport(ERROR,
3780 : errcode(ERRCODE_DUPLICATE_TABLE),
3781 : errmsg("partition with name \"%s\" is already used", name->relname),
3782 : parser_errposition(cxt->pstate, name->location));
3783 : }
3784 :
3785 352 : checkPartition(parent, partOid, true);
3786 :
3787 328 : partOids = lappend_oid(partOids, partOid);
3788 : }
3789 :
3790 : /* Allocate the bound of the resulting partition. */
3791 : Assert(partcmd->bound == NULL);
3792 124 : partcmd->bound = makeNode(PartitionBoundSpec);
3793 :
3794 : /* Fill the partition bound. */
3795 124 : partcmd->bound->strategy = strategy;
3796 124 : partcmd->bound->location = -1;
3797 124 : partcmd->bound->is_default = isDefaultPart;
3798 124 : if (!isDefaultPart)
3799 120 : calculate_partition_bound_for_merge(parent, partcmd->partlist,
3800 : partOids, partcmd->bound,
3801 : cxt->pstate);
3802 116 : }
3803 :
3804 : /*
3805 : * transformAlterTableStmt -
3806 : * parse analysis for ALTER TABLE
3807 : *
3808 : * Returns the transformed AlterTableStmt. There may be additional actions
3809 : * to be done before and after the transformed statement, which are returned
3810 : * in *beforeStmts and *afterStmts as lists of utility command parsetrees.
3811 : *
3812 : * To avoid race conditions, it's important that this function rely only on
3813 : * the passed-in relid (and not on stmt->relation) to determine the target
3814 : * relation.
3815 : */
3816 : AlterTableStmt *
3817 15113 : transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
3818 : const char *queryString,
3819 : List **beforeStmts, List **afterStmts)
3820 : {
3821 : Relation rel;
3822 : TupleDesc tupdesc;
3823 : ParseState *pstate;
3824 : CreateStmtContext cxt;
3825 : List *save_alist;
3826 : ListCell *lcmd,
3827 : *l;
3828 15113 : List *newcmds = NIL;
3829 15113 : bool skipValidation = true;
3830 : AlterTableCmd *newcmd;
3831 : ParseNamespaceItem *nsitem;
3832 :
3833 : /* Caller is responsible for locking the relation */
3834 15113 : rel = relation_open(relid, NoLock);
3835 15113 : tupdesc = RelationGetDescr(rel);
3836 :
3837 : /* Set up pstate */
3838 15113 : pstate = make_parsestate(NULL);
3839 15113 : pstate->p_sourcetext = queryString;
3840 15113 : nsitem = addRangeTableEntryForRelation(pstate,
3841 : rel,
3842 : AccessShareLock,
3843 : NULL,
3844 : false,
3845 : true);
3846 15113 : addNSItemToQuery(pstate, nsitem, false, true, true);
3847 :
3848 : /* Set up CreateStmtContext */
3849 15113 : cxt.pstate = pstate;
3850 15113 : if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
3851 : {
3852 120 : cxt.stmtType = "ALTER FOREIGN TABLE";
3853 120 : cxt.isforeign = true;
3854 : }
3855 : else
3856 : {
3857 14993 : cxt.stmtType = "ALTER TABLE";
3858 14993 : cxt.isforeign = false;
3859 : }
3860 15113 : cxt.relation = stmt->relation;
3861 15113 : cxt.rel = rel;
3862 15113 : cxt.inhRelations = NIL;
3863 15113 : cxt.isalter = true;
3864 15113 : cxt.columns = NIL;
3865 15113 : cxt.ckconstraints = NIL;
3866 15113 : cxt.nnconstraints = NIL;
3867 15113 : cxt.fkconstraints = NIL;
3868 15113 : cxt.ixconstraints = NIL;
3869 15113 : cxt.likeclauses = NIL;
3870 15113 : cxt.blist = NIL;
3871 15113 : cxt.alist = NIL;
3872 15113 : cxt.pkey = NULL;
3873 15113 : cxt.ispartitioned = (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
3874 15113 : cxt.partbound = NULL;
3875 15113 : cxt.ofType = false;
3876 :
3877 : /*
3878 : * Transform ALTER subcommands that need it (most don't). These largely
3879 : * re-use code from CREATE TABLE.
3880 : */
3881 30022 : foreach(lcmd, stmt->cmds)
3882 : {
3883 15113 : AlterTableCmd *cmd = (AlterTableCmd *) lfirst(lcmd);
3884 :
3885 15113 : switch (cmd->subtype)
3886 : {
3887 1476 : case AT_AddColumn:
3888 : {
3889 1476 : ColumnDef *def = castNode(ColumnDef, cmd->def);
3890 :
3891 1476 : transformColumnDefinition(&cxt, def);
3892 :
3893 : /*
3894 : * If the column has a non-null default, we can't skip
3895 : * validation of foreign keys.
3896 : */
3897 1472 : if (def->raw_default != NULL)
3898 649 : skipValidation = false;
3899 :
3900 : /*
3901 : * All constraints are processed in other ways. Remove the
3902 : * original list
3903 : */
3904 1472 : def->constraints = NIL;
3905 :
3906 1472 : newcmds = lappend(newcmds, cmd);
3907 1472 : break;
3908 : }
3909 :
3910 9908 : case AT_AddConstraint:
3911 :
3912 : /*
3913 : * The original AddConstraint cmd node doesn't go to newcmds
3914 : */
3915 9908 : if (IsA(cmd->def, Constraint))
3916 : {
3917 9908 : transformTableConstraint(&cxt, (Constraint *) cmd->def);
3918 9904 : if (((Constraint *) cmd->def)->contype == CONSTR_FOREIGN)
3919 1735 : skipValidation = false;
3920 : }
3921 : else
3922 0 : elog(ERROR, "unrecognized node type: %d",
3923 : (int) nodeTag(cmd->def));
3924 9904 : break;
3925 :
3926 939 : case AT_AlterColumnType:
3927 : {
3928 939 : ColumnDef *def = castNode(ColumnDef, cmd->def);
3929 : AttrNumber attnum;
3930 :
3931 : /*
3932 : * For ALTER COLUMN TYPE, transform the USING clause if
3933 : * one was specified.
3934 : */
3935 939 : if (def->raw_default)
3936 : {
3937 167 : def->cooked_default =
3938 167 : transformExpr(pstate, def->raw_default,
3939 : EXPR_KIND_ALTER_COL_TRANSFORM);
3940 : }
3941 :
3942 : /*
3943 : * For identity column, create ALTER SEQUENCE command to
3944 : * change the data type of the sequence. Identity sequence
3945 : * is associated with the top level partitioned table.
3946 : * Hence ignore partitions.
3947 : */
3948 939 : if (!RelationGetForm(rel)->relispartition)
3949 : {
3950 871 : attnum = get_attnum(relid, cmd->name);
3951 871 : if (attnum == InvalidAttrNumber)
3952 0 : ereport(ERROR,
3953 : (errcode(ERRCODE_UNDEFINED_COLUMN),
3954 : errmsg("column \"%s\" of relation \"%s\" does not exist",
3955 : cmd->name, RelationGetRelationName(rel))));
3956 :
3957 871 : if (attnum > 0 &&
3958 867 : TupleDescAttr(tupdesc, attnum - 1)->attidentity)
3959 : {
3960 24 : Oid seq_relid = getIdentitySequence(rel, attnum, false);
3961 24 : Oid typeOid = typenameTypeId(pstate, def->typeName);
3962 24 : AlterSeqStmt *altseqstmt = makeNode(AlterSeqStmt);
3963 :
3964 : altseqstmt->sequence
3965 24 : = makeRangeVar(get_namespace_name(get_rel_namespace(seq_relid)),
3966 : get_rel_name(seq_relid),
3967 : -1);
3968 24 : altseqstmt->options = list_make1(makeDefElem("as",
3969 : (Node *) makeTypeNameFromOid(typeOid, -1),
3970 : -1));
3971 24 : altseqstmt->for_identity = true;
3972 24 : cxt.blist = lappend(cxt.blist, altseqstmt);
3973 : }
3974 : }
3975 :
3976 939 : newcmds = lappend(newcmds, cmd);
3977 939 : break;
3978 : }
3979 :
3980 107 : case AT_AddIdentity:
3981 : {
3982 107 : Constraint *def = castNode(Constraint, cmd->def);
3983 107 : ColumnDef *newdef = makeNode(ColumnDef);
3984 : AttrNumber attnum;
3985 :
3986 107 : newdef->colname = cmd->name;
3987 107 : newdef->identity = def->generated_when;
3988 107 : cmd->def = (Node *) newdef;
3989 :
3990 107 : attnum = get_attnum(relid, cmd->name);
3991 107 : if (attnum == InvalidAttrNumber)
3992 4 : ereport(ERROR,
3993 : (errcode(ERRCODE_UNDEFINED_COLUMN),
3994 : errmsg("column \"%s\" of relation \"%s\" does not exist",
3995 : cmd->name, RelationGetRelationName(rel))));
3996 :
3997 103 : generateSerialExtraStmts(&cxt, newdef,
3998 : get_atttype(relid, attnum),
3999 : def->options, true, true,
4000 : NULL, NULL);
4001 :
4002 103 : newcmds = lappend(newcmds, cmd);
4003 103 : break;
4004 : }
4005 :
4006 41 : case AT_SetIdentity:
4007 : {
4008 : /*
4009 : * Create an ALTER SEQUENCE statement for the internal
4010 : * sequence of the identity column.
4011 : */
4012 : ListCell *lc;
4013 41 : List *newseqopts = NIL;
4014 41 : List *newdef = NIL;
4015 : AttrNumber attnum;
4016 : Oid seq_relid;
4017 :
4018 : /*
4019 : * Split options into those handled by ALTER SEQUENCE and
4020 : * those for ALTER TABLE proper.
4021 : */
4022 122 : foreach(lc, castNode(List, cmd->def))
4023 : {
4024 81 : DefElem *def = lfirst_node(DefElem, lc);
4025 :
4026 81 : if (strcmp(def->defname, "generated") == 0)
4027 29 : newdef = lappend(newdef, def);
4028 : else
4029 52 : newseqopts = lappend(newseqopts, def);
4030 : }
4031 :
4032 41 : attnum = get_attnum(relid, cmd->name);
4033 41 : if (attnum == InvalidAttrNumber)
4034 0 : ereport(ERROR,
4035 : (errcode(ERRCODE_UNDEFINED_COLUMN),
4036 : errmsg("column \"%s\" of relation \"%s\" does not exist",
4037 : cmd->name, RelationGetRelationName(rel))));
4038 :
4039 41 : seq_relid = getIdentitySequence(rel, attnum, true);
4040 :
4041 41 : if (seq_relid)
4042 : {
4043 : AlterSeqStmt *seqstmt;
4044 :
4045 33 : seqstmt = makeNode(AlterSeqStmt);
4046 33 : seqstmt->sequence = makeRangeVar(get_namespace_name(get_rel_namespace(seq_relid)),
4047 : get_rel_name(seq_relid), -1);
4048 33 : seqstmt->options = newseqopts;
4049 33 : seqstmt->for_identity = true;
4050 33 : seqstmt->missing_ok = false;
4051 :
4052 33 : cxt.blist = lappend(cxt.blist, seqstmt);
4053 : }
4054 :
4055 : /*
4056 : * If column was not an identity column, we just let the
4057 : * ALTER TABLE command error out later. (There are cases
4058 : * this fails to cover, but we'll need to restructure
4059 : * where creation of the sequence dependency linkage
4060 : * happens before we can fix it.)
4061 : */
4062 :
4063 41 : cmd->def = (Node *) newdef;
4064 41 : newcmds = lappend(newcmds, cmd);
4065 41 : break;
4066 : }
4067 :
4068 2222 : case AT_AttachPartition:
4069 : case AT_DetachPartition:
4070 : {
4071 2222 : PartitionCmd *partcmd = (PartitionCmd *) cmd->def;
4072 :
4073 2222 : transformPartitionCmd(&cxt, partcmd->bound);
4074 : /* assign the transformed value of the partition bound */
4075 2206 : partcmd->bound = cxt.partbound;
4076 : }
4077 :
4078 2206 : newcmds = lappend(newcmds, cmd);
4079 2206 : break;
4080 :
4081 172 : case AT_MergePartitions:
4082 : {
4083 172 : PartitionCmd *partcmd = (PartitionCmd *) cmd->def;
4084 :
4085 172 : if (list_length(partcmd->partlist) < 2)
4086 4 : ereport(ERROR,
4087 : errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
4088 : errmsg("list of partitions to be merged should include at least two partitions"));
4089 :
4090 168 : transformPartitionCmdForMerge(&cxt, partcmd);
4091 116 : newcmds = lappend(newcmds, cmd);
4092 116 : break;
4093 : }
4094 :
4095 248 : case AT_SplitPartition:
4096 : {
4097 248 : PartitionCmd *partcmd = (PartitionCmd *) cmd->def;
4098 :
4099 248 : if (list_length(partcmd->partlist) < 2)
4100 8 : ereport(ERROR,
4101 : errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
4102 : errmsg("list of new partitions should contain at least two partitions"));
4103 :
4104 240 : transformPartitionCmdForSplit(&cxt, partcmd);
4105 128 : newcmds = lappend(newcmds, cmd);
4106 128 : break;
4107 : }
4108 :
4109 0 : default:
4110 :
4111 : /*
4112 : * Currently, we shouldn't actually get here for the
4113 : * subcommand types that don't require transformation; but if
4114 : * we do, just emit them unchanged.
4115 : */
4116 0 : newcmds = lappend(newcmds, cmd);
4117 0 : break;
4118 : }
4119 : }
4120 :
4121 : /*
4122 : * Transfer anything we already have in cxt.alist into save_alist, to keep
4123 : * it separate from the output of transformIndexConstraints.
4124 : */
4125 14909 : save_alist = cxt.alist;
4126 14909 : cxt.alist = NIL;
4127 :
4128 : /* Postprocess constraints */
4129 14909 : transformIndexConstraints(&cxt);
4130 14893 : transformFKConstraints(&cxt, skipValidation, true);
4131 14893 : transformCheckConstraints(&cxt, false);
4132 :
4133 : /*
4134 : * Push any index-creation commands into the ALTER, so that they can be
4135 : * scheduled nicely by tablecmds.c. Note that tablecmds.c assumes that
4136 : * the IndexStmt attached to an AT_AddIndex or AT_AddIndexConstraint
4137 : * subcommand has already been through transformIndexStmt.
4138 : */
4139 21750 : foreach(l, cxt.alist)
4140 : {
4141 6857 : Node *istmt = (Node *) lfirst(l);
4142 :
4143 : /*
4144 : * We assume here that cxt.alist contains only IndexStmts generated
4145 : * from primary key constraints.
4146 : */
4147 6857 : if (IsA(istmt, IndexStmt))
4148 : {
4149 6857 : IndexStmt *idxstmt = (IndexStmt *) istmt;
4150 :
4151 6857 : idxstmt = transformIndexStmt(relid, idxstmt, queryString);
4152 6857 : newcmd = makeNode(AlterTableCmd);
4153 6857 : newcmd->subtype = OidIsValid(idxstmt->indexOid) ? AT_AddIndexConstraint : AT_AddIndex;
4154 6857 : newcmd->def = (Node *) idxstmt;
4155 6857 : newcmds = lappend(newcmds, newcmd);
4156 : }
4157 : else
4158 0 : elog(ERROR, "unexpected stmt type %d", (int) nodeTag(istmt));
4159 : }
4160 14893 : cxt.alist = NIL;
4161 :
4162 : /* Append any CHECK, NOT NULL or FK constraints to the commands list */
4163 30530 : foreach_node(Constraint, def, cxt.ckconstraints)
4164 : {
4165 744 : newcmd = makeNode(AlterTableCmd);
4166 744 : newcmd->subtype = AT_AddConstraint;
4167 744 : newcmd->def = (Node *) def;
4168 744 : newcmds = lappend(newcmds, newcmd);
4169 : }
4170 34917 : foreach_node(Constraint, def, cxt.nnconstraints)
4171 : {
4172 5131 : newcmd = makeNode(AlterTableCmd);
4173 5131 : newcmd->subtype = AT_AddConstraint;
4174 5131 : newcmd->def = (Node *) def;
4175 5131 : newcmds = lappend(newcmds, newcmd);
4176 : }
4177 31525 : foreach_node(Constraint, def, cxt.fkconstraints)
4178 : {
4179 1739 : newcmd = makeNode(AlterTableCmd);
4180 1739 : newcmd->subtype = AT_AddConstraint;
4181 1739 : newcmd->def = (Node *) def;
4182 1739 : newcmds = lappend(newcmds, newcmd);
4183 : }
4184 :
4185 : /* Close rel */
4186 14893 : relation_close(rel, NoLock);
4187 :
4188 : /*
4189 : * Output results.
4190 : */
4191 14893 : stmt->cmds = newcmds;
4192 :
4193 14893 : *beforeStmts = cxt.blist;
4194 14893 : *afterStmts = list_concat(cxt.alist, save_alist);
4195 :
4196 14893 : return stmt;
4197 : }
4198 :
4199 :
4200 : /*
4201 : * Preprocess a list of column constraint clauses
4202 : * to attach constraint attributes to their primary constraint nodes
4203 : * and detect inconsistent/misplaced constraint attributes.
4204 : *
4205 : * NOTE: currently, attributes are only supported for FOREIGN KEY, UNIQUE,
4206 : * EXCLUSION, and PRIMARY KEY constraints, but someday they ought to be
4207 : * supported for other constraint types.
4208 : */
4209 : static void
4210 43647 : transformConstraintAttrs(CreateStmtContext *cxt, List *constraintList)
4211 : {
4212 43647 : Constraint *lastprimarycon = NULL;
4213 43647 : bool saw_deferrability = false;
4214 43647 : bool saw_initially = false;
4215 43647 : bool saw_enforced = false;
4216 : ListCell *clist;
4217 :
4218 : #define SUPPORTS_ATTRS(node) \
4219 : ((node) != NULL && \
4220 : ((node)->contype == CONSTR_PRIMARY || \
4221 : (node)->contype == CONSTR_UNIQUE || \
4222 : (node)->contype == CONSTR_EXCLUSION || \
4223 : (node)->contype == CONSTR_FOREIGN))
4224 :
4225 55471 : foreach(clist, constraintList)
4226 : {
4227 11840 : Constraint *con = (Constraint *) lfirst(clist);
4228 :
4229 11840 : if (!IsA(con, Constraint))
4230 0 : elog(ERROR, "unrecognized node type: %d",
4231 : (int) nodeTag(con));
4232 11840 : switch (con->contype)
4233 : {
4234 61 : case CONSTR_ATTR_DEFERRABLE:
4235 61 : if (!SUPPORTS_ATTRS(lastprimarycon))
4236 0 : ereport(ERROR,
4237 : (errcode(ERRCODE_SYNTAX_ERROR),
4238 : errmsg("misplaced DEFERRABLE clause"),
4239 : parser_errposition(cxt->pstate, con->location)));
4240 61 : if (saw_deferrability)
4241 0 : ereport(ERROR,
4242 : (errcode(ERRCODE_SYNTAX_ERROR),
4243 : errmsg("multiple DEFERRABLE/NOT DEFERRABLE clauses not allowed"),
4244 : parser_errposition(cxt->pstate, con->location)));
4245 61 : saw_deferrability = true;
4246 61 : lastprimarycon->deferrable = true;
4247 61 : break;
4248 :
4249 0 : case CONSTR_ATTR_NOT_DEFERRABLE:
4250 0 : if (!SUPPORTS_ATTRS(lastprimarycon))
4251 0 : ereport(ERROR,
4252 : (errcode(ERRCODE_SYNTAX_ERROR),
4253 : errmsg("misplaced NOT DEFERRABLE clause"),
4254 : parser_errposition(cxt->pstate, con->location)));
4255 0 : if (saw_deferrability)
4256 0 : ereport(ERROR,
4257 : (errcode(ERRCODE_SYNTAX_ERROR),
4258 : errmsg("multiple DEFERRABLE/NOT DEFERRABLE clauses not allowed"),
4259 : parser_errposition(cxt->pstate, con->location)));
4260 0 : saw_deferrability = true;
4261 0 : lastprimarycon->deferrable = false;
4262 0 : if (saw_initially &&
4263 0 : lastprimarycon->initdeferred)
4264 0 : ereport(ERROR,
4265 : (errcode(ERRCODE_SYNTAX_ERROR),
4266 : errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
4267 : parser_errposition(cxt->pstate, con->location)));
4268 0 : break;
4269 :
4270 47 : case CONSTR_ATTR_DEFERRED:
4271 47 : if (!SUPPORTS_ATTRS(lastprimarycon))
4272 0 : ereport(ERROR,
4273 : (errcode(ERRCODE_SYNTAX_ERROR),
4274 : errmsg("misplaced INITIALLY DEFERRED clause"),
4275 : parser_errposition(cxt->pstate, con->location)));
4276 47 : if (saw_initially)
4277 0 : ereport(ERROR,
4278 : (errcode(ERRCODE_SYNTAX_ERROR),
4279 : errmsg("multiple INITIALLY IMMEDIATE/DEFERRED clauses not allowed"),
4280 : parser_errposition(cxt->pstate, con->location)));
4281 47 : saw_initially = true;
4282 47 : lastprimarycon->initdeferred = true;
4283 :
4284 : /*
4285 : * If only INITIALLY DEFERRED appears, assume DEFERRABLE
4286 : */
4287 47 : if (!saw_deferrability)
4288 15 : lastprimarycon->deferrable = true;
4289 32 : else if (!lastprimarycon->deferrable)
4290 0 : ereport(ERROR,
4291 : (errcode(ERRCODE_SYNTAX_ERROR),
4292 : errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
4293 : parser_errposition(cxt->pstate, con->location)));
4294 47 : break;
4295 :
4296 4 : case CONSTR_ATTR_IMMEDIATE:
4297 4 : if (!SUPPORTS_ATTRS(lastprimarycon))
4298 0 : ereport(ERROR,
4299 : (errcode(ERRCODE_SYNTAX_ERROR),
4300 : errmsg("misplaced INITIALLY IMMEDIATE clause"),
4301 : parser_errposition(cxt->pstate, con->location)));
4302 4 : if (saw_initially)
4303 0 : ereport(ERROR,
4304 : (errcode(ERRCODE_SYNTAX_ERROR),
4305 : errmsg("multiple INITIALLY IMMEDIATE/DEFERRED clauses not allowed"),
4306 : parser_errposition(cxt->pstate, con->location)));
4307 4 : saw_initially = true;
4308 4 : lastprimarycon->initdeferred = false;
4309 4 : break;
4310 :
4311 24 : case CONSTR_ATTR_ENFORCED:
4312 24 : if (lastprimarycon == NULL ||
4313 24 : (lastprimarycon->contype != CONSTR_CHECK &&
4314 8 : lastprimarycon->contype != CONSTR_FOREIGN))
4315 4 : ereport(ERROR,
4316 : (errcode(ERRCODE_SYNTAX_ERROR),
4317 : errmsg("misplaced ENFORCED clause"),
4318 : parser_errposition(cxt->pstate, con->location)));
4319 20 : if (saw_enforced)
4320 4 : ereport(ERROR,
4321 : (errcode(ERRCODE_SYNTAX_ERROR),
4322 : errmsg("multiple ENFORCED/NOT ENFORCED clauses not allowed"),
4323 : parser_errposition(cxt->pstate, con->location)));
4324 16 : saw_enforced = true;
4325 16 : lastprimarycon->is_enforced = true;
4326 16 : break;
4327 :
4328 48 : case CONSTR_ATTR_NOT_ENFORCED:
4329 48 : if (lastprimarycon == NULL ||
4330 48 : (lastprimarycon->contype != CONSTR_CHECK &&
4331 12 : lastprimarycon->contype != CONSTR_FOREIGN))
4332 4 : ereport(ERROR,
4333 : (errcode(ERRCODE_SYNTAX_ERROR),
4334 : errmsg("misplaced NOT ENFORCED clause"),
4335 : parser_errposition(cxt->pstate, con->location)));
4336 44 : if (saw_enforced)
4337 4 : ereport(ERROR,
4338 : (errcode(ERRCODE_SYNTAX_ERROR),
4339 : errmsg("multiple ENFORCED/NOT ENFORCED clauses not allowed"),
4340 : parser_errposition(cxt->pstate, con->location)));
4341 40 : saw_enforced = true;
4342 40 : lastprimarycon->is_enforced = false;
4343 :
4344 : /* A NOT ENFORCED constraint must be marked as invalid. */
4345 40 : lastprimarycon->skip_validation = true;
4346 40 : lastprimarycon->initially_valid = false;
4347 40 : break;
4348 :
4349 11656 : default:
4350 : /* Otherwise it's not an attribute */
4351 11656 : lastprimarycon = con;
4352 : /* reset flags for new primary node */
4353 11656 : saw_deferrability = false;
4354 11656 : saw_initially = false;
4355 11656 : saw_enforced = false;
4356 11656 : break;
4357 : }
4358 : }
4359 43631 : }
4360 :
4361 : /*
4362 : * Special handling of type definition for a column
4363 : */
4364 : static void
4365 43452 : transformColumnType(CreateStmtContext *cxt, ColumnDef *column)
4366 : {
4367 : /*
4368 : * All we really need to do here is verify that the type is valid,
4369 : * including any collation spec that might be present.
4370 : */
4371 43452 : Type ctype = typenameType(cxt->pstate, column->typeName, NULL);
4372 :
4373 43443 : if (column->collClause)
4374 : {
4375 347 : Form_pg_type typtup = (Form_pg_type) GETSTRUCT(ctype);
4376 :
4377 347 : LookupCollation(cxt->pstate,
4378 347 : column->collClause->collname,
4379 347 : column->collClause->location);
4380 : /* Complain if COLLATE is applied to an uncollatable type */
4381 339 : if (!OidIsValid(typtup->typcollation))
4382 8 : ereport(ERROR,
4383 : (errcode(ERRCODE_DATATYPE_MISMATCH),
4384 : errmsg("collations are not supported by type %s",
4385 : format_type_be(typtup->oid)),
4386 : parser_errposition(cxt->pstate,
4387 : column->collClause->location)));
4388 : }
4389 :
4390 43427 : ReleaseSysCache(ctype);
4391 43427 : }
4392 :
4393 :
4394 : /*
4395 : * transformCreateSchemaStmtElements -
4396 : * analyzes the elements of a CREATE SCHEMA statement
4397 : *
4398 : * Split the schema element list from a CREATE SCHEMA statement into
4399 : * individual commands and place them in the result list in an order
4400 : * such that there are no forward references (e.g. GRANT to a table
4401 : * created later in the list). Note that the logic we use for determining
4402 : * forward references is presently quite incomplete.
4403 : *
4404 : * "schemaName" is the name of the schema that will be used for the creation
4405 : * of the objects listed, that may be compiled from the schema name defined
4406 : * in the statement or a role specification.
4407 : *
4408 : * SQL also allows constraints to make forward references, so thumb through
4409 : * the table columns and move forward references to a posterior alter-table
4410 : * command.
4411 : *
4412 : * The result is a list of parse nodes that still need to be analyzed ---
4413 : * but we can't analyze the later commands until we've executed the earlier
4414 : * ones, because of possible inter-object references.
4415 : *
4416 : * Note: this breaks the rules a little bit by modifying schema-name fields
4417 : * within passed-in structs. However, the transformation would be the same
4418 : * if done over, so it should be all right to scribble on the input to this
4419 : * extent.
4420 : */
4421 : List *
4422 684 : transformCreateSchemaStmtElements(List *schemaElts, const char *schemaName)
4423 : {
4424 : CreateSchemaStmtContext cxt;
4425 : List *result;
4426 : ListCell *elements;
4427 :
4428 684 : cxt.schemaname = schemaName;
4429 684 : cxt.sequences = NIL;
4430 684 : cxt.tables = NIL;
4431 684 : cxt.views = NIL;
4432 684 : cxt.indexes = NIL;
4433 684 : cxt.triggers = NIL;
4434 684 : cxt.grants = NIL;
4435 :
4436 : /*
4437 : * Run through each schema element in the schema element list. Separate
4438 : * statements by type, and do preliminary analysis.
4439 : */
4440 1012 : foreach(elements, schemaElts)
4441 : {
4442 388 : Node *element = lfirst(elements);
4443 :
4444 388 : switch (nodeTag(element))
4445 : {
4446 12 : case T_CreateSeqStmt:
4447 : {
4448 12 : CreateSeqStmt *elp = (CreateSeqStmt *) element;
4449 :
4450 12 : setSchemaName(cxt.schemaname, &elp->sequence->schemaname);
4451 0 : cxt.sequences = lappend(cxt.sequences, element);
4452 : }
4453 0 : break;
4454 :
4455 305 : case T_CreateStmt:
4456 : {
4457 305 : CreateStmt *elp = (CreateStmt *) element;
4458 :
4459 305 : setSchemaName(cxt.schemaname, &elp->relation->schemaname);
4460 :
4461 : /*
4462 : * XXX todo: deal with constraints
4463 : */
4464 293 : cxt.tables = lappend(cxt.tables, element);
4465 : }
4466 293 : break;
4467 :
4468 33 : case T_ViewStmt:
4469 : {
4470 33 : ViewStmt *elp = (ViewStmt *) element;
4471 :
4472 33 : setSchemaName(cxt.schemaname, &elp->view->schemaname);
4473 :
4474 : /*
4475 : * XXX todo: deal with references between views
4476 : */
4477 21 : cxt.views = lappend(cxt.views, element);
4478 : }
4479 21 : break;
4480 :
4481 26 : case T_IndexStmt:
4482 : {
4483 26 : IndexStmt *elp = (IndexStmt *) element;
4484 :
4485 26 : setSchemaName(cxt.schemaname, &elp->relation->schemaname);
4486 14 : cxt.indexes = lappend(cxt.indexes, element);
4487 : }
4488 14 : break;
4489 :
4490 12 : case T_CreateTrigStmt:
4491 : {
4492 12 : CreateTrigStmt *elp = (CreateTrigStmt *) element;
4493 :
4494 12 : setSchemaName(cxt.schemaname, &elp->relation->schemaname);
4495 0 : cxt.triggers = lappend(cxt.triggers, element);
4496 : }
4497 0 : break;
4498 :
4499 0 : case T_GrantStmt:
4500 0 : cxt.grants = lappend(cxt.grants, element);
4501 0 : break;
4502 :
4503 0 : default:
4504 0 : elog(ERROR, "unrecognized node type: %d",
4505 : (int) nodeTag(element));
4506 : }
4507 : }
4508 :
4509 624 : result = NIL;
4510 624 : result = list_concat(result, cxt.sequences);
4511 624 : result = list_concat(result, cxt.tables);
4512 624 : result = list_concat(result, cxt.views);
4513 624 : result = list_concat(result, cxt.indexes);
4514 624 : result = list_concat(result, cxt.triggers);
4515 624 : result = list_concat(result, cxt.grants);
4516 :
4517 624 : return result;
4518 : }
4519 :
4520 : /*
4521 : * setSchemaName
4522 : * Set or check schema name in an element of a CREATE SCHEMA command
4523 : */
4524 : static void
4525 388 : setSchemaName(const char *context_schema, char **stmt_schema_name)
4526 : {
4527 388 : if (*stmt_schema_name == NULL)
4528 312 : *stmt_schema_name = unconstify(char *, context_schema);
4529 76 : else if (strcmp(context_schema, *stmt_schema_name) != 0)
4530 60 : ereport(ERROR,
4531 : (errcode(ERRCODE_INVALID_SCHEMA_DEFINITION),
4532 : errmsg("CREATE specifies a schema (%s) "
4533 : "different from the one being created (%s)",
4534 : *stmt_schema_name, context_schema)));
4535 328 : }
4536 :
4537 : /*
4538 : * transformPartitionCmd
4539 : * Analyze the ATTACH/DETACH/SPLIT PARTITION command
4540 : *
4541 : * In case of the ATTACH/SPLIT PARTITION command, cxt->partbound is set to the
4542 : * transformed value of bound.
4543 : */
4544 : static void
4545 2914 : transformPartitionCmd(CreateStmtContext *cxt, PartitionBoundSpec *bound)
4546 : {
4547 2914 : Relation parentRel = cxt->rel;
4548 :
4549 2914 : switch (parentRel->rd_rel->relkind)
4550 : {
4551 2663 : case RELKIND_PARTITIONED_TABLE:
4552 : /* transform the partition bound, if any */
4553 : Assert(RelationGetPartitionKey(parentRel) != NULL);
4554 2663 : if (bound != NULL)
4555 2298 : cxt->partbound = transformPartitionBound(cxt->pstate, parentRel,
4556 : bound);
4557 2647 : break;
4558 251 : case RELKIND_PARTITIONED_INDEX:
4559 :
4560 : /*
4561 : * A partitioned index cannot have a partition bound set. ALTER
4562 : * INDEX prevents that with its grammar, but not ALTER TABLE.
4563 : */
4564 251 : if (bound != NULL)
4565 4 : ereport(ERROR,
4566 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
4567 : errmsg("\"%s\" is not a partitioned table",
4568 : RelationGetRelationName(parentRel))));
4569 247 : break;
4570 0 : case RELKIND_RELATION:
4571 : /* the table must be partitioned */
4572 0 : ereport(ERROR,
4573 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
4574 : errmsg("table \"%s\" is not partitioned",
4575 : RelationGetRelationName(parentRel))));
4576 : break;
4577 0 : case RELKIND_INDEX:
4578 : /* the index must be partitioned */
4579 0 : ereport(ERROR,
4580 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
4581 : errmsg("index \"%s\" is not partitioned",
4582 : RelationGetRelationName(parentRel))));
4583 : break;
4584 0 : default:
4585 : /* parser shouldn't let this case through */
4586 0 : elog(ERROR, "\"%s\" is not a partitioned table or index",
4587 : RelationGetRelationName(parentRel));
4588 : break;
4589 : }
4590 2894 : }
4591 :
4592 : /*
4593 : * transformPartitionBound
4594 : *
4595 : * Transform a partition bound specification
4596 : */
4597 : PartitionBoundSpec *
4598 8073 : transformPartitionBound(ParseState *pstate, Relation parent,
4599 : PartitionBoundSpec *spec)
4600 : {
4601 : PartitionBoundSpec *result_spec;
4602 8073 : PartitionKey key = RelationGetPartitionKey(parent);
4603 8073 : char strategy = get_partition_strategy(key);
4604 8073 : int partnatts = get_partition_natts(key);
4605 8073 : List *partexprs = get_partition_exprs(key);
4606 :
4607 : /* Avoid scribbling on input */
4608 8073 : result_spec = copyObject(spec);
4609 :
4610 8073 : if (spec->is_default)
4611 : {
4612 : /*
4613 : * Hash partitioning does not support a default partition; there's no
4614 : * use case for it (since the set of partitions to create is perfectly
4615 : * defined), and if users do get into it accidentally, it's hard to
4616 : * back out from it afterwards.
4617 : */
4618 527 : if (strategy == PARTITION_STRATEGY_HASH)
4619 4 : ereport(ERROR,
4620 : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
4621 : errmsg("a hash-partitioned table may not have a default partition")));
4622 :
4623 : /*
4624 : * In case of the default partition, parser had no way to identify the
4625 : * partition strategy. Assign the parent's strategy to the default
4626 : * partition bound spec.
4627 : */
4628 523 : result_spec->strategy = strategy;
4629 :
4630 523 : return result_spec;
4631 : }
4632 :
4633 7546 : if (strategy == PARTITION_STRATEGY_HASH)
4634 : {
4635 488 : if (spec->strategy != PARTITION_STRATEGY_HASH)
4636 8 : ereport(ERROR,
4637 : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
4638 : errmsg("invalid bound specification for a hash partition"),
4639 : parser_errposition(pstate, exprLocation((Node *) spec))));
4640 :
4641 480 : if (spec->modulus <= 0)
4642 8 : ereport(ERROR,
4643 : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
4644 : errmsg("modulus for hash partition must be an integer value greater than zero")));
4645 :
4646 : Assert(spec->remainder >= 0);
4647 :
4648 472 : if (spec->remainder >= spec->modulus)
4649 8 : ereport(ERROR,
4650 : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
4651 : errmsg("remainder for hash partition must be less than modulus")));
4652 : }
4653 7058 : else if (strategy == PARTITION_STRATEGY_LIST)
4654 : {
4655 : ListCell *cell;
4656 : char *colname;
4657 : Oid coltype;
4658 : int32 coltypmod;
4659 : Oid partcollation;
4660 :
4661 3266 : if (spec->strategy != PARTITION_STRATEGY_LIST)
4662 12 : ereport(ERROR,
4663 : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
4664 : errmsg("invalid bound specification for a list partition"),
4665 : parser_errposition(pstate, exprLocation((Node *) spec))));
4666 :
4667 : /* Get the only column's name in case we need to output an error */
4668 3254 : if (key->partattrs[0] != 0)
4669 3167 : colname = get_attname(RelationGetRelid(parent),
4670 3167 : key->partattrs[0], false);
4671 : else
4672 87 : colname = deparse_expression((Node *) linitial(partexprs),
4673 87 : deparse_context_for(RelationGetRelationName(parent),
4674 : RelationGetRelid(parent)),
4675 : false, false);
4676 : /* Need its type data too */
4677 3254 : coltype = get_partition_col_typid(key, 0);
4678 3254 : coltypmod = get_partition_col_typmod(key, 0);
4679 3254 : partcollation = get_partition_col_collation(key, 0);
4680 :
4681 3254 : result_spec->listdatums = NIL;
4682 8395 : foreach(cell, spec->listdatums)
4683 : {
4684 5181 : Node *expr = lfirst(cell);
4685 : Const *value;
4686 : ListCell *cell2;
4687 : bool duplicate;
4688 :
4689 5181 : value = transformPartitionBoundValue(pstate, expr,
4690 : colname, coltype, coltypmod,
4691 : partcollation);
4692 :
4693 : /* Don't add to the result if the value is a duplicate */
4694 5141 : duplicate = false;
4695 9475 : foreach(cell2, result_spec->listdatums)
4696 : {
4697 4334 : Const *value2 = lfirst_node(Const, cell2);
4698 :
4699 4334 : if (equal(value, value2))
4700 : {
4701 0 : duplicate = true;
4702 0 : break;
4703 : }
4704 : }
4705 5141 : if (duplicate)
4706 0 : continue;
4707 :
4708 5141 : result_spec->listdatums = lappend(result_spec->listdatums,
4709 : value);
4710 : }
4711 : }
4712 3792 : else if (strategy == PARTITION_STRATEGY_RANGE)
4713 : {
4714 3792 : if (spec->strategy != PARTITION_STRATEGY_RANGE)
4715 16 : ereport(ERROR,
4716 : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
4717 : errmsg("invalid bound specification for a range partition"),
4718 : parser_errposition(pstate, exprLocation((Node *) spec))));
4719 :
4720 3776 : if (list_length(spec->lowerdatums) != partnatts)
4721 4 : ereport(ERROR,
4722 : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
4723 : errmsg("FROM must specify exactly one value per partitioning column")));
4724 3772 : if (list_length(spec->upperdatums) != partnatts)
4725 4 : ereport(ERROR,
4726 : (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
4727 : errmsg("TO must specify exactly one value per partitioning column")));
4728 :
4729 : /*
4730 : * Convert raw parse nodes into PartitionRangeDatum nodes and perform
4731 : * any necessary validation.
4732 : */
4733 3724 : result_spec->lowerdatums =
4734 3768 : transformPartitionRangeBounds(pstate, spec->lowerdatums,
4735 : parent);
4736 3720 : result_spec->upperdatums =
4737 3724 : transformPartitionRangeBounds(pstate, spec->upperdatums,
4738 : parent);
4739 : }
4740 : else
4741 0 : elog(ERROR, "unexpected partition strategy: %d", (int) strategy);
4742 :
4743 7398 : return result_spec;
4744 : }
4745 :
4746 : /*
4747 : * transformPartitionRangeBounds
4748 : * This converts the expressions for range partition bounds from the raw
4749 : * grammar representation to PartitionRangeDatum structs
4750 : */
4751 : static List *
4752 7492 : transformPartitionRangeBounds(ParseState *pstate, List *blist,
4753 : Relation parent)
4754 : {
4755 7492 : List *result = NIL;
4756 7492 : PartitionKey key = RelationGetPartitionKey(parent);
4757 7492 : List *partexprs = get_partition_exprs(key);
4758 : ListCell *lc;
4759 : int i,
4760 : j;
4761 :
4762 7492 : j = 0;
4763 16438 : foreach(lc, blist)
4764 : {
4765 8982 : Node *expr = lfirst(lc);
4766 8982 : PartitionRangeDatum *prd = NULL;
4767 :
4768 8982 : i = foreach_current_index(lc);
4769 :
4770 : /*
4771 : * Infinite range bounds -- "minvalue" and "maxvalue" -- get passed in
4772 : * as ColumnRefs.
4773 : */
4774 8982 : if (IsA(expr, ColumnRef))
4775 : {
4776 523 : ColumnRef *cref = (ColumnRef *) expr;
4777 523 : char *cname = NULL;
4778 :
4779 : /*
4780 : * There should be a single field named either "minvalue" or
4781 : * "maxvalue".
4782 : */
4783 523 : if (list_length(cref->fields) == 1 &&
4784 519 : IsA(linitial(cref->fields), String))
4785 519 : cname = strVal(linitial(cref->fields));
4786 :
4787 523 : if (cname == NULL)
4788 : {
4789 : /*
4790 : * ColumnRef is not in the desired single-field-name form. For
4791 : * consistency between all partition strategies, let the
4792 : * expression transformation report any errors rather than
4793 : * doing it ourselves.
4794 : */
4795 : }
4796 519 : else if (strcmp("minvalue", cname) == 0)
4797 : {
4798 252 : prd = makeNode(PartitionRangeDatum);
4799 252 : prd->kind = PARTITION_RANGE_DATUM_MINVALUE;
4800 252 : prd->value = NULL;
4801 : }
4802 267 : else if (strcmp("maxvalue", cname) == 0)
4803 : {
4804 259 : prd = makeNode(PartitionRangeDatum);
4805 259 : prd->kind = PARTITION_RANGE_DATUM_MAXVALUE;
4806 259 : prd->value = NULL;
4807 : }
4808 : }
4809 :
4810 8982 : if (prd == NULL)
4811 : {
4812 : char *colname;
4813 : Oid coltype;
4814 : int32 coltypmod;
4815 : Oid partcollation;
4816 : Const *value;
4817 :
4818 : /* Get the column's name in case we need to output an error */
4819 8471 : if (key->partattrs[i] != 0)
4820 7893 : colname = get_attname(RelationGetRelid(parent),
4821 7893 : key->partattrs[i], false);
4822 : else
4823 : {
4824 578 : colname = deparse_expression((Node *) list_nth(partexprs, j),
4825 578 : deparse_context_for(RelationGetRelationName(parent),
4826 : RelationGetRelid(parent)),
4827 : false, false);
4828 578 : ++j;
4829 : }
4830 :
4831 : /* Need its type data too */
4832 8471 : coltype = get_partition_col_typid(key, i);
4833 8471 : coltypmod = get_partition_col_typmod(key, i);
4834 8471 : partcollation = get_partition_col_collation(key, i);
4835 :
4836 8471 : value = transformPartitionBoundValue(pstate, expr,
4837 : colname,
4838 : coltype, coltypmod,
4839 : partcollation);
4840 8439 : if (value->constisnull)
4841 4 : ereport(ERROR,
4842 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
4843 : errmsg("cannot specify NULL in range bound")));
4844 8435 : prd = makeNode(PartitionRangeDatum);
4845 8435 : prd->kind = PARTITION_RANGE_DATUM_VALUE;
4846 8435 : prd->value = (Node *) value;
4847 : }
4848 :
4849 8946 : prd->location = exprLocation(expr);
4850 :
4851 8946 : result = lappend(result, prd);
4852 : }
4853 :
4854 : /*
4855 : * Once we see MINVALUE or MAXVALUE for one column, the remaining columns
4856 : * must be the same.
4857 : */
4858 7456 : validateInfiniteBounds(pstate, result);
4859 :
4860 7444 : return result;
4861 : }
4862 :
4863 : /*
4864 : * validateInfiniteBounds
4865 : *
4866 : * Check that a MAXVALUE or MINVALUE specification in a partition bound is
4867 : * followed only by more of the same.
4868 : */
4869 : static void
4870 7456 : validateInfiniteBounds(ParseState *pstate, List *blist)
4871 : {
4872 : ListCell *lc;
4873 7456 : PartitionRangeDatumKind kind = PARTITION_RANGE_DATUM_VALUE;
4874 :
4875 16386 : foreach(lc, blist)
4876 : {
4877 8942 : PartitionRangeDatum *prd = lfirst_node(PartitionRangeDatum, lc);
4878 :
4879 8942 : if (kind == prd->kind)
4880 8575 : continue;
4881 :
4882 367 : switch (kind)
4883 : {
4884 355 : case PARTITION_RANGE_DATUM_VALUE:
4885 355 : kind = prd->kind;
4886 355 : break;
4887 :
4888 4 : case PARTITION_RANGE_DATUM_MAXVALUE:
4889 4 : ereport(ERROR,
4890 : (errcode(ERRCODE_DATATYPE_MISMATCH),
4891 : errmsg("every bound following MAXVALUE must also be MAXVALUE"),
4892 : parser_errposition(pstate, exprLocation((Node *) prd))));
4893 : break;
4894 :
4895 8 : case PARTITION_RANGE_DATUM_MINVALUE:
4896 8 : ereport(ERROR,
4897 : (errcode(ERRCODE_DATATYPE_MISMATCH),
4898 : errmsg("every bound following MINVALUE must also be MINVALUE"),
4899 : parser_errposition(pstate, exprLocation((Node *) prd))));
4900 : break;
4901 : }
4902 : }
4903 7444 : }
4904 :
4905 : /*
4906 : * Transform one entry in a partition bound spec, producing a constant.
4907 : */
4908 : static Const *
4909 13652 : transformPartitionBoundValue(ParseState *pstate, Node *val,
4910 : const char *colName, Oid colType, int32 colTypmod,
4911 : Oid partCollation)
4912 : {
4913 : Node *value;
4914 :
4915 : /* Transform raw parsetree */
4916 13652 : value = transformExpr(pstate, val, EXPR_KIND_PARTITION_BOUND);
4917 :
4918 : /*
4919 : * transformExpr() should have already rejected column references,
4920 : * subqueries, aggregates, window functions, and SRFs, based on the
4921 : * EXPR_KIND_ of a partition bound expression.
4922 : */
4923 : Assert(!contain_var_clause(value));
4924 :
4925 : /*
4926 : * Coerce to the correct type. This might cause an explicit coercion step
4927 : * to be added on top of the expression, which must be evaluated before
4928 : * returning the result to the caller.
4929 : */
4930 13584 : value = coerce_to_target_type(pstate,
4931 : value, exprType(value),
4932 : colType,
4933 : colTypmod,
4934 : COERCION_ASSIGNMENT,
4935 : COERCE_IMPLICIT_CAST,
4936 : -1);
4937 :
4938 13584 : if (value == NULL)
4939 4 : ereport(ERROR,
4940 : (errcode(ERRCODE_DATATYPE_MISMATCH),
4941 : errmsg("specified value cannot be cast to type %s for column \"%s\"",
4942 : format_type_be(colType), colName),
4943 : parser_errposition(pstate, exprLocation(val))));
4944 :
4945 : /*
4946 : * Evaluate the expression, if needed, assigning the partition key's data
4947 : * type and collation to the resulting Const node.
4948 : */
4949 13580 : if (!IsA(value, Const))
4950 : {
4951 970 : assign_expr_collations(pstate, value);
4952 970 : value = (Node *) expression_planner((Expr *) value);
4953 970 : value = (Node *) evaluate_expr((Expr *) value, colType, colTypmod,
4954 : partCollation);
4955 970 : if (!IsA(value, Const))
4956 0 : elog(ERROR, "could not evaluate partition bound expression");
4957 : }
4958 : else
4959 : {
4960 : /*
4961 : * If the expression is already a Const, as is often the case, we can
4962 : * skip the rather expensive steps above. But we still have to insert
4963 : * the right collation, since coerce_to_target_type doesn't handle
4964 : * that.
4965 : */
4966 12610 : ((Const *) value)->constcollid = partCollation;
4967 : }
4968 :
4969 : /*
4970 : * Attach original expression's parse location to the Const, so that
4971 : * that's what will be reported for any later errors related to this
4972 : * partition bound.
4973 : */
4974 13580 : ((Const *) value)->location = exprLocation(val);
4975 :
4976 13580 : return (Const *) value;
4977 : }
|