From 1df4b8ba32f4f16dbe45730b82173efdd02b4049 Mon Sep 17 00:00:00 2001 From: Brian Cloutier Date: Mon, 28 Mar 2016 06:08:46 -0700 Subject: [PATCH] Better error on "CREATE INDEX already_exists ..." Previously (if you're creating the index with the same name on different tables) we successfully ran the command on the workers before failing it on the master and leaving no record of the index. Now we check whether the index exists on the master before sending commands to the workers. -- Also make the error better when user attampts to create an index without a name. Previously those statements returned: brian=# create index on c (b); WARNING: could not receive query results from localhost:9700 DETAIL: Client error: cannot extend name for null index name ERROR: could not execute DDL command on worker node shards They now return brian=# create index on c (b); ERROR: creating index without a name on a distributed table is currently unsupported --- .../distributed/executor/multi_utility.c | 19 +++++++++++++++++++ .../expected/multi_index_statements.out | 6 +++--- .../regress/sql/multi_index_statements.sql | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/backend/distributed/executor/multi_utility.c b/src/backend/distributed/executor/multi_utility.c index c6fca17b5..564c91251 100644 --- a/src/backend/distributed/executor/multi_utility.c +++ b/src/backend/distributed/executor/multi_utility.c @@ -572,6 +572,25 @@ ProcessAlterTableStmt(AlterTableStmt *alterTableStatement, const char *alterTabl static void ErrorIfUnsupportedIndexStmt(IndexStmt *createIndexStatement) { + Oid namespaceId; + Oid indexRelationId; + char* indexRelationName = createIndexStatement->idxname; + + if (indexRelationName == NULL) + { + ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("creating index without a name on a distributed table is " + "currently unsupported"))); + } + + namespaceId = get_namespace_oid(createIndexStatement->relation->schemaname, false); + indexRelationId = get_relname_relid(indexRelationName, namespaceId); + if (indexRelationId != InvalidOid) + { + ereport(ERROR, (errcode(ERRCODE_DUPLICATE_TABLE), + errmsg("relation \"%s\" already exists", indexRelationName))); + } + if (createIndexStatement->tableSpace != NULL) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), diff --git a/src/test/regress/expected/multi_index_statements.out b/src/test/regress/expected/multi_index_statements.out index 73e834e50..d6f903a94 100644 --- a/src/test/regress/expected/multi_index_statements.out +++ b/src/test/regress/expected/multi_index_statements.out @@ -135,9 +135,7 @@ ERROR: creating unique indexes on append-partitioned tables is currently unsupp -- Verify that we error out in case of postgres errors on supported statement -- types. CREATE INDEX lineitem_orderkey_index ON lineitem (l_orderkey); -WARNING: could not receive query results from localhost:57638 -DETAIL: Client error: relation "lineitem_orderkey_index_102014" already exists -ERROR: could not execute DDL command on worker node shards +ERROR: relation "lineitem_orderkey_index" already exists CREATE INDEX try_index ON lineitem USING gist (l_orderkey); WARNING: could not receive query results from localhost:57638 DETAIL: Client error: data type bigint has no default operator class for access method "gist" @@ -146,6 +144,8 @@ CREATE INDEX try_index ON lineitem (non_existent_column); WARNING: could not receive query results from localhost:57638 DETAIL: Client error: column "non_existent_column" does not exist ERROR: could not execute DDL command on worker node shards +CREATE INDEX ON lineitem (l_orderkey); +ERROR: creating index without a name on a distributed table is currently unsupported -- Verify that none of failed indexes got created on the master node SELECT * FROM pg_indexes WHERE tablename = 'lineitem' or tablename like 'index_test_%' ORDER BY indexname; schemaname | tablename | indexname | tablespace | indexdef diff --git a/src/test/regress/sql/multi_index_statements.sql b/src/test/regress/sql/multi_index_statements.sql index 942680da6..66be10f63 100644 --- a/src/test/regress/sql/multi_index_statements.sql +++ b/src/test/regress/sql/multi_index_statements.sql @@ -73,6 +73,7 @@ CREATE UNIQUE INDEX try_unique_append_index_a_b ON index_test_append(a,b); CREATE INDEX lineitem_orderkey_index ON lineitem (l_orderkey); CREATE INDEX try_index ON lineitem USING gist (l_orderkey); CREATE INDEX try_index ON lineitem (non_existent_column); +CREATE INDEX ON lineitem (l_orderkey); -- Verify that none of failed indexes got created on the master node SELECT * FROM pg_indexes WHERE tablename = 'lineitem' or tablename like 'index_test_%' ORDER BY indexname;