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:
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();
}
;
......@@ -298,18 +314,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();
}
;
......
This diff is collapsed.
......@@ -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,
......
......@@ -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);
......
......@@ -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);
......
......@@ -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);
......
......@@ -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;
}
;
......
......@@ -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;
/*
......
......@@ -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;
......
......@@ -20,26 +20,12 @@
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,
......
......@@ -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? */
......
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