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();
} }
; ;
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "catalog/pg_opfamily.h" #include "catalog/pg_opfamily.h"
#include "catalog/pg_tablespace.h" #include "catalog/pg_tablespace.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "commands/comment.h"
#include "commands/dbcommands.h" #include "commands/dbcommands.h"
#include "commands/defrem.h" #include "commands/defrem.h"
#include "commands/tablecmds.h" #include "commands/tablecmds.h"
...@@ -65,7 +66,11 @@ static void ComputeIndexAttrs(IndexInfo *indexInfo, ...@@ -65,7 +66,11 @@ static void ComputeIndexAttrs(IndexInfo *indexInfo,
bool isconstraint); bool isconstraint);
static Oid GetIndexOpClass(List *opclass, Oid attrType, static Oid GetIndexOpClass(List *opclass, Oid attrType,
char *accessMethodName, Oid accessMethodId); 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 char *ChooseIndexNameAddition(List *colnames);
static List *ChooseIndexColumnNames(List *indexElems);
static void RangeVarCallbackForReindexIndex(const RangeVar *relation, static void RangeVarCallbackForReindexIndex(const RangeVar *relation,
Oid relId, Oid oldRelId, void *arg); Oid relId, Oid oldRelId, void *arg);
...@@ -268,60 +273,28 @@ CheckIndexCompatible(Oid oldId, ...@@ -268,60 +273,28 @@ CheckIndexCompatible(Oid oldId,
* DefineIndex * DefineIndex
* Creates a new index. * Creates a new index.
* *
* 'heapRelation': the relation the index will apply to. * 'stmt': IndexStmt describing the properties of the new index.
* 'indexRelationName': the name for the new index, or NULL to indicate
* that a nonconflicting default name should be picked.
* 'indexRelationId': normally InvalidOid, but during bootstrap can be * 'indexRelationId': normally InvalidOid, but during bootstrap can be
* nonzero to specify a preselected OID for the index. * 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. * '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 * 'check_rights': check for CREATE rights in the namespace. (This should
* be true except when ALTER is deleting/recreating an index.) * be true except when ALTER is deleting/recreating an index.)
* 'skip_build': make the catalog entries but leave the index file empty; * 'skip_build': make the catalog entries but leave the index file empty;
* it will be filled later. * it will be filled later.
* 'quiet': suppress the NOTICE chatter ordinarily provided for constraints. * '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. * Returns the OID of the created index.
*/ */
Oid Oid
DefineIndex(RangeVar *heapRelation, 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)
{ {
char *indexRelationName;
char *accessMethodName;
Oid *typeObjectId; Oid *typeObjectId;
Oid *collationObjectId; Oid *collationObjectId;
Oid *classObjectId; Oid *classObjectId;
...@@ -354,7 +327,7 @@ DefineIndex(RangeVar *heapRelation, ...@@ -354,7 +327,7 @@ DefineIndex(RangeVar *heapRelation,
/* /*
* count attributes in index * count attributes in index
*/ */
numberOfAttributes = list_length(attributeList); numberOfAttributes = list_length(stmt->indexParams);
if (numberOfAttributes <= 0) if (numberOfAttributes <= 0)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION), (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
...@@ -372,8 +345,8 @@ DefineIndex(RangeVar *heapRelation, ...@@ -372,8 +345,8 @@ DefineIndex(RangeVar *heapRelation,
* index build; but for concurrent builds we allow INSERT/UPDATE/DELETE * index build; but for concurrent builds we allow INSERT/UPDATE/DELETE
* (but not VACUUM). * (but not VACUUM).
*/ */
rel = heap_openrv(heapRelation, rel = heap_openrv(stmt->relation,
(concurrent ? ShareUpdateExclusiveLock : ShareLock)); (stmt->concurrent ? ShareUpdateExclusiveLock : ShareLock));
relationId = RelationGetRelid(rel); relationId = RelationGetRelid(rel);
namespaceId = RelationGetNamespace(rel); namespaceId = RelationGetNamespace(rel);
...@@ -389,12 +362,12 @@ DefineIndex(RangeVar *heapRelation, ...@@ -389,12 +362,12 @@ DefineIndex(RangeVar *heapRelation,
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE), (errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("cannot create index on foreign table \"%s\"", errmsg("cannot create index on foreign table \"%s\"",
heapRelation->relname))); RelationGetRelationName(rel))));
else else
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE), (errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is not a table", errmsg("\"%s\" is not a table",
heapRelation->relname))); RelationGetRelationName(rel))));
} }
/* /*
...@@ -426,9 +399,9 @@ DefineIndex(RangeVar *heapRelation, ...@@ -426,9 +399,9 @@ DefineIndex(RangeVar *heapRelation,
* Select tablespace to use. If not specified, use default tablespace * Select tablespace to use. If not specified, use default tablespace
* (which may in turn default to database's default). * (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 else
{ {
...@@ -463,22 +436,24 @@ DefineIndex(RangeVar *heapRelation, ...@@ -463,22 +436,24 @@ DefineIndex(RangeVar *heapRelation,
/* /*
* Choose the index column names. * Choose the index column names.
*/ */
indexColNames = ChooseIndexColumnNames(attributeList); indexColNames = ChooseIndexColumnNames(stmt->indexParams);
/* /*
* Select name for index if caller didn't specify * Select name for index if caller didn't specify
*/ */
indexRelationName = stmt->idxname;
if (indexRelationName == NULL) if (indexRelationName == NULL)
indexRelationName = ChooseIndexName(RelationGetRelationName(rel), indexRelationName = ChooseIndexName(RelationGetRelationName(rel),
namespaceId, namespaceId,
indexColNames, indexColNames,
exclusionOpNames, stmt->excludeOpNames,
primary, stmt->primary,
isconstraint); stmt->isconstraint);
/* /*
* look up the access method, verify it can handle the requested features * look up the access method, verify it can handle the requested features
*/ */
accessMethodName = stmt->accessMethod;
tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName)); tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
if (!HeapTupleIsValid(tuple)) if (!HeapTupleIsValid(tuple))
{ {
...@@ -503,7 +478,7 @@ DefineIndex(RangeVar *heapRelation, ...@@ -503,7 +478,7 @@ DefineIndex(RangeVar *heapRelation,
accessMethodId = HeapTupleGetOid(tuple); accessMethodId = HeapTupleGetOid(tuple);
accessMethodForm = (Form_pg_am) GETSTRUCT(tuple); accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
if (unique && !accessMethodForm->amcanunique) if (stmt->unique && !accessMethodForm->amcanunique)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("access method \"%s\" does not support unique indexes", errmsg("access method \"%s\" does not support unique indexes",
...@@ -513,7 +488,7 @@ DefineIndex(RangeVar *heapRelation, ...@@ -513,7 +488,7 @@ DefineIndex(RangeVar *heapRelation,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("access method \"%s\" does not support multicolumn indexes", errmsg("access method \"%s\" does not support multicolumn indexes",
accessMethodName))); accessMethodName)));
if (exclusionOpNames != NIL && !OidIsValid(accessMethodForm->amgettuple)) if (stmt->excludeOpNames && !OidIsValid(accessMethodForm->amgettuple))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("access method \"%s\" does not support exclusion constraints", errmsg("access method \"%s\" does not support exclusion constraints",
...@@ -527,13 +502,14 @@ DefineIndex(RangeVar *heapRelation, ...@@ -527,13 +502,14 @@ DefineIndex(RangeVar *heapRelation,
/* /*
* Validate predicate, if given * Validate predicate, if given
*/ */
if (predicate) if (stmt->whereClause)
CheckPredicate(predicate); CheckPredicate((Expr *) stmt->whereClause);
/* /*
* Parse AM-specific options, convert to text array form, validate. * 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); (void) index_reloptions(amoptions, reloptions, true);
...@@ -545,15 +521,15 @@ DefineIndex(RangeVar *heapRelation, ...@@ -545,15 +521,15 @@ DefineIndex(RangeVar *heapRelation,
indexInfo->ii_NumIndexAttrs = numberOfAttributes; indexInfo->ii_NumIndexAttrs = numberOfAttributes;
indexInfo->ii_Expressions = NIL; /* for now */ indexInfo->ii_Expressions = NIL; /* for now */
indexInfo->ii_ExpressionsState = NIL; 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_PredicateState = NIL;
indexInfo->ii_ExclusionOps = NULL; indexInfo->ii_ExclusionOps = NULL;
indexInfo->ii_ExclusionProcs = NULL; indexInfo->ii_ExclusionProcs = NULL;
indexInfo->ii_ExclusionStrats = NULL; indexInfo->ii_ExclusionStrats = NULL;
indexInfo->ii_Unique = unique; indexInfo->ii_Unique = stmt->unique;
/* In a concurrent build, mark it not-ready-for-inserts */ /* In a concurrent build, mark it not-ready-for-inserts */
indexInfo->ii_ReadyForInserts = !concurrent; indexInfo->ii_ReadyForInserts = !stmt->concurrent;
indexInfo->ii_Concurrent = concurrent; indexInfo->ii_Concurrent = stmt->concurrent;
indexInfo->ii_BrokenHotChain = false; indexInfo->ii_BrokenHotChain = false;
typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid)); typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
...@@ -562,30 +538,30 @@ DefineIndex(RangeVar *heapRelation, ...@@ -562,30 +538,30 @@ DefineIndex(RangeVar *heapRelation,
coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16)); coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16));
ComputeIndexAttrs(indexInfo, ComputeIndexAttrs(indexInfo,
typeObjectId, collationObjectId, classObjectId, typeObjectId, collationObjectId, classObjectId,
coloptions, attributeList, coloptions, stmt->indexParams,
exclusionOpNames, relationId, stmt->excludeOpNames, relationId,
accessMethodName, accessMethodId, accessMethodName, accessMethodId,
amcanorder, isconstraint); amcanorder, stmt->isconstraint);
/* /*
* Extra checks when creating a PRIMARY KEY index. * Extra checks when creating a PRIMARY KEY index.
*/ */
if (primary) if (stmt->primary)
index_check_primary_key(rel, indexInfo, is_alter_table); index_check_primary_key(rel, indexInfo, is_alter_table);
/* /*
* Report index creation if appropriate (delay this till after most of the * Report index creation if appropriate (delay this till after most of the
* error checks) * error checks)
*/ */
if (isconstraint && !quiet) if (stmt->isconstraint && !quiet)
{ {
const char *constraint_type; const char *constraint_type;
if (primary) if (stmt->primary)
constraint_type = "PRIMARY KEY"; constraint_type = "PRIMARY KEY";
else if (unique) else if (stmt->unique)
constraint_type = "UNIQUE"; constraint_type = "UNIQUE";
else if (exclusionOpNames != NIL) else if (stmt->excludeOpNames != NIL)
constraint_type = "EXCLUDE"; constraint_type = "EXCLUDE";
else else
{ {
...@@ -601,27 +577,32 @@ DefineIndex(RangeVar *heapRelation, ...@@ -601,27 +577,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. * 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 * Make the catalog entries for the index, including constraints. Then, if
* not skip_build || concurrent, actually build the index. * not skip_build || concurrent, actually build the index.
*/ */
indexRelationId = indexRelationId =
index_create(rel, indexRelationName, indexRelationId, relFileNode, index_create(rel, indexRelationName, indexRelationId, stmt->oldNode,
indexInfo, indexColNames, indexInfo, indexColNames,
accessMethodId, tablespaceId, accessMethodId, tablespaceId,
collationObjectId, classObjectId, collationObjectId, classObjectId,
coloptions, reloptions, primary, coloptions, reloptions, stmt->primary,
isconstraint, deferrable, initdeferred, stmt->isconstraint, stmt->deferrable, stmt->initdeferred,
allowSystemTableMods, allowSystemTableMods,
skip_build || concurrent, skip_build || stmt->concurrent,
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 */ /* Close the heap and we're done, in the non-concurrent case */
heap_close(rel, NoLock); heap_close(rel, NoLock);
...@@ -709,7 +690,7 @@ DefineIndex(RangeVar *heapRelation, ...@@ -709,7 +690,7 @@ DefineIndex(RangeVar *heapRelation,
*/ */
/* Open and lock the parent heap relation */ /* Open and lock the parent heap relation */
rel = heap_openrv(heapRelation, ShareUpdateExclusiveLock); rel = heap_openrv(stmt->relation, ShareUpdateExclusiveLock);
/* And the target index relation */ /* And the target index relation */
indexRelation = index_open(indexRelationId, RowExclusiveLock); indexRelation = index_open(indexRelationId, RowExclusiveLock);
...@@ -724,7 +705,7 @@ DefineIndex(RangeVar *heapRelation, ...@@ -724,7 +705,7 @@ DefineIndex(RangeVar *heapRelation,
indexInfo->ii_BrokenHotChain = false; indexInfo->ii_BrokenHotChain = false;
/* Now build the index */ /* 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 */ /* Close both the relations, but keep the locks */
heap_close(rel, NoLock); heap_close(rel, NoLock);
...@@ -1594,7 +1575,7 @@ ChooseRelationName(const char *name1, const char *name2, ...@@ -1594,7 +1575,7 @@ ChooseRelationName(const char *name1, const char *name2,
* *
* The argument list is pretty ad-hoc :-( * The argument list is pretty ad-hoc :-(
*/ */
char * static char *
ChooseIndexName(const char *tabname, Oid namespaceId, ChooseIndexName(const char *tabname, Oid namespaceId,
List *colnames, List *exclusionOpNames, List *colnames, List *exclusionOpNames,
bool primary, bool isconstraint) bool primary, bool isconstraint)
...@@ -1676,7 +1657,7 @@ ChooseIndexNameAddition(List *colnames) ...@@ -1676,7 +1657,7 @@ ChooseIndexNameAddition(List *colnames)
* *
* Returns a List of plain strings (char *, not String nodes). * Returns a List of plain strings (char *, not String nodes).
*/ */
List * static List *
ChooseIndexColumnNames(List *indexElems) ChooseIndexColumnNames(List *indexElems)
{ {
List *result = NIL; List *result = NIL;
......
...@@ -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) /*
{ * We make use of IndexStmt's idxcomment option, so as not to
CommentStmt *stmt; * need to know now what name the index will have.
*/
/* index_stmt->idxcomment = comment;
* 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);
}
} }
/* 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