Commit c92be3c0 authored by Tom Lane's avatar Tom Lane

Avoid pre-determining index names during CREATE TABLE LIKE parsing.

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.
parent 54fd196f
...@@ -279,18 +279,34 @@ Boot_InsertStmt: ...@@ -279,18 +279,34 @@ Boot_InsertStmt:
Boot_DeclareIndexStmt: Boot_DeclareIndexStmt:
XDECLARE INDEX boot_ident oidspec ON boot_ident USING boot_ident LPAREN boot_index_params RPAREN XDECLARE INDEX boot_ident oidspec ON boot_ident USING boot_ident LPAREN boot_index_params RPAREN
{ {
IndexStmt *stmt = makeNode(IndexStmt);
do_start(); do_start();
DefineIndex(makeRangeVar(NULL, $6, -1), stmt->idxname = $3;
$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, $4,
InvalidOid, false,
$8, false,
NULL, true, /* skip_build */
$10, false);
NULL, NIL, NIL,
false, false, false, false, false,
false, false, true, false, false);
do_end(); do_end();
} }
; ;
...@@ -298,18 +314,34 @@ Boot_DeclareIndexStmt: ...@@ -298,18 +314,34 @@ Boot_DeclareIndexStmt:
Boot_DeclareUniqueIndexStmt: Boot_DeclareUniqueIndexStmt:
XDECLARE UNIQUE INDEX boot_ident oidspec ON boot_ident USING boot_ident LPAREN boot_index_params RPAREN XDECLARE UNIQUE INDEX boot_ident oidspec ON boot_ident USING boot_ident LPAREN boot_index_params RPAREN
{ {
IndexStmt *stmt = makeNode(IndexStmt);
do_start(); do_start();
DefineIndex(makeRangeVar(NULL, $7, -1), stmt->idxname = $4;
$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, $5,
InvalidOid, false,
$9, false,
NULL, true, /* skip_build */
$11, false);
NULL, NIL, NIL,
true, false, false, false, false,
false, false, true, false, false);
do_end(); do_end();
} }
; ;
......
This diff is collapsed.
...@@ -5389,6 +5389,7 @@ ATExecAddIndex(AlteredTableInfo *tab, Relation rel, ...@@ -5389,6 +5389,7 @@ ATExecAddIndex(AlteredTableInfo *tab, Relation rel,
Oid new_index; Oid new_index;
Assert(IsA(stmt, IndexStmt)); Assert(IsA(stmt, IndexStmt));
Assert(!stmt->concurrent);
/* suppress schema rights check when rebuilding existing index */ /* suppress schema rights check when rebuilding existing index */
check_rights = !is_rebuild; check_rights = !is_rebuild;
...@@ -5399,26 +5400,12 @@ ATExecAddIndex(AlteredTableInfo *tab, Relation rel, ...@@ -5399,26 +5400,12 @@ ATExecAddIndex(AlteredTableInfo *tab, Relation rel,
/* The IndexStmt has already been through transformIndexStmt */ /* The IndexStmt has already been through transformIndexStmt */
new_index = DefineIndex(stmt->relation, /* relation */ new_index = DefineIndex(stmt,
stmt->idxname, /* index name */
InvalidOid, /* no predefined OID */ 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 */ true, /* is_alter_table */
check_rights, check_rights,
skip_build, skip_build,
quiet, quiet);
false);
/* /*
* If TryReuseIndex() stashed a relfilenode for us, we used it for the new * If TryReuseIndex() stashed a relfilenode for us, we used it for the new
...@@ -7968,7 +7955,6 @@ ATPostAlterTypeParse(Oid oldId, char *cmd, ...@@ -7968,7 +7955,6 @@ ATPostAlterTypeParse(Oid oldId, char *cmd,
static void static void
TryReuseIndex(Oid oldId, IndexStmt *stmt) TryReuseIndex(Oid oldId, IndexStmt *stmt)
{ {
if (CheckIndexCompatible(oldId, if (CheckIndexCompatible(oldId,
stmt->relation, stmt->relation,
stmt->accessMethod, stmt->accessMethod,
......
...@@ -2826,6 +2826,7 @@ _copyIndexStmt(const IndexStmt *from) ...@@ -2826,6 +2826,7 @@ _copyIndexStmt(const IndexStmt *from)
COPY_NODE_FIELD(options); COPY_NODE_FIELD(options);
COPY_NODE_FIELD(whereClause); COPY_NODE_FIELD(whereClause);
COPY_NODE_FIELD(excludeOpNames); COPY_NODE_FIELD(excludeOpNames);
COPY_STRING_FIELD(idxcomment);
COPY_SCALAR_FIELD(indexOid); COPY_SCALAR_FIELD(indexOid);
COPY_SCALAR_FIELD(oldNode); COPY_SCALAR_FIELD(oldNode);
COPY_SCALAR_FIELD(unique); COPY_SCALAR_FIELD(unique);
......
...@@ -1250,6 +1250,7 @@ _equalIndexStmt(const IndexStmt *a, const IndexStmt *b) ...@@ -1250,6 +1250,7 @@ _equalIndexStmt(const IndexStmt *a, const IndexStmt *b)
COMPARE_NODE_FIELD(options); COMPARE_NODE_FIELD(options);
COMPARE_NODE_FIELD(whereClause); COMPARE_NODE_FIELD(whereClause);
COMPARE_NODE_FIELD(excludeOpNames); COMPARE_NODE_FIELD(excludeOpNames);
COMPARE_STRING_FIELD(idxcomment);
COMPARE_SCALAR_FIELD(indexOid); COMPARE_SCALAR_FIELD(indexOid);
COMPARE_SCALAR_FIELD(oldNode); COMPARE_SCALAR_FIELD(oldNode);
COMPARE_SCALAR_FIELD(unique); COMPARE_SCALAR_FIELD(unique);
......
...@@ -1994,6 +1994,7 @@ _outIndexStmt(StringInfo str, const IndexStmt *node) ...@@ -1994,6 +1994,7 @@ _outIndexStmt(StringInfo str, const IndexStmt *node)
WRITE_NODE_FIELD(options); WRITE_NODE_FIELD(options);
WRITE_NODE_FIELD(whereClause); WRITE_NODE_FIELD(whereClause);
WRITE_NODE_FIELD(excludeOpNames); WRITE_NODE_FIELD(excludeOpNames);
WRITE_STRING_FIELD(idxcomment);
WRITE_OID_FIELD(indexOid); WRITE_OID_FIELD(indexOid);
WRITE_OID_FIELD(oldNode); WRITE_OID_FIELD(oldNode);
WRITE_BOOL_FIELD(unique); WRITE_BOOL_FIELD(unique);
......
...@@ -5837,7 +5837,14 @@ IndexStmt: CREATE opt_unique INDEX opt_concurrently opt_index_name ...@@ -5837,7 +5837,14 @@ IndexStmt: CREATE opt_unique INDEX opt_concurrently opt_index_name
n->options = $12; n->options = $12;
n->tableSpace = $13; n->tableSpace = $13;
n->whereClause = $14; n->whereClause = $14;
n->excludeOpNames = NIL;
n->idxcomment = NULL;
n->indexOid = InvalidOid; n->indexOid = InvalidOid;
n->oldNode = InvalidOid;
n->primary = false;
n->isconstraint = false;
n->deferrable = false;
n->initdeferred = false;
$$ = (Node *)n; $$ = (Node *)n;
} }
; ;
......
...@@ -106,7 +106,6 @@ static void transformTableLikeClause(CreateStmtContext *cxt, ...@@ -106,7 +106,6 @@ static void transformTableLikeClause(CreateStmtContext *cxt,
TableLikeClause *table_like_clause); TableLikeClause *table_like_clause);
static void transformOfType(CreateStmtContext *cxt, static void transformOfType(CreateStmtContext *cxt,
TypeName *ofTypename); TypeName *ofTypename);
static char *chooseIndexName(const RangeVar *relation, IndexStmt *index_stmt);
static IndexStmt *generateClonedIndexStmt(CreateStmtContext *cxt, static IndexStmt *generateClonedIndexStmt(CreateStmtContext *cxt,
Relation source_idx, Relation source_idx,
const AttrNumber *attmap, int attmap_length); const AttrNumber *attmap, int attmap_length);
...@@ -872,33 +871,16 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla ...@@ -872,33 +871,16 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
index_stmt = generateClonedIndexStmt(cxt, parent_index, index_stmt = generateClonedIndexStmt(cxt, parent_index,
attmap, tupleDesc->natts); attmap, tupleDesc->natts);
/* Copy comment on index */ /* Copy comment on index, if requested */
if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS) if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
{ {
comment = GetComment(parent_index_oid, RelationRelationId, 0); 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 * We make use of IndexStmt's idxcomment option, so as not to
* reference it in CommentStmt. * need to know now what name the index will have.
*/ */
if (index_stmt->idxname == NULL) index_stmt->idxcomment = comment;
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);
}
} }
/* Save it in the inh_indexes list for the time being */ /* Save it in the inh_indexes list for the time being */
...@@ -960,29 +942,6 @@ transformOfType(CreateStmtContext *cxt, TypeName *ofTypename) ...@@ -960,29 +942,6 @@ transformOfType(CreateStmtContext *cxt, TypeName *ofTypename)
ReleaseSysCache(tuple); 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 * Generate an IndexStmt node using information from an already existing index
* "source_idx". Attribute numbers should be adjusted according to attmap. * "source_idx". Attribute numbers should be adjusted according to attmap.
...@@ -1046,7 +1005,10 @@ generateClonedIndexStmt(CreateStmtContext *cxt, Relation source_idx, ...@@ -1046,7 +1005,10 @@ generateClonedIndexStmt(CreateStmtContext *cxt, Relation source_idx,
index->tableSpace = get_tablespace_name(idxrelrec->reltablespace); index->tableSpace = get_tablespace_name(idxrelrec->reltablespace);
else else
index->tableSpace = NULL; index->tableSpace = NULL;
index->excludeOpNames = NIL;
index->idxcomment = NULL;
index->indexOid = InvalidOid; index->indexOid = InvalidOid;
index->oldNode = InvalidOid;
index->unique = idxrec->indisunique; index->unique = idxrec->indisunique;
index->primary = idxrec->indisprimary; index->primary = idxrec->indisprimary;
index->concurrent = false; index->concurrent = false;
...@@ -1504,7 +1466,9 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) ...@@ -1504,7 +1466,9 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
index->whereClause = constraint->where_clause; index->whereClause = constraint->where_clause;
index->indexParams = NIL; index->indexParams = NIL;
index->excludeOpNames = NIL; index->excludeOpNames = NIL;
index->idxcomment = NULL;
index->indexOid = InvalidOid; index->indexOid = InvalidOid;
index->oldNode = InvalidOid;
index->concurrent = false; index->concurrent = false;
/* /*
......
...@@ -921,26 +921,12 @@ standard_ProcessUtility(Node *parsetree, ...@@ -921,26 +921,12 @@ standard_ProcessUtility(Node *parsetree,
stmt = transformIndexStmt(stmt, queryString); stmt = transformIndexStmt(stmt, queryString);
/* ... and do it */ /* ... and do it */
DefineIndex(stmt->relation, /* relation */ DefineIndex(stmt,
stmt->idxname, /* index name */
InvalidOid, /* no predefined OID */ 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 */ false, /* is_alter_table */
true, /* check_rights */ true, /* check_rights */
false, /* skip_build */ false, /* skip_build */
false, /* quiet */ false); /* quiet */
stmt->concurrent); /* concurrent */
} }
break; break;
......
...@@ -20,26 +20,12 @@ ...@@ -20,26 +20,12 @@
extern void RemoveObjects(DropStmt *stmt); extern void RemoveObjects(DropStmt *stmt);
/* commands/indexcmds.c */ /* commands/indexcmds.c */
extern Oid DefineIndex(RangeVar *heapRelation, extern Oid DefineIndex(IndexStmt *stmt,
char *indexRelationName,
Oid indexRelationId, 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 is_alter_table,
bool check_rights, bool check_rights,
bool skip_build, bool skip_build,
bool quiet, bool quiet);
bool concurrent);
extern void ReindexIndex(RangeVar *indexRelation); extern void ReindexIndex(RangeVar *indexRelation);
extern void ReindexTable(RangeVar *relation); extern void ReindexTable(RangeVar *relation);
extern void ReindexDatabase(const char *databaseName, extern void ReindexDatabase(const char *databaseName,
...@@ -48,10 +34,6 @@ extern char *makeObjectName(const char *name1, const char *name2, ...@@ -48,10 +34,6 @@ extern char *makeObjectName(const char *name1, const char *name2,
const char *label); const char *label);
extern char *ChooseRelationName(const char *name1, const char *name2, extern char *ChooseRelationName(const char *name1, const char *name2,
const char *label, Oid namespaceid); 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, extern bool CheckIndexCompatible(Oid oldId,
RangeVar *heapRelation, RangeVar *heapRelation,
char *accessMethodName, char *accessMethodName,
......
...@@ -2018,10 +2018,11 @@ typedef struct FetchStmt ...@@ -2018,10 +2018,11 @@ typedef struct FetchStmt
* Create Index Statement * Create Index Statement
* *
* This represents creation of an index and/or an associated constraint. * This represents creation of an index and/or an associated constraint.
* If indexOid isn't InvalidOid, we are not creating an index, just a * If isconstraint is true, we should create a pg_constraint entry along
* UNIQUE/PKEY constraint using an existing index. isconstraint must always * with the index. But if indexOid isn't InvalidOid, we are not creating an
* be true in this case, and the fields describing the index properties are * index, just a UNIQUE/PKEY constraint using an existing index. isconstraint
* empty. * must always be true in this case, and the fields describing the index
* properties are empty.
* ---------------------- * ----------------------
*/ */
typedef struct IndexStmt typedef struct IndexStmt
...@@ -2031,15 +2032,16 @@ typedef struct IndexStmt ...@@ -2031,15 +2032,16 @@ typedef struct IndexStmt
RangeVar *relation; /* relation to build index on */ RangeVar *relation; /* relation to build index on */
char *accessMethod; /* name of access method (eg. btree) */ char *accessMethod; /* name of access method (eg. btree) */
char *tableSpace; /* tablespace, or NULL for default */ char *tableSpace; /* tablespace, or NULL for default */
List *indexParams; /* a list of IndexElem */ List *indexParams; /* columns to index: a list of IndexElem */
List *options; /* options from WITH clause */ List *options; /* WITH clause options: a list of DefElem */
Node *whereClause; /* qualification (partial-index predicate) */ Node *whereClause; /* qualification (partial-index predicate) */
List *excludeOpNames; /* exclusion operator names, or NIL if none */ 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 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 unique; /* is index unique? */
bool primary; /* is index on primary key? */ bool primary; /* is index a primary key? */
bool isconstraint; /* is it from a CONSTRAINT clause? */ bool isconstraint; /* is it for a pkey/unique constraint? */
bool deferrable; /* is the constraint DEFERRABLE? */ bool deferrable; /* is the constraint DEFERRABLE? */
bool initdeferred; /* is the constraint INITIALLY DEFERRED? */ bool initdeferred; /* is the constraint INITIALLY DEFERRED? */
bool concurrent; /* should this be a concurrent index build? */ bool concurrent; /* should this be a concurrent index build? */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment