Moves creation of ALTER INDEX STATISTICS commands next to index commands

pull/4522/head
Halil Ozan Akgul 2021-01-18 11:52:18 +03:00
parent 7124a7715d
commit 27c2bd1599
5 changed files with 14 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

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