LCOV - code coverage report
Current view: top level - src/backend/parser - parse_utilcmd.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 1459 1620 90.1 %
Date: 2025-02-21 18:14:53 Functions: 28 28 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14