]> granicus.if.org Git - postgresql/commitdiff
Avoid pre-determining index names during CREATE TABLE LIKE parsing.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 16 Jul 2012 17:25:26 +0000 (13:25 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 16 Jul 2012 17:25:26 +0000 (13:25 -0400)
Formerly, when trying to copy both indexes and comments, CREATE TABLE LIKE
had to pre-assign names to indexes that had comments, because it made up an
explicit CommentStmt command to apply the comment and so it had to know the
name for the index.  This creates bad interactions with other indexes, as
shown in bug #6734 from Daniele Varrazzo: the preassignment logic couldn't
take any other indexes into account so it could choose a conflicting name.

To fix, add a field to IndexStmt that allows it to carry a comment to be
assigned to the new index.  (This isn't a user-exposed feature of CREATE
INDEX, only an internal option.)  Now we don't need preassignment of index
names in any situation.

I also took the opportunity to refactor DefineIndex to accept the IndexStmt
as such, rather than passing all its fields individually in a mile-long
parameter list.

Back-patch to 9.2, but no further, because it seems too dangerous to change
IndexStmt or DefineIndex's API in released branches.  The bug exists back
to 9.0 where CREATE TABLE LIKE grew the ability to copy comments, but given
the lack of prior complaints we'll just let it go unfixed before 9.2.

src/backend/bootstrap/bootparse.y
src/backend/commands/indexcmds.c
src/backend/commands/tablecmds.c
src/backend/nodes/copyfuncs.c
src/backend/nodes/equalfuncs.c
src/backend/nodes/outfuncs.c
src/backend/parser/gram.y
src/backend/parser/parse_utilcmd.c
src/backend/tcop/utility.c
src/include/commands/defrem.h
src/include/nodes/parsenodes.h

index d7bc5727403ba7c30475ce42512ebd8b6f577290..f4a1b8f0fdf30801051db5aff920f8ce77cf923e 100644 (file)
@@ -280,18 +280,34 @@ Boot_InsertStmt:
 Boot_DeclareIndexStmt:
                  XDECLARE INDEX boot_ident oidspec ON boot_ident USING boot_ident LPAREN boot_index_params RPAREN
                                {
+                                       IndexStmt *stmt = makeNode(IndexStmt);
+
                                        do_start();
 
-                                       DefineIndex(makeRangeVar(NULL, $6, -1),
-                                                               $3,
+                                       stmt->idxname = $3;
+                                       stmt->relation = makeRangeVar(NULL, $6, -1);
+                                       stmt->accessMethod = $8;
+                                       stmt->tableSpace = NULL;
+                                       stmt->indexParams = $10;
+                                       stmt->options = NIL;
+                                       stmt->whereClause = NULL;
+                                       stmt->excludeOpNames = NIL;
+                                       stmt->idxcomment = NULL;
+                                       stmt->indexOid = InvalidOid;
+                                       stmt->oldNode = InvalidOid;
+                                       stmt->unique = false;
+                                       stmt->primary = false;
+                                       stmt->isconstraint = false;
+                                       stmt->deferrable = false;
+                                       stmt->initdeferred = false;
+                                       stmt->concurrent = false;
+
+                                       DefineIndex(stmt,
                                                                $4,
-                                                               InvalidOid,
-                                                               $8,
-                                                               NULL,
-                                                               $10,
-                                                               NULL, NIL, NIL,
-                                                               false, false, false, false, false,
-                                                               false, false, true, false, false);
+                                                               false,
+                                                               false,
+                                                               true, /* skip_build */
+                                                               false);
                                        do_end();
                                }
                ;
@@ -299,18 +315,34 @@ Boot_DeclareIndexStmt:
 Boot_DeclareUniqueIndexStmt:
                  XDECLARE UNIQUE INDEX boot_ident oidspec ON boot_ident USING boot_ident LPAREN boot_index_params RPAREN
                                {
+                                       IndexStmt *stmt = makeNode(IndexStmt);
+
                                        do_start();
 
-                                       DefineIndex(makeRangeVar(NULL, $7, -1),
-                                                               $4,
+                                       stmt->idxname = $4;
+                                       stmt->relation = makeRangeVar(NULL, $7, -1);
+                                       stmt->accessMethod = $9;
+                                       stmt->tableSpace = NULL;
+                                       stmt->indexParams = $11;
+                                       stmt->options = NIL;
+                                       stmt->whereClause = NULL;
+                                       stmt->excludeOpNames = NIL;
+                                       stmt->idxcomment = NULL;
+                                       stmt->indexOid = InvalidOid;
+                                       stmt->oldNode = InvalidOid;
+                                       stmt->unique = true;
+                                       stmt->primary = false;
+                                       stmt->isconstraint = false;
+                                       stmt->deferrable = false;
+                                       stmt->initdeferred = false;
+                                       stmt->concurrent = false;
+
+                                       DefineIndex(stmt,
                                                                $5,
-                                                               InvalidOid,
-                                                               $9,
-                                                               NULL,
-                                                               $11,
-                                                               NULL, NIL, NIL,
-                                                               true, false, false, false, false,
-                                                               false, false, true, false, false);
+                                                               false,
+                                                               false,
+                                                               true, /* skip_build */
+                                                               false);
                                        do_end();
                                }
                ;
index a68d500e5b42c374fa87c6e7e410b52a550118a0..d32839fd6dca0c093f4b2038f2afccda018f6192 100644 (file)
@@ -24,6 +24,7 @@
 #include "catalog/pg_opfamily.h"
 #include "catalog/pg_tablespace.h"
 #include "catalog/pg_type.h"
+#include "commands/comment.h"
 #include "commands/dbcommands.h"
 #include "commands/defrem.h"
 #include "commands/tablecmds.h"
@@ -65,7 +66,11 @@ static void ComputeIndexAttrs(IndexInfo *indexInfo,
                                  bool isconstraint);
 static Oid GetIndexOpClass(List *opclass, Oid attrType,
                                char *accessMethodName, Oid accessMethodId);
+static char *ChooseIndexName(const char *tabname, Oid namespaceId,
+                               List *colnames, List *exclusionOpNames,
+                               bool primary, bool isconstraint);
 static char *ChooseIndexNameAddition(List *colnames);
+static List *ChooseIndexColumnNames(List *indexElems);
 static void RangeVarCallbackForReindexIndex(const RangeVar *relation,
                                                                Oid relId, Oid oldRelId, void *arg);
 
@@ -268,60 +273,28 @@ CheckIndexCompatible(Oid oldId,
  * DefineIndex
  *             Creates a new index.
  *
- * 'heapRelation': the relation the index will apply to.
- * 'indexRelationName': the name for the new index, or NULL to indicate
- *             that a nonconflicting default name should be picked.
+ * 'stmt': IndexStmt describing the properties of the new index.
  * 'indexRelationId': normally InvalidOid, but during bootstrap can be
  *             nonzero to specify a preselected OID for the index.
- * 'relFileNode': normally InvalidOid, but can be nonzero to specify existing
- *             storage constituting a valid build of this index.
- * 'accessMethodName': name of the AM to use.
- * 'tableSpaceName': name of the tablespace to create the index in.
- *             NULL specifies using the appropriate default.
- * 'attributeList': a list of IndexElem specifying columns and expressions
- *             to index on.
- * 'predicate': the partial-index condition, or NULL if none.
- * 'options': reloptions from WITH (in list-of-DefElem form).
- * 'exclusionOpNames': list of names of exclusion-constraint operators,
- *             or NIL if not an exclusion constraint.
- * 'unique': make the index enforce uniqueness.
- * 'primary': mark the index as a primary key in the catalogs.
- * 'isconstraint': index is for a PRIMARY KEY or UNIQUE constraint,
- *             so build a pg_constraint entry for it.
- * 'deferrable': constraint is DEFERRABLE.
- * 'initdeferred': constraint is INITIALLY DEFERRED.
  * 'is_alter_table': this is due to an ALTER rather than a CREATE operation.
  * 'check_rights': check for CREATE rights in the namespace.  (This should
  *             be true except when ALTER is deleting/recreating an index.)
  * 'skip_build': make the catalog entries but leave the index file empty;
  *             it will be filled later.
  * 'quiet': suppress the NOTICE chatter ordinarily provided for constraints.
- * 'concurrent': avoid blocking writers to the table while building.
  *
  * Returns the OID of the created index.
  */
 Oid
-DefineIndex(RangeVar *heapRelation,
-                       char *indexRelationName,
+DefineIndex(IndexStmt *stmt,
                        Oid indexRelationId,
-                       Oid relFileNode,
-                       char *accessMethodName,
-                       char *tableSpaceName,
-                       List *attributeList,
-                       Expr *predicate,
-                       List *options,
-                       List *exclusionOpNames,
-                       bool unique,
-                       bool primary,
-                       bool isconstraint,
-                       bool deferrable,
-                       bool initdeferred,
                        bool is_alter_table,
                        bool check_rights,
                        bool skip_build,
-                       bool quiet,
-                       bool concurrent)
+                       bool quiet)
 {
+       char       *indexRelationName;
+       char       *accessMethodName;
        Oid                *typeObjectId;
        Oid                *collationObjectId;
        Oid                *classObjectId;
@@ -354,7 +327,7 @@ DefineIndex(RangeVar *heapRelation,
        /*
         * count attributes in index
         */
-       numberOfAttributes = list_length(attributeList);
+       numberOfAttributes = list_length(stmt->indexParams);
        if (numberOfAttributes <= 0)
                ereport(ERROR,
                                (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
@@ -372,8 +345,8 @@ DefineIndex(RangeVar *heapRelation,
         * index build; but for concurrent builds we allow INSERT/UPDATE/DELETE
         * (but not VACUUM).
         */
-       rel = heap_openrv(heapRelation,
-                                         (concurrent ? ShareUpdateExclusiveLock : ShareLock));
+       rel = heap_openrv(stmt->relation,
+                                         (stmt->concurrent ? ShareUpdateExclusiveLock : ShareLock));
 
        relationId = RelationGetRelid(rel);
        namespaceId = RelationGetNamespace(rel);
@@ -391,12 +364,12 @@ DefineIndex(RangeVar *heapRelation,
                        ereport(ERROR,
                                        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                         errmsg("cannot create index on foreign table \"%s\"",
-                                                       heapRelation->relname)));
+                                                       RelationGetRelationName(rel))));
                else
                        ereport(ERROR,
                                        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                         errmsg("\"%s\" is not a table",
-                                                       heapRelation->relname)));
+                                                       RelationGetRelationName(rel))));
        }
 
        /*
@@ -428,9 +401,9 @@ DefineIndex(RangeVar *heapRelation,
         * Select tablespace to use.  If not specified, use default tablespace
         * (which may in turn default to database's default).
         */
