LCOV - code coverage report
Current view: top level - src/backend/parser - parse_utilcmd.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 1429 1591 89.8 %
Date: 2024-11-21 08:14:44 Functions: 28 28 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14