From e99f0529041fff97842b22f6fb9d18105fedea4c Mon Sep 17 00:00:00 2001 From: Ahmet Gedemenli Date: Mon, 25 Jan 2021 21:25:31 +0300 Subject: [PATCH] Fix index renaming when creating citus local tables --- .../commands/create_citus_local_table.c | 31 +++++++++---------- .../regress/expected/citus_local_tables.out | 29 +++++++++++------ src/test/regress/sql/citus_local_tables.sql | 11 +++++-- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/backend/distributed/commands/create_citus_local_table.c b/src/backend/distributed/commands/create_citus_local_table.c index c60e4f115..6158c1806 100644 --- a/src/backend/distributed/commands/create_citus_local_table.c +++ b/src/backend/distributed/commands/create_citus_local_table.c @@ -52,7 +52,7 @@ static char * GetRenameShardConstraintCommand(Oid relationId, char *constraintNa static void RenameShardRelationIndexes(Oid shardRelationId, uint64 shardId); static void RenameShardRelationStatistics(Oid shardRelationId, uint64 shardId); static char * GetDropTriggerCommand(Oid relationId, char *triggerName); -static char * GetRenameShardIndexCommand(char *indexName, uint64 shardId); +static char * GetRenameShardIndexCommand(Oid indexOid, uint64 shardId); static char * GetRenameShardStatsCommand(char *statSchema, char *statsName, char *statsNameWithShardId); static void RenameShardRelationNonTruncateTriggers(Oid shardRelationId, uint64 shardId); @@ -60,7 +60,7 @@ static char * GetRenameShardTriggerCommand(Oid shardRelationId, char *triggerNam uint64 shardId); static void DropRelationTruncateTriggers(Oid relationId); static char * GetDropTriggerCommand(Oid relationId, char *triggerName); -static List * GetExplicitIndexNameList(Oid relationId); +static List * GetExplicitIndexOidList(Oid relationId); static List * GetRenameStatsCommandList(List *statsOidList, uint64 shardId); static void DropAndMoveDefaultSequenceOwnerships(Oid sourceRelationId, Oid targetRelationId); @@ -539,18 +539,18 @@ GetRenameShardConstraintCommand(Oid relationId, char *constraintName, uint64 sha * RenameShardRelationIndexes appends given shardId to the end of the names * of shard relation indexes except the ones that are already renamed via * RenameShardRelationConstraints. This function utilizes - * GetExplicitIndexNameList to pick the indexes to be renamed, see more + * GetExplicitIndexOidList to pick the indexes to be renamed, see more * details in function's comment. */ static void RenameShardRelationIndexes(Oid shardRelationId, uint64 shardId) { - List *indexNameList = GetExplicitIndexNameList(shardRelationId); + List *indexOidList = GetExplicitIndexOidList(shardRelationId); - char *indexName = NULL; - foreach_ptr(indexName, indexNameList) + Oid indexOid = InvalidOid; + foreach_oid(indexOid, indexOidList) { - const char *commandString = GetRenameShardIndexCommand(indexName, shardId); + const char *commandString = GetRenameShardIndexCommand(indexOid, shardId); ExecuteAndLogDDLCommand(commandString); } } @@ -561,13 +561,14 @@ RenameShardRelationIndexes(Oid shardRelationId, uint64 shardId) * the index with indexName. */ static char * -GetRenameShardIndexCommand(char *indexName, uint64 shardId) +GetRenameShardIndexCommand(Oid indexOid, uint64 shardId) { + char *indexName = get_rel_name(indexOid); char *shardIndexName = pstrdup(indexName); AppendShardIdToName(&shardIndexName, shardId); const char *quotedShardIndexName = quote_identifier(shardIndexName); - const char *quotedIndexName = quote_identifier(indexName); + const char *quotedIndexName = generate_qualified_relation_name(indexOid); StringInfo renameCommand = makeStringInfo(); appendStringInfo(renameCommand, "ALTER INDEX %s RENAME TO %s;", @@ -724,7 +725,7 @@ GetDropTriggerCommand(Oid relationId, char *triggerName) /* - * GetExplicitIndexNameList returns a list of index names defined "explicitly" + * GetExplicitIndexOidList returns a list of index oids defined "explicitly" * on the relation with relationId by the "CREATE INDEX" commands. That means, * all the constraints defined on the relation except: * - primary indexes, @@ -733,7 +734,7 @@ GetDropTriggerCommand(Oid relationId, char *triggerName) * that are actually applied by the related constraints. */ static List * -GetExplicitIndexNameList(Oid relationId) +GetExplicitIndexOidList(Oid relationId) { int scanKeyCount = 1; ScanKeyData scanKey[1]; @@ -750,7 +751,7 @@ GetExplicitIndexNameList(Oid relationId) useIndex, NULL, scanKeyCount, scanKey); - List *indexNameList = NIL; + List *indexOidList = NIL; HeapTuple heapTuple = systable_getnext(scanDescriptor); while (HeapTupleIsValid(heapTuple)) @@ -767,9 +768,7 @@ GetExplicitIndexNameList(Oid relationId) */ if (!indexImpliedByConstraint) { - char *indexName = get_rel_name(indexId); - - indexNameList = lappend(indexNameList, pstrdup(indexName)); + indexOidList = lappend_oid(indexOidList, indexId); } heapTuple = systable_getnext(scanDescriptor); @@ -781,7 +780,7 @@ GetExplicitIndexNameList(Oid relationId) /* revert back to original search_path */ PopOverrideSearchPath(); - return indexNameList; + return indexOidList; } diff --git a/src/test/regress/expected/citus_local_tables.out b/src/test/regress/expected/citus_local_tables.out index 1df2ca16a..5c50152fa 100644 --- a/src/test/regress/expected/citus_local_tables.out +++ b/src/test/regress/expected/citus_local_tables.out @@ -535,6 +535,18 @@ ORDER BY 1; unique_a_b_1504027 | citus_local_table_1_1504027 | 1 2 (2 rows) +-- test creating citus local table with an index from non-default schema +CREATE SCHEMA "test_\'index_schema"; +CREATE TABLE "test_\'index_schema".testindex (a int, b int); +CREATE INDEX ind ON "test_\'index_schema".testindex (a); +ALTER TABLE "test_\'index_schema".testindex ADD CONSTRAINT fkey_to_dummy_ref FOREIGN KEY (a) REFERENCES dummy_reference_table(a); +NOTICE: executing the command locally: SELECT worker_apply_inter_shard_ddl_command (1504040, E'test_\\''index_schema', 1504037, 'citus_local_tables_test_schema', E'ALTER TABLE "test_\\''index_schema".testindex ADD CONSTRAINT fkey_to_dummy_ref FOREIGN KEY (a) REFERENCES dummy_reference_table(a);') +SELECT COUNT(*)=2 FROM pg_indexes WHERE tablename LIKE 'testindex%' AND indexname LIKE 'ind%'; + ?column? +--------------------------------------------------------------------- + t +(1 row) + -- execute truncate & drop commands for multiple relations to see that we don't break local execution TRUNCATE citus_local_table_1, citus_local_table_2, distributed_table, local_table, reference_table, local_table_4; NOTICE: executing the command locally: TRUNCATE TABLE citus_local_tables_test_schema.citus_local_table_1_xxxxx CASCADE @@ -741,15 +753,12 @@ CREATE STATISTICS stx1 ON a, b FROM test_citus_local_table_with_stats; ALTER TABLE test_citus_local_table_with_stats ADD CONSTRAINT fkey_to_dummy_ref FOREIGN KEY (a) REFERENCES dummy_reference_table(a); NOTICE: executing the command locally: SELECT worker_apply_inter_shard_ddl_command (xxxxx, 'citus_local_tables_test_schema', xxxxx, 'citus_local_tables_test_schema', 'ALTER TABLE test_citus_local_table_with_stats ADD CONSTRAINT fkey_to_dummy_ref FOREIGN KEY (a) REFERENCES dummy_reference_table(a);') CREATE STATISTICS "CiTUS!LocalTables"."Bad\'StatName" ON a, b FROM test_citus_local_table_with_stats; -NOTICE: executing the command locally: SELECT worker_apply_shard_ddl_command (1504043, 'citus_local_tables_test_schema', E'CREATE STATISTICS "CiTUS!LocalTables"."Bad\\''StatName" ON a, b FROM citus_local_tables_test_schema.test_citus_local_table_with_stats') -SELECT stxname FROM pg_statistic_ext ORDER BY stxname; - stxname +NOTICE: executing the command locally: SELECT worker_apply_shard_ddl_command (1504044, 'citus_local_tables_test_schema', E'CREATE STATISTICS "CiTUS!LocalTables"."Bad\\''StatName" ON a, b FROM citus_local_tables_test_schema.test_citus_local_table_with_stats') +SELECT COUNT(*)=4 FROM pg_statistic_ext WHERE stxname LIKE 'stx1%' or stxname LIKE 'Bad\\''StatName%' ; + ?column? --------------------------------------------------------------------- - Bad\'StatName - Bad\'StatName_1504043 - stx1 - stx1_1504043 -(4 rows) + t +(1 row) -- observe the debug messages telling that we switch to sequential -- execution when truncating a citus local table that is referenced @@ -766,5 +775,5 @@ NOTICE: executing the command locally: TRUNCATE TABLE citus_local_tables_test_s RESET client_min_messages; \set VERBOSITY terse -- cleanup at exit -DROP SCHEMA citus_local_tables_test_schema, "CiTUS!LocalTables" CASCADE; -NOTICE: drop cascades to 24 other objects +DROP SCHEMA citus_local_tables_test_schema, "CiTUS!LocalTables", "test_\'index_schema" CASCADE; +NOTICE: drop cascades to 26 other objects diff --git a/src/test/regress/sql/citus_local_tables.sql b/src/test/regress/sql/citus_local_tables.sql index 861c7a51d..4fd8e045c 100644 --- a/src/test/regress/sql/citus_local_tables.sql +++ b/src/test/regress/sql/citus_local_tables.sql @@ -388,6 +388,13 @@ FROM pg_index WHERE indrelid::regclass::text LIKE 'citus_local_table_1%' AND indexrelid::regclass::text LIKE 'unique_a_b%' ORDER BY 1; +-- test creating citus local table with an index from non-default schema +CREATE SCHEMA "test_\'index_schema"; +CREATE TABLE "test_\'index_schema".testindex (a int, b int); +CREATE INDEX ind ON "test_\'index_schema".testindex (a); +ALTER TABLE "test_\'index_schema".testindex ADD CONSTRAINT fkey_to_dummy_ref FOREIGN KEY (a) REFERENCES dummy_reference_table(a); +SELECT COUNT(*)=2 FROM pg_indexes WHERE tablename LIKE 'testindex%' AND indexname LIKE 'ind%'; + -- execute truncate & drop commands for multiple relations to see that we don't break local execution TRUNCATE citus_local_table_1, citus_local_table_2, distributed_table, local_table, reference_table, local_table_4; @@ -489,7 +496,7 @@ CREATE TABLE test_citus_local_table_with_stats(a int, b int); CREATE STATISTICS stx1 ON a, b FROM test_citus_local_table_with_stats; ALTER TABLE test_citus_local_table_with_stats ADD CONSTRAINT fkey_to_dummy_ref FOREIGN KEY (a) REFERENCES dummy_reference_table(a); CREATE STATISTICS "CiTUS!LocalTables"."Bad\'StatName" ON a, b FROM test_citus_local_table_with_stats; -SELECT stxname FROM pg_statistic_ext ORDER BY stxname; +SELECT COUNT(*)=4 FROM pg_statistic_ext WHERE stxname LIKE 'stx1%' or stxname LIKE 'Bad\\''StatName%' ; -- observe the debug messages telling that we switch to sequential -- execution when truncating a citus local table that is referenced @@ -503,4 +510,4 @@ RESET client_min_messages; \set VERBOSITY terse -- cleanup at exit -DROP SCHEMA citus_local_tables_test_schema, "CiTUS!LocalTables" CASCADE; +DROP SCHEMA citus_local_tables_test_schema, "CiTUS!LocalTables", "test_\'index_schema" CASCADE;