-       if (tableSpaceName)
+       if (stmt->tableSpace)
        {
-               tablespaceId = get_tablespace_oid(tableSpaceName, false);
+               tablespaceId = get_tablespace_oid(stmt->tableSpace, false);
        }
        else
        {
@@ -465,22 +438,24 @@ DefineIndex(RangeVar *heapRelation,
        /*
         * Choose the index column names.
         */
-       indexColNames = ChooseIndexColumnNames(attributeList);
+       indexColNames = ChooseIndexColumnNames(stmt->indexParams);
 
        /*
         * Select name for index if caller didn't specify
         */
+       indexRelationName = stmt->idxname;
        if (indexRelationName == NULL)
                indexRelationName = ChooseIndexName(RelationGetRelationName(rel),
                                                                                        namespaceId,
                                                                                        indexColNames,
-                                                                                       exclusionOpNames,
-                                                                                       primary,
-                                                                                       isconstraint);
+                                                                                       stmt->excludeOpNames,
+                                                                                       stmt->primary,
+                                                                                       stmt->isconstraint);
 
        /*
         * look up the access method, verify it can handle the requested features
         */
+       accessMethodName = stmt->accessMethod;
        tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
        if (!HeapTupleIsValid(tuple))
        {
@@ -505,7 +480,7 @@ DefineIndex(RangeVar *heapRelation,
        accessMethodId = HeapTupleGetOid(tuple);
        accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
 
-       if (unique && !accessMethodForm->amcanunique)
+       if (stmt->unique && !accessMethodForm->amcanunique)
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                           errmsg("access method \"%s\" does not support unique indexes",
@@ -515,7 +490,7 @@ DefineIndex(RangeVar *heapRelation,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                  errmsg("access method \"%s\" does not support multicolumn indexes",
                                 accessMethodName)));
-       if (exclusionOpNames != NIL && !OidIsValid(accessMethodForm->amgettuple))
+       if (stmt->excludeOpNames && !OidIsValid(accessMethodForm->amgettuple))
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                errmsg("access method \"%s\" does not support exclusion constraints",
@@ -529,13 +504,14 @@ DefineIndex(RangeVar *heapRelation,
        /*
         * Validate predicate, if given
         */
-       if (predicate)
-               CheckPredicate(predicate);
+       if (stmt->whereClause)
+               CheckPredicate((Expr *) stmt->whereClause);
 
        /*
         * Parse AM-specific options, convert to text array form, validate.
         */
-       reloptions = transformRelOptions((Datum) 0, options, NULL, NULL, false, false);
+       reloptions = transformRelOptions((Datum) 0, stmt->options,
+                                                                        NULL, NULL, false, false);
 
        (void) index_reloptions(amoptions, reloptions, true);
 
@@ -547,15 +523,15 @@ DefineIndex(RangeVar *heapRelation,
        indexInfo->ii_NumIndexAttrs = numberOfAttributes;
        indexInfo->ii_Expressions = NIL;        /* for now */
        indexInfo->ii_ExpressionsState = NIL;
-       indexInfo->ii_Predicate = make_ands_implicit(predicate);
+       indexInfo->ii_Predicate = make_ands_implicit((Expr *) stmt->whereClause);
        indexInfo->ii_PredicateState = NIL;
        indexInfo->ii_ExclusionOps = NULL;
        indexInfo->ii_ExclusionProcs = NULL;
        indexInfo->ii_ExclusionStrats = NULL;
-       indexInfo->ii_Unique = unique;
+       indexInfo->ii_Unique = stmt->unique;
        /* In a concurrent build, mark it not-ready-for-inserts */
-       indexInfo->ii_ReadyForInserts = !concurrent;
-       indexInfo->ii_Concurrent = concurrent;
+       indexInfo->ii_ReadyForInserts = !stmt->concurrent;
+       indexInfo->ii_Concurrent = stmt->concurrent;
        indexInfo->ii_BrokenHotChain = false;
 
        typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
@@ -564,30 +540,30 @@ DefineIndex(RangeVar *heapRelation,
        coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16));
        ComputeIndexAttrs(indexInfo,
                                          typeObjectId, collationObjectId, classObjectId,
-                                         coloptions, attributeList,
-                                         exclusionOpNames, relationId,
+                                         coloptions, stmt->indexParams,
+                                         stmt->excludeOpNames, relationId,
                                          accessMethodName, accessMethodId,
-                                         amcanorder, isconstraint);
+                                         amcanorder, stmt->isconstraint);
 
        /*
         * Extra checks when creating a PRIMARY KEY index.
         */
-       if (primary)
+       if (stmt->primary)
                index_check_primary_key(rel, indexInfo, is_alter_table);
 
        /*
         * Report index creation if appropriate (delay this till after most of the
         * error checks)
         */
-       if (isconstraint && !quiet)
+       if (stmt->isconstraint && !quiet)
        {
                const char *constraint_type;
 
-               if (primary)
+               if (stmt->primary)
                        constraint_type = "PRIMARY KEY";
-               else if (unique)
+               else if (stmt->unique)
                        constraint_type = "UNIQUE";
-               else if (exclusionOpNames != NIL)
+               else if (stmt->excludeOpNames != NIL)
                        constraint_type = "EXCLUDE";
                else
                {
@@ -603,27 +579,32 @@ DefineIndex(RangeVar *heapRelation,
        }
 
        /*
-        * A valid relFileNode implies that we already have a built form of the
+        * A valid stmt->oldNode implies that we already have a built form of the
         * index.  The caller should also decline any index build.
         */
-       Assert(!OidIsValid(relFileNode) || (skip_build && !concurrent));
+       Assert(!OidIsValid(stmt->oldNode) || (skip_build && !stmt->concurrent));
 
        /*
         * Make the catalog entries for the index, including constraints. Then, if
         * not skip_build || concurrent, actually build the index.
         */
        indexRelationId =
-               index_create(rel, indexRelationName, indexRelationId, relFileNode,
+               index_create(rel, indexRelationName, indexRelationId, stmt->oldNode,
                                         indexInfo, indexColNames,
                                         accessMethodId, tablespaceId,
                                         collationObjectId, classObjectId,
-                                        coloptions, reloptions, primary,
-                                        isconstraint, deferrable, initdeferred,
+                                        coloptions, reloptions, stmt->primary,
+                                        stmt->isconstraint, stmt->deferrable, stmt->initdeferred,
                                         allowSystemTableMods,
-                                        skip_build || concurrent,
-                                        concurrent);
+                                        skip_build || stmt->concurrent,
+                                        stmt->concurrent);
 
-       if (!concurrent)
+       /* Add any requested comment */
+       if (stmt->idxcomment != NULL)
+               CreateComments(indexRelationId, RelationRelationId, 0,
+                                          stmt->idxcomment);
+
+       if (!stmt->concurrent)
        {
                /* Close the heap and we're done, in the non-concurrent case */
                heap_close(rel, NoLock);
@@ -711,7 +692,7 @@ DefineIndex(RangeVar *heapRelation,
         */
 
        /* Open and lock the parent heap relation */
-       rel = heap_openrv(heapRelation, ShareUpdateExclusiveLock);
+       rel = heap_openrv(stmt->relation, ShareUpdateExclusiveLock);
 
        /* And the target index relation */
        indexRelation = index_open(indexRelationId, RowExclusiveLock);
@@ -726,7 +707,7 @@ DefineIndex(RangeVar *heapRelation,
        indexInfo->ii_BrokenHotChain = false;
 
        /* Now build the index */
-       index_build(rel, indexRelation, indexInfo, primary, false);
+       index_build(rel, indexRelation, indexInfo, stmt->primary, false);
 
        /* Close both the relations, but keep the locks */
        heap_close(rel, NoLock);
@@ -1596,7 +1577,7 @@ ChooseRelationName(const char *name1, const char *name2,
  *
  * The argument list is pretty ad-hoc :-(
  */
-char *
+static char *
 ChooseIndexName(const char *tabname, Oid namespaceId,
                                List *colnames, List *exclusionOpNames,
                                bool primary, bool isconstraint)
@@ -1678,7 +1659,7 @@ ChooseIndexNameAddition(List *colnames)
  *
  * Returns a List of plain strings (char *, not String nodes).
  */
-List *
+static List *
 ChooseIndexColumnNames(List *indexElems)
 {
        List       *result = NIL;
index d69809a2f866de511e7ce278b8dca9bf9db33d40..70e408cb6ea1fc6a80b943f22d3a91d226226bb8 100644 (file)
@@ -5389,6 +5389,7 @@ ATExecAddIndex(AlteredTableInfo *tab, Relation rel,
        Oid                     new_index;
 
        Assert(IsA(stmt, IndexStmt));
+       Assert(!stmt->concurrent);
 
        /* suppress schema rights check when rebuilding existing index */
        check_rights = !is_rebuild;
@@ -5399,26 +5400,12 @@ ATExecAddIndex(AlteredTableInfo *tab, Relation rel,
 
        /* The IndexStmt has already been through transformIndexStmt */
 
-       new_index = DefineIndex(stmt->relation,         /* relation */
-                                                       stmt->idxname,          /* index name */
+       new_index = DefineIndex(stmt,
                                                        InvalidOid, /* no predefined OID */
-                                                       stmt->oldNode,
-                                                       stmt->accessMethod, /* am name */
-                                                       stmt->tableSpace,
-                                                       stmt->indexParams,      /* parameters */
-                                                       (Expr *) stmt->whereClause,
-                                                       stmt->options,
-                                                       stmt->excludeOpNames,
-                                                       stmt->unique,
-                                                       stmt->primary,
-                                                       stmt->isconstraint,
-                                                       stmt->deferrable,
-                                                       stmt->initdeferred,
                                                        true,           /* is_alter_table */
                                                        check_rights,
                                                        skip_build,
-                                                       quiet,
-                                                       false);
+                                                       quiet);
 
        /*
         * If TryReuseIndex() stashed a relfilenode for us, we used it for the new
@@ -7968,7 +7955,6 @@ ATPostAlterTypeParse(Oid oldId, char *cmd,
 static void
 TryReuseIndex(Oid oldId, IndexStmt *stmt)
 {
-
        if (CheckIndexCompatible(oldId,
                                                         stmt->relation,
                                                         stmt->accessMethod,
index 1743b8fdc89735f0f619c290e7286be23f3e72e6..9bac99452d334b6e7a92c662b1287d552945c6d9 100644 (file)
@@ -2826,6 +2826,7 @@ _copyIndexStmt(const IndexStmt *from)
        COPY_NODE_FIELD(options);
        COPY_NODE_FIELD(whereClause);
        COPY_NODE_FIELD(excludeOpNames);
+       COPY_STRING_FIELD(idxcomment);
        COPY_SCALAR_FIELD(indexOid);
        COPY_SCALAR_FIELD(oldNode);
        COPY_SCALAR_FIELD(unique);
index f19ad7702618dcfb7f3be416dc535e6ac11b1b5e..2171d8d018bab95e0ed118384ddf8416502a86e8 100644 (file)
@@ -1250,6 +1250,7 @@ _equalIndexStmt(const IndexStmt *a, const IndexStmt *b)
        COMPARE_NODE_FIELD(options);
        COMPARE_NODE_FIELD(whereClause);
        COMPARE_NODE_FIELD(excludeOpNames);
+       COMPARE_STRING_FIELD(idxcomment);
        COMPARE_SCALAR_FIELD(indexOid);
        COMPARE_SCALAR_FIELD(oldNode);
        COMPARE_SCALAR_FIELD(unique);
index d6dff9de47a982d6e41ea191af2f0415be181d7f..91b54265afe6dadb4f171ec9abdf5ff7ec9c69c5 100644 (file)
@@ -1994,6 +1994,7 @@ _outIndexStmt(StringInfo str, const IndexStmt *node)
        WRITE_NODE_FIELD(options);
        WRITE_NODE_FIELD(whereClause);
        WRITE_NODE_FIELD(excludeOpNames);
+       WRITE_STRING_FIELD(idxcomment);
        WRITE_OID_FIELD(indexOid);
        WRITE_OID_FIELD(oldNode);
        WRITE_BOOL_FIELD(unique);
index 8389337ecc25fa11ce53fd71ac2b5792843ec7cb..142eac3b6f078e995b1e8e0eca1dd37f7e02e4d3 100644 (file)
@@ -5837,7 +5837,14 @@ IndexStmt:       CREATE opt_unique INDEX opt_concurrently opt_index_name
                                        n->options = $12;
                                        n->tableSpace = $13;
                                        n->whereClause = $14;
+                                       n->excludeOpNames = NIL;
+                                       n->idxcomment = NULL;
                                        n->indexOid = InvalidOid;
+                                       n->oldNode = InvalidOid;
+                                       n->primary = false;
+                                       n->isconstraint = false;
+                                       n->deferrable = false;
+                                       n->initdeferred = false;
                                        $$ = (Node *)n;
                                }
                ;
index 53f774cf0fa5964f118bfc21a36fabca20b33a48..f70e2df58fe0625a24aa5b81b8e07e8d9583ec96 100644 (file)
@@ -106,7 +106,6 @@ static void transformTableLikeClause(CreateStmtContext *cxt,
                                                 TableLikeClause *table_like_clause);
 static void transformOfType(CreateStmtContext *cxt,
                                TypeName *ofTypename);
-static char *chooseIndexName(const RangeVar *relation, IndexStmt *index_stmt);
 static IndexStmt *generateClonedIndexStmt(CreateStmtContext *cxt,
                                                Relation source_idx,
                                                const AttrNumber *attmap, int attmap_length);
@@ -872,33 +871,16 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
                        index_stmt = generateClonedIndexStmt(cxt, parent_index,
                                                                                                 attmap, tupleDesc->natts);
 
-                       /* Copy comment on index */
+                       /* Copy comment on index, if requested */
                        if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
                        {
                                comment = GetComment(parent_index_oid, RelationRelationId, 0);
 
-                               if (comment != NULL)
-                               {
-                                       CommentStmt *stmt;
-
-                                       /*
-                                        * We have to assign the index a name now, so that we can
-                                        * reference it in CommentStmt.
-                                        */
-                                       if (index_stmt->idxname == NULL)
-                                               index_stmt->idxname = chooseIndexName(cxt->relation,
-                                                                                                                         index_stmt);
-
-                                       stmt = makeNode(CommentStmt);
-                                       stmt->objtype = OBJECT_INDEX;
-                                       stmt->objname =
-                                               list_make2(makeString(cxt->relation->schemaname),
-                                                                  makeString(index_stmt->idxname));
-                                       stmt->objargs = NIL;
-                                       stmt->comment = comment;
-
-                                       cxt->alist = lappend(cxt->alist, stmt);
-                               }
+                               /*
+                                * We make use of IndexStmt's idxcomment option, so as not to
+                                * need to know now what name the index will have.
+                                */
+                               index_stmt->idxcomment = comment;
                        }
 
                        /* Save it in the inh_indexes list for the time being */
@@ -960,29 +942,6 @@ transformOfType(CreateStmtContext *cxt, TypeName *ofTypename)
        ReleaseSysCache(tuple);
 }
 
-/*
- * chooseIndexName
- *
- * Compute name for an index.  This must match code in indexcmds.c.
- *
- * XXX this is inherently broken because the indexes aren't created
- * immediately, so we fail to resolve conflicts when the same name is
- * derived for multiple indexes.  However, that's a reasonably uncommon
- * situation, so we'll live with it for now.
- */
-static char *
-chooseIndexName(const RangeVar *relation, IndexStmt *index_stmt)
-{
-       Oid                     namespaceId;
-       List       *colnames;
-
-       namespaceId = RangeVarGetCreationNamespace(relation);
-       colnames = ChooseIndexColumnNames(index_stmt->indexParams);
-       return ChooseIndexName(relation->relname, namespaceId,
-                                                  colnames, index_stmt->excludeOpNames,
-                                                  index_stmt->primary, index_stmt->isconstraint);
-}
-
 /*
  * Generate an IndexStmt node using information from an already existing index
  * "source_idx".  Attribute numbers should be adjusted according to attmap.
@@ -1046,7 +1005,10 @@ generateClonedIndexStmt(CreateStmtContext *cxt, Relation source_idx,
                index->tableSpace = get_tablespace_name(idxrelrec->reltablespace);
        else
                index->tableSpace = NULL;
+       index->excludeOpNames = NIL;
+       index->idxcomment = NULL;
        index->indexOid = InvalidOid;
+       index->oldNode = InvalidOid;
        index->unique = idxrec->indisunique;
        index->primary = idxrec->indisprimary;
        index->concurrent = false;
@@ -1504,7 +1466,9 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
        index->whereClause = constraint->where_clause;
        index->indexParams = NIL;
        index->excludeOpNames = NIL;
+       index->idxcomment = NULL;
        index->indexOid = InvalidOid;
+       index->oldNode = InvalidOid;
        index->concurrent = false;
 
        /*
index 33b292e6b7a9c1e47fb34de126cbc34dc5246693..7f36a09f46d297d4e98782c842a0a0f9d7ccb8e7 100644 (file)
@@ -921,26 +921,12 @@ standard_ProcessUtility(Node *parsetree,
                                stmt = transformIndexStmt(stmt, queryString);
 
                                /* ... and do it */
-                               DefineIndex(stmt->relation,             /* relation */
-                                                       stmt->idxname,          /* index name */
+                               DefineIndex(stmt,
                                                        InvalidOid, /* no predefined OID */
-                                                       InvalidOid, /* no previous storage */
-                                                       stmt->accessMethod, /* am name */
-                                                       stmt->tableSpace,
-                                                       stmt->indexParams,      /* parameters */
-                                                       (Expr *) stmt->whereClause,
-                                                       stmt->options,
-                                                       stmt->excludeOpNames,
-                                                       stmt->unique,
-                                                       stmt->primary,
-                                                       stmt->isconstraint,
-                                                       stmt->deferrable,
-                                                       stmt->initdeferred,
                                                        false,          /* is_alter_table */
                                                        true,           /* check_rights */
                                                        false,          /* skip_build */
-                                                       false,          /* quiet */
-                                                       stmt->concurrent);      /* concurrent */
+                                                       false);         /* quiet */
                        }
                        break;
 
index 8f3d2c358dceb73587a934284fb6340b91d91cb4..9b6d57a21e23c38688874bef9fe4a423c6d49289 100644 (file)
 extern void RemoveObjects(DropStmt *stmt);
 
 /* commands/indexcmds.c */
-extern Oid DefineIndex(RangeVar *heapRelation,
-                       char *indexRelationName,
+extern Oid DefineIndex(IndexStmt *stmt,
                        Oid indexRelationId,
-                       Oid relFileNode,
-                       char *accessMethodName,
-                       char *tableSpaceName,
-                       List *attributeList,
-                       Expr *predicate,
-                       List *options,
-                       List *exclusionOpNames,
-                       bool unique,
-                       bool primary,
-                       bool isconstraint,
-                       bool deferrable,
-                       bool initdeferred,
                        bool is_alter_table,
                        bool check_rights,
                        bool skip_build,
-                       bool quiet,
-                       bool concurrent);
+                       bool quiet);
 extern void ReindexIndex(RangeVar *indexRelation);
 extern void ReindexTable(RangeVar *relation);
 extern void ReindexDatabase(const char *databaseName,
@@ -48,10 +34,6 @@ extern char *makeObjectName(const char *name1, const char *name2,
                           const char *label);
 extern char *ChooseRelationName(const char *name1, const char *name2,
                                   const char *label, Oid namespaceid);
-extern char *ChooseIndexName(const char *tabname, Oid namespaceId,
-                               List *colnames, List *exclusionOpNames,
-                               bool primary, bool isconstraint);
-extern List *ChooseIndexColumnNames(List *indexElems);
 extern bool CheckIndexCompatible(Oid oldId,
                                         RangeVar *heapRelation,
                                         char *accessMethodName,
index deff1a374c05fb799ea054039861f253728513fe..e6f1df7feb0f12759a94616de90a5375798e6ee5 100644 (file)
@@ -2018,10 +2018,11 @@ typedef struct FetchStmt
  *             Create Index Statement
  *
  * This represents creation of an index and/or an associated constraint.
- * If indexOid isn't InvalidOid, we are not creating an index, just a
- * UNIQUE/PKEY constraint using an existing index.     isconstraint must always
- * be true in this case, and the fields describing the index properties are
- * empty.
+ * If isconstraint is true, we should create a pg_constraint entry along
+ * with the index.  But if indexOid isn't InvalidOid, we are not creating an
+ * index, just a UNIQUE/PKEY constraint using an existing index.  isconstraint
+ * must always be true in this case, and the fields describing the index
+ * properties are empty.
  * ----------------------
  */
 typedef struct IndexStmt
@@ -2031,15 +2032,16 @@ typedef struct IndexStmt
        RangeVar   *relation;           /* relation to build index on */
        char       *accessMethod;       /* name of access method (eg. btree) */
        char       *tableSpace;         /* tablespace, or NULL for default */
-       List       *indexParams;        /* a list of IndexElem */
-       List       *options;            /* options from WITH clause */
+       List       *indexParams;        /* columns to index: a list of IndexElem */
+       List       *options;            /* WITH clause options: a list of DefElem */
        Node       *whereClause;        /* qualification (partial-index predicate) */
        List       *excludeOpNames; /* exclusion operator names, or NIL if none */
+       char       *idxcomment;         /* comment to apply to index, or NULL */
        Oid                     indexOid;               /* OID of an existing index, if any */
-       Oid                     oldNode;                /* relfilenode of my former self */
+       Oid                     oldNode;                /* relfilenode of existing storage, if any */
        bool            unique;                 /* is index unique? */
-       bool            primary;                /* is index on primary key? */
-       bool            isconstraint;   /* is it from a CONSTRAINT clause? */
+       bool            primary;                /* is index a primary key? */
+       bool            isconstraint;   /* is it for a pkey/unique constraint? */
        bool            deferrable;             /* is the constraint DEFERRABLE? */
        bool            initdeferred;   /* is the constraint INITIALLY DEFERRED? */
        bool            concurrent;             /* should this be a concurrent index build? */