mirror of
https://github.com/postgres/postgres.git
synced 2025-11-15 03:41:20 +03:00
Implement IF NOT EXIST for CREATE INDEX.
Fabrízio de Royes Mello, reviewed by Marti Raudsepp, Adam Brightwell and me.
This commit is contained in:
@@ -674,6 +674,8 @@ UpdateIndexRelation(Oid indexoid,
|
||||
* will be marked "invalid" and the caller must take additional steps
|
||||
* to fix it up.
|
||||
* is_internal: if true, post creation hook for new index
|
||||
* if_not_exists: if true, do not throw an error if a relation with
|
||||
* the same name already exists.
|
||||
*
|
||||
* Returns the OID of the created index.
|
||||
*/
|
||||
@@ -697,7 +699,8 @@ index_create(Relation heapRelation,
|
||||
bool allow_system_table_mods,
|
||||
bool skip_build,
|
||||
bool concurrent,
|
||||
bool is_internal)
|
||||
bool is_internal,
|
||||
bool if_not_exists)
|
||||
{
|
||||
Oid heapRelationId = RelationGetRelid(heapRelation);
|
||||
Relation pg_class;
|
||||
@@ -773,10 +776,22 @@ index_create(Relation heapRelation,
|
||||
elog(ERROR, "shared relations must be placed in pg_global tablespace");
|
||||
|
||||
if (get_relname_relid(indexRelationName, namespaceId))
|
||||
{
|
||||
if (if_not_exists)
|
||||
{
|
||||
ereport(NOTICE,
|
||||
(errcode(ERRCODE_DUPLICATE_TABLE),
|
||||
errmsg("relation \"%s\" already exists, skipping",
|
||||
indexRelationName)));
|
||||
heap_close(pg_class, RowExclusiveLock);
|
||||
return InvalidOid;
|
||||
}
|
||||
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_DUPLICATE_TABLE),
|
||||
errmsg("relation \"%s\" already exists",
|
||||
indexRelationName)));
|
||||
}
|
||||
|
||||
/*
|
||||
* construct tuple descriptor for index tuples
|
||||
|
||||
@@ -342,7 +342,7 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
|
||||
rel->rd_rel->reltablespace,
|
||||
collationObjectId, classObjectId, coloptions, (Datum) 0,
|
||||
true, false, false, false,
|
||||
true, false, false, true);
|
||||
true, false, false, true, false);
|
||||
|
||||
heap_close(toast_rel, NoLock);
|
||||
|
||||
|
||||
@@ -610,7 +610,14 @@ DefineIndex(Oid relationId,
|
||||
stmt->isconstraint, stmt->deferrable, stmt->initdeferred,
|
||||
allowSystemTableMods,
|
||||
skip_build || stmt->concurrent,
|
||||
stmt->concurrent, !check_rights);
|
||||
stmt->concurrent, !check_rights,
|
||||
stmt->if_not_exists);
|
||||
|
||||
if (!OidIsValid(indexRelationId))
|
||||
{
|
||||
heap_close(rel, NoLock);
|
||||
return indexRelationId;
|
||||
}
|
||||
|
||||
/* Add any requested comment */
|
||||
if (stmt->idxcomment != NULL)
|
||||
|
||||
@@ -2907,6 +2907,7 @@ _copyIndexStmt(const IndexStmt *from)
|
||||
COPY_SCALAR_FIELD(deferrable);
|
||||
COPY_SCALAR_FIELD(initdeferred);
|
||||
COPY_SCALAR_FIELD(concurrent);
|
||||
COPY_SCALAR_FIELD(if_not_exists);
|
||||
|
||||
return newnode;
|
||||
}
|
||||
|
||||
@@ -1210,6 +1210,7 @@ _equalIndexStmt(const IndexStmt *a, const IndexStmt *b)
|
||||
COMPARE_SCALAR_FIELD(deferrable);
|
||||
COMPARE_SCALAR_FIELD(initdeferred);
|
||||
COMPARE_SCALAR_FIELD(concurrent);
|
||||
COMPARE_SCALAR_FIELD(if_not_exists);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6434,6 +6434,32 @@ IndexStmt: CREATE opt_unique INDEX opt_concurrently opt_index_name
|
||||
n->isconstraint = false;
|
||||
n->deferrable = false;
|
||||
n->initdeferred = false;
|
||||
n->if_not_exists = false;
|
||||
$$ = (Node *)n;
|
||||
}
|
||||
| CREATE opt_unique INDEX opt_concurrently IF_P NOT EXISTS index_name
|
||||
ON qualified_name access_method_clause '(' index_params ')'
|
||||
opt_reloptions OptTableSpace where_clause
|
||||
{
|
||||
IndexStmt *n = makeNode(IndexStmt);
|
||||
n->unique = $2;
|
||||
n->concurrent = $4;
|
||||
n->idxname = $8;
|
||||
n->relation = $10;
|
||||
n->accessMethod = $11;
|
||||
n->indexParams = $13;
|
||||
n->options = $15;
|
||||
n->tableSpace = $16;
|
||||
n->whereClause = $17;
|
||||
n->excludeOpNames = NIL;
|
||||
n->idxcomment = NULL;
|
||||
n->indexOid = InvalidOid;
|
||||
n->oldNode = InvalidOid;
|
||||
n->primary = false;
|
||||
n->isconstraint = false;
|
||||
n->deferrable = false;
|
||||
n->initdeferred = false;
|
||||
n->if_not_exists = true;
|
||||
$$ = (Node *)n;
|
||||
}
|
||||
;
|
||||
|
||||
Reference in New Issue
Block a user