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: 7a15cff1f11193467898da1c1fabf06fd2caee04 vs 84a3778c79c2d28b4dc281d03ef2ab019b16483b Lines: 90.7 % 1745 1583 2 160 103 1480 4
Current Date: 2025-12-15 18:36:29 -0500 Functions: 100.0 % 31 31 5 26 1
Baseline: lcov-20251216-010103-baseline Branches: 69.4 % 1545 1073 26 1 445 1 1 101 970
Baseline Date: 2025-12-15 13:30:48 -0800 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 98.1 % 105 103 2 103
(30,360] days: 100.0 % 48 48 48
(360..) days: 89.9 % 1592 1432 160 1432
Function coverage date bins:
(1,7] days: 100.0 % 4 4 4
(360..) days: 100.0 % 27 27 1 26
Branch coverage date bins:
(1,7] days: 79.5 % 127 101 26 101
(30,360] days: 86.5 % 52 45 7 45
(360..) days: 67.9 % 1366 927 1 438 1 1 925

 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-2025, PostgreSQL Global Development Group
                                 16                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 17                 :                :  *
                                 18                 :                :  *  src/backend/parser/parse_utilcmd.c
                                 19                 :                :  *
                                 20                 :                :  *-------------------------------------------------------------------------
                                 21                 :                :  */
                                 22                 :                : 
                                 23                 :                : #include "postgres.h"
                                 24                 :                : 
                                 25                 :                : #include "access/amapi.h"
                                 26                 :                : #include "access/htup_details.h"
                                 27                 :                : #include "access/relation.h"
                                 28                 :                : #include "access/reloptions.h"
                                 29                 :                : #include "access/table.h"
                                 30                 :                : #include "access/toast_compression.h"
                                 31                 :                : #include "catalog/dependency.h"
                                 32                 :                : #include "catalog/heap.h"
                                 33                 :                : #include "catalog/index.h"
                                 34                 :                : #include "catalog/namespace.h"
                                 35                 :                : #include "catalog/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 *
 6751 tgl@sss.pgh.pa.us         167                 :CBC       20178 : 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 */
 3926 alvherre@alvh.no-ip.      179                 :          20178 :     pstate = make_parsestate(NULL);
                                180                 :          20178 :     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                 :          20178 :     setup_parser_errposition_callback(&pcbstate, pstate,
                                189                 :          20178 :                                       stmt->relation->location);
                                190                 :                :     namespaceid =
 5083 rhaas@postgresql.org      191                 :          20178 :         RangeVarGetAndCheckCreationNamespace(stmt->relation, NoLock,
                                192                 :                :                                              &existing_relid);
 3926 alvherre@alvh.no-ip.      193                 :          20172 :     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                 :                :      */
 5083 rhaas@postgresql.org      199   [ +  +  +  + ]:          20172 :     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                 :                : 
 1226 tgl@sss.pgh.pa.us         207                 :              5 :         ObjectAddressSet(address, RelationRelationId, existing_relid);
                                208                 :              5 :         checkMembershipInCurrentExtension(&address);
                                209                 :                : 
                                210                 :                :         /* OK to skip */
 5083 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                 :                :      */
 5482                           225         [ +  + ]:          20167 :     if (stmt->relation->schemaname == NULL
                                226         [ +  + ]:          18517 :         && stmt->relation->relpersistence != RELPERSISTENCE_TEMP)
 6686 tgl@sss.pgh.pa.us         227                 :          17257 :         stmt->relation->schemaname = get_namespace_name(namespaceid);
                                228                 :                : 
                                229                 :                :     /* Set up CreateStmtContext */
 5439                           230                 :          20167 :     cxt.pstate = pstate;
 5463 rhaas@postgresql.org      231         [ +  + ]:          20167 :     if (IsA(stmt, CreateForeignTableStmt))
                                232                 :                :     {
                                233                 :            257 :         cxt.stmtType = "CREATE FOREIGN TABLE";
 4662 tgl@sss.pgh.pa.us         234                 :            257 :         cxt.isforeign = true;
                                235                 :                :     }
                                236                 :                :     else
                                237                 :                :     {
 5463 rhaas@postgresql.org      238                 :          19910 :         cxt.stmtType = "CREATE TABLE";
 4662 tgl@sss.pgh.pa.us         239                 :          19910 :         cxt.isforeign = false;
                                240                 :                :     }
 6751                           241                 :          20167 :     cxt.relation = stmt->relation;
                                242                 :          20167 :     cxt.rel = NULL;
                                243                 :          20167 :     cxt.inhRelations = stmt->inhRelations;
                                244                 :          20167 :     cxt.isalter = false;
                                245                 :          20167 :     cxt.columns = NIL;
                                246                 :          20167 :     cxt.ckconstraints = NIL;
  403 alvherre@alvh.no-ip.      247                 :          20167 :     cxt.nnconstraints = NIL;
 6751 tgl@sss.pgh.pa.us         248                 :          20167 :     cxt.fkconstraints = NIL;
                                249                 :          20167 :     cxt.ixconstraints = NIL;
 1853                           250                 :          20167 :     cxt.likeclauses = NIL;
 6751                           251                 :          20167 :     cxt.blist = NIL;
                                252                 :          20167 :     cxt.alist = NIL;
                                253                 :          20167 :     cxt.pkey = NULL;
 3296 rhaas@postgresql.org      254                 :          20167 :     cxt.ispartitioned = stmt->partspec != NULL;
 2930 peter_e@gmx.net           255                 :          20167 :     cxt.partbound = stmt->partbound;
                                256                 :          20167 :     cxt.ofType = (stmt->ofTypename != NULL);
                                257                 :                : 
 5772 bruce@momjian.us          258   [ +  +  -  + ]:          20167 :     Assert(!stmt->ofTypename || !stmt->inhRelations); /* grammar enforces */
                                259                 :                : 
 5801 peter_e@gmx.net           260         [ +  + ]:          20167 :     if (stmt->ofTypename)
 5439 tgl@sss.pgh.pa.us         261                 :             61 :         transformOfType(&cxt, stmt->ofTypename);
                                262                 :                : 
 3296 rhaas@postgresql.org      263         [ +  + ]:          20158 :     if (stmt->partspec)
                                264                 :                :     {
                                265   [ +  +  +  + ]:           2796 :         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                 :                :      */
 6751 tgl@sss.pgh.pa.us         275   [ +  +  +  +  :          55708 :     foreach(elements, stmt->tableElts)
                                              +  + ]
                                276                 :                :     {
                                277                 :          35674 :         Node       *element = lfirst(elements);
                                278                 :                : 
                                279   [ +  +  +  - ]:          35674 :         switch (nodeTag(element))
                                280                 :                :         {
                                281                 :          33863 :             case T_ColumnDef:
 5439                           282                 :          33863 :                 transformColumnDefinition(&cxt, (ColumnDef *) element);
 6751                           283                 :          33754 :                 break;
                                284                 :                : 
 3281                           285                 :           1418 :             case T_Constraint:
                                286                 :           1418 :                 transformTableConstraint(&cxt, (Constraint *) element);
 6751                           287                 :           1412 :                 break;
                                288                 :                : 
 3281                           289                 :            393 :             case T_TableLikeClause:
                                290                 :            393 :                 transformTableLikeClause(&cxt, (TableLikeClause *) element);
 3725 bruce@momjian.us          291                 :            387 :                 break;
                                292                 :                : 
 6751 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                 :                :      */
 6751 tgl@sss.pgh.pa.us         306                 :CBC       20034 :     save_alist = cxt.alist;
                                307                 :          20034 :     cxt.alist = NIL;
                                308                 :                : 
                                309         [ -  + ]:          20034 :     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                 :                :      */
  403 alvherre@alvh.no-ip.      319   [ +  +  +  +  :          46224 :     foreach_node(Constraint, nn, cxt.nnconstraints)
                                              +  + ]
                                320                 :                :     {
                                321                 :           6156 :         char       *colname = strVal(linitial(nn->keys));
                                322                 :                : 
                                323   [ +  +  +  +  :          14477 :         foreach_node(ColumnDef, cd, cxt.columns)
                                              +  + ]
                                324                 :                :         {
                                325                 :                :             /* not our column? */
                                326         [ +  + ]:           8309 :             if (strcmp(cd->colname, colname) != 0)
                                327                 :           2165 :                 continue;
                                328                 :                :             /* Already marked not-null? Nothing to do */
                                329         [ +  + ]:           6144 :             if (cd->is_not_null)
                                330                 :           5894 :                 break;
                                331                 :                :             /* Bingo, we're done for this constraint */
                                332                 :            250 :             cd->is_not_null = true;
                                333                 :            250 :             break;
                                334                 :                :         }
                                335                 :                :     }
                                336                 :                : 
                                337                 :                :     /*
                                338                 :                :      * Postprocess constraints that give rise to index definitions.
                                339                 :                :      */
 5439 tgl@sss.pgh.pa.us         340                 :          20034 :     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                 :                :      */
 1853                           354                 :          20013 :     cxt.alist = list_concat(cxt.alist, cxt.likeclauses);
                                355                 :                : 
                                356                 :                :     /*
                                357                 :                :      * Postprocess foreign-key constraints.
                                358                 :                :      */
 5439                           359                 :          20013 :     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                 :                :      */
 1685 alvherre@alvh.no-ip.      367                 :          20013 :     transformCheckConstraints(&cxt, !cxt.isforeign);
                                368                 :                : 
                                369                 :                :     /*
                                370                 :                :      * Output results.
                                371                 :                :      */
 6751 tgl@sss.pgh.pa.us         372                 :          20013 :     stmt->tableElts = cxt.columns;
                                373                 :          20013 :     stmt->constraints = cxt.ckconstraints;
  403 alvherre@alvh.no-ip.      374                 :          20013 :     stmt->nnconstraints = cxt.nnconstraints;
                                375                 :                : 
 6751 tgl@sss.pgh.pa.us         376                 :          20013 :     result = lappend(cxt.blist, stmt);
                                377                 :          20013 :     result = list_concat(result, cxt.alist);
                                378                 :          20013 :     result = list_concat(result, save_alist);
                                379                 :                : 
                                380                 :          20013 :     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
 3176 peter_e@gmx.net           393                 :            678 : 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;
 3135 bruce@momjian.us          399                 :            678 :     DefElem    *nameEl = NULL;
  455 tgl@sss.pgh.pa.us         400                 :            678 :     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 */
  991 drowley@postgresql.o      410                 :            678 :     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                 :                :      */
 3176 peter_e@gmx.net           417   [ +  +  +  +  :            866 :     foreach(option, seqoptions)
                                              +  + ]
                                418                 :                :     {
 3172 tgl@sss.pgh.pa.us         419                 :            188 :         DefElem    *defel = lfirst_node(DefElem, option);
                                420                 :                : 
 3176 peter_e@gmx.net           421         [ +  + ]:            188 :         if (strcmp(defel->defname, "sequence_name") == 0)
                                422                 :                :         {
                                423         [ -  + ]:             22 :             if (nameEl)
 1615 dean.a.rasheed@gmail      424                 :UBC           0 :                 errorConflictingDefElem(defel, cxt->pstate);
 3176 peter_e@gmx.net           425                 :CBC          22 :             nameEl = defel;
  455 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)
  455 tgl@sss.pgh.pa.us         432                 :UBC           0 :                 errorConflictingDefElem(defel, cxt->pstate);
  455 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                 :                :      */
 3176 peter_e@gmx.net           441         [ +  + ]:            678 :     if (nameEl)
                                442                 :                :     {
                                443                 :                :         /* Use specified name */
 3135 bruce@momjian.us          444                 :             22 :         RangeVar   *rv = makeRangeVarFromNameList(castNode(List, nameEl->arg));
                                445                 :                : 
 3176 peter_e@gmx.net           446                 :             22 :         snamespace = rv->schemaname;
 3110 tgl@sss.pgh.pa.us         447         [ -  + ]:             22 :         if (!snamespace)
                                448                 :                :         {
                                449                 :                :             /* Given unqualified SEQUENCE NAME, select namespace */
 3110 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                 :                :         }
 3176 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         [ +  + ]:            656 :         if (cxt->rel)
                                472                 :            104 :             snamespaceid = RelationGetNamespace(cxt->rel);
                                473                 :                :         else
                                474                 :                :         {
                                475                 :            552 :             snamespaceid = RangeVarGetCreationNamespace(cxt->relation);
                                476                 :            552 :             RangeVarAdjustRelationPersistence(cxt->relation, snamespaceid);
                                477                 :                :         }
                                478                 :            656 :         snamespace = get_namespace_name(snamespaceid);
                                479                 :            656 :         sname = ChooseRelationName(cxt->relation->relname,
                                480                 :            656 :                                    column->colname,
                                481                 :                :                                    "seq",
                                482                 :                :                                    snamespaceid,
                                483                 :                :                                    false);
                                484                 :                :     }
                                485                 :                : 
                                486         [ +  + ]:            678 :     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                 :                :      */
  455 tgl@sss.pgh.pa.us         501         [ +  + ]:            678 :     seqpersistence = cxt->rel ? cxt->rel->rd_rel->relpersistence : cxt->relation->relpersistence;
                                502         [ +  + ]:            678 :     if (loggedEl)
                                503                 :                :     {
                                504         [ -  + ]:              2 :         if (seqpersistence == RELPERSISTENCE_TEMP)
  455 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)));
  455 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                 :                :      */
 3176 peter_e@gmx.net           519                 :            678 :     seqstmt = makeNode(CreateSeqStmt);
                                520                 :            678 :     seqstmt->for_identity = for_identity;
                                521                 :            678 :     seqstmt->sequence = makeRangeVar(snamespace, sname, -1);
  455 tgl@sss.pgh.pa.us         522                 :            678 :     seqstmt->sequence->relpersistence = seqpersistence;
 3176 peter_e@gmx.net           523                 :            678 :     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         [ +  + ]:            678 :     if (seqtypid)
 3110 tgl@sss.pgh.pa.us         532                 :            672 :         seqstmt->options = lcons(makeDefElem("as",
 3100                           533                 :            672 :                                              (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                 :                :      */
 3176 peter_e@gmx.net           544         [ +  + ]:            678 :     if (cxt->rel)
                                545                 :            126 :         seqstmt->ownerId = cxt->rel->rd_rel->relowner;
                                546                 :                :     else
                                547                 :            552 :         seqstmt->ownerId = InvalidOid;
                                548                 :                : 
                                549                 :            678 :     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                 :                :      */
 2874                           557                 :            678 :     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                 :                :      */
 3176                           568                 :            678 :     altseqstmt = makeNode(AlterSeqStmt);
                                569                 :            678 :     altseqstmt->sequence = makeRangeVar(snamespace, sname, -1);
                                570                 :            678 :     attnamelist = list_make3(makeString(snamespace),
                                571                 :                :                              makeString(cxt->relation->relname),
                                572                 :                :                              makeString(column->colname));
                                573                 :            678 :     altseqstmt->options = list_make1(makeDefElem("owned_by",
                                574                 :                :                                                  (Node *) attnamelist, -1));
                                575                 :            678 :     altseqstmt->for_identity = for_identity;
                                576                 :                : 
 2162 tgl@sss.pgh.pa.us         577         [ +  + ]:            678 :     if (col_exists)
                                578                 :             83 :         cxt->blist = lappend(cxt->blist, altseqstmt);
                                579                 :                :     else
                                580                 :            595 :         cxt->alist = lappend(cxt->alist, altseqstmt);
                                581                 :                : 
 3176 peter_e@gmx.net           582         [ +  + ]:            678 :     if (snamespace_p)
                                583                 :            423 :         *snamespace_p = snamespace;
                                584         [ +  + ]:            678 :     if (sname_p)
                                585                 :            423 :         *sname_p = sname;
                                586                 :            678 : }
                                587                 :                : 
                                588                 :                : /*
                                589                 :                :  * transformColumnDefinition -
                                590                 :                :  *      transform a single ColumnDef within CREATE TABLE
                                591                 :                :  *      Also used in ALTER TABLE ADD COLUMN
                                592                 :                :  */
                                593                 :                : static void
 5439 tgl@sss.pgh.pa.us         594                 :          34900 : 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;
  403 alvherre@alvh.no-ip.      601                 :          34900 :     bool        need_notnull = false;
                                602                 :          34900 :     bool        disallow_noinherit_notnull = false;
                                603                 :          34900 :     Constraint *notnull_constraint = NULL;
                                604                 :                : 
 6751 tgl@sss.pgh.pa.us         605                 :          34900 :     cxt->columns = lappend(cxt->columns, column);
                                606                 :                : 
                                607                 :                :     /* Check for SERIAL pseudo-types */
                                608                 :          34900 :     is_serial = false;
 5801 peter_e@gmx.net           609         [ +  + ]:          34900 :     if (column->typeName
                                610         [ +  + ]:          34721 :         && list_length(column->typeName->names) == 1
                                611         [ +  - ]:          14635 :         && !column->typeName->pct_type)
                                612                 :                :     {
 5997                           613                 :          14635 :         char       *typname = strVal(linitial(column->typeName->names));
                                614                 :                : 
 5292 rhaas@postgresql.org      615         [ +  + ]:          14635 :         if (strcmp(typname, "smallserial") == 0 ||
                                616         [ +  + ]:          14631 :             strcmp(typname, "serial2") == 0)
                                617                 :                :         {
                                618                 :              7 :             is_serial = true;
                                619                 :              7 :             column->typeName->names = NIL;
                                620                 :              7 :             column->typeName->typeOid = INT2OID;
                                621                 :                :         }
                                622         [ +  + ]:          14628 :         else if (strcmp(typname, "serial") == 0 ||
 4937 bruce@momjian.us          623         [ -  + ]:          14223 :                  strcmp(typname, "serial4") == 0)
                                624                 :                :         {
 6751 tgl@sss.pgh.pa.us         625                 :            405 :             is_serial = true;
 5997 peter_e@gmx.net           626                 :            405 :             column->typeName->names = NIL;
                                627                 :            405 :             column->typeName->typeOid = INT4OID;
                                628                 :                :         }
 6751 tgl@sss.pgh.pa.us         629         [ +  + ]:          14223 :         else if (strcmp(typname, "bigserial") == 0 ||
                                630         [ +  + ]:          14218 :                  strcmp(typname, "serial8") == 0)
                                631                 :                :         {
                                632                 :             11 :             is_serial = true;
 5997 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   [ +  +  -  + ]:          14635 :         if (is_serial && column->typeName->arrayBounds != NIL)
 6479 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 */
 5801 peter_e@gmx.net           651         [ +  + ]:CBC       34900 :     if (column->typeName)
 5439 tgl@sss.pgh.pa.us         652                 :          34721 :         transformColumnType(cxt, column);
                                653                 :                : 
                                654                 :                :     /* Special actions for SERIAL pseudo-types */
 6751                           655         [ +  + ]:          34881 :     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                 :                : 
 3176 peter_e@gmx.net           665                 :            423 :         generateSerialExtraStmts(cxt, column,
 2162 tgl@sss.pgh.pa.us         666                 :            423 :                                  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                 :                :          */
 6751                           680                 :            423 :         qstring = quote_qualified_identifier(snamespace, sname);
                                681                 :            423 :         snamenode = makeNode(A_Const);
 1559 peter@eisentraut.org      682                 :            423 :         snamenode->val.node.type = T_String;
 1432                           683                 :            423 :         snamenode->val.sval.sval = qstring;
 6319 tgl@sss.pgh.pa.us         684                 :            423 :         snamenode->location = -1;
 6440 alvherre@alvh.no-ip.      685                 :            423 :         castnode = makeNode(TypeCast);
 5997 peter_e@gmx.net           686                 :            423 :         castnode->typeName = SystemTypeName("regclass");
 6440 alvherre@alvh.no-ip.      687                 :            423 :         castnode->arg = (Node *) snamenode;
 6319 tgl@sss.pgh.pa.us         688                 :            423 :         castnode->location = -1;
 4551 rhaas@postgresql.org      689                 :            423 :         funccallnode = makeFuncCall(SystemFuncName("nextval"),
                                690                 :            423 :                                     list_make1(castnode),
                                691                 :                :                                     COERCE_EXPLICIT_CALL,
                                692                 :                :                                     -1);
 6751 tgl@sss.pgh.pa.us         693                 :            423 :         constraint = makeNode(Constraint);
                                694                 :            423 :         constraint->contype = CONSTR_DEFAULT;
 5983                           695                 :            423 :         constraint->location = -1;
 6751                           696                 :            423 :         constraint->raw_expr = (Node *) funccallnode;
                                697                 :            423 :         constraint->cooked_expr = NULL;
                                698                 :            423 :         column->constraints = lappend(column->constraints, constraint);
                                699                 :                : 
                                700                 :                :         /* have a not-null constraint added later */
  403 alvherre@alvh.no-ip.      701                 :            423 :         need_notnull = true;
                                702                 :            423 :         disallow_noinherit_notnull = true;
                                703                 :                :     }
                                704                 :                : 
                                705                 :                :     /* Process column constraints, if any... */
 5439 tgl@sss.pgh.pa.us         706                 :          34881 :     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                 :                :      */
  403 alvherre@alvh.no-ip.      720         [ +  + ]:          34869 :     if (!disallow_noinherit_notnull)
                                721                 :                :     {
                                722   [ +  +  +  +  :          77894 :         foreach_node(Constraint, constraint, column->constraints)
                                              +  + ]
                                723                 :                :         {
                                724         [ +  + ]:           9002 :             switch (constraint->contype)
                                725                 :                :             {
                                726                 :           2908 :                 case CONSTR_IDENTITY:
                                727                 :                :                 case CONSTR_PRIMARY:
                                728                 :           2908 :                     disallow_noinherit_notnull = true;
                                729                 :           2908 :                     break;
                                730                 :           6094 :                 default:
                                731                 :           6094 :                     break;
                                732                 :                :             }
                                733                 :                :         }
                                734                 :                :     }
                                735                 :                : 
                                736                 :                :     /* Now scan them again to do full processing */
 6751 tgl@sss.pgh.pa.us         737                 :          34869 :     saw_nullable = false;
                                738                 :          34869 :     saw_default = false;
 3176 peter_e@gmx.net           739                 :          34869 :     saw_identity = false;
 2453 peter@eisentraut.org      740                 :          34869 :     saw_generated = false;
                                741                 :                : 
  403 alvherre@alvh.no-ip.      742   [ +  +  +  +  :          79288 :     foreach_node(Constraint, constraint, column->constraints)
                                              +  + ]
                                743                 :                :     {
 6751 tgl@sss.pgh.pa.us         744   [ +  +  +  +  :           9712 :         switch (constraint->contype)
                                     +  +  +  +  -  
                                           +  +  - ]
                                745                 :                :         {
                                746                 :             11 :             case CONSTR_NULL:
  403 alvherre@alvh.no-ip.      747   [ -  +  -  -  :             11 :                 if ((saw_nullable && column->is_not_null) || need_notnull)
                                              +  + ]
 6751 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)));
 3044 peter_e@gmx.net           754                 :              8 :                 column->is_not_null = false;
 6751 tgl@sss.pgh.pa.us         755                 :              8 :                 saw_nullable = true;
                                756                 :              8 :                 break;
                                757                 :                : 
                                758                 :           3402 :             case CONSTR_NOTNULL:
  403 alvherre@alvh.no-ip.      759   [ +  +  +  + ]:           3402 :                 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 */
  979                           765   [ +  +  -  + ]:           3399 :                 if (saw_nullable && !column->is_not_null)
  979 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                 :                : 
  403 alvherre@alvh.no-ip.      773   [ +  +  +  + ]:CBC        3399 :                 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         [ +  + ]:           3384 :                 if (!column->is_not_null)
                                791                 :                :                 {
                                792                 :           3372 :                     column->is_not_null = true;
                                793                 :           3372 :                     saw_nullable = true;
                                794                 :           3372 :                     need_notnull = false;
                                795                 :                : 
                                796                 :           3372 :                     constraint->keys = list_make1(makeString(column->colname));
                                797                 :           3372 :                     notnull_constraint = constraint;
                                798                 :           3372 :                     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)
  403 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                 :                : 
  403 alvherre@alvh.no-ip.      814   [ +  +  +  + ]:CBC           9 :                     if (!notnull_constraint->conname && constraint->conname)
                                815                 :              3 :                         notnull_constraint->conname = constraint->conname;
                                816                 :                :                 }
                                817                 :                : 
 6751 tgl@sss.pgh.pa.us         818                 :           3381 :                 break;
                                819                 :                : 
                                820                 :           1267 :             case CONSTR_DEFAULT:
                                821         [ -  + ]:           1267 :                 if (saw_default)
 6751 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)));
 6751 tgl@sss.pgh.pa.us         828                 :CBC        1267 :                 column->raw_default = constraint->raw_expr;
                                829         [ -  + ]:           1267 :                 Assert(constraint->cooked_expr == NULL);
                                830                 :           1267 :                 saw_default = true;
                                831                 :           1267 :                 break;
                                832                 :                : 
 3176 peter_e@gmx.net           833                 :            184 :             case CONSTR_IDENTITY:
                                834                 :                :                 {
                                835                 :                :                     Type        ctype;
                                836                 :                :                     Oid         typeOid;
                                837                 :                : 
 2930                           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                 :                : 
 3135 bruce@momjian.us          847                 :            169 :                     ctype = typenameType(cxt->pstate, column->typeName, NULL);
 2583 andres@anarazel.de        848                 :            169 :                     typeOid = ((Form_pg_type) GETSTRUCT(ctype))->oid;
 3135 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                 :                :                      */
  403 alvherre@alvh.no-ip.      871         [ +  + ]:            166 :                     if (!saw_nullable)
                                872                 :            154 :                         need_notnull = true;
                                873         [ +  + ]:             12 :                     else if (!column->is_not_null)
 1740 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)));
 3135 bruce@momjian.us          880                 :            163 :                     break;
                                881                 :                :                 }
                                882                 :                : 
 2453 peter@eisentraut.org      883                 :            930 :             case CONSTR_GENERATED:
                                884         [ +  + ]:            930 :                 if (cxt->ofType)
                                885         [ +  - ]:              6 :                     ereport(ERROR,
                                886                 :                :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                887                 :                :                              errmsg("generated columns are not supported on typed tables")));
                                888         [ +  + ]:            924 :                 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)));
  312                           895                 :            918 :                 column->generated = constraint->generated_kind;
 2453                           896                 :            918 :                 column->raw_default = constraint->raw_expr;
                                897         [ -  + ]:            918 :                 Assert(constraint->cooked_expr == NULL);
                                898                 :            918 :                 saw_generated = true;
                                899                 :            918 :                 break;
                                900                 :                : 
 5853 tgl@sss.pgh.pa.us         901                 :            253 :             case CONSTR_CHECK:
 4017                           902                 :            253 :                 cxt->ckconstraints = lappend(cxt->ckconstraints, constraint);
                                903                 :            253 :                 break;
                                904                 :                : 
                                905                 :           2943 :             case CONSTR_PRIMARY:
  403 alvherre@alvh.no-ip.      906   [ +  +  -  + ]:           2943 :                 if (saw_nullable && !column->is_not_null)
  403 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)));
  403 alvherre@alvh.no-ip.      913                 :CBC        2943 :                 need_notnull = true;
                                914                 :                : 
 4662 tgl@sss.pgh.pa.us         915         [ +  + ]:           2943 :                 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                 :                :                 /* FALL THRU */
                                922                 :                : 
                                923                 :                :             case CONSTR_UNIQUE:
                                924         [ -  + ]:           3127 :                 if (cxt->isforeign)
 4662 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)));
 6751 tgl@sss.pgh.pa.us         930         [ +  - ]:CBC        3127 :                 if (constraint->keys == NIL)
                                931                 :           3127 :                     constraint->keys = list_make1(makeString(column->colname));
                                932                 :           3127 :                 cxt->ixconstraints = lappend(cxt->ixconstraints, constraint);
                                933                 :           3127 :                 break;
                                934                 :                : 
 5853 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                 :                : 
 5983 tgl@sss.pgh.pa.us         940                 :CBC         417 :             case CONSTR_FOREIGN:
 4662                           941         [ +  + ]:            417 :                 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                 :                :                  */
 5983                           952                 :            414 :                 constraint->fk_attrs = list_make1(makeString(column->colname));
                                953                 :            414 :                 cxt->fkconstraints = lappend(cxt->fkconstraints, constraint);
                                954                 :            414 :                 break;
                                955                 :                : 
 6751                           956                 :            118 :             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                 :            118 :                 break;
                                964                 :                : 
 6751 tgl@sss.pgh.pa.us         965                 :UBC           0 :             default:
                                966         [ #  # ]:              0 :                 elog(ERROR, "unrecognized constraint type: %d",
                                967                 :                :                      constraint->contype);
                                968                 :                :                 break;
                                969                 :                :         }
                                970                 :                : 
 3176 peter_e@gmx.net           971   [ +  +  +  + ]:CBC        9649 :         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                 :                : 
 2453 peter@eisentraut.org      979   [ +  +  +  + ]:           9643 :         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   [ +  +  +  + ]:           9637 :         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                 :                :      */
  403 alvherre@alvh.no-ip.     1000   [ +  +  +  +  :          34788 :     if (need_notnull && !(saw_nullable && column->is_not_null))
                                              -  + ]
                               1001                 :                :     {
                               1002                 :           2663 :         column->is_not_null = true;
                               1003                 :           2663 :         notnull_constraint = makeNotNullConstraint(makeString(column->colname));
                               1004                 :           2663 :         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                 :                :      */
 5247 rhaas@postgresql.org     1011         [ +  + ]:          34788 :     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;
 1984 michael@paquier.xyz      1026                 :             80 :         stmt->objtype = OBJECT_FOREIGN_TABLE;
 5247 rhaas@postgresql.org     1027                 :             80 :         stmt->cmds = lappend(stmt->cmds, cmd);
                               1028                 :                : 
                               1029                 :             80 :         cxt->alist = lappend(cxt->alist, stmt);
                               1030                 :                :     }
 6751 tgl@sss.pgh.pa.us        1031                 :          34788 : }
                               1032                 :                : 
                               1033                 :                : /*
                               1034                 :                :  * transformTableConstraint
                               1035                 :                :  *      transform a Constraint node within CREATE TABLE or ALTER TABLE
                               1036                 :                :  */
                               1037                 :                : static void
 5439                          1038                 :           9887 : transformTableConstraint(CreateStmtContext *cxt, Constraint *constraint)
                               1039                 :                : {
 6751                          1040   [ +  +  +  +  :           9887 :     switch (constraint->contype)
                                        +  +  -  - ]
                               1041                 :                :     {
                               1042                 :           4138 :         case CONSTR_PRIMARY:
 4017                          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                 :                : 
 6751                          1052                 :           2667 :         case CONSTR_UNIQUE:
 4017                          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                 :                : 
 5853                          1062                 :            117 :         case CONSTR_EXCLUSION:
 4017                          1063         [ -  + ]:            117 :             if (cxt->isforeign)
 4017 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)));
 6751 tgl@sss.pgh.pa.us        1069                 :CBC         117 :             cxt->ixconstraints = lappend(cxt->ixconstraints, constraint);
                               1070                 :            117 :             break;
                               1071                 :                : 
                               1072                 :            736 :         case CONSTR_CHECK:
                               1073                 :            736 :             cxt->ckconstraints = lappend(cxt->ckconstraints, constraint);
                               1074                 :            736 :             break;
                               1075                 :                : 
  403 alvherre@alvh.no-ip.     1076                 :            551 :         case CONSTR_NOTNULL:
                               1077   [ +  +  +  + ]:            551 :             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                 :            548 :             cxt->nnconstraints = lappend(cxt->nnconstraints, constraint);
                               1083                 :            548 :             break;
                               1084                 :                : 
 5983 tgl@sss.pgh.pa.us        1085                 :           1678 :         case CONSTR_FOREIGN:
 4017                          1086         [ -  + ]:           1678 :             if (cxt->isforeign)
 4017 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)));
 5983 tgl@sss.pgh.pa.us        1092                 :CBC        1678 :             cxt->fkconstraints = lappend(cxt->fkconstraints, constraint);
                               1093                 :           1678 :             break;
                               1094                 :                : 
 6751 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                 :                :     }
 6751 tgl@sss.pgh.pa.us        1112                 :CBC        9878 : }
                               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
 5092 peter_e@gmx.net          1129                 :            393 : 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                 :                : 
 4662 tgl@sss.pgh.pa.us        1138                 :            393 :     setup_parser_errposition_callback(&pcbstate, cxt->pstate,
                               1139                 :            393 :                                       table_like_clause->relation->location);
                               1140                 :                : 
                               1141                 :                :     /* Open the relation referenced by the LIKE clause */
 5036 peter_e@gmx.net          1142                 :            393 :     relation = relation_openrv(table_like_clause->relation, AccessShareLock);
                               1143                 :                : 
 4917 tgl@sss.pgh.pa.us        1144         [ +  + ]:            390 :     if (relation->rd_rel->relkind != RELKIND_RELATION &&
                               1145         [ +  + ]:            199 :         relation->rd_rel->relkind != RELKIND_VIEW &&
 4671 kgrittn@postgresql.o     1146         [ +  - ]:            193 :         relation->rd_rel->relkind != RELKIND_MATVIEW &&
 4917 tgl@sss.pgh.pa.us        1147         [ +  + ]:            193 :         relation->rd_rel->relkind != RELKIND_COMPOSITE_TYPE &&
 3296 rhaas@postgresql.org     1148         [ +  - ]:            190 :         relation->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
                               1149         [ +  + ]:            190 :         relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
 6751 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                 :                : 
 5036 peter_e@gmx.net          1156                 :            387 :     cancel_parser_errposition_callback(&pcbstate);
                               1157                 :                : 
                               1158                 :                :     /*
                               1159                 :                :      * Check for privileges
                               1160                 :                :      */
                               1161         [ +  + ]:            387 :     if (relation->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
                               1162                 :                :     {
 1129 peter@eisentraut.org     1163                 :              3 :         aclresult = object_aclcheck(TypeRelationId, relation->rd_rel->reltype, GetUserId(),
                               1164                 :                :                                     ACL_USAGE);
 5036 peter_e@gmx.net          1165         [ -  + ]:              3 :         if (aclresult != ACLCHECK_OK)
 2936 peter_e@gmx.net          1166                 :UBC           0 :             aclcheck_error(aclresult, OBJECT_TYPE,
 5036                          1167                 :              0 :                            RelationGetRelationName(relation));
                               1168                 :                :     }
                               1169                 :                :     else
                               1170                 :                :     {
 5036 peter_e@gmx.net          1171                 :CBC         384 :         aclresult = pg_class_aclcheck(RelationGetRelid(relation), GetUserId(),
                               1172                 :                :                                       ACL_SELECT);
                               1173         [ -  + ]:            384 :         if (aclresult != ACLCHECK_OK)
 2936 peter_e@gmx.net          1174                 :UBC           0 :             aclcheck_error(aclresult, get_relkind_objtype(relation->rd_rel->relkind),
 5036                          1175                 :              0 :                            RelationGetRelationName(relation));
                               1176                 :                :     }
                               1177                 :                : 
 6751 tgl@sss.pgh.pa.us        1178                 :CBC         387 :     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         [ +  + ]:           1258 :     for (parent_attno = 1; parent_attno <= tupleDesc->natts;
                               1186                 :            871 :          parent_attno++)
                               1187                 :                :     {
 3040 andres@anarazel.de       1188                 :            871 :         Form_pg_attribute attribute = TupleDescAttr(tupleDesc,
                               1189                 :                :                                                     parent_attno - 1);
                               1190                 :                :         ColumnDef  *def;
                               1191                 :                : 
                               1192                 :                :         /*
                               1193                 :                :          * Ignore dropped columns in the parent.
                               1194                 :                :          */
 6751 tgl@sss.pgh.pa.us        1195         [ +  + ]:            871 :         if (attribute->attisdropped)
                               1196                 :             12 :             continue;
                               1197                 :                : 
                               1198                 :                :         /*
                               1199                 :                :          * Create a new column definition
                               1200                 :                :          */
  840 peter@eisentraut.org     1201                 :            859 :         def = makeColumnDef(NameStr(attribute->attname), attribute->atttypid,
                               1202                 :                :                             attribute->atttypmod, attribute->attcollation);
                               1203                 :                : 
                               1204                 :                :         /*
                               1205                 :                :          * Add to column list
                               1206                 :                :          */
 6751 tgl@sss.pgh.pa.us        1207                 :            859 :         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                 :                :          */
 1943                          1213   [ +  +  +  + ]:            859 :         if (attribute->atthasdef && attribute->attgenerated &&
                               1214         [ +  + ]:             39 :             (table_like_clause->options & CREATE_TABLE_LIKE_GENERATED))
 2274                          1215                 :             24 :             def->generated = attribute->attgenerated;
                               1216                 :                : 
                               1217                 :                :         /*
                               1218                 :                :          * Copy identity if requested
                               1219                 :                :          */
 3176 peter_e@gmx.net          1220         [ +  + ]:            859 :         if (attribute->attidentity &&
  300 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                 :                :              */
  588 peter@eisentraut.org     1231                 :              6 :             seq_relid = getIdentitySequence(relation, attribute->attnum, false);
 3176 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 */
  300 michael@paquier.xyz      1241         [ +  + ]:            859 :         if ((table_like_clause->options & CREATE_TABLE_LIKE_STORAGE) &&
                               1242         [ +  + ]:             99 :             !cxt->isforeign)
  665 peter@eisentraut.org     1243                 :             84 :             def->storage = attribute->attstorage;
                               1244                 :                :         else
                               1245                 :            775 :             def->storage = 0;
                               1246                 :                : 
                               1247                 :                :         /* Likewise, copy compression if requested */
  300 michael@paquier.xyz      1248         [ +  + ]:            859 :         if ((table_like_clause->options & CREATE_TABLE_LIKE_COMPRESSION) != 0 &&
                               1249         [ +  + ]:             75 :             CompressionMethodIsValid(attribute->attcompression) &&
                               1250         [ +  + ]:              6 :             !cxt->isforeign)
  665 peter@eisentraut.org     1251                 :              3 :             def->compression =
                               1252                 :              3 :                 pstrdup(GetCompressionMethodName(attribute->attcompression));
                               1253                 :                :         else
 1733 rhaas@postgresql.org     1254                 :            856 :             def->compression = NULL;
                               1255                 :                : 
                               1256                 :                :         /* Likewise, copy comment if requested */
 5092 peter_e@gmx.net          1257   [ +  +  +  + ]:            961 :         if ((table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS) &&
 5877 tgl@sss.pgh.pa.us        1258                 :            102 :             (comment = GetComment(attribute->attrelid,
                               1259                 :                :                                   RelationRelationId,
                               1260                 :            102 :                                   attribute->attnum)) != NULL)
                               1261                 :                :         {
 5909 andrew@dunslane.net      1262                 :             42 :             CommentStmt *stmt = makeNode(CommentStmt);
                               1263                 :                : 
                               1264                 :             42 :             stmt->objtype = OBJECT_COLUMN;
 3321 peter_e@gmx.net          1265                 :             42 :             stmt->object = (Node *) list_make3(makeString(cxt->relation->schemaname),
                               1266                 :                :                                                makeString(cxt->relation->relname),
                               1267                 :                :                                                makeString(def->colname));
 5909 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                 :                :      */
  403 alvherre@alvh.no-ip.     1278   [ +  +  +  + ]:            387 :     if (tupleDesc->constr && tupleDesc->constr->has_not_null)
                               1279                 :                :     {
                               1280                 :                :         List       *lst;
                               1281                 :                : 
                               1282                 :            169 :         lst = RelationGetNotNullConstraints(RelationGetRelid(relation), false,
                               1283                 :                :                                             true);
                               1284                 :            169 :         cxt->nnconstraints = list_concat(cxt->nnconstraints, lst);
                               1285                 :                : 
                               1286                 :                :         /* Copy comments on not-null constraints */
  173 fujii@postgresql.org     1287         [ +  + ]:            169 :         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                 :                :      */
  582 alvherre@alvh.no-ip.     1319         [ +  + ]:            387 :     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                 :                :     {
 1841 tgl@sss.pgh.pa.us        1326                 :             97 :         table_like_clause->relationOid = RelationGetRelid(relation);
 1853                          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                 :                :      */
 1943                          1335                 :            387 :     table_close(relation, NoLock);
                               1336                 :            387 : }
                               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                 :                :      */
 1841                          1366         [ -  + ]:             97 :     if (!OidIsValid(table_like_clause->relationOid))
 1841 tgl@sss.pgh.pa.us        1367         [ #  # ]:UBC           0 :         elog(ERROR, "expandTableLikeClause called on untransformed LIKE clause");
                               1368                 :                : 
 1841 tgl@sss.pgh.pa.us        1369                 :CBC          97 :     relation = relation_open(table_like_clause->relationOid, NoLock);
                               1370                 :                : 
 1943                          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                 :                : 
  811 peter@eisentraut.org     1421                 :             40 :                 this_default = TupleDescGetDefault(tupleDesc, parent_attno);
 1715 tgl@sss.pgh.pa.us        1422         [ -  + ]:             40 :                 if (this_default == NULL)
 1715 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                 :                : 
 1943 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)
 1943 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                 :                : 
 1943 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                 :                :      */
 5092 peter_e@gmx.net          1457   [ +  +  +  + ]:             97 :     if ((table_like_clause->options & CREATE_TABLE_LIKE_CONSTRAINTS) &&
                               1458                 :                :         constr != NULL)
                               1459                 :                :     {
                               1460                 :                :         int         ccnum;
                               1461                 :                : 
 2107 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;
  339 peter@eisentraut.org     1466                 :             75 :             bool        ccenforced = constr->check[ccnum].ccenforced;
 2107 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                 :                : 
 4917                          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)
 4917 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                 :                : 
 1943 tgl@sss.pgh.pa.us        1492                 :CBC          75 :             n = makeNode(Constraint);
 6751                          1493                 :             75 :             n->contype = CONSTR_CHECK;
 5983                          1494                 :             75 :             n->conname = pstrdup(ccname);
 2107                          1495                 :             75 :             n->location = -1;
  339 peter@eisentraut.org     1496                 :             75 :             n->is_enforced = ccenforced;
   97                          1497                 :             75 :             n->initially_valid = ccenforced; /* sic */
 2107 tgl@sss.pgh.pa.us        1498                 :             75 :             n->is_no_inherit = ccnoinherit;
 6751                          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. */
 1943                          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 */
 5092 peter_e@gmx.net          1511   [ +  +  +  + ]:            132 :             if ((table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS) &&
 5005                          1512                 :             57 :                 (comment = GetComment(get_relation_constraint_oid(RelationGetRelid(relation),
 3100 tgl@sss.pgh.pa.us        1513                 :             57 :                                                                   n->conname, false),
                               1514                 :                :                                       ConstraintRelationId,
                               1515                 :                :                                       0)) != NULL)
                               1516                 :                :             {
 5909 andrew@dunslane.net      1517                 :             15 :                 CommentStmt *stmt = makeNode(CommentStmt);
                               1518                 :                : 
 4011 alvherre@alvh.no-ip.     1519                 :             15 :                 stmt->objtype = OBJECT_TABCONSTRAINT;
 1943 tgl@sss.pgh.pa.us        1520                 :             15 :                 stmt->object = (Node *) list_make3(makeString(heapRel->schemaname),
                               1521                 :                :                                                    makeString(heapRel->relname),
                               1522                 :                :                                                    makeString(n->conname));
 5909 andrew@dunslane.net      1523                 :             15 :                 stmt->comment = comment;
                               1524                 :                : 
 1943 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                 :                :      */
 5092 peter_e@gmx.net          1549         [ +  + ]:             97 :     if ((table_like_clause->options & CREATE_TABLE_LIKE_INDEXES) &&
  300 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                 :                : 
 6727 neilc@samurai.com        1556                 :             40 :         parent_indexes = RelationGetIndexList(relation);
                               1557                 :                : 
                               1558   [ +  -  +  +  :            105 :         foreach(l, parent_indexes)
                                              +  + ]
                               1559                 :                :         {
 6606 bruce@momjian.us         1560                 :             65 :             Oid         parent_index_oid = lfirst_oid(l);
                               1561                 :                :             Relation    parent_index;
                               1562                 :                :             IndexStmt  *index_stmt;
                               1563                 :                : 
 6727 neilc@samurai.com        1564                 :             65 :             parent_index = index_open(parent_index_oid, AccessShareLock);
                               1565                 :                : 
                               1566                 :                :             /* Build CREATE INDEX statement to recreate the parent_index */
 1943 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 */
 5092 peter_e@gmx.net          1573         [ +  + ]:             65 :             if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
                               1574                 :                :             {
 5909 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                 :                :                  */
 4901 tgl@sss.pgh.pa.us        1581                 :             36 :                 index_stmt->idxcomment = comment;
                               1582                 :                :             }
                               1583                 :                : 
 1943                          1584                 :             65 :             result = lappend(result, index_stmt);
                               1585                 :                : 
 6590                          1586                 :             65 :             index_close(parent_index, AccessShareLock);
                               1587                 :                :         }
                               1588                 :                :     }
                               1589                 :                : 
                               1590                 :                :     /*
                               1591                 :                :      * Process extended statistics if required.
                               1592                 :                :      */
  573                          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 */
 1943                          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                 :                :      */
 2521 andres@anarazel.de       1636                 :             97 :     table_close(relation, NoLock);
                               1637                 :                : 
 1943 tgl@sss.pgh.pa.us        1638                 :             97 :     return result;
                               1639                 :                : }
                               1640                 :                : 
                               1641                 :                : static void
 5439                          1642                 :             61 : transformOfType(CreateStmtContext *cxt, TypeName *ofTypename)
                               1643                 :                : {
                               1644                 :                :     HeapTuple   tuple;
                               1645                 :                :     TupleDesc   tupdesc;
                               1646                 :                :     int         i;
                               1647                 :                :     Oid         ofTypeId;
                               1648                 :                : 
 1145 peter@eisentraut.org     1649         [ -  + ]:             61 :     Assert(ofTypename);
                               1650                 :                : 
  364 michael@paquier.xyz      1651                 :             61 :     tuple = typenameType(cxt->pstate, ofTypename, NULL);
 5354 rhaas@postgresql.org     1652                 :             58 :     check_of_type(tuple);
 2583 andres@anarazel.de       1653                 :             52 :     ofTypeId = ((Form_pg_type) GETSTRUCT(tuple))->oid;
 3100 tgl@sss.pgh.pa.us        1654                 :             52 :     ofTypename->typeOid = ofTypeId; /* cached for later */
                               1655                 :                : 
 5801 peter_e@gmx.net          1656                 :             52 :     tupdesc = lookup_rowtype_tupdesc(ofTypeId, -1);
                               1657         [ +  + ]:            156 :     for (i = 0; i < tupdesc->natts; i++)
                               1658                 :                :     {
 3040 andres@anarazel.de       1659                 :            104 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
                               1660                 :                :         ColumnDef  *n;
                               1661                 :                : 
 5560 peter_e@gmx.net          1662         [ -  + ]:            104 :         if (attr->attisdropped)
 5560 peter_e@gmx.net          1663                 :UBC           0 :             continue;
                               1664                 :                : 
  840 peter@eisentraut.org     1665                 :CBC         104 :         n = makeColumnDef(NameStr(attr->attname), attr->atttypid,
                               1666                 :                :                           attr->atttypmod, attr->attcollation);
 5801 peter_e@gmx.net          1667                 :            104 :         n->is_from_type = true;
                               1668                 :                : 
                               1669                 :            104 :         cxt->columns = lappend(cxt->columns, n);
                               1670                 :                :     }
 1462 tgl@sss.pgh.pa.us        1671         [ +  - ]:             52 :     ReleaseTupleDesc(tupdesc);
                               1672                 :                : 
 5801 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 *
 2429 tgl@sss.pgh.pa.us        1695                 :           1632 : generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx,
                               1696                 :                :                         const AttrMap *attmap,
                               1697                 :                :                         Oid *constraintOid)
                               1698                 :                : {
 6590                          1699                 :           1632 :     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                 :                : 
 2429                          1717         [ +  + ]:           1632 :     if (constraintOid)
                               1718                 :           1059 :         *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                 :                :      */
 5784 rhaas@postgresql.org     1724                 :           1632 :     ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(source_relid));
 6590 tgl@sss.pgh.pa.us        1725         [ -  + ]:           1632 :     if (!HeapTupleIsValid(ht_idxrel))
 6590 tgl@sss.pgh.pa.us        1726         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for relation %u", source_relid);
 6590 tgl@sss.pgh.pa.us        1727                 :CBC        1632 :     idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
                               1728                 :                : 
                               1729                 :                :     /* Fetch pg_index tuple for source index from relcache entry */
                               1730                 :           1632 :     ht_idx = source_idx->rd_indextuple;
 6727 neilc@samurai.com        1731                 :           1632 :     idxrec = (Form_pg_index) GETSTRUCT(ht_idx);
                               1732                 :           1632 :     indrelid = idxrec->indrelid;
                               1733                 :                : 
                               1734                 :                :     /* Fetch the pg_am tuple of the index' access method */
 3621 tgl@sss.pgh.pa.us        1735                 :           1632 :     ht_am = SearchSysCache1(AMOID, ObjectIdGetDatum(idxrelrec->relam));
                               1736         [ -  + ]:           1632 :     if (!HeapTupleIsValid(ht_am))
 3621 tgl@sss.pgh.pa.us        1737         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for access method %u",
                               1738                 :                :              idxrelrec->relam);
 3621 tgl@sss.pgh.pa.us        1739                 :CBC        1632 :     amrec = (Form_pg_am) GETSTRUCT(ht_am);
                               1740                 :                : 
                               1741                 :                :     /* Extract indcollation from the pg_index tuple */
  997 dgustafsson@postgres     1742                 :           1632 :     datum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx,
                               1743                 :                :                                    Anum_pg_index_indcollation);
 5379 tgl@sss.pgh.pa.us        1744                 :           1632 :     indcollation = (oidvector *) DatumGetPointer(datum);
                               1745                 :                : 
                               1746                 :                :     /* Extract indclass from the pg_index tuple */
  997 dgustafsson@postgres     1747                 :           1632 :     datum = SysCacheGetAttrNotNull(INDEXRELID, ht_idx, Anum_pg_index_indclass);
 6590 tgl@sss.pgh.pa.us        1748                 :           1632 :     indclass = (oidvector *) DatumGetPointer(datum);
                               1749                 :                : 
                               1750                 :                :     /* Begin building the IndexStmt */
 6727 neilc@samurai.com        1751                 :           1632 :     index = makeNode(IndexStmt);
 2888 alvherre@alvh.no-ip.     1752                 :           1632 :     index->relation = heapRel;
 6590 tgl@sss.pgh.pa.us        1753                 :           1632 :     index->accessMethod = pstrdup(NameStr(amrec->amname));
 6522                          1754         [ +  + ]:           1632 :     if (OidIsValid(idxrelrec->reltablespace))
                               1755                 :             44 :         index->tableSpace = get_tablespace_name(idxrelrec->reltablespace);
                               1756                 :                :     else
                               1757                 :           1588 :         index->tableSpace = NULL;
 4901                          1758                 :           1632 :     index->excludeOpNames = NIL;
                               1759                 :           1632 :     index->idxcomment = NULL;
 5439                          1760                 :           1632 :     index->indexOid = InvalidOid;
 1259 rhaas@postgresql.org     1761                 :           1632 :     index->oldNumber = InvalidRelFileNumber;
 2082 noah@leadboat.com        1762                 :           1632 :     index->oldCreateSubid = InvalidSubTransactionId;
 1259 rhaas@postgresql.org     1763                 :           1632 :     index->oldFirstRelfilelocatorSubid = InvalidSubTransactionId;
 6727 neilc@samurai.com        1764                 :           1632 :     index->unique = idxrec->indisunique;
 1412 peter@eisentraut.org     1765                 :           1632 :     index->nulls_not_distinct = idxrec->indnullsnotdistinct;
 6727 neilc@samurai.com        1766                 :           1632 :     index->primary = idxrec->indisprimary;
  455 peter@eisentraut.org     1767   [ +  +  +  +  :           1632 :     index->iswithoutoverlaps = (idxrec->indisprimary || idxrec->indisunique) && idxrec->indisexclusion;
                                              +  + ]
 3950 tgl@sss.pgh.pa.us        1768                 :           1632 :     index->transformed = true;   /* don't need transformIndexStmt */
 6590                          1769                 :           1632 :     index->concurrent = false;
 3950                          1770                 :           1632 :     index->if_not_exists = false;
 2427 alvherre@alvh.no-ip.     1771                 :           1632 :     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                 :                :      */
 6727 neilc@samurai.com        1779                 :           1632 :     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                 :                :      */
 5439 tgl@sss.pgh.pa.us        1787   [ +  +  +  +  :           1632 :     if (index->primary || index->unique || idxrec->indisexclusion)
                                              +  + ]
 5984                          1788                 :            876 :     {
 5772 bruce@momjian.us         1789                 :            876 :         Oid         constraintId = get_index_constraint(source_relid);
                               1790                 :                : 
 5984 tgl@sss.pgh.pa.us        1791         [ +  + ]:            876 :         if (OidIsValid(constraintId))
                               1792                 :                :         {
                               1793                 :                :             HeapTuple   ht_constr;
                               1794                 :                :             Form_pg_constraint conrec;
                               1795                 :                : 
 2857 alvherre@alvh.no-ip.     1796         [ +  + ]:            851 :             if (constraintOid)
                               1797                 :            757 :                 *constraintOid = constraintId;
                               1798                 :                : 
 5784 rhaas@postgresql.org     1799                 :            851 :             ht_constr = SearchSysCache1(CONSTROID,
                               1800                 :                :                                         ObjectIdGetDatum(constraintId));
 5984 tgl@sss.pgh.pa.us        1801         [ -  + ]:            851 :             if (!HeapTupleIsValid(ht_constr))
 5984 tgl@sss.pgh.pa.us        1802         [ #  # ]:UBC           0 :                 elog(ERROR, "cache lookup failed for constraint %u",
                               1803                 :                :                      constraintId);
 5984 tgl@sss.pgh.pa.us        1804                 :CBC         851 :             conrec = (Form_pg_constraint) GETSTRUCT(ht_constr);
                               1805                 :                : 
                               1806                 :            851 :             index->isconstraint = true;
                               1807                 :            851 :             index->deferrable = conrec->condeferrable;
                               1808                 :            851 :             index->initdeferred = conrec->condeferred;
                               1809                 :                : 
                               1810                 :                :             /* If it's an exclusion constraint, we need the operator names */
 5439                          1811         [ +  + ]:            851 :             if (idxrec->indisexclusion)
                               1812                 :                :             {
                               1813                 :                :                 Datum      *elems;
                               1814                 :                :                 int         nElems;
                               1815                 :                :                 int         i;
                               1816                 :                : 
  455 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 */
  997 dgustafsson@postgres     1821                 :             42 :                 datum = SysCacheGetAttrNotNull(CONSTROID, ht_constr,
                               1822                 :                :                                                Anum_pg_constraint_conexclop);
 1264 peter@eisentraut.org     1823                 :             42 :                 deconstruct_array_builtin(DatumGetArrayTypeP(datum), OIDOID, &elems, NULL, &nElems);
                               1824                 :                : 
 5853 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                 :                : 
 5784 rhaas@postgresql.org     1834                 :             83 :                     opertup = SearchSysCache1(OPEROID,
                               1835                 :                :                                               ObjectIdGetDatum(operid));
 5853 tgl@sss.pgh.pa.us        1836         [ -  + ]:             83 :                     if (!HeapTupleIsValid(opertup))
 5853 tgl@sss.pgh.pa.us        1837         [ #  # ]:UBC           0 :                         elog(ERROR, "cache lookup failed for operator %u",
                               1838                 :                :                              operid);
 5853 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                 :                : 
 5984                          1851                 :            851 :             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 */
 6590                          1860                 :           1632 :     datum = SysCacheGetAttr(INDEXRELID, ht_idx,
                               1861                 :                :                             Anum_pg_index_indexprs, &isnull);
                               1862         [ +  + ]:           1632 :     if (!isnull)
                               1863                 :                :     {
                               1864                 :                :         char       *exprsString;
                               1865                 :                : 
 6475                          1866                 :             92 :         exprsString = TextDatumGetCString(datum);
 6727 neilc@samurai.com        1867                 :             92 :         indexprs = (List *) stringToNode(exprsString);
                               1868                 :                :     }
                               1869                 :                :     else
 6590 tgl@sss.pgh.pa.us        1870                 :           1540 :         indexprs = NIL;
                               1871                 :                : 
                               1872                 :                :     /* Build the list of IndexElem */
                               1873                 :           1632 :     index->indexParams = NIL;
 2810 teodor@sigaev.ru         1874                 :           1632 :     index->indexIncludingParams = NIL;
                               1875                 :                : 
 6590 tgl@sss.pgh.pa.us        1876                 :           1632 :     indexpr_item = list_head(indexprs);
 2810 teodor@sigaev.ru         1877         [ +  + ]:           3535 :     for (keyno = 0; keyno < idxrec->indnkeyatts; keyno++)
                               1878                 :                :     {
                               1879                 :                :         IndexElem  *iparam;
 6727 neilc@samurai.com        1880                 :           1903 :         AttrNumber  attnum = idxrec->indkey.values[keyno];
 3040 andres@anarazel.de       1881                 :           1903 :         Form_pg_attribute attr = TupleDescAttr(RelationGetDescr(source_idx),
                               1882                 :                :                                                keyno);
 6590 tgl@sss.pgh.pa.us        1883                 :           1903 :         int16       opt = source_idx->rd_indoption[keyno];
                               1884                 :                : 
 6727 neilc@samurai.com        1885                 :           1903 :         iparam = makeNode(IndexElem);
                               1886                 :                : 
                               1887         [ +  + ]:           1903 :         if (AttributeNumberIsValid(attnum))
                               1888                 :                :         {
                               1889                 :                :             /* Simple index column */
                               1890                 :                :             char       *attname;
                               1891                 :                : 
 2864 alvherre@alvh.no-ip.     1892                 :           1811 :             attname = get_attname(indrelid, attnum, false);
 6727 neilc@samurai.com        1893                 :           1811 :             keycoltype = get_atttype(indrelid, attnum);
                               1894                 :                : 
                               1895                 :           1811 :             iparam->name = attname;
                               1896                 :           1811 :             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)
 6727 neilc@samurai.com        1905         [ #  # ]:UBC           0 :                 elog(ERROR, "too few entries in indexprs list");
 6727 neilc@samurai.com        1906                 :CBC          92 :             indexkey = (Node *) lfirst(indexpr_item);
 2346 tgl@sss.pgh.pa.us        1907                 :             92 :             indexpr_item = lnext(indexprs, indexpr_item);
                               1908                 :                : 
                               1909                 :                :             /* Adjust Vars to match new table's column numbering */
 4917                          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)
 4917 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                 :                : 
 6727 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 */
 3040 andres@anarazel.de       1930                 :           1903 :         iparam->indexcolname = pstrdup(NameStr(attr->attname));
                               1931                 :                : 
                               1932                 :                :         /* Add the collation name, if non-default */
 5379 tgl@sss.pgh.pa.us        1933                 :           1903 :         iparam->collation = get_collation(indcollation->values[keyno], keycoltype);
                               1934                 :                : 
                               1935                 :                :         /* Add the operator class name, if non-default */
 6727 neilc@samurai.com        1936                 :           1903 :         iparam->opclass = get_opclass(indclass->values[keyno], keycoltype);
 2087 akorotkov@postgresql     1937                 :           1903 :         iparam->opclassopts =
                               1938                 :           1903 :             untransformRelOptions(get_attoptions(source_relid, keyno + 1));
                               1939                 :                : 
 6727 neilc@samurai.com        1940                 :           1903 :         iparam->ordering = SORTBY_DEFAULT;
                               1941                 :           1903 :         iparam->nulls_ordering = SORTBY_NULLS_DEFAULT;
                               1942                 :                : 
                               1943                 :                :         /* Adjust options if necessary */
 2521 andres@anarazel.de       1944         [ +  + ]:           1903 :         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                 :                :              */
 6727 neilc@samurai.com        1952         [ -  + ]:           1806 :             if (opt & INDOPTION_DESC)
                               1953                 :                :             {
 6727 neilc@samurai.com        1954                 :UBC           0 :                 iparam->ordering = SORTBY_DESC;
 6590 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                 :                :             {
 6590 tgl@sss.pgh.pa.us        1960         [ -  + ]:CBC        1806 :                 if (opt & INDOPTION_NULLS_FIRST)
 6590 tgl@sss.pgh.pa.us        1961                 :UBC           0 :                     iparam->nulls_ordering = SORTBY_NULLS_FIRST;
                               1962                 :                :             }
                               1963                 :                :         }
                               1964                 :                : 
 6727 neilc@samurai.com        1965                 :CBC        1903 :         index->indexParams = lappend(index->indexParams, iparam);
                               1966                 :                :     }
                               1967                 :                : 
                               1968                 :                :     /* Handle included columns separately */
 2810 teodor@sigaev.ru         1969         [ +  + ]:           1641 :     for (keyno = idxrec->indnkeyatts; keyno < idxrec->indnatts; keyno++)
                               1970                 :                :     {
                               1971                 :                :         IndexElem  *iparam;
                               1972                 :              9 :         AttrNumber  attnum = idxrec->indkey.values[keyno];
                               1973                 :              9 :         Form_pg_attribute attr = TupleDescAttr(RelationGetDescr(source_idx),
                               1974                 :                :                                                keyno);
                               1975                 :                : 
                               1976                 :              9 :         iparam = makeNode(IndexElem);
                               1977                 :                : 
                               1978         [ +  - ]:              9 :         if (AttributeNumberIsValid(attnum))
                               1979                 :                :         {
                               1980                 :                :             /* Simple index column */
                               1981                 :                :             char       *attname;
                               1982                 :                : 
                               1983                 :              9 :             attname = get_attname(indrelid, attnum, false);
                               1984                 :                : 
                               1985                 :              9 :             iparam->name = attname;
                               1986                 :              9 :             iparam->expr = NULL;
                               1987                 :                :         }
                               1988                 :                :         else
 2810 teodor@sigaev.ru         1989         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1990                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1991                 :                :                      errmsg("expressions are not supported in included columns")));
                               1992                 :                : 
                               1993                 :                :         /* Copy the original index column name */
 2810 teodor@sigaev.ru         1994                 :CBC           9 :         iparam->indexcolname = pstrdup(NameStr(attr->attname));
                               1995                 :                : 
                               1996                 :              9 :         index->indexIncludingParams = lappend(index->indexIncludingParams, iparam);
                               1997                 :                :     }
                               1998                 :                :     /* Copy reloptions if any */
 6590 tgl@sss.pgh.pa.us        1999                 :           1632 :     datum = SysCacheGetAttr(RELOID, ht_idxrel,
                               2000                 :                :                             Anum_pg_class_reloptions, &isnull);
                               2001         [ -  + ]:           1632 :     if (!isnull)
 6590 tgl@sss.pgh.pa.us        2002                 :UBC           0 :         index->options = untransformRelOptions(datum);
                               2003                 :                : 
                               2004                 :                :     /* If it's a partial index, decompile and append the predicate */
 6590 tgl@sss.pgh.pa.us        2005                 :CBC        1632 :     datum = SysCacheGetAttr(INDEXRELID, ht_idx,
                               2006                 :                :                             Anum_pg_index_indpred, &isnull);
                               2007         [ +  + ]:           1632 :     if (!isnull)
                               2008                 :                :     {
                               2009                 :                :         char       *pred_str;
                               2010                 :                :         Node       *pred_tree;
                               2011                 :                :         bool        found_whole_row;
                               2012                 :                : 
                               2013                 :                :         /* Convert text string to node tree */
 6475                          2014                 :             15 :         pred_str = TextDatumGetCString(datum);
 4917                          2015                 :             15 :         pred_tree = (Node *) stringToNode(pred_str);
                               2016                 :                : 
                               2017                 :                :         /* Adjust Vars to match new table's column numbering */
                               2018                 :             15 :         pred_tree = map_variable_attnos(pred_tree,
                               2019                 :                :                                         1, 0,
                               2020                 :                :                                         attmap,
                               2021                 :                :                                         InvalidOid, &found_whole_row);
                               2022                 :                : 
                               2023                 :                :         /* As in expandTableLikeClause, reject whole-row variables */
                               2024         [ -  + ]:             15 :         if (found_whole_row)
 4917 tgl@sss.pgh.pa.us        2025         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2026                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               2027                 :                :                      errmsg("cannot convert whole-row table reference"),
                               2028                 :                :                      errdetail("Index \"%s\" contains a whole-row table reference.",
                               2029                 :                :                                RelationGetRelationName(source_idx))));
                               2030                 :                : 
 4917 tgl@sss.pgh.pa.us        2031                 :CBC          15 :         index->whereClause = pred_tree;
                               2032                 :                :     }
                               2033                 :                : 
                               2034                 :                :     /* Clean up */
 6727 neilc@samurai.com        2035                 :           1632 :     ReleaseSysCache(ht_idxrel);
 3621 tgl@sss.pgh.pa.us        2036                 :           1632 :     ReleaseSysCache(ht_am);
                               2037                 :                : 
 6727 neilc@samurai.com        2038                 :           1632 :     return index;
                               2039                 :                : }
                               2040                 :                : 
                               2041                 :                : /*
                               2042                 :                :  * Generate a CreateStatsStmt node using information from an already existing
                               2043                 :                :  * extended statistic "source_statsid", for the rel identified by heapRel and
                               2044                 :                :  * heapRelid.
                               2045                 :                :  *
                               2046                 :                :  * Attribute numbers in expression Vars are adjusted according to attmap.
                               2047                 :                :  */
                               2048                 :                : static CreateStatsStmt *
 2843 alvherre@alvh.no-ip.     2049                 :             24 : generateClonedExtStatsStmt(RangeVar *heapRel, Oid heapRelid,
                               2050                 :                :                            Oid source_statsid, const AttrMap *attmap)
                               2051                 :                : {
                               2052                 :                :     HeapTuple   ht_stats;
                               2053                 :                :     Form_pg_statistic_ext statsrec;
                               2054                 :                :     CreateStatsStmt *stats;
 2791 tgl@sss.pgh.pa.us        2055                 :             24 :     List       *stat_types = NIL;
                               2056                 :             24 :     List       *def_names = NIL;
                               2057                 :                :     bool        isnull;
                               2058                 :                :     Datum       datum;
                               2059                 :                :     ArrayType  *arr;
                               2060                 :                :     char       *enabled;
                               2061                 :                :     int         i;
                               2062                 :                : 
 2843 alvherre@alvh.no-ip.     2063         [ -  + ]:             24 :     Assert(OidIsValid(heapRelid));
                               2064         [ -  + ]:             24 :     Assert(heapRel != NULL);
                               2065                 :                : 
                               2066                 :                :     /*
                               2067                 :                :      * Fetch pg_statistic_ext tuple of source statistics object.
                               2068                 :                :      */
                               2069                 :             24 :     ht_stats = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(source_statsid));
                               2070         [ -  + ]:             24 :     if (!HeapTupleIsValid(ht_stats))
 2843 alvherre@alvh.no-ip.     2071         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for statistics object %u", source_statsid);
 2843 alvherre@alvh.no-ip.     2072                 :CBC          24 :     statsrec = (Form_pg_statistic_ext) GETSTRUCT(ht_stats);
                               2073                 :                : 
                               2074                 :                :     /* Determine which statistics types exist */
  997 dgustafsson@postgres     2075                 :             24 :     datum = SysCacheGetAttrNotNull(STATEXTOID, ht_stats,
                               2076                 :                :                                    Anum_pg_statistic_ext_stxkind);
 2843 alvherre@alvh.no-ip.     2077                 :             24 :     arr = DatumGetArrayTypeP(datum);
                               2078         [ +  - ]:             24 :     if (ARR_NDIM(arr) != 1 ||
                               2079         [ +  - ]:             24 :         ARR_HASNULL(arr) ||
                               2080         [ -  + ]:             24 :         ARR_ELEMTYPE(arr) != CHAROID)
 2843 alvherre@alvh.no-ip.     2081         [ #  # ]:UBC           0 :         elog(ERROR, "stxkind is not a 1-D char array");
 2843 alvherre@alvh.no-ip.     2082         [ -  + ]:CBC          24 :     enabled = (char *) ARR_DATA_PTR(arr);
                               2083         [ +  + ]:             72 :     for (i = 0; i < ARR_DIMS(arr)[0]; i++)
                               2084                 :                :     {
                               2085         [ +  + ]:             48 :         if (enabled[i] == STATS_EXT_NDISTINCT)
                               2086                 :             12 :             stat_types = lappend(stat_types, makeString("ndistinct"));
                               2087         [ +  + ]:             36 :         else if (enabled[i] == STATS_EXT_DEPENDENCIES)
                               2088                 :             12 :             stat_types = lappend(stat_types, makeString("dependencies"));
 2456 tomas.vondra@postgre     2089         [ +  + ]:             24 :         else if (enabled[i] == STATS_EXT_MCV)
                               2090                 :             12 :             stat_types = lappend(stat_types, makeString("mcv"));
 1726                          2091         [ +  - ]:             12 :         else if (enabled[i] == STATS_EXT_EXPRESSIONS)
                               2092                 :                :             /* expression stats are not exposed to users */
                               2093                 :             12 :             continue;
                               2094                 :                :         else
 2843 alvherre@alvh.no-ip.     2095         [ #  # ]:UBC           0 :             elog(ERROR, "unrecognized statistics kind %c", enabled[i]);
                               2096                 :                :     }
                               2097                 :                : 
                               2098                 :                :     /* Determine which columns the statistics are on */
 2843 alvherre@alvh.no-ip.     2099         [ +  + ]:CBC          48 :     for (i = 0; i < statsrec->stxkeys.dim1; i++)
                               2100                 :                :     {
 1726 tomas.vondra@postgre     2101                 :             24 :         StatsElem  *selem = makeNode(StatsElem);
 2843 alvherre@alvh.no-ip.     2102                 :             24 :         AttrNumber  attnum = statsrec->stxkeys.values[i];
                               2103                 :                : 
 1726 tomas.vondra@postgre     2104                 :             24 :         selem->name = get_attname(heapRelid, attnum, false);
                               2105                 :             24 :         selem->expr = NULL;
                               2106                 :                : 
                               2107                 :             24 :         def_names = lappend(def_names, selem);
                               2108                 :                :     }
                               2109                 :                : 
                               2110                 :                :     /*
                               2111                 :                :      * Now handle expressions, if there are any. The order (with respect to
                               2112                 :                :      * regular attributes) does not really matter for extended stats, so we
                               2113                 :                :      * simply append them after simple column references.
                               2114                 :                :      *
                               2115                 :                :      * XXX Some places during build/estimation treat expressions as if they
                               2116                 :                :      * are before attributes, but for the CREATE command that's entirely
                               2117                 :                :      * irrelevant.
                               2118                 :                :      */
                               2119                 :             24 :     datum = SysCacheGetAttr(STATEXTOID, ht_stats,
                               2120                 :                :                             Anum_pg_statistic_ext_stxexprs, &isnull);
                               2121                 :                : 
                               2122         [ +  + ]:             24 :     if (!isnull)
                               2123                 :                :     {
                               2124                 :                :         ListCell   *lc;
                               2125                 :             12 :         List       *exprs = NIL;
                               2126                 :                :         char       *exprsString;
                               2127                 :                : 
                               2128                 :             12 :         exprsString = TextDatumGetCString(datum);
                               2129                 :             12 :         exprs = (List *) stringToNode(exprsString);
                               2130                 :                : 
                               2131   [ +  -  +  +  :             24 :         foreach(lc, exprs)
                                              +  + ]
                               2132                 :                :         {
  573 tgl@sss.pgh.pa.us        2133                 :             12 :             Node       *expr = (Node *) lfirst(lc);
 1726 tomas.vondra@postgre     2134                 :             12 :             StatsElem  *selem = makeNode(StatsElem);
                               2135                 :                :             bool        found_whole_row;
                               2136                 :                : 
                               2137                 :                :             /* Adjust Vars to match new table's column numbering */
  573 tgl@sss.pgh.pa.us        2138                 :             12 :             expr = map_variable_attnos(expr,
                               2139                 :                :                                        1, 0,
                               2140                 :                :                                        attmap,
                               2141                 :                :                                        InvalidOid,
                               2142                 :                :                                        &found_whole_row);
                               2143                 :                : 
 1726 tomas.vondra@postgre     2144                 :             12 :             selem->name = NULL;
  573 tgl@sss.pgh.pa.us        2145                 :             12 :             selem->expr = expr;
                               2146                 :                : 
 1726 tomas.vondra@postgre     2147                 :             12 :             def_names = lappend(def_names, selem);
                               2148                 :                :         }
                               2149                 :                : 
                               2150                 :             12 :         pfree(exprsString);
                               2151                 :                :     }
                               2152                 :                : 
                               2153                 :                :     /* finally, build the output node */
 2843 alvherre@alvh.no-ip.     2154                 :             24 :     stats = makeNode(CreateStatsStmt);
                               2155                 :             24 :     stats->defnames = NULL;
                               2156                 :             24 :     stats->stat_types = stat_types;
                               2157                 :             24 :     stats->exprs = def_names;
                               2158                 :             24 :     stats->relations = list_make1(heapRel);
                               2159                 :             24 :     stats->stxcomment = NULL;
 1726 tomas.vondra@postgre     2160                 :             24 :     stats->transformed = true;   /* don't need transformStatsStmt again */
 1650 noah@leadboat.com        2161                 :             24 :     stats->if_not_exists = false;
                               2162                 :                : 
                               2163                 :                :     /* Clean up */
 2843 alvherre@alvh.no-ip.     2164                 :             24 :     ReleaseSysCache(ht_stats);
                               2165                 :                : 
                               2166                 :             24 :     return stats;
                               2167                 :                : }
                               2168                 :                : 
                               2169                 :                : /*
                               2170                 :                :  * get_collation        - fetch qualified name of a collation
                               2171                 :                :  *
                               2172                 :                :  * If collation is InvalidOid or is the default for the given actual_datatype,
                               2173                 :                :  * then the return value is NIL.
                               2174                 :                :  */
                               2175                 :                : static List *
 5379 tgl@sss.pgh.pa.us        2176                 :           1903 : get_collation(Oid collation, Oid actual_datatype)
                               2177                 :                : {
                               2178                 :                :     List       *result;
                               2179                 :                :     HeapTuple   ht_coll;
                               2180                 :                :     Form_pg_collation coll_rec;
                               2181                 :                :     char       *nsp_name;
                               2182                 :                :     char       *coll_name;
                               2183                 :                : 
                               2184         [ +  + ]:           1903 :     if (!OidIsValid(collation))
                               2185                 :           1698 :         return NIL;             /* easy case */
                               2186         [ +  + ]:            205 :     if (collation == get_typcollation(actual_datatype))
                               2187                 :            194 :         return NIL;             /* just let it default */
                               2188                 :                : 
                               2189                 :             11 :     ht_coll = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation));
                               2190         [ -  + ]:             11 :     if (!HeapTupleIsValid(ht_coll))
 5379 tgl@sss.pgh.pa.us        2191         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for collation %u", collation);
 5379 tgl@sss.pgh.pa.us        2192                 :CBC          11 :     coll_rec = (Form_pg_collation) GETSTRUCT(ht_coll);
                               2193                 :                : 
                               2194                 :                :     /* For simplicity, we always schema-qualify the name */
                               2195                 :             11 :     nsp_name = get_namespace_name(coll_rec->collnamespace);
                               2196                 :             11 :     coll_name = pstrdup(NameStr(coll_rec->collname));
                               2197                 :             11 :     result = list_make2(makeString(nsp_name), makeString(coll_name));
                               2198                 :                : 
                               2199                 :             11 :     ReleaseSysCache(ht_coll);
                               2200                 :             11 :     return result;
                               2201                 :                : }
                               2202                 :                : 
                               2203                 :                : /*
                               2204                 :                :  * get_opclass          - fetch qualified name of an index operator class
                               2205                 :                :  *
                               2206                 :                :  * If the opclass is the default for the given actual_datatype, then
                               2207                 :                :  * the return value is NIL.
                               2208                 :                :  */
                               2209                 :                : static List *
 6727 neilc@samurai.com        2210                 :           1903 : get_opclass(Oid opclass, Oid actual_datatype)
                               2211                 :                : {
 5379 tgl@sss.pgh.pa.us        2212                 :           1903 :     List       *result = NIL;
                               2213                 :                :     HeapTuple   ht_opc;
                               2214                 :                :     Form_pg_opclass opc_rec;
                               2215                 :                : 
 5784 rhaas@postgresql.org     2216                 :           1903 :     ht_opc = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
 6727 neilc@samurai.com        2217         [ -  + ]:           1903 :     if (!HeapTupleIsValid(ht_opc))
 6727 neilc@samurai.com        2218         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for opclass %u", opclass);
 6727 neilc@samurai.com        2219                 :CBC        1903 :     opc_rec = (Form_pg_opclass) GETSTRUCT(ht_opc);
                               2220                 :                : 
 6590 tgl@sss.pgh.pa.us        2221         [ +  + ]:           1903 :     if (GetDefaultOpClass(actual_datatype, opc_rec->opcmethod) != opclass)
                               2222                 :                :     {
                               2223                 :                :         /* For simplicity, we always schema-qualify the name */
 6606 bruce@momjian.us         2224                 :             12 :         char       *nsp_name = get_namespace_name(opc_rec->opcnamespace);
 6590 tgl@sss.pgh.pa.us        2225                 :             12 :         char       *opc_name = pstrdup(NameStr(opc_rec->opcname));
                               2226                 :                : 
 6727 neilc@samurai.com        2227                 :             12 :         result = list_make2(makeString(nsp_name), makeString(opc_name));
                               2228                 :                :     }
                               2229                 :                : 
                               2230                 :           1903 :     ReleaseSysCache(ht_opc);
                               2231                 :           1903 :     return result;
                               2232                 :                : }
                               2233                 :                : 
                               2234                 :                : 
                               2235                 :                : /*
                               2236                 :                :  * transformIndexConstraints
                               2237                 :                :  *      Handle UNIQUE, PRIMARY KEY, EXCLUDE constraints, which create indexes.
                               2238                 :                :  *      We also merge in any index definitions arising from
                               2239                 :                :  *      LIKE ... INCLUDING INDEXES.
                               2240                 :                :  */
                               2241                 :                : static void
 5439 tgl@sss.pgh.pa.us        2242                 :          32297 : transformIndexConstraints(CreateStmtContext *cxt)
                               2243                 :                : {
                               2244                 :                :     IndexStmt  *index;
 6727 neilc@samurai.com        2245                 :          32297 :     List       *indexlist = NIL;
 2429 tgl@sss.pgh.pa.us        2246                 :          32297 :     List       *finalindexlist = NIL;
                               2247                 :                :     ListCell   *lc;
                               2248                 :                : 
                               2249                 :                :     /*
                               2250                 :                :      * Run through the constraints that need to generate an index, and do so.
                               2251                 :                :      *
                               2252                 :                :      * For PRIMARY KEY, this queues not-null constraints for each column, if
                               2253                 :                :      * needed.
                               2254                 :                :      */
 6727 neilc@samurai.com        2255   [ +  +  +  +  :          42286 :     foreach(lc, cxt->ixconstraints)
                                              +  + ]
                               2256                 :                :     {
 3172 tgl@sss.pgh.pa.us        2257                 :          10022 :         Constraint *constraint = lfirst_node(Constraint, lc);
                               2258                 :                : 
 6590                          2259   [ +  +  +  +  :          10022 :         Assert(constraint->contype == CONSTR_PRIMARY ||
                                              -  + ]
                               2260                 :                :                constraint->contype == CONSTR_UNIQUE ||
                               2261                 :                :                constraint->contype == CONSTR_EXCLUSION);
                               2262                 :                : 
 6727 neilc@samurai.com        2263                 :          10022 :         index = transformIndexConstraint(constraint, cxt);
                               2264                 :                : 
 6590 tgl@sss.pgh.pa.us        2265                 :           9989 :         indexlist = lappend(indexlist, index);
                               2266                 :                :     }
                               2267                 :                : 
                               2268                 :                :     /*
                               2269                 :                :      * Scan the index list and remove any redundant index specifications. This
                               2270                 :                :      * can happen if, for instance, the user writes UNIQUE PRIMARY KEY. A
                               2271                 :                :      * strict reading of SQL would suggest raising an error instead, but that
                               2272                 :                :      * strikes me as too anal-retentive. - tgl 2001-02-14
                               2273                 :                :      *
                               2274                 :                :      * XXX in ALTER TABLE case, it'd be nice to look for duplicate
                               2275                 :                :      * pre-existing indexes, too.
                               2276                 :                :      */
 6751                          2277         [ +  + ]:          32264 :     if (cxt->pkey != NULL)
                               2278                 :                :     {
                               2279                 :                :         /* Make sure we keep the PKEY index in preference to others... */
 2429                          2280                 :           7039 :         finalindexlist = list_make1(cxt->pkey);
                               2281                 :                :     }
                               2282                 :                : 
 6727 neilc@samurai.com        2283   [ +  +  +  +  :          42253 :     foreach(lc, indexlist)
                                              +  + ]
                               2284                 :                :     {
 6751 tgl@sss.pgh.pa.us        2285                 :           9989 :         bool        keep = true;
                               2286                 :                :         ListCell   *k;
                               2287                 :                : 
 6727 neilc@samurai.com        2288                 :           9989 :         index = lfirst(lc);
                               2289                 :                : 
                               2290                 :                :         /* if it's pkey, it's already in finalindexlist */
 6751 tgl@sss.pgh.pa.us        2291         [ +  + ]:           9989 :         if (index == cxt->pkey)
                               2292                 :           7039 :             continue;
                               2293                 :                : 
 2429                          2294   [ +  +  +  +  :           3047 :         foreach(k, finalindexlist)
                                              +  + ]
                               2295                 :                :         {
 6751                          2296                 :             97 :             IndexStmt  *priorindex = lfirst(k);
                               2297                 :                : 
 6590                          2298   [ +  +  +  - ]:            100 :             if (equal(index->indexParams, priorindex->indexParams) &&
 2810 teodor@sigaev.ru         2299         [ +  - ]:              6 :                 equal(index->indexIncludingParams, priorindex->indexIncludingParams) &&
 6590 tgl@sss.pgh.pa.us        2300         [ +  - ]:              6 :                 equal(index->whereClause, priorindex->whereClause) &&
 5853                          2301                 :              3 :                 equal(index->excludeOpNames, priorindex->excludeOpNames) &&
 5984                          2302         [ +  - ]:              3 :                 strcmp(index->accessMethod, priorindex->accessMethod) == 0 &&
 1412 peter@eisentraut.org     2303         [ +  - ]:              3 :                 index->nulls_not_distinct == priorindex->nulls_not_distinct &&
 5984 tgl@sss.pgh.pa.us        2304         [ -  + ]:              3 :                 index->deferrable == priorindex->deferrable &&
 5984 tgl@sss.pgh.pa.us        2305         [ #  # ]:UBC           0 :                 index->initdeferred == priorindex->initdeferred)
                               2306                 :                :             {
 6590                          2307                 :              0 :                 priorindex->unique |= index->unique;
                               2308                 :                : 
                               2309                 :                :                 /*
                               2310                 :                :                  * If the prior index is as yet unnamed, and this one is
                               2311                 :                :                  * named, then transfer the name to the prior index. This
                               2312                 :                :                  * ensures that if we have named and unnamed constraints,
                               2313                 :                :                  * we'll use (at least one of) the names for the index.
                               2314                 :                :                  */
 6751                          2315         [ #  # ]:              0 :                 if (priorindex->idxname == NULL)
                               2316                 :              0 :                     priorindex->idxname = index->idxname;
                               2317                 :              0 :                 keep = false;
                               2318                 :              0 :                 break;
                               2319                 :                :             }
                               2320                 :                :         }
                               2321                 :                : 
 6751 tgl@sss.pgh.pa.us        2322         [ +  - ]:CBC        2950 :         if (keep)
 2429                          2323                 :           2950 :             finalindexlist = lappend(finalindexlist, index);
                               2324                 :                :     }
                               2325                 :                : 
                               2326                 :                :     /*
                               2327                 :                :      * Now append all the IndexStmts to cxt->alist.
                               2328                 :                :      */
                               2329                 :          32264 :     cxt->alist = list_concat(cxt->alist, finalindexlist);
 6727 neilc@samurai.com        2330                 :          32264 : }
                               2331                 :                : 
                               2332                 :                : /*
                               2333                 :                :  * transformIndexConstraint
                               2334                 :                :  *      Transform one UNIQUE, PRIMARY KEY, or EXCLUDE constraint for
                               2335                 :                :  *      transformIndexConstraints. An IndexStmt is returned.
                               2336                 :                :  *
                               2337                 :                :  * For a PRIMARY KEY constraint, we additionally create not-null constraints
                               2338                 :                :  * for columns that don't already have them.
                               2339                 :                :  */
                               2340                 :                : static IndexStmt *
                               2341                 :          10022 : transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
                               2342                 :                : {
                               2343                 :                :     IndexStmt  *index;
                               2344                 :                :     ListCell   *lc;
                               2345                 :                : 
                               2346                 :          10022 :     index = makeNode(IndexStmt);
                               2347                 :                : 
 5853 tgl@sss.pgh.pa.us        2348                 :          10022 :     index->unique = (constraint->contype != CONSTR_EXCLUSION);
 6727 neilc@samurai.com        2349                 :          10022 :     index->primary = (constraint->contype == CONSTR_PRIMARY);
                               2350         [ +  + ]:          10022 :     if (index->primary)
                               2351                 :                :     {
                               2352         [ -  + ]:           7054 :         if (cxt->pkey != NULL)
 6727 neilc@samurai.com        2353         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2354                 :                :                     (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                               2355                 :                :                      errmsg("multiple primary keys for table \"%s\" are not allowed",
                               2356                 :                :                             cxt->relation->relname),
                               2357                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
 6727 neilc@samurai.com        2358                 :CBC        7054 :         cxt->pkey = index;
                               2359                 :                : 
                               2360                 :                :         /*
                               2361                 :                :          * In ALTER TABLE case, a primary index might already exist, but
                               2362                 :                :          * DefineIndex will check for it.
                               2363                 :                :          */
                               2364                 :                :     }
 1412 peter@eisentraut.org     2365                 :          10022 :     index->nulls_not_distinct = constraint->nulls_not_distinct;
 6727 neilc@samurai.com        2366                 :          10022 :     index->isconstraint = true;
  455 peter@eisentraut.org     2367                 :          10022 :     index->iswithoutoverlaps = constraint->without_overlaps;
 5984 tgl@sss.pgh.pa.us        2368                 :          10022 :     index->deferrable = constraint->deferrable;
                               2369                 :          10022 :     index->initdeferred = constraint->initdeferred;
                               2370                 :                : 
 5983                          2371         [ +  + ]:          10022 :     if (constraint->conname != NULL)
                               2372                 :            740 :         index->idxname = pstrdup(constraint->conname);
                               2373                 :                :     else
 6606 bruce@momjian.us         2374                 :           9282 :         index->idxname = NULL;   /* DefineIndex will choose name */
                               2375                 :                : 
 6727 neilc@samurai.com        2376                 :          10022 :     index->relation = cxt->relation;
 5853 tgl@sss.pgh.pa.us        2377         [ +  + ]:          10022 :     index->accessMethod = constraint->access_method ? constraint->access_method : DEFAULT_INDEX_TYPE;
 6727 neilc@samurai.com        2378                 :          10022 :     index->options = constraint->options;
                               2379                 :          10022 :     index->tableSpace = constraint->indexspace;
 5853 tgl@sss.pgh.pa.us        2380                 :          10022 :     index->whereClause = constraint->where_clause;
 6727 neilc@samurai.com        2381                 :          10022 :     index->indexParams = NIL;
 2810 teodor@sigaev.ru         2382                 :          10022 :     index->indexIncludingParams = NIL;
 5853 tgl@sss.pgh.pa.us        2383                 :          10022 :     index->excludeOpNames = NIL;
 4901                          2384                 :          10022 :     index->idxcomment = NULL;
 5439                          2385                 :          10022 :     index->indexOid = InvalidOid;
 1259 rhaas@postgresql.org     2386                 :          10022 :     index->oldNumber = InvalidRelFileNumber;
 2082 noah@leadboat.com        2387                 :          10022 :     index->oldCreateSubid = InvalidSubTransactionId;
 1259 rhaas@postgresql.org     2388                 :          10022 :     index->oldFirstRelfilelocatorSubid = InvalidSubTransactionId;
 3950 tgl@sss.pgh.pa.us        2389                 :          10022 :     index->transformed = false;
 6727 neilc@samurai.com        2390                 :          10022 :     index->concurrent = false;
 3950 tgl@sss.pgh.pa.us        2391                 :          10022 :     index->if_not_exists = false;
 2427 alvherre@alvh.no-ip.     2392                 :          10022 :     index->reset_default_tblspc = constraint->reset_default_tblspc;
                               2393                 :                : 
                               2394                 :                :     /*
                               2395                 :                :      * If it's ALTER TABLE ADD CONSTRAINT USING INDEX, look up the index and
                               2396                 :                :      * verify it's usable, then extract the implied column name list.  (We
                               2397                 :                :      * will not actually need the column name list at runtime, but we need it
                               2398                 :                :      * now to check for duplicate column entries below.)
                               2399                 :                :      */
 5439 tgl@sss.pgh.pa.us        2400         [ +  + ]:          10022 :     if (constraint->indexname != NULL)
                               2401                 :                :     {
                               2402                 :           5442 :         char       *index_name = constraint->indexname;
                               2403                 :           5442 :         Relation    heap_rel = cxt->rel;
                               2404                 :                :         Oid         index_oid;
                               2405                 :                :         Relation    index_rel;
                               2406                 :                :         Form_pg_index index_form;
                               2407                 :                :         oidvector  *indclass;
                               2408                 :                :         Datum       indclassDatum;
                               2409                 :                :         int         i;
                               2410                 :                : 
                               2411                 :                :         /* Grammar should not allow this with explicit column list */
                               2412         [ -  + ]:           5442 :         Assert(constraint->keys == NIL);
                               2413                 :                : 
                               2414                 :                :         /* Grammar should only allow PRIMARY and UNIQUE constraints */
                               2415   [ +  +  -  + ]:           5442 :         Assert(constraint->contype == CONSTR_PRIMARY ||
                               2416                 :                :                constraint->contype == CONSTR_UNIQUE);
                               2417                 :                : 
                               2418                 :                :         /* Must be ALTER, not CREATE, but grammar doesn't enforce that */
                               2419         [ -  + ]:           5442 :         if (!cxt->isalter)
 5439 tgl@sss.pgh.pa.us        2420         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2421                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               2422                 :                :                      errmsg("cannot use an existing index in CREATE TABLE"),
                               2423                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2424                 :                : 
                               2425                 :                :         /* Look for the index in the same schema as the table */
 5439 tgl@sss.pgh.pa.us        2426                 :CBC        5442 :         index_oid = get_relname_relid(index_name, RelationGetNamespace(heap_rel));
                               2427                 :                : 
                               2428         [ -  + ]:           5442 :         if (!OidIsValid(index_oid))
 5439 tgl@sss.pgh.pa.us        2429         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2430                 :                :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
                               2431                 :                :                      errmsg("index \"%s\" does not exist", index_name),
                               2432                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2433                 :                : 
                               2434                 :                :         /* Open the index (this will throw an error if it is not an index) */
 5439 tgl@sss.pgh.pa.us        2435                 :CBC        5442 :         index_rel = index_open(index_oid, AccessShareLock);
                               2436                 :           5442 :         index_form = index_rel->rd_index;
                               2437                 :                : 
                               2438                 :                :         /* Check that it does not have an associated constraint already */
                               2439         [ -  + ]:           5442 :         if (OidIsValid(get_index_constraint(index_oid)))
 5439 tgl@sss.pgh.pa.us        2440         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2441                 :                :                     (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               2442                 :                :                      errmsg("index \"%s\" is already associated with a constraint",
                               2443                 :                :                             index_name),
                               2444                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2445                 :                : 
                               2446                 :                :         /* Perform validity checks on the index */
 5439 tgl@sss.pgh.pa.us        2447         [ -  + ]:CBC        5442 :         if (index_form->indrelid != RelationGetRelid(heap_rel))
 5439 tgl@sss.pgh.pa.us        2448         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2449                 :                :                     (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               2450                 :                :                      errmsg("index \"%s\" does not belong to table \"%s\"",
                               2451                 :                :                             index_name, RelationGetRelationName(heap_rel)),
                               2452                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2453                 :                : 
 2546 peter_e@gmx.net          2454         [ -  + ]:CBC        5442 :         if (!index_form->indisvalid)
 5439 tgl@sss.pgh.pa.us        2455         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2456                 :                :                     (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               2457                 :                :                      errmsg("index \"%s\" is not valid", index_name),
                               2458                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2459                 :                : 
                               2460                 :                :         /*
                               2461                 :                :          * Today we forbid non-unique indexes, but we could permit GiST
                               2462                 :                :          * indexes whose last entry is a range type and use that to create a
                               2463                 :                :          * WITHOUT OVERLAPS constraint (i.e. a temporal constraint).
                               2464                 :                :          */
 5439 tgl@sss.pgh.pa.us        2465         [ +  + ]:CBC        5442 :         if (!index_form->indisunique)
                               2466         [ +  - ]:              6 :             ereport(ERROR,
                               2467                 :                :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               2468                 :                :                      errmsg("\"%s\" is not a unique index", index_name),
                               2469                 :                :                      errdetail("Cannot create a primary key or unique constraint using such an index."),
                               2470                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2471                 :                : 
                               2472         [ -  + ]:           5436 :         if (RelationGetIndexExpressions(index_rel) != NIL)
 5439 tgl@sss.pgh.pa.us        2473         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2474                 :                :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               2475                 :                :                      errmsg("index \"%s\" contains expressions", index_name),
                               2476                 :                :                      errdetail("Cannot create a primary key or unique constraint using such an index."),
                               2477                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2478                 :                : 
 5439 tgl@sss.pgh.pa.us        2479         [ -  + ]:CBC        5436 :         if (RelationGetIndexPredicate(index_rel) != NIL)
 5439 tgl@sss.pgh.pa.us        2480         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2481                 :                :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               2482                 :                :                      errmsg("\"%s\" is a partial index", index_name),
                               2483                 :                :                      errdetail("Cannot create a primary key or unique constraint using such an index."),
                               2484                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2485                 :                : 
                               2486                 :                :         /*
                               2487                 :                :          * It's probably unsafe to change a deferred index to non-deferred. (A
                               2488                 :                :          * non-constraint index couldn't be deferred anyway, so this case
                               2489                 :                :          * should never occur; no need to sweat, but let's check it.)
                               2490                 :                :          */
 5439 tgl@sss.pgh.pa.us        2491   [ -  +  -  - ]:CBC        5436 :         if (!index_form->indimmediate && !constraint->deferrable)
 5439 tgl@sss.pgh.pa.us        2492         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2493                 :                :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               2494                 :                :                      errmsg("\"%s\" is a deferrable index", index_name),
                               2495                 :                :                      errdetail("Cannot create a non-deferrable constraint using a deferrable index."),
                               2496                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2497                 :                : 
                               2498                 :                :         /*
                               2499                 :                :          * Insist on it being a btree.  We must have an index that exactly
                               2500                 :                :          * matches what you'd get from plain ADD CONSTRAINT syntax, else dump
                               2501                 :                :          * and reload will produce a different index (breaking pg_upgrade in
                               2502                 :                :          * particular).
                               2503                 :                :          */
 3555 alvherre@alvh.no-ip.     2504         [ -  + ]:CBC        5436 :         if (index_rel->rd_rel->relam != get_index_am_oid(DEFAULT_INDEX_TYPE, false))
 5439 tgl@sss.pgh.pa.us        2505         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2506                 :                :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               2507                 :                :                      errmsg("index \"%s\" is not a btree", index_name),
                               2508                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2509                 :                : 
                               2510                 :                :         /* Must get indclass the hard way */
  997 dgustafsson@postgres     2511                 :CBC        5436 :         indclassDatum = SysCacheGetAttrNotNull(INDEXRELID,
                               2512                 :           5436 :                                                index_rel->rd_indextuple,
                               2513                 :                :                                                Anum_pg_index_indclass);
 5439 tgl@sss.pgh.pa.us        2514                 :           5436 :         indclass = (oidvector *) DatumGetPointer(indclassDatum);
                               2515                 :                : 
                               2516         [ +  + ]:          14378 :         for (i = 0; i < index_form->indnatts; i++)
                               2517                 :                :         {
 4922 peter_e@gmx.net          2518                 :           8948 :             int16       attnum = index_form->indkey.values[i];
                               2519                 :                :             const FormData_pg_attribute *attform;
                               2520                 :                :             char       *attname;
                               2521                 :                :             Oid         defopclass;
                               2522                 :                : 
                               2523                 :                :             /*
                               2524                 :                :              * We shouldn't see attnum == 0 here, since we already rejected
                               2525                 :                :              * expression indexes.  If we do, SystemAttributeDefinition will
                               2526                 :                :              * throw an error.
                               2527                 :                :              */
 5439 tgl@sss.pgh.pa.us        2528         [ +  - ]:           8948 :             if (attnum > 0)
                               2529                 :                :             {
                               2530         [ -  + ]:           8948 :                 Assert(attnum <= heap_rel->rd_att->natts);
 3040 andres@anarazel.de       2531                 :           8948 :                 attform = TupleDescAttr(heap_rel->rd_att, attnum - 1);
                               2532                 :                :             }
                               2533                 :                :             else
 2583 andres@anarazel.de       2534                 :UBC           0 :                 attform = SystemAttributeDefinition(attnum);
 5439 tgl@sss.pgh.pa.us        2535                 :CBC        8948 :             attname = pstrdup(NameStr(attform->attname));
                               2536                 :                : 
 2810 teodor@sigaev.ru         2537         [ +  + ]:           8948 :             if (i < index_form->indnkeyatts)
                               2538                 :                :             {
                               2539                 :                :                 /*
                               2540                 :                :                  * Insist on default opclass, collation, and sort options.
                               2541                 :                :                  * While the index would still work as a constraint with
                               2542                 :                :                  * non-default settings, it might not provide exactly the same
                               2543                 :                :                  * uniqueness semantics as you'd get from a normally-created
                               2544                 :                :                  * constraint; and there's also the dump/reload problem
                               2545                 :                :                  * mentioned above.
                               2546                 :                :                  */
                               2547                 :                :                 Datum       attoptions =
  942 tgl@sss.pgh.pa.us        2548                 :           8933 :                     get_attoptions(RelationGetRelid(index_rel), i + 1);
                               2549                 :                : 
 2810 teodor@sigaev.ru         2550                 :           8933 :                 defopclass = GetDefaultOpClass(attform->atttypid,
                               2551                 :           8933 :                                                index_rel->rd_rel->relam);
                               2552         [ +  - ]:           8933 :                 if (indclass->values[i] != defopclass ||
 2202 tgl@sss.pgh.pa.us        2553   [ +  +  +  - ]:           8933 :                     attform->attcollation != index_rel->rd_indcollation[i] ||
 2087 akorotkov@postgresql     2554                 :           8930 :                     attoptions != (Datum) 0 ||
 2810 teodor@sigaev.ru         2555         [ +  + ]:           8930 :                     index_rel->rd_indoption[i] != 0)
                               2556         [ +  - ]:              6 :                     ereport(ERROR,
                               2557                 :                :                             (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               2558                 :                :                              errmsg("index \"%s\" column number %d does not have default sorting behavior", index_name, i + 1),
                               2559                 :                :                              errdetail("Cannot create a primary key or unique constraint using such an index."),
                               2560                 :                :                              parser_errposition(cxt->pstate, constraint->location)));
                               2561                 :                : 
                               2562                 :                :                 /* If a PK, ensure the columns get not null constraints */
  403 alvherre@alvh.no-ip.     2563         [ +  + ]:           8927 :                 if (constraint->contype == CONSTR_PRIMARY)
                               2564                 :           4060 :                     cxt->nnconstraints =
                               2565                 :           4060 :                         lappend(cxt->nnconstraints,
                               2566                 :           4060 :                                 makeNotNullConstraint(makeString(attname)));
                               2567                 :                : 
 2810 teodor@sigaev.ru         2568                 :           8927 :                 constraint->keys = lappend(constraint->keys, makeString(attname));
                               2569                 :                :             }
                               2570                 :                :             else
                               2571                 :             15 :                 constraint->including = lappend(constraint->including, makeString(attname));
                               2572                 :                :         }
                               2573                 :                : 
                               2574                 :                :         /* Close the index relation but keep the lock */
 5439 tgl@sss.pgh.pa.us        2575                 :           5430 :         relation_close(index_rel, NoLock);
                               2576                 :                : 
                               2577                 :           5430 :         index->indexOid = index_oid;
                               2578                 :                :     }
                               2579                 :                : 
                               2580                 :                :     /*
                               2581                 :                :      * If it's an EXCLUDE constraint, the grammar returns a list of pairs of
                               2582                 :                :      * IndexElems and operator names.  We have to break that apart into
                               2583                 :                :      * separate lists.
                               2584                 :                :      */
 5853                          2585         [ +  + ]:          10010 :     if (constraint->contype == CONSTR_EXCLUSION)
                               2586                 :                :     {
                               2587   [ +  -  +  +  :            287 :         foreach(lc, constraint->exclusions)
                                              +  + ]
                               2588                 :                :         {
 5772 bruce@momjian.us         2589                 :            170 :             List       *pair = (List *) lfirst(lc);
                               2590                 :                :             IndexElem  *elem;
                               2591                 :                :             List       *opname;
                               2592                 :                : 
 5853 tgl@sss.pgh.pa.us        2593         [ -  + ]:            170 :             Assert(list_length(pair) == 2);
 3172                          2594                 :            170 :             elem = linitial_node(IndexElem, pair);
                               2595                 :            170 :             opname = lsecond_node(List, pair);
                               2596                 :                : 
 5853                          2597                 :            170 :             index->indexParams = lappend(index->indexParams, elem);
                               2598                 :            170 :             index->excludeOpNames = lappend(index->excludeOpNames, opname);
                               2599                 :                :         }
                               2600                 :                :     }
                               2601                 :                : 
                               2602                 :                :     /*
                               2603                 :                :      * For UNIQUE and PRIMARY KEY, we just have a list of column names.
                               2604                 :                :      *
                               2605                 :                :      * Make sure referenced keys exist.  If we are making a PRIMARY KEY index,
                               2606                 :                :      * also make sure they are not-null.  For WITHOUT OVERLAPS constraints, we
                               2607                 :                :      * make sure the last part is a range or multirange.
                               2608                 :                :      */
                               2609                 :                :     else
                               2610                 :                :     {
 2810 teodor@sigaev.ru         2611   [ +  -  +  +  :          24016 :         foreach(lc, constraint->keys)
                                              +  + ]
                               2612                 :                :         {
                               2613                 :          14138 :             char       *key = strVal(lfirst(lc));
                               2614                 :          14138 :             bool        found = false;
                               2615                 :          14138 :             ColumnDef  *column = NULL;
                               2616                 :                :             ListCell   *columns;
                               2617                 :                :             IndexElem  *iparam;
  455 peter@eisentraut.org     2618                 :          14138 :             Oid         typid = InvalidOid;
                               2619                 :                : 
                               2620                 :                :             /* Make sure referenced column exists. */
 2810 teodor@sigaev.ru         2621   [ +  +  +  +  :          14990 :             foreach(columns, cxt->columns)
                                              +  + ]
                               2622                 :                :             {
 1611 peter@eisentraut.org     2623                 :           5183 :                 column = lfirst_node(ColumnDef, columns);
 2810 teodor@sigaev.ru         2624         [ +  + ]:           5183 :                 if (strcmp(column->colname, key) == 0)
                               2625                 :                :                 {
                               2626                 :           4331 :                     found = true;
                               2627                 :           4331 :                     break;
                               2628                 :                :                 }
                               2629                 :                :             }
  455 peter@eisentraut.org     2630         [ +  + ]:          14138 :             if (!found)
                               2631                 :           9807 :                 column = NULL;
                               2632                 :                : 
 2810 teodor@sigaev.ru         2633         [ +  + ]:          14138 :             if (found)
                               2634                 :                :             {
                               2635                 :                :                 /*
                               2636                 :                :                  * column is defined in the new table.  For CREATE TABLE with
                               2637                 :                :                  * a PRIMARY KEY, we can apply the not-null constraint cheaply
                               2638                 :                :                  * here.  If the not-null constraint already exists, we can
                               2639                 :                :                  * (albeit not so cheaply) verify that it's not a NO INHERIT
                               2640                 :                :                  * constraint.
                               2641                 :                :                  *
                               2642                 :                :                  * Note that ALTER TABLE never needs either check, because
                               2643                 :                :                  * those constraints have already been added by
                               2644                 :                :                  * ATPrepAddPrimaryKey.
                               2645                 :                :                  */
 2429 tgl@sss.pgh.pa.us        2646         [ +  + ]:           4331 :                 if (constraint->contype == CONSTR_PRIMARY &&
  403 alvherre@alvh.no-ip.     2647         [ +  + ]:           3930 :                     !cxt->isalter)
                               2648                 :                :                 {
                               2649         [ +  + ]:           3918 :                     if (column->is_not_null)
                               2650                 :                :                     {
                               2651   [ +  -  +  -  :           6327 :                         foreach_node(Constraint, nn, cxt->nnconstraints)
                                              +  + ]
                               2652                 :                :                         {
                               2653         [ +  + ]:           3207 :                             if (strcmp(strVal(linitial(nn->keys)), key) == 0)
                               2654                 :                :                             {
                               2655         [ +  + ]:           3123 :                                 if (nn->is_no_inherit)
                               2656         [ +  - ]:              3 :                                     ereport(ERROR,
                               2657                 :                :                                             errcode(ERRCODE_SYNTAX_ERROR),
                               2658                 :                :                                             errmsg("conflicting NO INHERIT declaration for not-null constraint on column \"%s\"",
                               2659                 :                :                                                    key));
                               2660                 :           3120 :                                 break;
                               2661                 :                :                             }
                               2662                 :                :                         }
                               2663                 :                :                     }
                               2664                 :                :                     else
                               2665                 :                :                     {
                               2666                 :            795 :                         column->is_not_null = true;
                               2667                 :            795 :                         cxt->nnconstraints =
                               2668                 :            795 :                             lappend(cxt->nnconstraints,
                               2669                 :            795 :                                     makeNotNullConstraint(makeString(key)));
                               2670                 :                :                     }
                               2671                 :                :                 }
                               2672         [ +  + ]:            413 :                 else if (constraint->contype == CONSTR_PRIMARY)
                               2673         [ -  + ]:             12 :                     Assert(column->is_not_null);
                               2674                 :                :             }
 2583 andres@anarazel.de       2675         [ -  + ]:           9807 :             else if (SystemAttributeByName(key) != NULL)
                               2676                 :                :             {
                               2677                 :                :                 /*
                               2678                 :                :                  * column will be a system column in the new table, so accept
                               2679                 :                :                  * it. System columns can't ever be null, so no need to worry
                               2680                 :                :                  * about PRIMARY/NOT NULL constraint.
                               2681                 :                :                  */
 2810 teodor@sigaev.ru         2682                 :UBC           0 :                 found = true;
                               2683                 :                :             }
 2810 teodor@sigaev.ru         2684         [ +  + ]:CBC        9807 :             else if (cxt->inhRelations)
                               2685                 :                :             {
                               2686                 :                :                 /* try inherited tables */
                               2687                 :                :                 ListCell   *inher;
                               2688                 :                : 
                               2689   [ +  -  +  -  :             48 :                 foreach(inher, cxt->inhRelations)
                                              +  - ]
                               2690                 :                :                 {
 1611 peter@eisentraut.org     2691                 :             48 :                     RangeVar   *inh = lfirst_node(RangeVar, inher);
                               2692                 :                :                     Relation    rel;
                               2693                 :                :                     int         count;
                               2694                 :                : 
 2521 andres@anarazel.de       2695                 :             48 :                     rel = table_openrv(inh, AccessShareLock);
                               2696                 :                :                     /* check user requested inheritance from valid relkind */
 2810 teodor@sigaev.ru         2697         [ -  + ]:             48 :                     if (rel->rd_rel->relkind != RELKIND_RELATION &&
 2810 teodor@sigaev.ru         2698         [ #  # ]:UBC           0 :                         rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
                               2699         [ #  # ]:              0 :                         rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
                               2700         [ #  # ]:              0 :                         ereport(ERROR,
                               2701                 :                :                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               2702                 :                :                                  errmsg("inherited relation \"%s\" is not a table or foreign table",
                               2703                 :                :                                         inh->relname)));
 2810 teodor@sigaev.ru         2704         [ +  - ]:CBC          51 :                     for (count = 0; count < rel->rd_att->natts; count++)
                               2705                 :                :                     {
                               2706                 :             51 :                         Form_pg_attribute inhattr = TupleDescAttr(rel->rd_att,
                               2707                 :                :                                                                   count);
                               2708                 :             51 :                         char       *inhname = NameStr(inhattr->attname);
                               2709                 :                : 
                               2710         [ -  + ]:             51 :                         if (inhattr->attisdropped)
 2810 teodor@sigaev.ru         2711                 :UBC           0 :                             continue;
 2810 teodor@sigaev.ru         2712         [ +  + ]:CBC          51 :                         if (strcmp(key, inhname) == 0)
                               2713                 :                :                         {
                               2714                 :             48 :                             found = true;
  455 peter@eisentraut.org     2715                 :             48 :                             typid = inhattr->atttypid;
                               2716                 :                : 
  403 alvherre@alvh.no-ip.     2717         [ +  + ]:             48 :                             if (constraint->contype == CONSTR_PRIMARY)
                               2718                 :             42 :                                 cxt->nnconstraints =
                               2719                 :             42 :                                     lappend(cxt->nnconstraints,
                               2720                 :             42 :                                             makeNotNullConstraint(makeString(pstrdup(inhname))));
 2810 teodor@sigaev.ru         2721                 :             48 :                             break;
                               2722                 :                :                         }
                               2723                 :                :                     }
 2521 andres@anarazel.de       2724                 :             48 :                     table_close(rel, NoLock);
 2810 teodor@sigaev.ru         2725         [ +  - ]:             48 :                     if (found)
                               2726                 :             48 :                         break;
                               2727                 :                :                 }
                               2728                 :                :             }
                               2729                 :                : 
                               2730                 :                :             /*
                               2731                 :                :              * In the ALTER TABLE case, don't complain about index keys not
                               2732                 :                :              * created in the command; they may well exist already.
                               2733                 :                :              * DefineIndex will complain about them if not.
                               2734                 :                :              */
                               2735   [ +  +  +  + ]:          14135 :             if (!found && !cxt->isalter)
                               2736         [ +  - ]:              6 :                 ereport(ERROR,
                               2737                 :                :                         (errcode(ERRCODE_UNDEFINED_COLUMN),
                               2738                 :                :                          errmsg("column \"%s\" named in key does not exist", key),
                               2739                 :                :                          parser_errposition(cxt->pstate, constraint->location)));
                               2740                 :                : 
                               2741                 :                :             /* Check for PRIMARY KEY(foo, foo) */
                               2742   [ +  +  +  +  :          19846 :             foreach(columns, index->indexParams)
                                              +  + ]
                               2743                 :                :             {
                               2744                 :           5717 :                 iparam = (IndexElem *) lfirst(columns);
                               2745   [ +  -  -  + ]:           5717 :                 if (iparam->name && strcmp(key, iparam->name) == 0)
                               2746                 :                :                 {
 2810 teodor@sigaev.ru         2747         [ #  # ]:UBC           0 :                     if (index->primary)
                               2748         [ #  # ]:              0 :                         ereport(ERROR,
                               2749                 :                :                                 (errcode(ERRCODE_DUPLICATE_COLUMN),
                               2750                 :                :                                  errmsg("column \"%s\" appears twice in primary key constraint",
                               2751                 :                :                                         key),
                               2752                 :                :                                  parser_errposition(cxt->pstate, constraint->location)));
                               2753                 :                :                     else
                               2754         [ #  # ]:              0 :                         ereport(ERROR,
                               2755                 :                :                                 (errcode(ERRCODE_DUPLICATE_COLUMN),
                               2756                 :                :                                  errmsg("column \"%s\" appears twice in unique constraint",
                               2757                 :                :                                         key),
                               2758                 :                :                                  parser_errposition(cxt->pstate, constraint->location)));
                               2759                 :                :                 }
                               2760                 :                :             }
                               2761                 :                : 
                               2762                 :                :             /*
                               2763                 :                :              * The WITHOUT OVERLAPS part (if any) must be a range or
                               2764                 :                :              * multirange type.
                               2765                 :                :              */
  455 peter@eisentraut.org     2766   [ +  +  +  + ]:CBC       14129 :             if (constraint->without_overlaps && lc == list_last_cell(constraint->keys))
                               2767                 :                :             {
                               2768   [ +  +  +  - ]:            289 :                 if (!found && cxt->isalter)
                               2769                 :                :                 {
                               2770                 :                :                     /*
                               2771                 :                :                      * Look up the column type on existing table. If we can't
                               2772                 :                :                      * find it, let things fail in DefineIndex.
                               2773                 :                :                      */
                               2774                 :             83 :                     Relation    rel = cxt->rel;
                               2775                 :                : 
                               2776         [ +  - ]:            168 :                     for (int i = 0; i < rel->rd_att->natts; i++)
                               2777                 :                :                     {
                               2778                 :            168 :                         Form_pg_attribute attr = TupleDescAttr(rel->rd_att, i);
                               2779                 :                :                         const char *attname;
                               2780                 :                : 
                               2781         [ -  + ]:            168 :                         if (attr->attisdropped)
  455 peter@eisentraut.org     2782                 :UBC           0 :                             break;
                               2783                 :                : 
  455 peter@eisentraut.org     2784                 :CBC         168 :                         attname = NameStr(attr->attname);
                               2785         [ +  + ]:            168 :                         if (strcmp(attname, key) == 0)
                               2786                 :                :                         {
                               2787                 :             83 :                             found = true;
                               2788                 :             83 :                             typid = attr->atttypid;
                               2789                 :             83 :                             break;
                               2790                 :                :                         }
                               2791                 :                :                     }
                               2792                 :                :                 }
                               2793         [ +  - ]:            289 :                 if (found)
                               2794                 :                :                 {
                               2795   [ +  +  +  - ]:            289 :                     if (!OidIsValid(typid) && column)
                               2796                 :            203 :                         typid = typenameTypeId(NULL, column->typeName);
                               2797                 :                : 
                               2798   [ +  -  +  +  :            289 :                     if (!OidIsValid(typid) || !(type_is_range(typid) || type_is_multirange(typid)))
                                              +  + ]
                               2799         [ +  - ]:              6 :                         ereport(ERROR,
                               2800                 :                :                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2801                 :                :                                  errmsg("column \"%s\" in WITHOUT OVERLAPS is not a range or multirange type", key),
                               2802                 :                :                                  parser_errposition(cxt->pstate, constraint->location)));
                               2803                 :                :                 }
                               2804                 :                :             }
                               2805                 :                : 
                               2806                 :                :             /* OK, add it to the index definition */
 2810 teodor@sigaev.ru         2807                 :          14123 :             iparam = makeNode(IndexElem);
                               2808                 :          14123 :             iparam->name = pstrdup(key);
                               2809                 :          14123 :             iparam->expr = NULL;
                               2810                 :          14123 :             iparam->indexcolname = NULL;
                               2811                 :          14123 :             iparam->collation = NIL;
                               2812                 :          14123 :             iparam->opclass = NIL;
 2087 akorotkov@postgresql     2813                 :          14123 :             iparam->opclassopts = NIL;
 2810 teodor@sigaev.ru         2814                 :          14123 :             iparam->ordering = SORTBY_DEFAULT;
                               2815                 :          14123 :             iparam->nulls_ordering = SORTBY_NULLS_DEFAULT;
                               2816                 :          14123 :             index->indexParams = lappend(index->indexParams, iparam);
                               2817                 :                :         }
                               2818                 :                : 
  455 peter@eisentraut.org     2819         [ +  + ]:           9878 :         if (constraint->without_overlaps)
                               2820                 :                :         {
                               2821                 :                :             /*
                               2822                 :                :              * This enforces that there is at least one equality column
                               2823                 :                :              * besides the WITHOUT OVERLAPS columns.  This is per SQL
                               2824                 :                :              * standard.  XXX Do we need this?
                               2825                 :                :              */
                               2826         [ +  + ]:            283 :             if (list_length(constraint->keys) < 2)
                               2827         [ +  - ]:              6 :                 ereport(ERROR,
                               2828                 :                :                         errcode(ERRCODE_SYNTAX_ERROR),
                               2829                 :                :                         errmsg("constraint using WITHOUT OVERLAPS needs at least two columns"));
                               2830                 :                : 
                               2831                 :                :             /* WITHOUT OVERLAPS requires a GiST index */
                               2832                 :            277 :             index->accessMethod = "gist";
                               2833                 :                :         }
                               2834                 :                : 
                               2835                 :                :     }
                               2836                 :                : 
                               2837                 :                :     /*
                               2838                 :                :      * Add included columns to index definition.  This is much like the
                               2839                 :                :      * simple-column-name-list code above, except that we don't worry about
                               2840                 :                :      * NOT NULL marking; included columns in a primary key should not be
                               2841                 :                :      * forced NOT NULL.  We don't complain about duplicate columns, either,
                               2842                 :                :      * though maybe we should?
                               2843                 :                :      */
 2810 teodor@sigaev.ru         2844   [ +  +  +  +  :          10143 :     foreach(lc, constraint->including)
                                              +  + ]
                               2845                 :                :     {
 5853 tgl@sss.pgh.pa.us        2846                 :            154 :         char       *key = strVal(lfirst(lc));
 6727 neilc@samurai.com        2847                 :            154 :         bool        found = false;
                               2848                 :            154 :         ColumnDef  *column = NULL;
                               2849                 :                :         ListCell   *columns;
                               2850                 :                :         IndexElem  *iparam;
                               2851                 :                : 
                               2852   [ +  +  +  -  :            331 :         foreach(columns, cxt->columns)
                                              +  + ]
                               2853                 :                :         {
 3172 tgl@sss.pgh.pa.us        2854                 :            272 :             column = lfirst_node(ColumnDef, columns);
 6727 neilc@samurai.com        2855         [ +  + ]:            272 :             if (strcmp(column->colname, key) == 0)
                               2856                 :                :             {
                               2857                 :             95 :                 found = true;
                               2858                 :             95 :                 break;
                               2859                 :                :             }
                               2860                 :                :         }
                               2861                 :                : 
 2810 teodor@sigaev.ru         2862         [ +  + ]:            154 :         if (!found)
                               2863                 :                :         {
 2583 andres@anarazel.de       2864         [ -  + ]:             59 :             if (SystemAttributeByName(key) != NULL)
                               2865                 :                :             {
                               2866                 :                :                 /*
                               2867                 :                :                  * column will be a system column in the new table, so accept
                               2868                 :                :                  * it.
                               2869                 :                :                  */
 2810 teodor@sigaev.ru         2870                 :UBC           0 :                 found = true;
                               2871                 :                :             }
 2810 teodor@sigaev.ru         2872         [ -  + ]:CBC          59 :             else if (cxt->inhRelations)
                               2873                 :                :             {
                               2874                 :                :                 /* try inherited tables */
                               2875                 :                :                 ListCell   *inher;
                               2876                 :                : 
 2810 teodor@sigaev.ru         2877   [ #  #  #  #  :UBC           0 :                 foreach(inher, cxt->inhRelations)
                                              #  # ]
                               2878                 :                :                 {
                               2879                 :              0 :                     RangeVar   *inh = lfirst_node(RangeVar, inher);
                               2880                 :                :                     Relation    rel;
                               2881                 :                :                     int         count;
                               2882                 :                : 
 2521 andres@anarazel.de       2883                 :              0 :                     rel = table_openrv(inh, AccessShareLock);
                               2884                 :                :                     /* check user requested inheritance from valid relkind */
 2810 teodor@sigaev.ru         2885         [ #  # ]:              0 :                     if (rel->rd_rel->relkind != RELKIND_RELATION &&
                               2886         [ #  # ]:              0 :                         rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
                               2887         [ #  # ]:              0 :                         rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
                               2888         [ #  # ]:              0 :                         ereport(ERROR,
                               2889                 :                :                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               2890                 :                :                                  errmsg("inherited relation \"%s\" is not a table or foreign table",
                               2891                 :                :                                         inh->relname)));
                               2892         [ #  # ]:              0 :                     for (count = 0; count < rel->rd_att->natts; count++)
                               2893                 :                :                     {
                               2894                 :              0 :                         Form_pg_attribute inhattr = TupleDescAttr(rel->rd_att,
                               2895                 :                :                                                                   count);
                               2896                 :              0 :                         char       *inhname = NameStr(inhattr->attname);
                               2897                 :                : 
                               2898         [ #  # ]:              0 :                         if (inhattr->attisdropped)
                               2899                 :              0 :                             continue;
                               2900         [ #  # ]:              0 :                         if (strcmp(key, inhname) == 0)
                               2901                 :                :                         {
                               2902                 :              0 :                             found = true;
                               2903                 :              0 :                             break;
                               2904                 :                :                         }
                               2905                 :                :                     }
 2521 andres@anarazel.de       2906                 :              0 :                     table_close(rel, NoLock);
 2810 teodor@sigaev.ru         2907         [ #  # ]:              0 :                     if (found)
                               2908                 :              0 :                         break;
                               2909                 :                :                 }
                               2910                 :                :             }
                               2911                 :                :         }
                               2912                 :                : 
                               2913                 :                :         /*
                               2914                 :                :          * In the ALTER TABLE case, don't complain about index keys not
                               2915                 :                :          * created in the command; they may well exist already. DefineIndex
                               2916                 :                :          * will complain about them if not.
                               2917                 :                :          */
 6727 neilc@samurai.com        2918   [ +  +  -  + ]:CBC         154 :         if (!found && !cxt->isalter)
 6727 neilc@samurai.com        2919         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2920                 :                :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
                               2921                 :                :                      errmsg("column \"%s\" named in key does not exist", key),
                               2922                 :                :                      parser_errposition(cxt->pstate, constraint->location)));
                               2923                 :                : 
                               2924                 :                :         /* OK, add it to the index definition */
 6727 neilc@samurai.com        2925                 :CBC         154 :         iparam = makeNode(IndexElem);
                               2926                 :            154 :         iparam->name = pstrdup(key);
                               2927                 :            154 :         iparam->expr = NULL;
 5837 tgl@sss.pgh.pa.us        2928                 :            154 :         iparam->indexcolname = NULL;
 5379                          2929                 :            154 :         iparam->collation = NIL;
 6727 neilc@samurai.com        2930                 :            154 :         iparam->opclass = NIL;
 2087 akorotkov@postgresql     2931                 :            154 :         iparam->opclassopts = NIL;
 2810 teodor@sigaev.ru         2932                 :            154 :         index->indexIncludingParams = lappend(index->indexIncludingParams, iparam);
                               2933                 :                :     }
                               2934                 :                : 
 6727 neilc@samurai.com        2935                 :           9989 :     return index;
                               2936                 :                : }
                               2937                 :                : 
                               2938                 :                : /*
                               2939                 :                :  * transformCheckConstraints
                               2940                 :                :  *      handle CHECK constraints
                               2941                 :                :  *
                               2942                 :                :  * Right now, there's nothing to do here when called from ALTER TABLE,
                               2943                 :                :  * but the other constraint-transformation functions are called in both
                               2944                 :                :  * the CREATE TABLE and ALTER TABLE paths, so do the same here, and just
                               2945                 :                :  * don't do anything if we're not authorized to skip validation.
                               2946                 :                :  */
                               2947                 :                : static void
 3653 rhaas@postgresql.org     2948                 :          32264 : transformCheckConstraints(CreateStmtContext *cxt, bool skipValidation)
                               2949                 :                : {
                               2950                 :                :     ListCell   *ckclist;
                               2951                 :                : 
                               2952         [ +  + ]:          32264 :     if (cxt->ckconstraints == NIL)
                               2953                 :          31349 :         return;
                               2954                 :                : 
                               2955                 :                :     /*
                               2956                 :                :      * When creating a new table (but not a foreign table), we can safely skip
                               2957                 :                :      * the validation of check constraints and mark them as valid based on the
                               2958                 :                :      * constraint enforcement flag, since NOT ENFORCED constraints must always
                               2959                 :                :      * be marked as NOT VALID. (This will override any user-supplied NOT VALID
                               2960                 :                :      * flag.)
                               2961                 :                :      */
                               2962         [ +  + ]:            915 :     if (skipValidation)
                               2963                 :                :     {
                               2964   [ +  -  +  +  :            796 :         foreach(ckclist, cxt->ckconstraints)
                                              +  + ]
                               2965                 :                :         {
                               2966                 :            433 :             Constraint *constraint = (Constraint *) lfirst(ckclist);
                               2967                 :                : 
                               2968                 :            433 :             constraint->skip_validation = true;
  339 peter@eisentraut.org     2969                 :            433 :             constraint->initially_valid = constraint->is_enforced;
                               2970                 :                :         }
                               2971                 :                :     }
                               2972                 :                : }
                               2973                 :                : 
                               2974                 :                : /*
                               2975                 :                :  * transformFKConstraints
                               2976                 :                :  *      handle FOREIGN KEY constraints
                               2977                 :                :  */
                               2978                 :                : static void
 5439 tgl@sss.pgh.pa.us        2979                 :          32264 : transformFKConstraints(CreateStmtContext *cxt,
                               2980                 :                :                        bool skipValidation, bool isAddConstraint)
                               2981                 :                : {
                               2982                 :                :     ListCell   *fkclist;
                               2983                 :                : 
 6751                          2984         [ +  + ]:          32264 :     if (cxt->fkconstraints == NIL)
                               2985                 :          30206 :         return;
                               2986                 :                : 
                               2987                 :                :     /*
                               2988                 :                :      * If CREATE TABLE or adding a column with NULL default, we can safely
                               2989                 :                :      * skip validation of FK constraints, and mark them as valid based on the
                               2990                 :                :      * constraint enforcement flag, since NOT ENFORCED constraints must always
                               2991                 :                :      * be marked as NOT VALID. (This will override any user-supplied NOT VALID
                               2992                 :                :      * flag.)
                               2993                 :                :      */
                               2994         [ +  + ]:           2058 :     if (skipValidation)
                               2995                 :                :     {
                               2996   [ +  -  +  +  :           1460 :         foreach(fkclist, cxt->fkconstraints)
                                              +  + ]
                               2997                 :                :         {
 5983                          2998                 :            747 :             Constraint *constraint = (Constraint *) lfirst(fkclist);
                               2999                 :                : 
                               3000                 :            747 :             constraint->skip_validation = true;
  258 peter@eisentraut.org     3001                 :            747 :             constraint->initially_valid = constraint->is_enforced;
                               3002                 :                :         }
                               3003                 :                :     }
                               3004                 :                : 
                               3005                 :                :     /*
                               3006                 :                :      * For CREATE TABLE or ALTER TABLE ADD COLUMN, gin up an ALTER TABLE ADD
                               3007                 :                :      * CONSTRAINT command to execute after the basic command is complete. (If
                               3008                 :                :      * called from ADD CONSTRAINT, that routine will add the FK constraints to
                               3009                 :                :      * its own subcommand list.)
                               3010                 :                :      *
                               3011                 :                :      * Note: the ADD CONSTRAINT command must also execute after any index
                               3012                 :                :      * creation commands.  Thus, this should run after
                               3013                 :                :      * transformIndexConstraints, so that the CREATE INDEX commands are
                               3014                 :                :      * already in cxt->alist.  See also the handling of cxt->likeclauses.
                               3015                 :                :      */
 6751 tgl@sss.pgh.pa.us        3016         [ +  + ]:           2058 :     if (!isAddConstraint)
                               3017                 :                :     {
                               3018                 :            710 :         AlterTableStmt *alterstmt = makeNode(AlterTableStmt);
                               3019                 :                : 
                               3020                 :            710 :         alterstmt->relation = cxt->relation;
                               3021                 :            710 :         alterstmt->cmds = NIL;
 1984 michael@paquier.xyz      3022                 :            710 :         alterstmt->objtype = OBJECT_TABLE;
                               3023                 :                : 
 6751 tgl@sss.pgh.pa.us        3024   [ +  -  +  +  :           1454 :         foreach(fkclist, cxt->fkconstraints)
                                              +  + ]
                               3025                 :                :         {
 5983                          3026                 :            744 :             Constraint *constraint = (Constraint *) lfirst(fkclist);
 6751                          3027                 :            744 :             AlterTableCmd *altercmd = makeNode(AlterTableCmd);
                               3028                 :                : 
 2162                          3029                 :            744 :             altercmd->subtype = AT_AddConstraint;
 6751                          3030                 :            744 :             altercmd->name = NULL;
 5983                          3031                 :            744 :             altercmd->def = (Node *) constraint;
 6751                          3032                 :            744 :             alterstmt->cmds = lappend(alterstmt->cmds, altercmd);
                               3033                 :                :         }
                               3034                 :                : 
                               3035                 :            710 :         cxt->alist = lappend(cxt->alist, alterstmt);
                               3036                 :                :     }
                               3037                 :                : }
                               3038                 :                : 
                               3039                 :                : /*
                               3040                 :                :  * transformIndexStmt - parse analysis for CREATE INDEX and ALTER TABLE
                               3041                 :                :  *
                               3042                 :                :  * Note: this is a no-op for an index not using either index expressions or
                               3043                 :                :  * a predicate expression.  There are several code paths that create indexes
                               3044                 :                :  * without bothering to call this, because they know they don't have any
                               3045                 :                :  * such expressions to deal with.
                               3046                 :                :  *
                               3047                 :                :  * To avoid race conditions, it's important that this function rely only on
                               3048                 :                :  * the passed-in relid (and not on stmt->relation) to determine the target
                               3049                 :                :  * relation.
                               3050                 :                :  */
                               3051                 :                : IndexStmt *
 4320 rhaas@postgresql.org     3052                 :          13345 : transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString)
                               3053                 :                : {
                               3054                 :                :     ParseState *pstate;
                               3055                 :                :     ParseNamespaceItem *nsitem;
                               3056                 :                :     ListCell   *l;
                               3057                 :                :     Relation    rel;
                               3058                 :                : 
                               3059                 :                :     /* Nothing to do if statement already transformed. */
 3950 tgl@sss.pgh.pa.us        3060         [ +  + ]:          13345 :     if (stmt->transformed)
                               3061                 :             65 :         return stmt;
                               3062                 :                : 
                               3063                 :                :     /* Set up pstate */
 6751                          3064                 :          13280 :     pstate = make_parsestate(NULL);
                               3065                 :          13280 :     pstate->p_sourcetext = queryString;
                               3066                 :                : 
                               3067                 :                :     /*
                               3068                 :                :      * Put the parent table into the rtable so that the expressions can refer
                               3069                 :                :      * to its fields without qualification.  Caller is responsible for locking
                               3070                 :                :      * relation, but we still need to open it.
                               3071                 :                :      */
 4320 rhaas@postgresql.org     3072                 :          13280 :     rel = relation_open(relid, NoLock);
 2175 tgl@sss.pgh.pa.us        3073                 :          13280 :     nsitem = addRangeTableEntryForRelation(pstate, rel,
                               3074                 :                :                                            AccessShareLock,
                               3075                 :                :                                            NULL, false, true);
                               3076                 :                : 
                               3077                 :                :     /* no to join list, yes to namespaces */
                               3078                 :          13280 :     addNSItemToQuery(pstate, nsitem, false, true, true);
                               3079                 :                : 
                               3080                 :                :     /* take care of the where clause */
 6751                          3081         [ +  + ]:          13280 :     if (stmt->whereClause)
                               3082                 :                :     {
                               3083                 :            201 :         stmt->whereClause = transformWhereClause(pstate,
                               3084                 :                :                                                  stmt->whereClause,
                               3085                 :                :                                                  EXPR_KIND_INDEX_PREDICATE,
                               3086                 :                :                                                  "WHERE");
                               3087                 :                :         /* we have to fix its collations too */
 5367                          3088                 :            201 :         assign_expr_collations(pstate, stmt->whereClause);
                               3089                 :                :     }
                               3090                 :                : 
                               3091                 :                :     /* take care of any index expressions */
 6751                          3092   [ +  -  +  +  :          31692 :     foreach(l, stmt->indexParams)
                                              +  + ]
                               3093                 :                :     {
                               3094                 :          18418 :         IndexElem  *ielem = (IndexElem *) lfirst(l);
                               3095                 :                : 
                               3096         [ +  + ]:          18418 :         if (ielem->expr)
                               3097                 :                :         {
                               3098                 :                :             /* Extract preliminary index col name before transforming expr */
 5837                          3099         [ +  - ]:            481 :             if (ielem->indexcolname == NULL)
                               3100                 :            481 :                 ielem->indexcolname = FigureIndexColname(ielem->expr);
                               3101                 :                : 
                               3102                 :                :             /* Now do parse transformation of the expression */
 4876                          3103                 :            481 :             ielem->expr = transformExpr(pstate, ielem->expr,
                               3104                 :                :                                         EXPR_KIND_INDEX_EXPRESSION);
                               3105                 :                : 
                               3106                 :                :             /* We have to fix its collations too */
 5386                          3107                 :            475 :             assign_expr_collations(pstate, ielem->expr);
                               3108                 :                : 
                               3109                 :                :             /*
                               3110                 :                :              * transformExpr() should have already rejected subqueries,
                               3111                 :                :              * aggregates, window functions, and SRFs, based on the EXPR_KIND_
                               3112                 :                :              * for an index expression.
                               3113                 :                :              *
                               3114                 :                :              * DefineIndex() will make more checks.
                               3115                 :                :              */
                               3116                 :                :         }
                               3117                 :                :     }
                               3118                 :                : 
                               3119                 :                :     /*
                               3120                 :                :      * Check that only the base rel is mentioned.  (This should be dead code
                               3121                 :                :      * now that add_missing_from is history.)
                               3122                 :                :      */
 6751                          3123         [ -  + ]:          13274 :     if (list_length(pstate->p_rtable) != 1)
 6751 tgl@sss.pgh.pa.us        3124         [ #  # ]:UBC           0 :         ereport(ERROR,
                               3125                 :                :                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
                               3126                 :                :                  errmsg("index expressions and predicates can refer only to the table being indexed")));
                               3127                 :                : 
 6751 tgl@sss.pgh.pa.us        3128                 :CBC       13274 :     free_parsestate(pstate);
                               3129                 :                : 
                               3130                 :                :     /* Close relation */
 2521 andres@anarazel.de       3131                 :          13274 :     table_close(rel, NoLock);
                               3132                 :                : 
                               3133                 :                :     /* Mark statement as successfully transformed */
 3950 tgl@sss.pgh.pa.us        3134                 :          13274 :     stmt->transformed = true;
                               3135                 :                : 
 6751                          3136                 :          13274 :     return stmt;
                               3137                 :                : }
                               3138                 :                : 
                               3139                 :                : /*
                               3140                 :                :  * transformStatsStmt - parse analysis for CREATE STATISTICS
                               3141                 :                :  *
                               3142                 :                :  * To avoid race conditions, it's important that this function relies only on
                               3143                 :                :  * the passed-in relid (and not on stmt->relation) to determine the target
                               3144                 :                :  * relation.
                               3145                 :                :  */
                               3146                 :                : CreateStatsStmt *
 1726 tomas.vondra@postgre     3147                 :            437 : transformStatsStmt(Oid relid, CreateStatsStmt *stmt, const char *queryString)
                               3148                 :                : {
                               3149                 :                :     ParseState *pstate;
                               3150                 :                :     ParseNamespaceItem *nsitem;
                               3151                 :                :     ListCell   *l;
                               3152                 :                :     Relation    rel;
                               3153                 :                : 
                               3154                 :                :     /* Nothing to do if statement already transformed. */
                               3155         [ +  + ]:            437 :     if (stmt->transformed)
                               3156                 :             24 :         return stmt;
                               3157                 :                : 
                               3158                 :                :     /* Set up pstate */
                               3159                 :            413 :     pstate = make_parsestate(NULL);
                               3160                 :            413 :     pstate->p_sourcetext = queryString;
                               3161                 :                : 
                               3162                 :                :     /*
                               3163                 :                :      * Put the parent table into the rtable so that the expressions can refer
                               3164                 :                :      * to its fields without qualification.  Caller is responsible for locking
                               3165                 :                :      * relation, but we still need to open it.
                               3166                 :                :      */
                               3167                 :            413 :     rel = relation_open(relid, NoLock);
                               3168                 :            413 :     nsitem = addRangeTableEntryForRelation(pstate, rel,
                               3169                 :                :                                            AccessShareLock,
                               3170                 :                :                                            NULL, false, true);
                               3171                 :                : 
                               3172                 :                :     /* no to join list, yes to namespaces */
                               3173                 :            413 :     addNSItemToQuery(pstate, nsitem, false, true, true);
                               3174                 :                : 
                               3175                 :                :     /* take care of any expressions */
                               3176   [ +  -  +  +  :           1420 :     foreach(l, stmt->exprs)
                                              +  + ]
                               3177                 :                :     {
                               3178                 :           1007 :         StatsElem  *selem = (StatsElem *) lfirst(l);
                               3179                 :                : 
                               3180         [ +  + ]:           1007 :         if (selem->expr)
                               3181                 :                :         {
                               3182                 :                :             /* Now do parse transformation of the expression */
                               3183                 :            255 :             selem->expr = transformExpr(pstate, selem->expr,
                               3184                 :                :                                         EXPR_KIND_STATS_EXPRESSION);
                               3185                 :                : 
                               3186                 :                :             /* We have to fix its collations too */
                               3187                 :            255 :             assign_expr_collations(pstate, selem->expr);
                               3188                 :                :         }
                               3189                 :                :     }
                               3190                 :                : 
                               3191                 :                :     /*
                               3192                 :                :      * Check that only the base rel is mentioned.  (This should be dead code
                               3193                 :                :      * now that add_missing_from is history.)
                               3194                 :                :      */
                               3195         [ -  + ]:            413 :     if (list_length(pstate->p_rtable) != 1)
 1726 tomas.vondra@postgre     3196         [ #  # ]:UBC           0 :         ereport(ERROR,
                               3197                 :                :                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
                               3198                 :                :                  errmsg("statistics expressions can refer only to the table being referenced")));
                               3199                 :                : 
 1726 tomas.vondra@postgre     3200                 :CBC         413 :     free_parsestate(pstate);
                               3201                 :                : 
                               3202                 :                :     /* Close relation */
                               3203                 :            413 :     table_close(rel, NoLock);
                               3204                 :                : 
                               3205                 :                :     /* Mark statement as successfully transformed */
                               3206                 :            413 :     stmt->transformed = true;
                               3207                 :                : 
                               3208                 :            413 :     return stmt;
                               3209                 :                : }
                               3210                 :                : 
                               3211                 :                : 
                               3212                 :                : /*
                               3213                 :                :  * transformRuleStmt -
                               3214                 :                :  *    transform a CREATE RULE Statement. The action is a list of parse
                               3215                 :                :  *    trees which is transformed into a list of query trees, and we also
                               3216                 :                :  *    transform the WHERE clause if any.
                               3217                 :                :  *
                               3218                 :                :  * actions and whereClause are output parameters that receive the
                               3219                 :                :  * transformed results.
                               3220                 :                :  */
                               3221                 :                : void
 6751 tgl@sss.pgh.pa.us        3222                 :            551 : transformRuleStmt(RuleStmt *stmt, const char *queryString,
                               3223                 :                :                   List **actions, Node **whereClause)
                               3224                 :                : {
                               3225                 :                :     Relation    rel;
                               3226                 :                :     ParseState *pstate;
                               3227                 :                :     ParseNamespaceItem *oldnsitem;
                               3228                 :                :     ParseNamespaceItem *newnsitem;
                               3229                 :                : 
                               3230                 :                :     /*
                               3231                 :                :      * To avoid deadlock, make sure the first thing we do is grab
                               3232                 :                :      * AccessExclusiveLock on the target relation.  This will be needed by
                               3233                 :                :      * DefineQueryRewrite(), and we don't want to grab a lesser lock
                               3234                 :                :      * beforehand.
                               3235                 :                :      */
 2521 andres@anarazel.de       3236                 :            551 :     rel = table_openrv(stmt->relation, AccessExclusiveLock);
                               3237                 :                : 
 4671 kgrittn@postgresql.o     3238         [ -  + ]:            551 :     if (rel->rd_rel->relkind == RELKIND_MATVIEW)
 4671 kgrittn@postgresql.o     3239         [ #  # ]:UBC           0 :         ereport(ERROR,
                               3240                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               3241                 :                :                  errmsg("rules on materialized views are not supported")));
                               3242                 :                : 
                               3243                 :                :     /* Set up pstate */
 6751 tgl@sss.pgh.pa.us        3244                 :CBC         551 :     pstate = make_parsestate(NULL);
                               3245                 :            551 :     pstate->p_sourcetext = queryString;
                               3246                 :                : 
                               3247                 :                :     /*
                               3248                 :                :      * NOTE: 'OLD' must always have a varno equal to 1 and 'NEW' equal to 2.
                               3249                 :                :      * Set up their ParseNamespaceItems in the main pstate for use in parsing
                               3250                 :                :      * the rule qualification.
                               3251                 :                :      */
 2175                          3252                 :            551 :     oldnsitem = addRangeTableEntryForRelation(pstate, rel,
                               3253                 :                :                                               AccessShareLock,
                               3254                 :                :                                               makeAlias("old", NIL),
                               3255                 :                :                                               false, false);
                               3256                 :            551 :     newnsitem = addRangeTableEntryForRelation(pstate, rel,
                               3257                 :                :                                               AccessShareLock,
                               3258                 :                :                                               makeAlias("new", NIL),
                               3259                 :                :                                               false, false);
                               3260                 :                : 
                               3261                 :                :     /*
                               3262                 :                :      * They must be in the namespace too for lookup purposes, but only add the
                               3263                 :                :      * one(s) that are relevant for the current kind of rule.  In an UPDATE
                               3264                 :                :      * rule, quals must refer to OLD.field or NEW.field to be unambiguous, but
                               3265                 :                :      * there's no need to be so picky for INSERT & DELETE.  We do not add them
                               3266                 :                :      * to the joinlist.
                               3267                 :                :      */
 6751                          3268   [ +  +  +  +  :            551 :     switch (stmt->event)
                                                 - ]
                               3269                 :                :     {
                               3270                 :              9 :         case CMD_SELECT:
 2175                          3271                 :              9 :             addNSItemToQuery(pstate, oldnsitem, false, true, true);
 6751                          3272                 :              9 :             break;
                               3273                 :            218 :         case CMD_UPDATE:
 2175                          3274                 :            218 :             addNSItemToQuery(pstate, oldnsitem, false, true, true);
                               3275                 :            218 :             addNSItemToQuery(pstate, newnsitem, false, true, true);
 6751                          3276                 :            218 :             break;
                               3277                 :            242 :         case CMD_INSERT:
 2175                          3278                 :            242 :             addNSItemToQuery(pstate, newnsitem, false, true, true);
 6751                          3279                 :            242 :             break;
                               3280                 :             82 :         case CMD_DELETE:
 2175                          3281                 :             82 :             addNSItemToQuery(pstate, oldnsitem, false, true, true);
 6751                          3282                 :             82 :             break;
 6751 tgl@sss.pgh.pa.us        3283                 :UBC           0 :         default:
                               3284         [ #  # ]:              0 :             elog(ERROR, "unrecognized event type: %d",
                               3285                 :                :                  (int) stmt->event);
                               3286                 :                :             break;
                               3287                 :                :     }
                               3288                 :                : 
                               3289                 :                :     /* take care of the where clause */
 6751 tgl@sss.pgh.pa.us        3290                 :CBC         551 :     *whereClause = transformWhereClause(pstate,
                               3291                 :                :                                         stmt->whereClause,
                               3292                 :                :                                         EXPR_KIND_WHERE,
                               3293                 :                :                                         "WHERE");
                               3294                 :                :     /* we have to fix its collations too */
 5367                          3295                 :            551 :     assign_expr_collations(pstate, *whereClause);
                               3296                 :                : 
                               3297                 :                :     /* this is probably dead code without add_missing_from: */
 3100                          3298         [ -  + ]:            551 :     if (list_length(pstate->p_rtable) != 2) /* naughty, naughty... */
 6751 tgl@sss.pgh.pa.us        3299         [ #  # ]:UBC           0 :         ereport(ERROR,
                               3300                 :                :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               3301                 :                :                  errmsg("rule WHERE condition cannot contain references to other relations")));
                               3302                 :                : 
                               3303                 :                :     /*
                               3304                 :                :      * 'instead nothing' rules with a qualification need a query rangetable so
                               3305                 :                :      * the rewrite handler can add the negated rule qualification to the
                               3306                 :                :      * original query. We create a query with the new command type CMD_NOTHING
                               3307                 :                :      * here that is treated specially by the rewrite system.
                               3308                 :                :      */
 6751 tgl@sss.pgh.pa.us        3309         [ +  + ]:CBC         551 :     if (stmt->actions == NIL)
                               3310                 :                :     {
                               3311                 :             82 :         Query      *nothing_qry = makeNode(Query);
                               3312                 :                : 
                               3313                 :             82 :         nothing_qry->commandType = CMD_NOTHING;
                               3314                 :             82 :         nothing_qry->rtable = pstate->p_rtable;
 1106 alvherre@alvh.no-ip.     3315                 :             82 :         nothing_qry->rteperminfos = pstate->p_rteperminfos;
 3100 tgl@sss.pgh.pa.us        3316                 :             82 :         nothing_qry->jointree = makeFromExpr(NIL, NULL); /* no join wanted */
                               3317                 :                : 
 6751                          3318                 :             82 :         *actions = list_make1(nothing_qry);
                               3319                 :                :     }
                               3320                 :                :     else
                               3321                 :                :     {
                               3322                 :                :         ListCell   *l;
                               3323                 :            469 :         List       *newactions = NIL;
                               3324                 :                : 
                               3325                 :                :         /*
                               3326                 :                :          * transform each statement, like parse_sub_analyze()
                               3327                 :                :          */
                               3328   [ +  -  +  +  :            952 :         foreach(l, stmt->actions)
                                              +  + ]
                               3329                 :                :         {
                               3330                 :            492 :             Node       *action = (Node *) lfirst(l);
                               3331                 :            492 :             ParseState *sub_pstate = make_parsestate(NULL);
                               3332                 :                :             Query      *sub_qry,
                               3333                 :                :                        *top_subqry;
                               3334                 :                :             bool        has_old,
                               3335                 :                :                         has_new;
                               3336                 :                : 
                               3337                 :                :             /*
                               3338                 :                :              * Since outer ParseState isn't parent of inner, have to pass down
                               3339                 :                :              * the query text by hand.
                               3340                 :                :              */
                               3341                 :            492 :             sub_pstate->p_sourcetext = queryString;
                               3342                 :                : 
                               3343                 :                :             /*
                               3344                 :                :              * Set up OLD/NEW in the rtable for this statement.  The entries
                               3345                 :                :              * are added only to relnamespace, not varnamespace, because we
                               3346                 :                :              * don't want them to be referred to by unqualified field names
                               3347                 :                :              * nor "*" in the rule actions.  We decide later whether to put
                               3348                 :                :              * them in the joinlist.
                               3349                 :                :              */
 2175                          3350                 :            492 :             oldnsitem = addRangeTableEntryForRelation(sub_pstate, rel,
                               3351                 :                :                                                       AccessShareLock,
                               3352                 :                :                                                       makeAlias("old", NIL),
                               3353                 :                :                                                       false, false);
                               3354                 :            492 :             newnsitem = addRangeTableEntryForRelation(sub_pstate, rel,
                               3355                 :                :                                                       AccessShareLock,
                               3356                 :                :                                                       makeAlias("new", NIL),
                               3357                 :                :                                                       false, false);
                               3358                 :            492 :             addNSItemToQuery(sub_pstate, oldnsitem, false, true, false);
                               3359                 :            492 :             addNSItemToQuery(sub_pstate, newnsitem, false, true, false);
                               3360                 :                : 
                               3361                 :                :             /* Transform the rule action statement */
 1642                          3362                 :            492 :             top_subqry = transformStmt(sub_pstate, action);
                               3363                 :                : 
                               3364                 :                :             /*
                               3365                 :                :              * We cannot support utility-statement actions (eg NOTIFY) with
                               3366                 :                :              * nonempty rule WHERE conditions, because there's no way to make
                               3367                 :                :              * the utility action execute conditionally.
                               3368                 :                :              */
 6751                          3369         [ +  + ]:            486 :             if (top_subqry->commandType == CMD_UTILITY &&
                               3370         [ -  + ]:             20 :                 *whereClause != NULL)
 6751 tgl@sss.pgh.pa.us        3371         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               3372                 :                :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               3373                 :                :                          errmsg("rules with WHERE conditions can only have SELECT, INSERT, UPDATE, or DELETE actions")));
                               3374                 :                : 
                               3375                 :                :             /*
                               3376                 :                :              * If the action is INSERT...SELECT, OLD/NEW have been pushed down
                               3377                 :                :              * into the SELECT, and that's what we need to look at. (Ugly
                               3378                 :                :              * kluge ... try to fix this when we redesign querytrees.)
                               3379                 :                :              */
 6751 tgl@sss.pgh.pa.us        3380                 :CBC         486 :             sub_qry = getInsertSelectQuery(top_subqry, NULL);
                               3381                 :                : 
                               3382                 :                :             /*
                               3383                 :                :              * If the sub_qry is a setop, we cannot attach any qualifications
                               3384                 :                :              * to it, because the planner won't notice them.  This could
                               3385                 :                :              * perhaps be relaxed someday, but for now, we may as well reject
                               3386                 :                :              * such a rule immediately.
                               3387                 :                :              */
                               3388   [ -  +  -  - ]:            486 :             if (sub_qry->setOperations != NULL && *whereClause != NULL)
 6751 tgl@sss.pgh.pa.us        3389         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               3390                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               3391                 :                :                          errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
                               3392                 :                : 
                               3393                 :                :             /*
                               3394                 :                :              * Validate action's use of OLD/NEW, qual too
                               3395                 :                :              */
 6751 tgl@sss.pgh.pa.us        3396                 :CBC         486 :             has_old =
                               3397   [ +  +  +  + ]:            777 :                 rangeTableEntry_used((Node *) sub_qry, PRS2_OLD_VARNO, 0) ||
                               3398                 :            291 :                 rangeTableEntry_used(*whereClause, PRS2_OLD_VARNO, 0);
                               3399                 :            486 :             has_new =
                               3400   [ +  +  +  + ]:            650 :                 rangeTableEntry_used((Node *) sub_qry, PRS2_NEW_VARNO, 0) ||
                               3401                 :            164 :                 rangeTableEntry_used(*whereClause, PRS2_NEW_VARNO, 0);
                               3402                 :                : 
                               3403   [ +  +  +  +  :            486 :             switch (stmt->event)
                                                 - ]
                               3404                 :                :             {
                               3405                 :              9 :                 case CMD_SELECT:
                               3406         [ -  + ]:              9 :                     if (has_old)
 6751 tgl@sss.pgh.pa.us        3407         [ #  # ]:UBC           0 :                         ereport(ERROR,
                               3408                 :                :                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               3409                 :                :                                  errmsg("ON SELECT rule cannot use OLD")));
 6751 tgl@sss.pgh.pa.us        3410         [ -  + ]:CBC           9 :                     if (has_new)
 6751 tgl@sss.pgh.pa.us        3411         [ #  # ]:UBC           0 :                         ereport(ERROR,
                               3412                 :                :                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               3413                 :                :                                  errmsg("ON SELECT rule cannot use NEW")));
 6751 tgl@sss.pgh.pa.us        3414                 :CBC           9 :                     break;
                               3415                 :            173 :                 case CMD_UPDATE:
                               3416                 :                :                     /* both are OK */
                               3417                 :            173 :                     break;
                               3418                 :            220 :                 case CMD_INSERT:
                               3419         [ -  + ]:            220 :                     if (has_old)
 6751 tgl@sss.pgh.pa.us        3420         [ #  # ]:UBC           0 :                         ereport(ERROR,
                               3421                 :                :                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               3422                 :                :                                  errmsg("ON INSERT rule cannot use OLD")));
 6751 tgl@sss.pgh.pa.us        3423                 :CBC         220 :                     break;
                               3424                 :             84 :                 case CMD_DELETE:
                               3425         [ -  + ]:             84 :                     if (has_new)
 6751 tgl@sss.pgh.pa.us        3426         [ #  # ]:UBC           0 :                         ereport(ERROR,
                               3427                 :                :                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               3428                 :                :                                  errmsg("ON DELETE rule cannot use NEW")));
 6751 tgl@sss.pgh.pa.us        3429                 :CBC          84 :                     break;
 6751 tgl@sss.pgh.pa.us        3430                 :UBC           0 :                 default:
                               3431         [ #  # ]:              0 :                     elog(ERROR, "unrecognized event type: %d",
                               3432                 :                :                          (int) stmt->event);
                               3433                 :                :                     break;
                               3434                 :                :             }
                               3435                 :                : 
                               3436                 :                :             /*
                               3437                 :                :              * OLD/NEW are not allowed in WITH queries, because they would
                               3438                 :                :              * amount to outer references for the WITH, which we disallow.
                               3439                 :                :              * However, they were already in the outer rangetable when we
                               3440                 :                :              * analyzed the query, so we have to check.
                               3441                 :                :              *
                               3442                 :                :              * Note that in the INSERT...SELECT case, we need to examine the
                               3443                 :                :              * CTE lists of both top_subqry and sub_qry.
                               3444                 :                :              *
                               3445                 :                :              * Note that we aren't digging into the body of the query looking
                               3446                 :                :              * for WITHs in nested sub-SELECTs.  A WITH down there can
                               3447                 :                :              * legitimately refer to OLD/NEW, because it'd be an
                               3448                 :                :              * indirect-correlated outer reference.
                               3449                 :                :              */
 5541 tgl@sss.pgh.pa.us        3450         [ +  + ]:CBC         486 :             if (rangeTableEntry_used((Node *) top_subqry->cteList,
                               3451         [ -  + ]:            483 :                                      PRS2_OLD_VARNO, 0) ||
                               3452                 :            483 :                 rangeTableEntry_used((Node *) sub_qry->cteList,
                               3453                 :                :                                      PRS2_OLD_VARNO, 0))
                               3454         [ +  - ]:              3 :                 ereport(ERROR,
                               3455                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               3456                 :                :                          errmsg("cannot refer to OLD within WITH query")));
                               3457         [ +  - ]:            483 :             if (rangeTableEntry_used((Node *) top_subqry->cteList,
                               3458         [ -  + ]:            483 :                                      PRS2_NEW_VARNO, 0) ||
                               3459                 :            483 :                 rangeTableEntry_used((Node *) sub_qry->cteList,
                               3460                 :                :                                      PRS2_NEW_VARNO, 0))
 5541 tgl@sss.pgh.pa.us        3461         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               3462                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               3463                 :                :                          errmsg("cannot refer to NEW within WITH query")));
                               3464                 :                : 
                               3465                 :                :             /*
                               3466                 :                :              * For efficiency's sake, add OLD to the rule action's jointree
                               3467                 :                :              * only if it was actually referenced in the statement or qual.
                               3468                 :                :              *
                               3469                 :                :              * For INSERT, NEW is not really a relation (only a reference to
                               3470                 :                :              * the to-be-inserted tuple) and should never be added to the
                               3471                 :                :              * jointree.
                               3472                 :                :              *
                               3473                 :                :              * For UPDATE, we treat NEW as being another kind of reference to
                               3474                 :                :              * OLD, because it represents references to *transformed* tuples
                               3475                 :                :              * of the existing relation.  It would be wrong to enter NEW
                               3476                 :                :              * separately in the jointree, since that would cause a double
                               3477                 :                :              * join of the updated relation.  It's also wrong to fail to make
                               3478                 :                :              * a jointree entry if only NEW and not OLD is mentioned.
                               3479                 :                :              */
 6751 tgl@sss.pgh.pa.us        3480   [ +  +  +  +  :CBC         483 :             if (has_old || (has_new && stmt->event == CMD_UPDATE))
                                              +  + ]
                               3481                 :                :             {
                               3482                 :                :                 RangeTblRef *rtr;
                               3483                 :                : 
                               3484                 :                :                 /*
                               3485                 :                :                  * If sub_qry is a setop, manipulating its jointree will do no
                               3486                 :                :                  * good at all, because the jointree is dummy. (This should be
                               3487                 :                :                  * a can't-happen case because of prior tests.)
                               3488                 :                :                  */
                               3489         [ -  + ]:            216 :                 if (sub_qry->setOperations != NULL)
 6751 tgl@sss.pgh.pa.us        3490         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               3491                 :                :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               3492                 :                :                              errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
                               3493                 :                :                 /* hackishly add OLD to the already-built FROM clause */
 2175 tgl@sss.pgh.pa.us        3494                 :CBC         216 :                 rtr = makeNode(RangeTblRef);
                               3495                 :            216 :                 rtr->rtindex = oldnsitem->p_rtindex;
                               3496                 :            216 :                 sub_qry->jointree->fromlist =
                               3497                 :            216 :                     lappend(sub_qry->jointree->fromlist, rtr);
                               3498                 :                :             }
                               3499                 :                : 
 6751                          3500                 :            483 :             newactions = lappend(newactions, top_subqry);
                               3501                 :                : 
                               3502                 :            483 :             free_parsestate(sub_pstate);
                               3503                 :                :         }
                               3504                 :                : 
                               3505                 :            460 :         *actions = newactions;
                               3506                 :                :     }
                               3507                 :                : 
                               3508                 :            542 :     free_parsestate(pstate);
                               3509                 :                : 
                               3510                 :                :     /* Close relation, but keep the exclusive lock */
 2521 andres@anarazel.de       3511                 :            542 :     table_close(rel, NoLock);
 6751 tgl@sss.pgh.pa.us        3512                 :            542 : }
                               3513                 :                : 
                               3514                 :                : 
                               3515                 :                : /*
                               3516                 :                :  * checkPartition
                               3517                 :                :  * Check whether partRelOid is a leaf partition of the parent table (rel).
                               3518                 :                :  * isMerge: true indicates the operation is "ALTER TABLE ... MERGE PARTITIONS";
                               3519                 :                :  * false indicates the operation is "ALTER TABLE ... SPLIT PARTITION".
                               3520                 :                :  */
                               3521                 :                : static void
    2 akorotkov@postgresql     3522                 :GNC         444 : checkPartition(Relation rel, Oid partRelOid, bool isMerge)
                               3523                 :                : {
                               3524                 :                :     Relation    partRel;
                               3525                 :                : 
                               3526                 :            444 :     partRel = table_open(partRelOid, NoLock);
                               3527                 :                : 
                               3528         [ +  + ]:            444 :     if (partRel->rd_rel->relkind != RELKIND_RELATION)
                               3529   [ +  -  +  - ]:              3 :         ereport(ERROR,
                               3530                 :                :                 errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               3531                 :                :                 errmsg("\"%s\" is not a table", RelationGetRelationName(partRel)),
                               3532                 :                :                 isMerge
                               3533                 :                :                 ? errhint("ALTER TABLE ... MERGE PARTITIONS can only merge partitions don't have sub-partitions")
                               3534                 :                :                 : errhint("ALTER TABLE ... SPLIT PARTITION can only split partitions don't have sub-partitions"));
                               3535                 :                : 
                               3536         [ +  + ]:            441 :     if (!partRel->rd_rel->relispartition)
                               3537   [ +  -  +  - ]:              9 :         ereport(ERROR,
                               3538                 :                :                 errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               3539                 :                :                 errmsg("\"%s\" is not a partition of partitioned table \"%s\"",
                               3540                 :                :                        RelationGetRelationName(partRel), RelationGetRelationName(rel)),
                               3541                 :                :                 isMerge
                               3542                 :                :                 ? errhint("ALTER TABLE ... MERGE PARTITIONS can only merge partitions don't have sub-partitions")
                               3543                 :                :                 : errhint("ALTER TABLE ... SPLIT PARTITION can only split partitions don't have sub-partitions"));
                               3544                 :                : 
                               3545         [ +  + ]:            432 :     if (get_partition_parent(partRelOid, false) != RelationGetRelid(rel))
                               3546   [ +  -  +  + ]:              9 :         ereport(ERROR,
                               3547                 :                :                 errcode(ERRCODE_UNDEFINED_TABLE),
                               3548                 :                :                 errmsg("relation \"%s\" is not a partition of relation \"%s\"",
                               3549                 :                :                        RelationGetRelationName(partRel), RelationGetRelationName(rel)),
                               3550                 :                :                 isMerge
                               3551                 :                :                 ? errhint("ALTER TABLE ... MERGE PARTITIONS can only merge partitions don't have sub-partitions")
                               3552                 :                :                 : errhint("ALTER TABLE ... SPLIT PARTITION can only split partitions don't have sub-partitions"));
                               3553                 :                : 
                               3554                 :            423 :     table_close(partRel, NoLock);
                               3555                 :            423 : }
                               3556                 :                : 
                               3557                 :                : /*
                               3558                 :                :  * transformPartitionCmdForSplit -
                               3559                 :                :  *      analyze the ALTER TABLE ... SPLIT PARTITION command
                               3560                 :                :  *
                               3561                 :                :  * For each new partition, sps->bound is set to the transformed value of bound.
                               3562                 :                :  * Does checks for bounds of new partitions.
                               3563                 :                :  */
                               3564                 :                : static void
                               3565                 :            183 : transformPartitionCmdForSplit(CreateStmtContext *cxt, PartitionCmd *partcmd)
                               3566                 :                : {
                               3567                 :            183 :     Relation    parent = cxt->rel;
                               3568                 :                :     PartitionKey key;
                               3569                 :                :     char        strategy;
                               3570                 :                :     Oid         splitPartOid;
                               3571                 :                :     Oid         defaultPartOid;
                               3572                 :            183 :     int         default_index = -1;
                               3573                 :                :     bool        isSplitPartDefault;
                               3574                 :                :     ListCell   *listptr,
                               3575                 :                :                *listptr2;
                               3576                 :                :     List       *splitlist;
                               3577                 :                : 
                               3578                 :            183 :     splitlist = partcmd->partlist;
                               3579                 :            183 :     key = RelationGetPartitionKey(parent);
                               3580                 :            183 :     strategy = get_partition_strategy(key);
                               3581                 :            183 :     defaultPartOid = get_default_oid_from_partdesc(RelationGetPartitionDesc(parent, true));
                               3582                 :                : 
                               3583                 :                :     /* Transform partition bounds for all partitions in the list: */
                               3584   [ +  -  +  +  :            885 :     foreach_node(SinglePartitionSpec, sps, splitlist)
                                              +  + ]
                               3585                 :                :     {
                               3586                 :            525 :         cxt->partbound = NULL;
                               3587                 :            525 :         transformPartitionCmd(cxt, sps->bound);
                               3588                 :                :         /* Assign the transformed value of the partition bound. */
                               3589                 :            522 :         sps->bound = cxt->partbound;
                               3590                 :                :     }
                               3591                 :                : 
                               3592                 :                :     /*
                               3593                 :                :      * Open and lock the partition, check ownership along the way. We need to
                               3594                 :                :      * use AccessExclusiveLock here because this split partition will be
                               3595                 :                :      * detached, then dropped in ATExecSplitPartition.
                               3596                 :                :      */
                               3597                 :            180 :     splitPartOid = RangeVarGetRelidExtended(partcmd->name, AccessExclusiveLock,
                               3598                 :                :                                             0, RangeVarCallbackOwnsRelation,
                               3599                 :                :                                             NULL);
                               3600                 :                : 
                               3601                 :            174 :     checkPartition(parent, splitPartOid, false);
                               3602                 :                : 
                               3603      [ +  +  - ]:            171 :     switch (strategy)
                               3604                 :                :     {
                               3605                 :            168 :         case PARTITION_STRATEGY_LIST:
                               3606                 :                :         case PARTITION_STRATEGY_RANGE:
                               3607                 :                :             {
                               3608   [ +  -  +  +  :            825 :                 foreach_node(SinglePartitionSpec, sps, splitlist)
                                              +  + ]
                               3609                 :                :                 {
                               3610         [ +  + ]:            495 :                     if (sps->bound->is_default)
                               3611                 :                :                     {
                               3612         [ +  + ]:             42 :                         if (default_index != -1)
                               3613         [ +  - ]:              3 :                             ereport(ERROR,
                               3614                 :                :                                     errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               3615                 :                :                                     errmsg("DEFAULT partition should be one"),
                               3616                 :                :                                     parser_errposition(cxt->pstate, sps->name->location));
                               3617                 :                : 
                               3618                 :             39 :                         default_index = foreach_current_index(sps);
                               3619                 :                :                     }
                               3620                 :                :                 }
                               3621                 :                :             }
                               3622                 :            165 :             break;
                               3623                 :                : 
                               3624                 :              3 :         case PARTITION_STRATEGY_HASH:
                               3625         [ +  - ]:              3 :             ereport(ERROR,
                               3626                 :                :                     errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               3627                 :                :                     errmsg("partition of hash-partitioned table cannot be split"));
                               3628                 :                :             break;
                               3629                 :                : 
    2 akorotkov@postgresql     3630                 :UNC           0 :         default:
                               3631         [ #  # ]:              0 :             elog(ERROR, "unexpected partition strategy: %d",
                               3632                 :                :                  (int) key->strategy);
                               3633                 :                :             break;
                               3634                 :                :     }
                               3635                 :                : 
                               3636                 :                :     /* isSplitPartDefault: is the being split partition a DEFAULT partition? */
    2 akorotkov@postgresql     3637                 :GNC         165 :     isSplitPartDefault = (defaultPartOid == splitPartOid);
                               3638                 :                : 
                               3639   [ +  +  +  + ]:            165 :     if (isSplitPartDefault && default_index == -1)
                               3640         [ +  - ]:              3 :         ereport(ERROR,
                               3641                 :                :                 errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               3642                 :                :                 errmsg("can not split DEFAULT partition \"%s\"",
                               3643                 :                :                        get_rel_name(splitPartOid)),
                               3644                 :                :                 errhint("To split DEFAULT partition one of the new partition msut be DEFAULT"),
                               3645                 :                :                 parser_errposition(cxt->pstate, ((SinglePartitionSpec *) linitial(splitlist))->name->location));
                               3646                 :                : 
                               3647                 :                :     /*
                               3648                 :                :      * If the partition being split is not the DEFAULT partition, but the
                               3649                 :                :      * DEFAULT partition exists, then none of the resulting split partitions
                               3650                 :                :      * can be the DEFAULT.
                               3651                 :                :      */
                               3652   [ +  +  +  +  :            162 :     if (!isSplitPartDefault && (default_index != -1) && OidIsValid(defaultPartOid))
                                              +  + ]
                               3653                 :                :     {
                               3654                 :                :         SinglePartitionSpec *spsDef =
                               3655                 :              3 :             (SinglePartitionSpec *) list_nth(splitlist, default_index);
                               3656                 :                : 
                               3657         [ +  - ]:              3 :         ereport(ERROR,
                               3658                 :                :                 errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               3659                 :                :                 errmsg("can not split non-DEFAULT partition \"%s\"",
                               3660                 :                :                        get_rel_name(splitPartOid)),
                               3661                 :                :                 errmsg("new partition cannot be DEFAULT because DEFAULT partition \"%s\" already exists",
                               3662                 :                :                        get_rel_name(defaultPartOid)),
                               3663                 :                :                 parser_errposition(cxt->pstate, spsDef->name->location));
                               3664                 :                :     }
                               3665                 :                : 
                               3666   [ +  -  +  +  :            591 :     foreach(listptr, splitlist)
                                              +  + ]
                               3667                 :                :     {
                               3668                 :                :         Oid         nspid;
                               3669                 :            441 :         SinglePartitionSpec *sps = (SinglePartitionSpec *) lfirst(listptr);
                               3670                 :            441 :         RangeVar   *name = sps->name;
                               3671                 :                : 
                               3672                 :            441 :         nspid = RangeVarGetCreationNamespace(sps->name);
                               3673                 :                : 
                               3674                 :                :         /* Partitions in the list should have different names. */
                               3675   [ +  -  +  +  :            894 :         for_each_cell(listptr2, splitlist, lnext(splitlist, listptr))
                                              +  + ]
                               3676                 :                :         {
                               3677                 :                :             Oid         nspid2;
                               3678                 :            462 :             SinglePartitionSpec *sps2 = (SinglePartitionSpec *) lfirst(listptr2);
                               3679                 :            462 :             RangeVar   *name2 = sps2->name;
                               3680                 :                : 
                               3681         [ +  + ]:            462 :             if (equal(name, name2))
                               3682         [ +  - ]:              6 :                 ereport(ERROR,
                               3683                 :                :                         errcode(ERRCODE_DUPLICATE_TABLE),
                               3684                 :                :                         errmsg("partition with name \"%s\" is already used", name->relname),
                               3685                 :                :                         parser_errposition(cxt->pstate, name2->location));
                               3686                 :                : 
                               3687                 :            456 :             nspid2 = RangeVarGetCreationNamespace(sps2->name);
                               3688                 :                : 
                               3689   [ +  +  +  + ]:            456 :             if (nspid2 == nspid && strcmp(name->relname, name2->relname) == 0)
                               3690         [ +  - ]:              3 :                 ereport(ERROR,
                               3691                 :                :                         errcode(ERRCODE_DUPLICATE_TABLE),
                               3692                 :                :                         errmsg("partition with name \"%s\" is already used", name->relname),
                               3693                 :                :                         parser_errposition(cxt->pstate, name2->location));
                               3694                 :                :         }
                               3695                 :                :     }
                               3696                 :                : 
                               3697                 :                :     /* Then we should check partitions with transformed bounds. */
                               3698                 :            150 :     check_partitions_for_split(parent, splitPartOid, splitlist, cxt->pstate);
                               3699                 :             99 : }
                               3700                 :                : 
                               3701                 :                : 
                               3702                 :                : /*
                               3703                 :                :  * transformPartitionCmdForMerge -
                               3704                 :                :  *      analyze the ALTER TABLE ... MERGE PARTITIONS command
                               3705                 :                :  *
                               3706                 :                :  * Does simple checks for merged partitions. Calculates bound of the resulting
                               3707                 :                :  * partition.
                               3708                 :                :  */
                               3709                 :                : static void
                               3710                 :            129 : transformPartitionCmdForMerge(CreateStmtContext *cxt, PartitionCmd *partcmd)
                               3711                 :                : {
                               3712                 :                :     Oid         defaultPartOid;
                               3713                 :                :     Oid         partOid;
                               3714                 :            129 :     Relation    parent = cxt->rel;
                               3715                 :                :     PartitionKey key;
                               3716                 :                :     char        strategy;
                               3717                 :                :     ListCell   *listptr,
                               3718                 :                :                *listptr2;
                               3719                 :            129 :     bool        isDefaultPart = false;
                               3720                 :            129 :     List       *partOids = NIL;
                               3721                 :                : 
                               3722                 :            129 :     key = RelationGetPartitionKey(parent);
                               3723                 :            129 :     strategy = get_partition_strategy(key);
                               3724                 :                : 
                               3725         [ +  + ]:            129 :     if (strategy == PARTITION_STRATEGY_HASH)
                               3726         [ +  - ]:              3 :         ereport(ERROR,
                               3727                 :                :                 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               3728                 :                :                 errmsg("partition of hash-partitioned table cannot be merged"));
                               3729                 :                : 
                               3730                 :                :     /* Does the partitioned table (parent) have a default partition? */
                               3731                 :            126 :     defaultPartOid = get_default_oid_from_partdesc(RelationGetPartitionDesc(parent, true));
                               3732                 :                : 
                               3733   [ +  -  +  +  :            378 :     foreach(listptr, partcmd->partlist)
                                              +  + ]
                               3734                 :                :     {
                               3735                 :            282 :         RangeVar   *name = (RangeVar *) lfirst(listptr);
                               3736                 :                : 
                               3737                 :                :         /* Partitions in the list should have different names. */
                               3738   [ +  -  +  +  :            492 :         for_each_cell(listptr2, partcmd->partlist, lnext(partcmd->partlist, listptr))
                                              +  + ]
                               3739                 :                :         {
                               3740                 :            213 :             RangeVar   *name2 = (RangeVar *) lfirst(listptr2);
                               3741                 :                : 
                               3742         [ +  + ]:            213 :             if (equal(name, name2))
                               3743         [ +  - ]:              3 :                 ereport(ERROR,
                               3744                 :                :                         errcode(ERRCODE_DUPLICATE_TABLE),
                               3745                 :                :                         errmsg("partition with name \"%s\" is already used", name->relname),
                               3746                 :                :                         parser_errposition(cxt->pstate, name2->location));
                               3747                 :                :         }
                               3748                 :                : 
                               3749                 :                :         /*
                               3750                 :                :          * Search the DEFAULT partition in the list. Open and lock partitions
                               3751                 :                :          * before calculating the boundary for resulting partition, we also
                               3752                 :                :          * check for ownership along the way.  We need to use
                               3753                 :                :          * AccessExclusiveLock here, because these merged partitions will be
                               3754                 :                :          * detached and then dropped in ATExecMergePartitions.
                               3755                 :                :          */
                               3756                 :            279 :         partOid = RangeVarGetRelidExtended(name, AccessExclusiveLock, 0,
                               3757                 :                :                                            RangeVarCallbackOwnsRelation,
                               3758                 :                :                                            NULL);
                               3759                 :                :         /* Is the current partition a DEFAULT partition? */
                               3760         [ +  + ]:            273 :         if (partOid == defaultPartOid)
                               3761                 :              3 :             isDefaultPart = true;
                               3762                 :                : 
                               3763                 :                :         /*
                               3764                 :                :          * Extended check because the same partition can have different names
                               3765                 :                :          * (for example, "part_name" and "public.part_name").
                               3766                 :                :          */
                               3767   [ +  +  +  +  :            459 :         foreach(listptr2, partOids)
                                              +  + ]
                               3768                 :                :         {
                               3769                 :            189 :             Oid         curOid = lfirst_oid(listptr2);
                               3770                 :                : 
                               3771         [ +  + ]:            189 :             if (curOid == partOid)
                               3772         [ +  - ]:              3 :                 ereport(ERROR,
                               3773                 :                :                         errcode(ERRCODE_DUPLICATE_TABLE),
                               3774                 :                :                         errmsg("partition with name \"%s\" is already used", name->relname),
                               3775                 :                :                         parser_errposition(cxt->pstate, name->location));
                               3776                 :                :         }
                               3777                 :                : 
                               3778                 :            270 :         checkPartition(parent, partOid, true);
                               3779                 :                : 
                               3780                 :            252 :         partOids = lappend_oid(partOids, partOid);
                               3781                 :                :     }
                               3782                 :                : 
                               3783                 :                :     /* Allocate the bound of the resulting partition. */
                               3784         [ -  + ]:             96 :     Assert(partcmd->bound == NULL);
                               3785                 :             96 :     partcmd->bound = makeNode(PartitionBoundSpec);
                               3786                 :                : 
                               3787                 :                :     /* Fill the partition bound. */
                               3788                 :             96 :     partcmd->bound->strategy = strategy;
                               3789                 :             96 :     partcmd->bound->location = -1;
                               3790                 :             96 :     partcmd->bound->is_default = isDefaultPart;
                               3791         [ +  + ]:             96 :     if (!isDefaultPart)
                               3792                 :             93 :         calculate_partition_bound_for_merge(parent, partcmd->partlist,
                               3793                 :                :                                             partOids, partcmd->bound,
                               3794                 :                :                                             cxt->pstate);
                               3795                 :             90 : }
                               3796                 :                : 
                               3797                 :                : /*
                               3798                 :                :  * transformAlterTableStmt -
                               3799                 :                :  *      parse analysis for ALTER TABLE
                               3800                 :                :  *
                               3801                 :                :  * Returns the transformed AlterTableStmt.  There may be additional actions
                               3802                 :                :  * to be done before and after the transformed statement, which are returned
                               3803                 :                :  * in *beforeStmts and *afterStmts as lists of utility command parsetrees.
                               3804                 :                :  *
                               3805                 :                :  * To avoid race conditions, it's important that this function rely only on
                               3806                 :                :  * the passed-in relid (and not on stmt->relation) to determine the target
                               3807                 :                :  * relation.
                               3808                 :                :  */
                               3809                 :                : AlterTableStmt *
 4320 rhaas@postgresql.org     3810                 :CBC       12416 : transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
                               3811                 :                :                         const char *queryString,
                               3812                 :                :                         List **beforeStmts, List **afterStmts)
                               3813                 :                : {
                               3814                 :                :     Relation    rel;
                               3815                 :                :     TupleDesc   tupdesc;
                               3816                 :                :     ParseState *pstate;
                               3817                 :                :     CreateStmtContext cxt;
                               3818                 :                :     List       *save_alist;
                               3819                 :                :     ListCell   *lcmd,
                               3820                 :                :                *l;
 6751 tgl@sss.pgh.pa.us        3821                 :          12416 :     List       *newcmds = NIL;
                               3822                 :          12416 :     bool        skipValidation = true;
                               3823                 :                :     AlterTableCmd *newcmd;
                               3824                 :                :     ParseNamespaceItem *nsitem;
                               3825                 :                : 
                               3826                 :                :     /* Caller is responsible for locking the relation */
 4320 rhaas@postgresql.org     3827                 :          12416 :     rel = relation_open(relid, NoLock);
 2611 peter_e@gmx.net          3828                 :          12416 :     tupdesc = RelationGetDescr(rel);
                               3829                 :                : 
                               3830                 :                :     /* Set up pstate */
 6751 tgl@sss.pgh.pa.us        3831                 :          12416 :     pstate = make_parsestate(NULL);
                               3832                 :          12416 :     pstate->p_sourcetext = queryString;
 2175                          3833                 :          12416 :     nsitem = addRangeTableEntryForRelation(pstate,
                               3834                 :                :                                            rel,
                               3835                 :                :                                            AccessShareLock,
                               3836                 :                :                                            NULL,
                               3837                 :                :                                            false,
                               3838                 :                :                                            true);
                               3839                 :          12416 :     addNSItemToQuery(pstate, nsitem, false, true, true);
                               3840                 :                : 
                               3841                 :                :     /* Set up CreateStmtContext */
 5439                          3842                 :          12416 :     cxt.pstate = pstate;
 2162                          3843         [ +  + ]:          12416 :     if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
                               3844                 :                :     {
 4662                          3845                 :             90 :         cxt.stmtType = "ALTER FOREIGN TABLE";
                               3846                 :             90 :         cxt.isforeign = true;
                               3847                 :                :     }
                               3848                 :                :     else
                               3849                 :                :     {
                               3850                 :          12326 :         cxt.stmtType = "ALTER TABLE";
                               3851                 :          12326 :         cxt.isforeign = false;
                               3852                 :                :     }
 6751                          3853                 :          12416 :     cxt.relation = stmt->relation;
                               3854                 :          12416 :     cxt.rel = rel;
                               3855                 :          12416 :     cxt.inhRelations = NIL;
                               3856                 :          12416 :     cxt.isalter = true;
                               3857                 :          12416 :     cxt.columns = NIL;
                               3858                 :          12416 :     cxt.ckconstraints = NIL;
  403 alvherre@alvh.no-ip.     3859                 :          12416 :     cxt.nnconstraints = NIL;
 6751 tgl@sss.pgh.pa.us        3860                 :          12416 :     cxt.fkconstraints = NIL;
                               3861                 :          12416 :     cxt.ixconstraints = NIL;
 1853                          3862                 :          12416 :     cxt.likeclauses = NIL;
 6751                          3863                 :          12416 :     cxt.blist = NIL;
                               3864                 :          12416 :     cxt.alist = NIL;
                               3865                 :          12416 :     cxt.pkey = NULL;
 3296 rhaas@postgresql.org     3866                 :          12416 :     cxt.ispartitioned = (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
                               3867                 :          12416 :     cxt.partbound = NULL;
 2930 peter_e@gmx.net          3868                 :          12416 :     cxt.ofType = false;
                               3869                 :                : 
                               3870                 :                :     /*
                               3871                 :                :      * Transform ALTER subcommands that need it (most don't).  These largely
                               3872                 :                :      * re-use code from CREATE TABLE.
                               3873                 :                :      */
 6751 tgl@sss.pgh.pa.us        3874   [ +  -  +  +  :          24679 :     foreach(lcmd, stmt->cmds)
                                              +  + ]
                               3875                 :                :     {
                               3876                 :          12416 :         AlterTableCmd *cmd = (AlterTableCmd *) lfirst(lcmd);
                               3877                 :                : 
                               3878   [ +  +  +  +  :          12416 :         switch (cmd->subtype)
                                        +  +  +  +  
                                                 - ]
                               3879                 :                :         {
                               3880                 :           1037 :             case AT_AddColumn:
                               3881                 :                :                 {
 3220 peter_e@gmx.net          3882                 :           1037 :                     ColumnDef  *def = castNode(ColumnDef, cmd->def);
                               3883                 :                : 
 5439 tgl@sss.pgh.pa.us        3884                 :           1037 :                     transformColumnDefinition(&cxt, def);
                               3885                 :                : 
                               3886                 :                :                     /*
                               3887                 :                :                      * If the column has a non-null default, we can't skip
                               3888                 :                :                      * validation of foreign keys.
                               3889                 :                :                      */
 6445                          3890         [ +  + ]:           1034 :                     if (def->raw_default != NULL)
 6751                          3891                 :            429 :                         skipValidation = false;
                               3892                 :                : 
                               3893                 :                :                     /*
                               3894                 :                :                      * All constraints are processed in other ways. Remove the
                               3895                 :                :                      * original list
                               3896                 :                :                      */
                               3897                 :           1034 :                     def->constraints = NIL;
                               3898                 :                : 
 6445                          3899                 :           1034 :                     newcmds = lappend(newcmds, cmd);
 6751                          3900                 :           1034 :                     break;
                               3901                 :                :                 }
                               3902                 :                : 
                               3903                 :           8469 :             case AT_AddConstraint:
                               3904                 :                : 
                               3905                 :                :                 /*
                               3906                 :                :                  * The original AddConstraint cmd node doesn't go to newcmds
                               3907                 :                :                  */
                               3908         [ +  - ]:           8469 :                 if (IsA(cmd->def, Constraint))
                               3909                 :                :                 {
 5439                          3910                 :           8469 :                     transformTableConstraint(&cxt, (Constraint *) cmd->def);
 5983                          3911         [ +  + ]:           8466 :                     if (((Constraint *) cmd->def)->contype == CONSTR_FOREIGN)
                               3912                 :           1345 :                         skipValidation = false;
                               3913                 :                :                 }
                               3914                 :                :                 else
 6751 tgl@sss.pgh.pa.us        3915         [ #  # ]:UBC           0 :                     elog(ERROR, "unrecognized node type: %d",
                               3916                 :                :                          (int) nodeTag(cmd->def));
 6751 tgl@sss.pgh.pa.us        3917                 :CBC        8466 :                 break;
                               3918                 :                : 
 3910 alvherre@alvh.no-ip.     3919                 :            714 :             case AT_AlterColumnType:
                               3920                 :                :                 {
 2162 tgl@sss.pgh.pa.us        3921                 :            714 :                     ColumnDef  *def = castNode(ColumnDef, cmd->def);
                               3922                 :                :                     AttrNumber  attnum;
                               3923                 :                : 
                               3924                 :                :                     /*
                               3925                 :                :                      * For ALTER COLUMN TYPE, transform the USING clause if
                               3926                 :                :                      * one was specified.
                               3927                 :                :                      */
 3910 alvherre@alvh.no-ip.     3928         [ +  + ]:            714 :                     if (def->raw_default)
                               3929                 :                :                     {
                               3930                 :            126 :                         def->cooked_default =
                               3931                 :            126 :                             transformExpr(pstate, def->raw_default,
                               3932                 :                :                                           EXPR_KIND_ALTER_COL_TRANSFORM);
                               3933                 :                :                     }
                               3934                 :                : 
                               3935                 :                :                     /*
                               3936                 :                :                      * For identity column, create ALTER SEQUENCE command to
                               3937                 :                :                      * change the data type of the sequence. Identity sequence
                               3938                 :                :                      * is associated with the top level partitioned table.
                               3939                 :                :                      * Hence ignore partitions.
                               3940                 :                :                      */
  588 peter@eisentraut.org     3941         [ +  + ]:            714 :                     if (!RelationGetForm(rel)->relispartition)
                               3942                 :                :                     {
                               3943                 :            663 :                         attnum = get_attnum(relid, cmd->name);
                               3944         [ -  + ]:            663 :                         if (attnum == InvalidAttrNumber)
  588 peter@eisentraut.org     3945         [ #  # ]:UBC           0 :                             ereport(ERROR,
                               3946                 :                :                                     (errcode(ERRCODE_UNDEFINED_COLUMN),
                               3947                 :                :                                      errmsg("column \"%s\" of relation \"%s\" does not exist",
                               3948                 :                :                                             cmd->name, RelationGetRelationName(rel))));
                               3949                 :                : 
  588 peter@eisentraut.org     3950         [ +  + ]:CBC         663 :                         if (attnum > 0 &&
                               3951         [ +  + ]:            660 :                             TupleDescAttr(tupdesc, attnum - 1)->attidentity)
                               3952                 :                :                         {
                               3953                 :             18 :                             Oid         seq_relid = getIdentitySequence(rel, attnum, false);
                               3954                 :             18 :                             Oid         typeOid = typenameTypeId(pstate, def->typeName);
                               3955                 :             18 :                             AlterSeqStmt *altseqstmt = makeNode(AlterSeqStmt);
                               3956                 :                : 
                               3957                 :                :                             altseqstmt->sequence
                               3958                 :             18 :                                 = makeRangeVar(get_namespace_name(get_rel_namespace(seq_relid)),
                               3959                 :                :                                                get_rel_name(seq_relid),
                               3960                 :                :                                                -1);
                               3961                 :             18 :                             altseqstmt->options = list_make1(makeDefElem("as",
                               3962                 :                :                                                                          (Node *) makeTypeNameFromOid(typeOid, -1),
                               3963                 :                :                                                                          -1));
                               3964                 :             18 :                             altseqstmt->for_identity = true;
                               3965                 :             18 :                             cxt.blist = lappend(cxt.blist, altseqstmt);
                               3966                 :                :                         }
                               3967                 :                :                     }
                               3968                 :                : 
 3176 peter_e@gmx.net          3969                 :            714 :                     newcmds = lappend(newcmds, cmd);
                               3970                 :            714 :                     break;
                               3971                 :                :                 }
                               3972                 :                : 
                               3973                 :             86 :             case AT_AddIdentity:
                               3974                 :                :                 {
 3135 bruce@momjian.us         3975                 :             86 :                     Constraint *def = castNode(Constraint, cmd->def);
                               3976                 :             86 :                     ColumnDef  *newdef = makeNode(ColumnDef);
                               3977                 :                :                     AttrNumber  attnum;
                               3978                 :                : 
 3176 peter_e@gmx.net          3979                 :             86 :                     newdef->colname = cmd->name;
                               3980                 :             86 :                     newdef->identity = def->generated_when;
                               3981                 :             86 :                     cmd->def = (Node *) newdef;
                               3982                 :                : 
                               3983                 :             86 :                     attnum = get_attnum(relid, cmd->name);
 2162 tgl@sss.pgh.pa.us        3984         [ +  + ]:             86 :                     if (attnum == InvalidAttrNumber)
                               3985         [ +  - ]:              3 :                         ereport(ERROR,
                               3986                 :                :                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
                               3987                 :                :                                  errmsg("column \"%s\" of relation \"%s\" does not exist",
                               3988                 :                :                                         cmd->name, RelationGetRelationName(rel))));
                               3989                 :                : 
                               3990                 :             83 :                     generateSerialExtraStmts(&cxt, newdef,
                               3991                 :                :                                              get_atttype(relid, attnum),
                               3992                 :                :                                              def->options, true, true,
                               3993                 :                :                                              NULL, NULL);
                               3994                 :                : 
 3176 peter_e@gmx.net          3995                 :             83 :                     newcmds = lappend(newcmds, cmd);
                               3996                 :             83 :                     break;
                               3997                 :                :                 }
                               3998                 :                : 
                               3999                 :             31 :             case AT_SetIdentity:
                               4000                 :                :                 {
                               4001                 :                :                     /*
                               4002                 :                :                      * Create an ALTER SEQUENCE statement for the internal
                               4003                 :                :                      * sequence of the identity column.
                               4004                 :                :                      */
                               4005                 :                :                     ListCell   *lc;
                               4006                 :             31 :                     List       *newseqopts = NIL;
                               4007                 :             31 :                     List       *newdef = NIL;
                               4008                 :                :                     AttrNumber  attnum;
                               4009                 :                :                     Oid         seq_relid;
                               4010                 :                : 
                               4011                 :                :                     /*
                               4012                 :                :                      * Split options into those handled by ALTER SEQUENCE and
                               4013                 :                :                      * those for ALTER TABLE proper.
                               4014                 :                :                      */
                               4015   [ +  -  +  +  :             92 :                     foreach(lc, castNode(List, cmd->def))
                                              +  + ]
                               4016                 :                :                     {
 3135 bruce@momjian.us         4017                 :             61 :                         DefElem    *def = lfirst_node(DefElem, lc);
                               4018                 :                : 
 3176 peter_e@gmx.net          4019         [ +  + ]:             61 :                         if (strcmp(def->defname, "generated") == 0)
                               4020                 :             22 :                             newdef = lappend(newdef, def);
                               4021                 :                :                         else
                               4022                 :             39 :                             newseqopts = lappend(newseqopts, def);
                               4023                 :                :                     }
                               4024                 :                : 
                               4025                 :             31 :                     attnum = get_attnum(relid, cmd->name);
 2162 tgl@sss.pgh.pa.us        4026         [ -  + ]:             31 :                     if (attnum == InvalidAttrNumber)
 2162 tgl@sss.pgh.pa.us        4027         [ #  # ]:UBC           0 :                         ereport(ERROR,
                               4028                 :                :                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
                               4029                 :                :                                  errmsg("column \"%s\" of relation \"%s\" does not exist",
                               4030                 :                :                                         cmd->name, RelationGetRelationName(rel))));
                               4031                 :                : 
  588 peter@eisentraut.org     4032                 :CBC          31 :                     seq_relid = getIdentitySequence(rel, attnum, true);
                               4033                 :                : 
 2162 tgl@sss.pgh.pa.us        4034         [ +  + ]:             31 :                     if (seq_relid)
                               4035                 :                :                     {
                               4036                 :                :                         AlterSeqStmt *seqstmt;
                               4037                 :                : 
                               4038                 :             25 :                         seqstmt = makeNode(AlterSeqStmt);
                               4039                 :             25 :                         seqstmt->sequence = makeRangeVar(get_namespace_name(get_rel_namespace(seq_relid)),
                               4040                 :                :                                                          get_rel_name(seq_relid), -1);
                               4041                 :             25 :                         seqstmt->options = newseqopts;
                               4042                 :             25 :                         seqstmt->for_identity = true;
                               4043                 :             25 :                         seqstmt->missing_ok = false;
                               4044                 :                : 
                               4045                 :             25 :                         cxt.blist = lappend(cxt.blist, seqstmt);
                               4046                 :                :                     }
                               4047                 :                : 
                               4048                 :                :                     /*
                               4049                 :                :                      * If column was not an identity column, we just let the
                               4050                 :                :                      * ALTER TABLE command error out later.  (There are cases
                               4051                 :                :                      * this fails to cover, but we'll need to restructure
                               4052                 :                :                      * where creation of the sequence dependency linkage
                               4053                 :                :                      * happens before we can fix it.)
                               4054                 :                :                      */
                               4055                 :                : 
 3176 peter_e@gmx.net          4056                 :             31 :                     cmd->def = (Node *) newdef;
 3910 alvherre@alvh.no-ip.     4057                 :             31 :                     newcmds = lappend(newcmds, cmd);
                               4058                 :             31 :                     break;
                               4059                 :                :                 }
                               4060                 :                : 
 3296 rhaas@postgresql.org     4061                 :           1758 :             case AT_AttachPartition:
                               4062                 :                :             case AT_DetachPartition:
                               4063                 :                :                 {
                               4064                 :           1758 :                     PartitionCmd *partcmd = (PartitionCmd *) cmd->def;
                               4065                 :                : 
    2 akorotkov@postgresql     4066                 :GNC        1758 :                     transformPartitionCmd(&cxt, partcmd->bound);
                               4067                 :                :                     /* assign the transformed value of the partition bound */
 3296 rhaas@postgresql.org     4068                 :CBC        1746 :                     partcmd->bound = cxt.partbound;
                               4069                 :                :                 }
                               4070                 :                : 
                               4071                 :           1746 :                 newcmds = lappend(newcmds, cmd);
                               4072                 :           1746 :                 break;
                               4073                 :                : 
    2 akorotkov@postgresql     4074                 :GNC         132 :             case AT_MergePartitions:
                               4075                 :                :                 {
                               4076                 :            132 :                     PartitionCmd *partcmd = (PartitionCmd *) cmd->def;
                               4077                 :                : 
                               4078         [ +  + ]:            132 :                     if (list_length(partcmd->partlist) < 2)
                               4079         [ +  - ]:              3 :                         ereport(ERROR,
                               4080                 :                :                                 errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               4081                 :                :                                 errmsg("list of partitions to be merged should include at least two partitions"));
                               4082                 :                : 
                               4083                 :            129 :                     transformPartitionCmdForMerge(&cxt, partcmd);
                               4084                 :             90 :                     newcmds = lappend(newcmds, cmd);
                               4085                 :             90 :                     break;
                               4086                 :                :                 }
                               4087                 :                : 
                               4088                 :            189 :             case AT_SplitPartition:
                               4089                 :                :                 {
                               4090                 :            189 :                     PartitionCmd *partcmd = (PartitionCmd *) cmd->def;
                               4091                 :                : 
                               4092         [ +  + ]:            189 :                     if (list_length(partcmd->partlist) < 2)
                               4093         [ +  - ]:              6 :                         ereport(ERROR,
                               4094                 :                :                                 errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               4095                 :                :                                 errmsg("list of new partitions should contain at least two partitions"));
                               4096                 :                : 
                               4097                 :            183 :                     transformPartitionCmdForSplit(&cxt, partcmd);
                               4098                 :             99 :                     newcmds = lappend(newcmds, cmd);
                               4099                 :             99 :                     break;
                               4100                 :                :                 }
                               4101                 :                : 
 6751 tgl@sss.pgh.pa.us        4102                 :UBC           0 :             default:
                               4103                 :                : 
                               4104                 :                :                 /*
                               4105                 :                :                  * Currently, we shouldn't actually get here for the
                               4106                 :                :                  * subcommand types that don't require transformation; but if
                               4107                 :                :                  * we do, just emit them unchanged.
                               4108                 :                :                  */
                               4109                 :              0 :                 newcmds = lappend(newcmds, cmd);
                               4110                 :              0 :                 break;
                               4111                 :                :         }
                               4112                 :                :     }
                               4113                 :                : 
                               4114                 :                :     /*
                               4115                 :                :      * Transfer anything we already have in cxt.alist into save_alist, to keep
                               4116                 :                :      * it separate from the output of transformIndexConstraints.
                               4117                 :                :      */
 6751 tgl@sss.pgh.pa.us        4118                 :CBC       12263 :     save_alist = cxt.alist;
                               4119                 :          12263 :     cxt.alist = NIL;
                               4120                 :                : 
                               4121                 :                :     /* Postprocess constraints */
 5439                          4122                 :          12263 :     transformIndexConstraints(&cxt);
                               4123                 :          12251 :     transformFKConstraints(&cxt, skipValidation, true);
 3653 rhaas@postgresql.org     4124                 :          12251 :     transformCheckConstraints(&cxt, false);
                               4125                 :                : 
                               4126                 :                :     /*
                               4127                 :                :      * Push any index-creation commands into the ALTER, so that they can be
                               4128                 :                :      * scheduled nicely by tablecmds.c.  Note that tablecmds.c assumes that
                               4129                 :                :      * the IndexStmt attached to an AT_AddIndex or AT_AddIndexConstraint
                               4130                 :                :      * subcommand has already been through transformIndexStmt.
                               4131                 :                :      */
 6751 tgl@sss.pgh.pa.us        4132   [ +  +  +  +  :          18400 :     foreach(l, cxt.alist)
                                              +  + ]
                               4133                 :                :     {
 2429                          4134                 :           6149 :         Node       *istmt = (Node *) lfirst(l);
                               4135                 :                : 
                               4136                 :                :         /*
                               4137                 :                :          * We assume here that cxt.alist contains only IndexStmts generated
                               4138                 :                :          * from primary key constraints.
                               4139                 :                :          */
                               4140         [ +  - ]:           6149 :         if (IsA(istmt, IndexStmt))
                               4141                 :                :         {
                               4142                 :           6149 :             IndexStmt  *idxstmt = (IndexStmt *) istmt;
                               4143                 :                : 
                               4144                 :           6149 :             idxstmt = transformIndexStmt(relid, idxstmt, queryString);
                               4145                 :           6149 :             newcmd = makeNode(AlterTableCmd);
                               4146         [ +  + ]:           6149 :             newcmd->subtype = OidIsValid(idxstmt->indexOid) ? AT_AddIndexConstraint : AT_AddIndex;
                               4147                 :           6149 :             newcmd->def = (Node *) idxstmt;
                               4148                 :           6149 :             newcmds = lappend(newcmds, newcmd);
                               4149                 :                :         }
                               4150                 :                :         else
 2429 tgl@sss.pgh.pa.us        4151         [ #  # ]:UBC           0 :             elog(ERROR, "unexpected stmt type %d", (int) nodeTag(istmt));
                               4152                 :                :     }
 6751 tgl@sss.pgh.pa.us        4153                 :CBC       12251 :     cxt.alist = NIL;
                               4154                 :                : 
                               4155                 :                :     /* Append any CHECK, NOT NULL or FK constraints to the commands list */
  403 alvherre@alvh.no-ip.     4156   [ +  +  +  +  :          25043 :     foreach_node(Constraint, def, cxt.ckconstraints)
                                              +  + ]
                               4157                 :                :     {
                               4158                 :            541 :         newcmd = makeNode(AlterTableCmd);
                               4159                 :            541 :         newcmd->subtype = AT_AddConstraint;
                               4160                 :            541 :         newcmd->def = (Node *) def;
                               4161                 :            541 :         newcmds = lappend(newcmds, newcmd);
                               4162                 :                :     }
                               4163   [ +  +  +  +  :          29173 :     foreach_node(Constraint, def, cxt.nnconstraints)
                                              +  + ]
                               4164                 :                :     {
 6751 tgl@sss.pgh.pa.us        4165                 :           4671 :         newcmd = makeNode(AlterTableCmd);
                               4166                 :           4671 :         newcmd->subtype = AT_AddConstraint;
  403 alvherre@alvh.no-ip.     4167                 :           4671 :         newcmd->def = (Node *) def;
  844                          4168                 :           4671 :         newcmds = lappend(newcmds, newcmd);
                               4169                 :                :     }
  403                          4170   [ +  +  +  +  :          25850 :     foreach_node(Constraint, def, cxt.fkconstraints)
                                              +  + ]
                               4171                 :                :     {
 6751 tgl@sss.pgh.pa.us        4172                 :           1348 :         newcmd = makeNode(AlterTableCmd);
                               4173                 :           1348 :         newcmd->subtype = AT_AddConstraint;
  403 alvherre@alvh.no-ip.     4174                 :           1348 :         newcmd->def = (Node *) def;
 6751 tgl@sss.pgh.pa.us        4175                 :           1348 :         newcmds = lappend(newcmds, newcmd);
                               4176                 :                :     }
                               4177                 :                : 
                               4178                 :                :     /* Close rel */
                               4179                 :          12251 :     relation_close(rel, NoLock);
                               4180                 :                : 
                               4181                 :                :     /*
                               4182                 :                :      * Output results.
                               4183                 :                :      */
                               4184                 :          12251 :     stmt->cmds = newcmds;
                               4185                 :                : 
 2162                          4186                 :          12251 :     *beforeStmts = cxt.blist;
                               4187                 :          12251 :     *afterStmts = list_concat(cxt.alist, save_alist);
                               4188                 :                : 
                               4189                 :          12251 :     return stmt;
                               4190                 :                : }
                               4191                 :                : 
                               4192                 :                : 
                               4193                 :                : /*
                               4194                 :                :  * Preprocess a list of column constraint clauses
                               4195                 :                :  * to attach constraint attributes to their primary constraint nodes
                               4196                 :                :  * and detect inconsistent/misplaced constraint attributes.
                               4197                 :                :  *
                               4198                 :                :  * NOTE: currently, attributes are only supported for FOREIGN KEY, UNIQUE,
                               4199                 :                :  * EXCLUSION, and PRIMARY KEY constraints, but someday they ought to be
                               4200                 :                :  * supported for other constraint types.
                               4201                 :                :  */
                               4202                 :                : static void
 5439                          4203                 :          34881 : transformConstraintAttrs(CreateStmtContext *cxt, List *constraintList)
                               4204                 :                : {
 5983                          4205                 :          34881 :     Constraint *lastprimarycon = NULL;
 6751                          4206                 :          34881 :     bool        saw_deferrability = false;
                               4207                 :          34881 :     bool        saw_initially = false;
  339 peter@eisentraut.org     4208                 :          34881 :     bool        saw_enforced = false;
                               4209                 :                :     ListCell   *clist;
                               4210                 :                : 
                               4211                 :                : #define SUPPORTS_ATTRS(node)                \
                               4212                 :                :     ((node) != NULL &&                      \
                               4213                 :                :      ((node)->contype == CONSTR_PRIMARY ||   \
                               4214                 :                :       (node)->contype == CONSTR_UNIQUE ||    \
                               4215                 :                :       (node)->contype == CONSTR_EXCLUSION || \
                               4216                 :                :       (node)->contype == CONSTR_FOREIGN))
                               4217                 :                : 
 6751 tgl@sss.pgh.pa.us        4218   [ +  +  +  +  :          44626 :     foreach(clist, constraintList)
                                              +  + ]
                               4219                 :                :     {
 5983                          4220                 :           9757 :         Constraint *con = (Constraint *) lfirst(clist);
                               4221                 :                : 
                               4222         [ -  + ]:           9757 :         if (!IsA(con, Constraint))
 5983 tgl@sss.pgh.pa.us        4223         [ #  # ]:UBC           0 :             elog(ERROR, "unrecognized node type: %d",
                               4224                 :                :                  (int) nodeTag(con));
 5983 tgl@sss.pgh.pa.us        4225   [ +  -  +  +  :CBC        9757 :         switch (con->contype)
                                           +  +  + ]
                               4226                 :                :         {
                               4227                 :             47 :             case CONSTR_ATTR_DEFERRABLE:
                               4228   [ +  -  +  +  :             47 :                 if (!SUPPORTS_ATTRS(lastprimarycon))
                                     +  +  +  -  -  
                                                 + ]
 5983 tgl@sss.pgh.pa.us        4229         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               4230                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4231                 :                :                              errmsg("misplaced DEFERRABLE clause"),
                               4232                 :                :                              parser_errposition(cxt->pstate, con->location)));
 5983 tgl@sss.pgh.pa.us        4233         [ -  + ]:CBC          47 :                 if (saw_deferrability)
 5983 tgl@sss.pgh.pa.us        4234         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               4235                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4236                 :                :                              errmsg("multiple DEFERRABLE/NOT DEFERRABLE clauses not allowed"),
                               4237                 :                :                              parser_errposition(cxt->pstate, con->location)));
 5983 tgl@sss.pgh.pa.us        4238                 :CBC          47 :                 saw_deferrability = true;
                               4239                 :             47 :                 lastprimarycon->deferrable = true;
                               4240                 :             47 :                 break;
                               4241                 :                : 
 5983 tgl@sss.pgh.pa.us        4242                 :UBC           0 :             case CONSTR_ATTR_NOT_DEFERRABLE:
                               4243   [ #  #  #  #  :              0 :                 if (!SUPPORTS_ATTRS(lastprimarycon))
                                     #  #  #  #  #  
                                                 # ]
                               4244         [ #  # ]:              0 :                     ereport(ERROR,
                               4245                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4246                 :                :                              errmsg("misplaced NOT DEFERRABLE clause"),
                               4247                 :                :                              parser_errposition(cxt->pstate, con->location)));
                               4248         [ #  # ]:              0 :                 if (saw_deferrability)
                               4249         [ #  # ]:              0 :                     ereport(ERROR,
                               4250                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4251                 :                :                              errmsg("multiple DEFERRABLE/NOT DEFERRABLE clauses not allowed"),
                               4252                 :                :                              parser_errposition(cxt->pstate, con->location)));
                               4253                 :              0 :                 saw_deferrability = true;
                               4254                 :              0 :                 lastprimarycon->deferrable = false;
                               4255         [ #  # ]:              0 :                 if (saw_initially &&
                               4256         [ #  # ]:              0 :                     lastprimarycon->initdeferred)
                               4257         [ #  # ]:              0 :                     ereport(ERROR,
                               4258                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4259                 :                :                              errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
                               4260                 :                :                              parser_errposition(cxt->pstate, con->location)));
                               4261                 :              0 :                 break;
                               4262                 :                : 
 5983 tgl@sss.pgh.pa.us        4263                 :CBC          38 :             case CONSTR_ATTR_DEFERRED:
                               4264   [ +  -  +  +  :             38 :                 if (!SUPPORTS_ATTRS(lastprimarycon))
                                     +  +  +  -  -  
                                                 + ]
 5983 tgl@sss.pgh.pa.us        4265         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               4266                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4267                 :                :                              errmsg("misplaced INITIALLY DEFERRED clause"),
                               4268                 :                :                              parser_errposition(cxt->pstate, con->location)));
 5983 tgl@sss.pgh.pa.us        4269         [ -  + ]:CBC          38 :                 if (saw_initially)
 5983 tgl@sss.pgh.pa.us        4270         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               4271                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4272                 :                :                              errmsg("multiple INITIALLY IMMEDIATE/DEFERRED clauses not allowed"),
                               4273                 :                :                              parser_errposition(cxt->pstate, con->location)));
 5983 tgl@sss.pgh.pa.us        4274                 :CBC          38 :                 saw_initially = true;
                               4275                 :             38 :                 lastprimarycon->initdeferred = true;
                               4276                 :                : 
                               4277                 :                :                 /*
                               4278                 :                :                  * If only INITIALLY DEFERRED appears, assume DEFERRABLE
                               4279                 :                :                  */
                               4280         [ +  + ]:             38 :                 if (!saw_deferrability)
                               4281                 :             13 :                     lastprimarycon->deferrable = true;
                               4282         [ -  + ]:             25 :                 else if (!lastprimarycon->deferrable)
 5983 tgl@sss.pgh.pa.us        4283         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               4284                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4285                 :                :                              errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
                               4286                 :                :                              parser_errposition(cxt->pstate, con->location)));
 5983 tgl@sss.pgh.pa.us        4287                 :CBC          38 :                 break;
                               4288                 :                : 
                               4289                 :              3 :             case CONSTR_ATTR_IMMEDIATE:
                               4290   [ +  -  +  -  :              3 :                 if (!SUPPORTS_ATTRS(lastprimarycon))
                                     +  -  +  -  -  
                                                 + ]
 5983 tgl@sss.pgh.pa.us        4291         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               4292                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4293                 :                :                              errmsg("misplaced INITIALLY IMMEDIATE clause"),
                               4294                 :                :                              parser_errposition(cxt->pstate, con->location)));
 5983 tgl@sss.pgh.pa.us        4295         [ -  + ]:CBC           3 :                 if (saw_initially)
 5983 tgl@sss.pgh.pa.us        4296         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               4297                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4298                 :                :                              errmsg("multiple INITIALLY IMMEDIATE/DEFERRED clauses not allowed"),
                               4299                 :                :                              parser_errposition(cxt->pstate, con->location)));
 5983 tgl@sss.pgh.pa.us        4300                 :CBC           3 :                 saw_initially = true;
                               4301                 :              3 :                 lastprimarycon->initdeferred = false;
                               4302                 :              3 :                 break;
                               4303                 :                : 
  339 peter@eisentraut.org     4304                 :             18 :             case CONSTR_ATTR_ENFORCED:
                               4305         [ +  - ]:             18 :                 if (lastprimarycon == NULL ||
  258                          4306         [ +  + ]:             18 :                     (lastprimarycon->contype != CONSTR_CHECK &&
                               4307         [ +  + ]:              6 :                      lastprimarycon->contype != CONSTR_FOREIGN))
  339                          4308         [ +  - ]:              3 :                     ereport(ERROR,
                               4309                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4310                 :                :                              errmsg("misplaced ENFORCED clause"),
                               4311                 :                :                              parser_errposition(cxt->pstate, con->location)));
                               4312         [ +  + ]:             15 :                 if (saw_enforced)
                               4313         [ +  - ]:              3 :                     ereport(ERROR,
                               4314                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4315                 :                :                              errmsg("multiple ENFORCED/NOT ENFORCED clauses not allowed"),
                               4316                 :                :                              parser_errposition(cxt->pstate, con->location)));
                               4317                 :             12 :                 saw_enforced = true;
                               4318                 :             12 :                 lastprimarycon->is_enforced = true;
                               4319                 :             12 :                 break;
                               4320                 :                : 
                               4321                 :             30 :             case CONSTR_ATTR_NOT_ENFORCED:
                               4322         [ +  - ]:             30 :                 if (lastprimarycon == NULL ||
  258                          4323         [ +  + ]:             30 :                     (lastprimarycon->contype != CONSTR_CHECK &&
                               4324         [ +  + ]:              9 :                      lastprimarycon->contype != CONSTR_FOREIGN))
  339                          4325         [ +  - ]:              3 :                     ereport(ERROR,
                               4326                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4327                 :                :                              errmsg("misplaced NOT ENFORCED clause"),
                               4328                 :                :                              parser_errposition(cxt->pstate, con->location)));
                               4329         [ +  + ]:             27 :                 if (saw_enforced)
                               4330         [ +  - ]:              3 :                     ereport(ERROR,
                               4331                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                               4332                 :                :                              errmsg("multiple ENFORCED/NOT ENFORCED clauses not allowed"),
                               4333                 :                :                              parser_errposition(cxt->pstate, con->location)));
                               4334                 :             24 :                 saw_enforced = true;
                               4335                 :             24 :                 lastprimarycon->is_enforced = false;
                               4336                 :                : 
                               4337                 :                :                 /* A NOT ENFORCED constraint must be marked as invalid. */
                               4338                 :             24 :                 lastprimarycon->skip_validation = true;
                               4339                 :             24 :                 lastprimarycon->initially_valid = false;
                               4340                 :             24 :                 break;
                               4341                 :                : 
 5983 tgl@sss.pgh.pa.us        4342                 :           9621 :             default:
                               4343                 :                :                 /* Otherwise it's not an attribute */
                               4344                 :           9621 :                 lastprimarycon = con;
                               4345                 :                :                 /* reset flags for new primary node */
                               4346                 :           9621 :                 saw_deferrability = false;
                               4347                 :           9621 :                 saw_initially = false;
  339 peter@eisentraut.org     4348                 :           9621 :                 saw_enforced = false;
 5983 tgl@sss.pgh.pa.us        4349                 :           9621 :                 break;
                               4350                 :                :         }
                               4351                 :                :     }
 6751                          4352                 :          34869 : }
                               4353                 :                : 
                               4354                 :                : /*
                               4355                 :                :  * Special handling of type definition for a column
                               4356                 :                :  */
                               4357                 :                : static void
 5439                          4358                 :          34721 : transformColumnType(CreateStmtContext *cxt, ColumnDef *column)
                               4359                 :                : {
                               4360                 :                :     /*
                               4361                 :                :      * All we really need to do here is verify that the type is valid,
                               4362                 :                :      * including any collation spec that might be present.
                               4363                 :                :      */
 5396                          4364                 :          34721 :     Type        ctype = typenameType(cxt->pstate, column->typeName, NULL);
                               4365                 :                : 
                               4366         [ +  + ]:          34714 :     if (column->collClause)
                               4367                 :                :     {
                               4368                 :            260 :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(ctype);
                               4369                 :                : 
 5363 peter_e@gmx.net          4370                 :            260 :         LookupCollation(cxt->pstate,
 5304 bruce@momjian.us         4371                 :            260 :                         column->collClause->collname,
                               4372                 :            260 :                         column->collClause->location);
                               4373                 :                :         /* Complain if COLLATE is applied to an uncollatable type */
 5396 tgl@sss.pgh.pa.us        4374         [ +  + ]:            254 :         if (!OidIsValid(typtup->typcollation))
                               4375         [ +  - ]:              6 :             ereport(ERROR,
                               4376                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               4377                 :                :                      errmsg("collations are not supported by type %s",
                               4378                 :                :                             format_type_be(typtup->oid)),
                               4379                 :                :                      parser_errposition(cxt->pstate,
                               4380                 :                :                                         column->collClause->location)));
                               4381                 :                :     }
                               4382                 :                : 
 6751                          4383                 :          34702 :     ReleaseSysCache(ctype);
                               4384                 :          34702 : }
                               4385                 :                : 
                               4386                 :                : 
                               4387                 :                : /*
                               4388                 :                :  * transformCreateSchemaStmtElements -
                               4389                 :                :  *    analyzes the elements of a CREATE SCHEMA statement
                               4390                 :                :  *
                               4391                 :                :  * Split the schema element list from a CREATE SCHEMA statement into
                               4392                 :                :  * individual commands and place them in the result list in an order
                               4393                 :                :  * such that there are no forward references (e.g. GRANT to a table
                               4394                 :                :  * created later in the list). Note that the logic we use for determining
                               4395                 :                :  * forward references is presently quite incomplete.
                               4396                 :                :  *
                               4397                 :                :  * "schemaName" is the name of the schema that will be used for the creation
                               4398                 :                :  * of the objects listed, that may be compiled from the schema name defined
                               4399                 :                :  * in the statement or a role specification.
                               4400                 :                :  *
                               4401                 :                :  * SQL also allows constraints to make forward references, so thumb through
                               4402                 :                :  * the table columns and move forward references to a posterior alter-table
                               4403                 :                :  * command.
                               4404                 :                :  *
                               4405                 :                :  * The result is a list of parse nodes that still need to be analyzed ---
                               4406                 :                :  * but we can't analyze the later commands until we've executed the earlier
                               4407                 :                :  * ones, because of possible inter-object references.
                               4408                 :                :  *
                               4409                 :                :  * Note: this breaks the rules a little bit by modifying schema-name fields
                               4410                 :                :  * within passed-in structs.  However, the transformation would be the same
                               4411                 :                :  * if done over, so it should be all right to scribble on the input to this
                               4412                 :                :  * extent.
                               4413                 :                :  */
                               4414                 :                : List *
  963 michael@paquier.xyz      4415                 :            525 : transformCreateSchemaStmtElements(List *schemaElts, const char *schemaName)
                               4416                 :                : {
                               4417                 :                :     CreateSchemaStmtContext cxt;
                               4418                 :                :     List       *result;
                               4419                 :                :     ListCell   *elements;
                               4420                 :                : 
                               4421                 :            525 :     cxt.schemaname = schemaName;
 6751 tgl@sss.pgh.pa.us        4422                 :            525 :     cxt.sequences = NIL;
                               4423                 :            525 :     cxt.tables = NIL;
                               4424                 :            525 :     cxt.views = NIL;
                               4425                 :            525 :     cxt.indexes = NIL;
                               4426                 :            525 :     cxt.triggers = NIL;
                               4427                 :            525 :     cxt.grants = NIL;
                               4428                 :                : 
                               4429                 :                :     /*
                               4430                 :                :      * Run through each schema element in the schema element list. Separate
                               4431                 :                :      * statements by type, and do preliminary analysis.
                               4432                 :                :      */
  963 michael@paquier.xyz      4433   [ +  +  +  +  :            762 :     foreach(elements, schemaElts)
                                              +  + ]
                               4434                 :                :     {
 6751 tgl@sss.pgh.pa.us        4435                 :            282 :         Node       *element = lfirst(elements);
                               4436                 :                : 
                               4437   [ +  +  +  +  :            282 :         switch (nodeTag(element))
                                           +  -  - ]
                               4438                 :                :         {
                               4439                 :              9 :             case T_CreateSeqStmt:
                               4440                 :                :                 {
                               4441                 :              9 :                     CreateSeqStmt *elp = (CreateSeqStmt *) element;
                               4442                 :                : 
                               4443                 :              9 :                     setSchemaName(cxt.schemaname, &elp->sequence->schemaname);
 6751 tgl@sss.pgh.pa.us        4444                 :UBC           0 :                     cxt.sequences = lappend(cxt.sequences, element);
                               4445                 :                :                 }
                               4446                 :              0 :                 break;
                               4447                 :                : 
 6751 tgl@sss.pgh.pa.us        4448                 :CBC         226 :             case T_CreateStmt:
                               4449                 :                :                 {
                               4450                 :            226 :                     CreateStmt *elp = (CreateStmt *) element;
                               4451                 :                : 
                               4452                 :            226 :                     setSchemaName(cxt.schemaname, &elp->relation->schemaname);
                               4453                 :                : 
                               4454                 :                :                     /*
                               4455                 :                :                      * XXX todo: deal with constraints
                               4456                 :                :                      */
                               4457                 :            217 :                     cxt.tables = lappend(cxt.tables, element);
                               4458                 :                :                 }
                               4459                 :            217 :                 break;
                               4460                 :                : 
                               4461                 :             22 :             case T_ViewStmt:
                               4462                 :                :                 {
                               4463                 :             22 :                     ViewStmt   *elp = (ViewStmt *) element;
                               4464                 :                : 
                               4465                 :             22 :                     setSchemaName(cxt.schemaname, &elp->view->schemaname);
                               4466                 :                : 
                               4467                 :                :                     /*
                               4468                 :                :                      * XXX todo: deal with references between views
                               4469                 :                :                      */
                               4470                 :             13 :                     cxt.views = lappend(cxt.views, element);
                               4471                 :                :                 }
                               4472                 :             13 :                 break;
                               4473                 :                : 
                               4474                 :             16 :             case T_IndexStmt:
                               4475                 :                :                 {
                               4476                 :             16 :                     IndexStmt  *elp = (IndexStmt *) element;
                               4477                 :                : 
                               4478                 :             16 :                     setSchemaName(cxt.schemaname, &elp->relation->schemaname);
                               4479                 :              7 :                     cxt.indexes = lappend(cxt.indexes, element);
                               4480                 :                :                 }
                               4481                 :              7 :                 break;
                               4482                 :                : 
                               4483                 :              9 :             case T_CreateTrigStmt:
                               4484                 :                :                 {
                               4485                 :              9 :                     CreateTrigStmt *elp = (CreateTrigStmt *) element;
                               4486                 :                : 
                               4487                 :              9 :                     setSchemaName(cxt.schemaname, &elp->relation->schemaname);
 6751 tgl@sss.pgh.pa.us        4488                 :UBC           0 :                     cxt.triggers = lappend(cxt.triggers, element);
                               4489                 :                :                 }
                               4490                 :              0 :                 break;
                               4491                 :                : 
                               4492                 :              0 :             case T_GrantStmt:
                               4493                 :              0 :                 cxt.grants = lappend(cxt.grants, element);
                               4494                 :              0 :                 break;
                               4495                 :                : 
                               4496                 :              0 :             default:
                               4497         [ #  # ]:              0 :                 elog(ERROR, "unrecognized node type: %d",
                               4498                 :                :                      (int) nodeTag(element));
                               4499                 :                :         }
                               4500                 :                :     }
                               4501                 :                : 
 6751 tgl@sss.pgh.pa.us        4502                 :CBC         480 :     result = NIL;
                               4503                 :            480 :     result = list_concat(result, cxt.sequences);
                               4504                 :            480 :     result = list_concat(result, cxt.tables);
                               4505                 :            480 :     result = list_concat(result, cxt.views);
                               4506                 :            480 :     result = list_concat(result, cxt.indexes);
                               4507                 :            480 :     result = list_concat(result, cxt.triggers);
                               4508                 :            480 :     result = list_concat(result, cxt.grants);
                               4509                 :                : 
                               4510                 :            480 :     return result;
                               4511                 :                : }
                               4512                 :                : 
                               4513                 :                : /*
                               4514                 :                :  * setSchemaName
                               4515                 :                :  *      Set or check schema name in an element of a CREATE SCHEMA command
                               4516                 :                :  */
                               4517                 :                : static void
  963 michael@paquier.xyz      4518                 :            282 : setSchemaName(const char *context_schema, char **stmt_schema_name)
                               4519                 :                : {
 6751 tgl@sss.pgh.pa.us        4520         [ +  + ]:            282 :     if (*stmt_schema_name == NULL)
  963 michael@paquier.xyz      4521                 :            225 :         *stmt_schema_name = unconstify(char *, context_schema);
 6751 tgl@sss.pgh.pa.us        4522         [ +  + ]:             57 :     else if (strcmp(context_schema, *stmt_schema_name) != 0)
                               4523         [ +  - ]:             45 :         ereport(ERROR,
                               4524                 :                :                 (errcode(ERRCODE_INVALID_SCHEMA_DEFINITION),
                               4525                 :                :                  errmsg("CREATE specifies a schema (%s) "
                               4526                 :                :                         "different from the one being created (%s)",
                               4527                 :                :                         *stmt_schema_name, context_schema)));
                               4528                 :            237 : }
                               4529                 :                : 
                               4530                 :                : /*
                               4531                 :                :  * transformPartitionCmd
                               4532                 :                :  *      Analyze the ATTACH/DETACH/SPLIT PARTITION command
                               4533                 :                :  *
                               4534                 :                :  * In case of the ATTACH/SPLIT PARTITION command, cxt->partbound is set to the
                               4535                 :                :  * transformed value of bound.
                               4536                 :                :  */
                               4537                 :                : static void
    2 akorotkov@postgresql     4538                 :GNC        2283 : transformPartitionCmd(CreateStmtContext *cxt, PartitionBoundSpec *bound)
                               4539                 :                : {
 3296 rhaas@postgresql.org     4540                 :CBC        2283 :     Relation    parentRel = cxt->rel;
                               4541                 :                : 
 2888 alvherre@alvh.no-ip.     4542   [ +  +  -  -  :           2283 :     switch (parentRel->rd_rel->relkind)
                                                 - ]
                               4543                 :                :     {
                               4544                 :           2088 :         case RELKIND_PARTITIONED_TABLE:
                               4545                 :                :             /* transform the partition bound, if any */
                               4546         [ -  + ]:           2088 :             Assert(RelationGetPartitionKey(parentRel) != NULL);
    2 akorotkov@postgresql     4547         [ +  + ]:GNC        2088 :             if (bound != NULL)
 2888 alvherre@alvh.no-ip.     4548                 :CBC        1796 :                 cxt->partbound = transformPartitionBound(cxt->pstate, parentRel,
                               4549                 :                :                                                          bound);
                               4550                 :           2076 :             break;
                               4551                 :            195 :         case RELKIND_PARTITIONED_INDEX:
                               4552                 :                : 
                               4553                 :                :             /*
                               4554                 :                :              * A partitioned index cannot have a partition bound set.  ALTER
                               4555                 :                :              * INDEX prevents that with its grammar, but not ALTER TABLE.
                               4556                 :                :              */
    2 akorotkov@postgresql     4557         [ +  + ]:GNC         195 :             if (bound != NULL)
 2114 michael@paquier.xyz      4558         [ +  - ]:CBC           3 :                 ereport(ERROR,
                               4559                 :                :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               4560                 :                :                          errmsg("\"%s\" is not a partitioned table",
                               4561                 :                :                                 RelationGetRelationName(parentRel))));
 2888 alvherre@alvh.no-ip.     4562                 :            192 :             break;
 2888 alvherre@alvh.no-ip.     4563                 :UBC           0 :         case RELKIND_RELATION:
                               4564                 :                :             /* the table must be partitioned */
                               4565         [ #  # ]:              0 :             ereport(ERROR,
                               4566                 :                :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               4567                 :                :                      errmsg("table \"%s\" is not partitioned",
                               4568                 :                :                             RelationGetRelationName(parentRel))));
                               4569                 :                :             break;
                               4570                 :              0 :         case RELKIND_INDEX:
                               4571                 :                :             /* the index must be partitioned */
                               4572         [ #  # ]:              0 :             ereport(ERROR,
                               4573                 :                :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               4574                 :                :                      errmsg("index \"%s\" is not partitioned",
                               4575                 :                :                             RelationGetRelationName(parentRel))));
                               4576                 :                :             break;
                               4577                 :              0 :         default:
                               4578                 :                :             /* parser shouldn't let this case through */
                               4579         [ #  # ]:              0 :             elog(ERROR, "\"%s\" is not a partitioned table or index",
                               4580                 :                :                  RelationGetRelationName(parentRel));
                               4581                 :                :             break;
                               4582                 :                :     }
 3296 rhaas@postgresql.org     4583                 :CBC        2268 : }
                               4584                 :                : 
                               4585                 :                : /*
                               4586                 :                :  * transformPartitionBound
                               4587                 :                :  *
                               4588                 :                :  * Transform a partition bound specification
                               4589                 :                :  */
                               4590                 :                : PartitionBoundSpec *
 3124 tgl@sss.pgh.pa.us        4591                 :           6314 : transformPartitionBound(ParseState *pstate, Relation parent,
                               4592                 :                :                         PartitionBoundSpec *spec)
                               4593                 :                : {
                               4594                 :                :     PartitionBoundSpec *result_spec;
 3296 rhaas@postgresql.org     4595                 :           6314 :     PartitionKey key = RelationGetPartitionKey(parent);
                               4596                 :           6314 :     char        strategy = get_partition_strategy(key);
                               4597                 :           6314 :     int         partnatts = get_partition_natts(key);
                               4598                 :           6314 :     List       *partexprs = get_partition_exprs(key);
                               4599                 :                : 
                               4600                 :                :     /* Avoid scribbling on input */
                               4601                 :           6314 :     result_spec = copyObject(spec);
                               4602                 :                : 
 3021                          4603         [ +  + ]:           6314 :     if (spec->is_default)
                               4604                 :                :     {
                               4605                 :                :         /*
                               4606                 :                :          * Hash partitioning does not support a default partition; there's no
                               4607                 :                :          * use case for it (since the set of partitions to create is perfectly
                               4608                 :                :          * defined), and if users do get into it accidentally, it's hard to
                               4609                 :                :          * back out from it afterwards.
                               4610                 :                :          */
 2959                          4611         [ +  + ]:            411 :         if (strategy == PARTITION_STRATEGY_HASH)
                               4612         [ +  - ]:              3 :             ereport(ERROR,
                               4613                 :                :                     (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                               4614                 :                :                      errmsg("a hash-partitioned table may not have a default partition")));
                               4615                 :                : 
                               4616                 :                :         /*
                               4617                 :                :          * In case of the default partition, parser had no way to identify the
                               4618                 :                :          * partition strategy. Assign the parent's strategy to the default
                               4619                 :                :          * partition bound spec.
                               4620                 :                :          */
 3021                          4621                 :            408 :         result_spec->strategy = strategy;
                               4622                 :                : 
                               4623                 :            408 :         return result_spec;
                               4624                 :                :     }
                               4625                 :                : 
 2959                          4626         [ +  + ]:           5903 :     if (strategy == PARTITION_STRATEGY_HASH)
                               4627                 :                :     {
                               4628         [ +  + ]:            378 :         if (spec->strategy != PARTITION_STRATEGY_HASH)
                               4629         [ +  - ]:              6 :             ereport(ERROR,
                               4630                 :                :                     (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                               4631                 :                :                      errmsg("invalid bound specification for a hash partition"),
                               4632                 :                :                      parser_errposition(pstate, exprLocation((Node *) spec))));
                               4633                 :                : 
                               4634         [ +  + ]:            372 :         if (spec->modulus <= 0)
                               4635         [ +  - ]:              6 :             ereport(ERROR,
                               4636                 :                :                     (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                               4637                 :                :                      errmsg("modulus for hash partition must be an integer value greater than zero")));
                               4638                 :                : 
                               4639         [ -  + ]:            366 :         Assert(spec->remainder >= 0);
                               4640                 :                : 
                               4641         [ +  + ]:            366 :         if (spec->remainder >= spec->modulus)
                               4642         [ +  - ]:              6 :             ereport(ERROR,
                               4643                 :                :                     (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                               4644                 :                :                      errmsg("remainder for hash partition must be less than modulus")));
                               4645                 :                :     }
                               4646         [ +  + ]:           5525 :     else if (strategy == PARTITION_STRATEGY_LIST)
                               4647                 :                :     {
                               4648                 :                :         ListCell   *cell;
                               4649                 :                :         char       *colname;
                               4650                 :                :         Oid         coltype;
                               4651                 :                :         int32       coltypmod;
                               4652                 :                :         Oid         partcollation;
                               4653                 :                : 
 3124 tgl@sss.pgh.pa.us        4654         [ +  + ]:           2623 :         if (spec->strategy != PARTITION_STRATEGY_LIST)
                               4655         [ +  - ]:              9 :             ereport(ERROR,
                               4656                 :                :                     (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                               4657                 :                :                      errmsg("invalid bound specification for a list partition"),
                               4658                 :                :                      parser_errposition(pstate, exprLocation((Node *) spec))));
                               4659                 :                : 
                               4660                 :                :         /* Get the only column's name in case we need to output an error */
 3296 rhaas@postgresql.org     4661         [ +  + ]:           2614 :         if (key->partattrs[0] != 0)
 2864 alvherre@alvh.no-ip.     4662                 :           2548 :             colname = get_attname(RelationGetRelid(parent),
                               4663                 :           2548 :                                   key->partattrs[0], false);
                               4664                 :                :         else
 3296 rhaas@postgresql.org     4665                 :             66 :             colname = deparse_expression((Node *) linitial(partexprs),
 3100 tgl@sss.pgh.pa.us        4666                 :             66 :                                          deparse_context_for(RelationGetRelationName(parent),
                               4667                 :                :                                                              RelationGetRelid(parent)),
                               4668                 :                :                                          false, false);
                               4669                 :                :         /* Need its type data too */
 3124                          4670                 :           2614 :         coltype = get_partition_col_typid(key, 0);
                               4671                 :           2614 :         coltypmod = get_partition_col_typmod(key, 0);
 2517 peter@eisentraut.org     4672                 :           2614 :         partcollation = get_partition_col_collation(key, 0);
                               4673                 :                : 
 3296 rhaas@postgresql.org     4674                 :           2614 :         result_spec->listdatums = NIL;
                               4675   [ +  -  +  +  :           6711 :         foreach(cell, spec->listdatums)
                                              +  + ]
                               4676                 :                :         {
 2517 peter@eisentraut.org     4677                 :           4127 :             Node       *expr = lfirst(cell);
                               4678                 :                :             Const      *value;
                               4679                 :                :             ListCell   *cell2;
                               4680                 :                :             bool        duplicate;
                               4681                 :                : 
                               4682                 :           4127 :             value = transformPartitionBoundValue(pstate, expr,
                               4683                 :                :                                                  colname, coltype, coltypmod,
                               4684                 :                :                                                  partcollation);
                               4685                 :                : 
                               4686                 :                :             /* Don't add to the result if the value is a duplicate */
 3296 rhaas@postgresql.org     4687                 :           4097 :             duplicate = false;
                               4688   [ +  +  +  +  :           7472 :             foreach(cell2, result_spec->listdatums)
                                              +  + ]
                               4689                 :                :             {
 1611 peter@eisentraut.org     4690                 :           3375 :                 Const      *value2 = lfirst_node(Const, cell2);
                               4691                 :                : 
 3296 rhaas@postgresql.org     4692         [ -  + ]:           3375 :                 if (equal(value, value2))
                               4693                 :                :                 {
 3296 rhaas@postgresql.org     4694                 :UBC           0 :                     duplicate = true;
                               4695                 :              0 :                     break;
                               4696                 :                :                 }
                               4697                 :                :             }
 3296 rhaas@postgresql.org     4698         [ -  + ]:CBC        4097 :             if (duplicate)
 3296 rhaas@postgresql.org     4699                 :UBC           0 :                 continue;
                               4700                 :                : 
 3296 rhaas@postgresql.org     4701                 :CBC        4097 :             result_spec->listdatums = lappend(result_spec->listdatums,
                               4702                 :                :                                               value);
                               4703                 :                :         }
                               4704                 :                :     }
                               4705         [ +  - ]:           2902 :     else if (strategy == PARTITION_STRATEGY_RANGE)
                               4706                 :                :     {
                               4707         [ +  + ]:           2902 :         if (spec->strategy != PARTITION_STRATEGY_RANGE)
                               4708         [ +  - ]:             12 :             ereport(ERROR,
                               4709                 :                :                     (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                               4710                 :                :                      errmsg("invalid bound specification for a range partition"),
                               4711                 :                :                      parser_errposition(pstate, exprLocation((Node *) spec))));
                               4712                 :                : 
                               4713         [ +  + ]:           2890 :         if (list_length(spec->lowerdatums) != partnatts)
                               4714         [ +  - ]:              3 :             ereport(ERROR,
                               4715                 :                :                     (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                               4716                 :                :                      errmsg("FROM must specify exactly one value per partitioning column")));
                               4717         [ +  + ]:           2887 :         if (list_length(spec->upperdatums) != partnatts)
                               4718         [ +  - ]:              3 :             ereport(ERROR,
                               4719                 :                :                     (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                               4720                 :                :                      errmsg("TO must specify exactly one value per partitioning column")));
                               4721                 :                : 
                               4722                 :                :         /*
                               4723                 :                :          * Convert raw parse nodes into PartitionRangeDatum nodes and perform
                               4724                 :                :          * any necessary validation.
                               4725                 :                :          */
 2517 peter@eisentraut.org     4726                 :           2851 :         result_spec->lowerdatums =
 2400 tgl@sss.pgh.pa.us        4727                 :           2884 :             transformPartitionRangeBounds(pstate, spec->lowerdatums,
                               4728                 :                :                                           parent);
 2517 peter@eisentraut.org     4729                 :           2848 :         result_spec->upperdatums =
 2400 tgl@sss.pgh.pa.us        4730                 :           2851 :             transformPartitionRangeBounds(pstate, spec->upperdatums,
                               4731                 :                :                                           parent);
                               4732                 :                :     }
                               4733                 :                :     else
 2517 peter@eisentraut.org     4734         [ #  # ]:UBC           0 :         elog(ERROR, "unexpected partition strategy: %d", (int) strategy);
                               4735                 :                : 
 2517 peter@eisentraut.org     4736                 :CBC        5792 :     return result_spec;
                               4737                 :                : }
                               4738                 :                : 
                               4739                 :                : /*
                               4740                 :                :  * transformPartitionRangeBounds
                               4741                 :                :  *      This converts the expressions for range partition bounds from the raw
                               4742                 :                :  *      grammar representation to PartitionRangeDatum structs
                               4743                 :                :  */
                               4744                 :                : static List *
                               4745                 :           5735 : transformPartitionRangeBounds(ParseState *pstate, List *blist,
                               4746                 :                :                               Relation parent)
                               4747                 :                : {
                               4748                 :           5735 :     List       *result = NIL;
                               4749                 :           5735 :     PartitionKey key = RelationGetPartitionKey(parent);
                               4750                 :           5735 :     List       *partexprs = get_partition_exprs(key);
                               4751                 :                :     ListCell   *lc;
                               4752                 :                :     int         i,
                               4753                 :                :                 j;
                               4754                 :                : 
                               4755                 :           5735 :     i = j = 0;
                               4756   [ +  -  +  +  :          12563 :     foreach(lc, blist)
                                              +  + ]
                               4757                 :                :     {
 2400 tgl@sss.pgh.pa.us        4758                 :           6855 :         Node       *expr = lfirst(lc);
 2517 peter@eisentraut.org     4759                 :           6855 :         PartitionRangeDatum *prd = NULL;
                               4760                 :                : 
                               4761                 :                :         /*
                               4762                 :                :          * Infinite range bounds -- "minvalue" and "maxvalue" -- get passed in
                               4763                 :                :          * as ColumnRefs.
                               4764                 :                :          */
                               4765         [ +  + ]:           6855 :         if (IsA(expr, ColumnRef))
                               4766                 :                :         {
 2400 tgl@sss.pgh.pa.us        4767                 :            394 :             ColumnRef  *cref = (ColumnRef *) expr;
                               4768                 :            394 :             char       *cname = NULL;
                               4769                 :                : 
                               4770                 :                :             /*
                               4771                 :                :              * There should be a single field named either "minvalue" or
                               4772                 :                :              * "maxvalue".
                               4773                 :                :              */
 2517 peter@eisentraut.org     4774         [ +  + ]:            394 :             if (list_length(cref->fields) == 1 &&
                               4775         [ +  - ]:            391 :                 IsA(linitial(cref->fields), String))
                               4776                 :            391 :                 cname = strVal(linitial(cref->fields));
                               4777                 :                : 
 2457 michael@paquier.xyz      4778         [ +  + ]:            394 :             if (cname == NULL)
                               4779                 :                :             {
                               4780                 :                :                 /*
                               4781                 :                :                  * ColumnRef is not in the desired single-field-name form. For
                               4782                 :                :                  * consistency between all partition strategies, let the
                               4783                 :                :                  * expression transformation report any errors rather than
                               4784                 :                :                  * doing it ourselves.
                               4785                 :                :                  */
                               4786                 :                :             }
                               4787         [ +  + ]:            391 :             else if (strcmp("minvalue", cname) == 0)
                               4788                 :                :             {
 2517 peter@eisentraut.org     4789                 :            190 :                 prd = makeNode(PartitionRangeDatum);
                               4790                 :            190 :                 prd->kind = PARTITION_RANGE_DATUM_MINVALUE;
                               4791                 :            190 :                 prd->value = NULL;
                               4792                 :                :             }
                               4793         [ +  + ]:            201 :             else if (strcmp("maxvalue", cname) == 0)
                               4794                 :                :             {
                               4795                 :            195 :                 prd = makeNode(PartitionRangeDatum);
                               4796                 :            195 :                 prd->kind = PARTITION_RANGE_DATUM_MAXVALUE;
                               4797                 :            195 :                 prd->value = NULL;
                               4798                 :                :             }
                               4799                 :                :         }
                               4800                 :                : 
                               4801         [ +  + ]:           6855 :         if (prd == NULL)
                               4802                 :                :         {
                               4803                 :                :             char       *colname;
                               4804                 :                :             Oid         coltype;
                               4805                 :                :             int32       coltypmod;
                               4806                 :                :             Oid         partcollation;
                               4807                 :                :             Const      *value;
                               4808                 :                : 
                               4809                 :                :             /* Get the column's name in case we need to output an error */
 3296 rhaas@postgresql.org     4810         [ +  + ]:           6470 :             if (key->partattrs[i] != 0)
 2864 alvherre@alvh.no-ip.     4811                 :           6031 :                 colname = get_attname(RelationGetRelid(parent),
                               4812                 :           6031 :                                       key->partattrs[i], false);
                               4813                 :                :             else
                               4814                 :                :             {
 3296 rhaas@postgresql.org     4815                 :            439 :                 colname = deparse_expression((Node *) list_nth(partexprs, j),
 3100 tgl@sss.pgh.pa.us        4816                 :            439 :                                              deparse_context_for(RelationGetRelationName(parent),
                               4817                 :                :                                                                  RelationGetRelid(parent)),
                               4818                 :                :                                              false, false);
 3296 rhaas@postgresql.org     4819                 :            439 :                 ++j;
                               4820                 :                :             }
                               4821                 :                : 
                               4822                 :                :             /* Need its type data too */
 3124 tgl@sss.pgh.pa.us        4823                 :           6470 :             coltype = get_partition_col_typid(key, i);
                               4824                 :           6470 :             coltypmod = get_partition_col_typmod(key, i);
 2517 peter@eisentraut.org     4825                 :           6470 :             partcollation = get_partition_col_collation(key, i);
                               4826                 :                : 
                               4827                 :           6470 :             value = transformPartitionBoundValue(pstate, expr,
                               4828                 :                :                                                  colname,
                               4829                 :                :                                                  coltype, coltypmod,
                               4830                 :                :                                                  partcollation);
                               4831         [ +  + ]:           6446 :             if (value->constisnull)
                               4832         [ +  - ]:              3 :                 ereport(ERROR,
                               4833                 :                :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                               4834                 :                :                          errmsg("cannot specify NULL in range bound")));
                               4835                 :           6443 :             prd = makeNode(PartitionRangeDatum);
                               4836                 :           6443 :             prd->kind = PARTITION_RANGE_DATUM_VALUE;
                               4837                 :           6443 :             prd->value = (Node *) value;
 3296 rhaas@postgresql.org     4838                 :           6443 :             ++i;
                               4839                 :                :         }
                               4840                 :                : 
 2517 peter@eisentraut.org     4841                 :           6828 :         prd->location = exprLocation(expr);
                               4842                 :                : 
                               4843                 :           6828 :         result = lappend(result, prd);
                               4844                 :                :     }
                               4845                 :                : 
                               4846                 :                :     /*
                               4847                 :                :      * Once we see MINVALUE or MAXVALUE for one column, the remaining columns
                               4848                 :                :      * must be the same.
                               4849                 :                :      */
                               4850                 :           5708 :     validateInfiniteBounds(pstate, result);
                               4851                 :                : 
                               4852                 :           5699 :     return result;
                               4853                 :                : }
                               4854                 :                : 
                               4855                 :                : /*
                               4856                 :                :  * validateInfiniteBounds
                               4857                 :                :  *
                               4858                 :                :  * Check that a MAXVALUE or MINVALUE specification in a partition bound is
                               4859                 :                :  * followed only by more of the same.
                               4860                 :                :  */
                               4861                 :                : static void
 3014 rhaas@postgresql.org     4862                 :           5708 : validateInfiniteBounds(ParseState *pstate, List *blist)
                               4863                 :                : {
                               4864                 :                :     ListCell   *lc;
                               4865                 :           5708 :     PartitionRangeDatumKind kind = PARTITION_RANGE_DATUM_VALUE;
                               4866                 :                : 
                               4867   [ +  -  +  +  :          12524 :     foreach(lc, blist)
                                              +  + ]
                               4868                 :                :     {
 1611 peter@eisentraut.org     4869                 :           6825 :         PartitionRangeDatum *prd = lfirst_node(PartitionRangeDatum, lc);
                               4870                 :                : 
 3014 rhaas@postgresql.org     4871         [ +  + ]:           6825 :         if (kind == prd->kind)
                               4872                 :           6548 :             continue;
                               4873                 :                : 
                               4874   [ +  +  +  - ]:            277 :         switch (kind)
                               4875                 :                :         {
                               4876                 :            268 :             case PARTITION_RANGE_DATUM_VALUE:
                               4877                 :            268 :                 kind = prd->kind;
                               4878                 :            268 :                 break;
                               4879                 :                : 
                               4880                 :              3 :             case PARTITION_RANGE_DATUM_MAXVALUE:
                               4881         [ +  - ]:              3 :                 ereport(ERROR,
                               4882                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               4883                 :                :                          errmsg("every bound following MAXVALUE must also be MAXVALUE"),
                               4884                 :                :                          parser_errposition(pstate, exprLocation((Node *) prd))));
                               4885                 :                :                 break;
                               4886                 :                : 
                               4887                 :              6 :             case PARTITION_RANGE_DATUM_MINVALUE:
                               4888         [ +  - ]:              6 :                 ereport(ERROR,
                               4889                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               4890                 :                :                          errmsg("every bound following MINVALUE must also be MINVALUE"),
                               4891                 :                :                          parser_errposition(pstate, exprLocation((Node *) prd))));
                               4892                 :                :                 break;
                               4893                 :                :         }
                               4894                 :                :     }
                               4895                 :           5699 : }
                               4896                 :                : 
                               4897                 :                : /*
                               4898                 :                :  * Transform one entry in a partition bound spec, producing a constant.
                               4899                 :                :  */
                               4900                 :                : static Const *
 2517 peter@eisentraut.org     4901                 :          10597 : transformPartitionBoundValue(ParseState *pstate, Node *val,
                               4902                 :                :                              const char *colName, Oid colType, int32 colTypmod,
                               4903                 :                :                              Oid partCollation)
                               4904                 :                : {
                               4905                 :                :     Node       *value;
                               4906                 :                : 
                               4907                 :                :     /* Transform raw parsetree */
                               4908                 :          10597 :     value = transformExpr(pstate, val, EXPR_KIND_PARTITION_BOUND);
                               4909                 :                : 
                               4910                 :                :     /*
                               4911                 :                :      * transformExpr() should have already rejected column references,
                               4912                 :                :      * subqueries, aggregates, window functions, and SRFs, based on the
                               4913                 :                :      * EXPR_KIND_ of a partition bound expression.
                               4914                 :                :      */
 1910 tgl@sss.pgh.pa.us        4915         [ -  + ]:          10546 :     Assert(!contain_var_clause(value));
                               4916                 :                : 
                               4917                 :                :     /*
                               4918                 :                :      * Coerce to the correct type.  This might cause an explicit coercion step
                               4919                 :                :      * to be added on top of the expression, which must be evaluated before
                               4920                 :                :      * returning the result to the caller.
                               4921                 :                :      */
 3124                          4922                 :          10546 :     value = coerce_to_target_type(pstate,
                               4923                 :                :                                   value, exprType(value),
                               4924                 :                :                                   colType,
                               4925                 :                :                                   colTypmod,
                               4926                 :                :                                   COERCION_ASSIGNMENT,
                               4927                 :                :                                   COERCE_IMPLICIT_CAST,
                               4928                 :                :                                   -1);
                               4929                 :                : 
                               4930         [ +  + ]:          10546 :     if (value == NULL)
                               4931         [ +  - ]:              3 :         ereport(ERROR,
                               4932                 :                :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
                               4933                 :                :                  errmsg("specified value cannot be cast to type %s for column \"%s\"",
                               4934                 :                :                         format_type_be(colType), colName),
                               4935                 :                :                  parser_errposition(pstate, exprLocation(val))));
                               4936                 :                : 
                               4937                 :                :     /*
                               4938                 :                :      * Evaluate the expression, if needed, assigning the partition key's data
                               4939                 :                :      * type and collation to the resulting Const node.
                               4940                 :                :      */
 1910                          4941         [ +  + ]:          10543 :     if (!IsA(value, Const))
                               4942                 :                :     {
 1905                          4943                 :            729 :         assign_expr_collations(pstate, value);
 1910                          4944                 :            729 :         value = (Node *) expression_planner((Expr *) value);
                               4945                 :            729 :         value = (Node *) evaluate_expr((Expr *) value, colType, colTypmod,
                               4946                 :                :                                        partCollation);
                               4947         [ -  + ]:            729 :         if (!IsA(value, Const))
 1910 tgl@sss.pgh.pa.us        4948         [ #  # ]:UBC           0 :             elog(ERROR, "could not evaluate partition bound expression");
                               4949                 :                :     }
                               4950                 :                :     else
                               4951                 :                :     {
                               4952                 :                :         /*
                               4953                 :                :          * If the expression is already a Const, as is often the case, we can
                               4954                 :                :          * skip the rather expensive steps above.  But we still have to insert
                               4955                 :                :          * the right collation, since coerce_to_target_type doesn't handle
                               4956                 :                :          * that.
                               4957                 :                :          */
 1910 tgl@sss.pgh.pa.us        4958                 :CBC        9814 :         ((Const *) value)->constcollid = partCollation;
                               4959                 :                :     }
                               4960                 :                : 
                               4961                 :                :     /*
                               4962                 :                :      * Attach original expression's parse location to the Const, so that
                               4963                 :                :      * that's what will be reported for any later errors related to this
                               4964                 :                :      * partition bound.
                               4965                 :                :      */
                               4966                 :          10543 :     ((Const *) value)->location = exprLocation(val);
                               4967                 :                : 
 3124                          4968                 :          10543 :     return (Const *) value;
                               4969                 :                : }
        

Generated by: LCOV version 2.4-beta