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
pull/410/head
Brian Cloutier 2016-03-28 06:08:46 -07:00
parent 1a370cd1ba
commit 1df4b8ba32
3 changed files with 23 additions and 3 deletions

View File

@ -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),

View File

@ -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

View File

@ -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;