From 27c2bd159961dcc70862402948cbe8999c701686 Mon Sep 17 00:00:00 2001 From: Halil Ozan Akgul Date: Mon, 18 Jan 2021 11:52:18 +0300 Subject: [PATCH] Moves creation of ALTER INDEX STATISTICS commands next to index commands --- src/backend/distributed/commands/statistics.c | 18 +----------------- .../distributed/operations/node_protocol.c | 4 ++++ src/include/distributed/commands.h | 1 + .../expected/alter_table_set_access_method.out | 6 +++++- .../sql/alter_table_set_access_method.sql | 3 +++ 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/backend/distributed/commands/statistics.c b/src/backend/distributed/commands/statistics.c index c87677794..16053bdc4 100644 --- a/src/backend/distributed/commands/statistics.c +++ b/src/backend/distributed/commands/statistics.c @@ -49,7 +49,6 @@ #define ALTER_INDEX_COLUMN_SET_STATS_COMMAND \ "ALTER INDEX %s ALTER COLUMN %d SET STATISTICS %d" -static List * GetAlterIndexStatisticsCommands(Oid indexOid); static List * GetExplicitStatisticsIdList(Oid relationId); static char * GenerateAlterIndexColumnSetStatsCommand(char *indexNameWithSchema, int16 attnum, @@ -474,21 +473,6 @@ GetExplicitStatisticsCommandList(Oid relationId) } } - /* find indexes on current relation, in case of modified stats target */ - Relation relation = relation_open(relationId, AccessShareLock); - List *indexOidList = RelationGetIndexList(relation); - relation_close(relation, NoLock); - - Oid indexId = InvalidOid; - foreach_oid(indexId, indexOidList) - { - /* we need alter index commands for altered targets on expression indexes */ - List *alterIndexStatisticsCommands = GetAlterIndexStatisticsCommands(indexId); - - explicitStatisticsCommandList = - list_concat(explicitStatisticsCommandList, alterIndexStatisticsCommands); - } - /* revert back to original search_path */ PopOverrideSearchPath(); @@ -547,7 +531,7 @@ GetExplicitStatisticsSchemaIdList(Oid relationId) * Note that this function only looks for expression indexes, since this command is * valid for only expression indexes. */ -static List * +List * GetAlterIndexStatisticsCommands(Oid indexOid) { List *alterIndexStatisticsCommandList = NIL; diff --git a/src/backend/distributed/operations/node_protocol.c b/src/backend/distributed/operations/node_protocol.c index ed71c7a6c..e794ef7e9 100644 --- a/src/backend/distributed/operations/node_protocol.c +++ b/src/backend/distributed/operations/node_protocol.c @@ -736,6 +736,10 @@ GatherIndexAndConstraintDefinitionList(Form_pg_index indexForm, List **indexDDLE *indexDDLEventList = lappend(*indexDDLEventList, makeTableDDLCommandString( clusteredDef)); } + + /* we need alter index commands for altered targets on expression indexes */ + List *alterIndexStatisticsCommands = GetAlterIndexStatisticsCommands(indexId); + *indexDDLEventList = list_concat(*indexDDLEventList, alterIndexStatisticsCommands); } diff --git a/src/include/distributed/commands.h b/src/include/distributed/commands.h index 0f13a5e09..d83574d99 100644 --- a/src/include/distributed/commands.h +++ b/src/include/distributed/commands.h @@ -348,6 +348,7 @@ extern List * PreprocessAlterStatisticsOwnerStmt(Node *node, const char *querySt processUtilityContext); extern List * GetExplicitStatisticsCommandList(Oid relationId); extern List * GetExplicitStatisticsSchemaIdList(Oid relationId); +extern List * GetAlterIndexStatisticsCommands(Oid indexOid); /* subscription.c - forward declarations */ extern Node * ProcessCreateSubscriptionStmt(CreateSubscriptionStmt *createSubStmt); diff --git a/src/test/regress/expected/alter_table_set_access_method.out b/src/test/regress/expected/alter_table_set_access_method.out index 32b842985..5c00c935d 100644 --- a/src/test/regress/expected/alter_table_set_access_method.out +++ b/src/test/regress/expected/alter_table_set_access_method.out @@ -294,11 +294,15 @@ DROP TABLE time_partitioned; -- the index will be dropped CREATE TABLE index_table (a INT) ; CREATE INDEX idx1 ON index_table (a); +-- also create an index with statistics +CREATE INDEX idx2 ON index_table ((a+1)); +ALTER INDEX idx2 ALTER COLUMN 1 SET STATISTICS 300; SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table'; indexname --------------------------------------------------------------------- idx1 -(1 row) + idx2 +(2 rows) SELECT a.amname FROM pg_class c, pg_am a where c.relname = 'index_table' AND c.relnamespace = 'alter_table_set_access_method'::regnamespace AND c.relam = a.oid; amname diff --git a/src/test/regress/sql/alter_table_set_access_method.sql b/src/test/regress/sql/alter_table_set_access_method.sql index bbc53e7e8..e5b817053 100644 --- a/src/test/regress/sql/alter_table_set_access_method.sql +++ b/src/test/regress/sql/alter_table_set_access_method.sql @@ -99,6 +99,9 @@ DROP TABLE time_partitioned; -- the index will be dropped CREATE TABLE index_table (a INT) USING heap; CREATE INDEX idx1 ON index_table (a); +-- also create an index with statistics +CREATE INDEX idx2 ON index_table ((a+1)); +ALTER INDEX idx2 ALTER COLUMN 1 SET STATISTICS 300; SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table'; SELECT a.amname FROM pg_class c, pg_am a where c.relname = 'index_table' AND c.relnamespace = 'alter_table_set_access_method'::regnamespace AND c.relam = a.oid; SELECT alter_table_set_access_method('index_table', 'columnar');