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