LCOV - differential code coverage report
Current view: top level - src/backend/parser - parse_utilcmd.c (source / functions) Coverage Total Hit UNC UIC UBC GBC GIC GNC CBC DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 90.7 % 1749 1587 2 160 108 1479 5
Current Date: 2026-03-14 14:10:32 -0400 Functions: 100.0 % 31 31 7 24 1
Baseline: lcov-20260315-024220-baseline Branches: 69.4 % 1545 1073 26 1 445 1 1 101 970 4
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 98.4 % 127 125 2 108 17
(360..) days: 90.1 % 1622 1462 160 1462
Function coverage date bins:
(30,360] days: 100.0 % 4 4 4
(360..) days: 100.0 % 27 27 3 24
Branch coverage date bins:
(30,360] days: 81.4 % 145 118 26 1 101 17
(360..) days: 68.2 % 1400 955 1 444 1 1 953

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

Generated by: LCOV version 2.4-beta