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

Generated by: LCOV version 2.0-1