Skip to content

Commit fdd8857

Browse files
committed
Block ALTER INDEX/TABLE index_name ALTER COLUMN colname SET (options)
The grammar of this command run on indexes with column names has always been authorized by the parser, and it has never been documented. Since 911e702, it is possible to define opclass parameters as of CREATE INDEX, which actually broke the old case of ALTER INDEX/TABLE where relation-level parameters n_distinct and n_distinct_inherited could be defined for an index (see 76a47c0 and its thread where this point has been touched, still remained unused). Attempting to do that in v13~ would cause the index to become unusable, as there is a new dedicated code path to load opclass parameters instead of the relation-level ones previously available. Note that it is possible to fix things with a manual catalog update to bring the relation back online. This commit disables this command for now as the use of column names for indexes does not make sense anyway, particularly when it comes to index expressions where names are automatically computed. One way to properly support this case properly in the future would be to use column numbers when it comes to indexes, in the same way as ALTER INDEX .. ALTER COLUMN .. SET STATISTICS. Partitioned indexes were already blocked, but not indexes. Some tests are added for both cases. There was some code in ANALYZE to enforce n_distinct to be used for an index expression if the parameter was defined, but just remove it for now until/if there is support for this (note that index-level parameters never had support in pg_dump either, previously), so this was just dead code. Reported-by: Matthijs van der Vleuten Author: Nathan Bossart, Michael Paquier Reviewed-by: Vik Fearing, Dilip Kumar Discussion: https://postgr.es/m/17220-15d684c6c2171a83@postgresql.org Backpatch-through: 13
1 parent d6f1e16 commit fdd8857

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

src/backend/commands/analyze.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,6 @@ compute_index_stats(Relation onerel, double totalrows,
965965
for (i = 0; i < attr_cnt; i++)
966966
{
967967
VacAttrStats *stats = thisdata->vacattrstats[i];
968-
AttributeOpts *aopt =
969-
get_attribute_options(stats->attr->attrelid,
970-
stats->attr->attnum);
971968

972969
stats->exprvals = exprvals + i;
973970
stats->exprnulls = exprnulls + i;
@@ -977,14 +974,6 @@ compute_index_stats(Relation onerel, double totalrows,
977974
numindexrows,
978975
totalindexrows);
979976

980-
/*
981-
* If the n_distinct option is specified, it overrides the
982-
* above computation. For indices, we always use just
983-
* n_distinct, not n_distinct_inherited.
984-
*/
985-
if (aopt != NULL && aopt->n_distinct != 0.0)
986-
stats->stadistinct = aopt->n_distinct;
987-
988977
MemoryContextResetAndDeleteChildren(col_context);
989978
}
990979
}

src/backend/commands/tablecmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4494,7 +4494,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
44944494
break;
44954495
case AT_SetOptions: /* ALTER COLUMN SET ( options ) */
44964496
case AT_ResetOptions: /* ALTER COLUMN RESET ( options ) */
4497-
ATSimplePermissions(cmd->subtype, rel, ATT_TABLE | ATT_MATVIEW | ATT_INDEX | ATT_FOREIGN_TABLE);
4497+
ATSimplePermissions(cmd->subtype, rel, ATT_TABLE | ATT_MATVIEW | ATT_FOREIGN_TABLE);
44984498
/* This command never recurses */
44994499
pass = AT_PASS_MISC;
45004500
break;

src/test/regress/expected/btree_index.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,17 @@ INSERT INTO delete_test_table SELECT i, 1, 2, 3 FROM generate_series(1,1000) i;
329329
-- Test unsupported btree opclass parameters
330330
create index on btree_tall_tbl (id int4_ops(foo=1));
331331
ERROR: operator class int4_ops has no options
332+
-- Test case of ALTER INDEX with abuse of column names for indexes.
333+
-- This grammar is not officially supported, but the parser allows it.
334+
CREATE INDEX btree_tall_idx2 ON btree_tall_tbl (id);
335+
ALTER INDEX btree_tall_idx2 ALTER COLUMN id SET (n_distinct=100);
336+
ERROR: ALTER action ALTER COLUMN ... SET cannot be performed on relation "btree_tall_idx2"
337+
DETAIL: This operation is not supported for indexes.
338+
DROP INDEX btree_tall_idx2;
339+
-- Partitioned index
340+
CREATE TABLE btree_part (id int4) PARTITION BY RANGE (id);
341+
CREATE INDEX btree_part_idx ON btree_part(id);
342+
ALTER INDEX btree_part_idx ALTER COLUMN id SET (n_distinct=100);
343+
ERROR: ALTER action ALTER COLUMN ... SET cannot be performed on relation "btree_part_idx"
344+
DETAIL: This operation is not supported for partitioned indexes.
345+
DROP TABLE btree_part;

src/test/regress/sql/btree_index.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,14 @@ INSERT INTO delete_test_table SELECT i, 1, 2, 3 FROM generate_series(1,1000) i;
172172

173173
-- Test unsupported btree opclass parameters
174174
create index on btree_tall_tbl (id int4_ops(foo=1));
175+
176+
-- Test case of ALTER INDEX with abuse of column names for indexes.
177+
-- This grammar is not officially supported, but the parser allows it.
178+
CREATE INDEX btree_tall_idx2 ON btree_tall_tbl (id);
179+
ALTER INDEX btree_tall_idx2 ALTER COLUMN id SET (n_distinct=100);
180+
DROP INDEX btree_tall_idx2;
181+
-- Partitioned index
182+
CREATE TABLE btree_part (id int4) PARTITION BY RANGE (id);
183+
CREATE INDEX btree_part_idx ON btree_part(id);
184+
ALTER INDEX btree_part_idx ALTER COLUMN id SET (n_distinct=100);
185+
DROP TABLE btree_part;

0 commit comments

Comments
 (0)