Skip to content

Commit cb9db2a

Browse files
alvherreyugo-n
andcommitted
Fix ALTER TABLE...SET STATS error message for included columns
The existing error message was complaining that the column is not an expression, which is not correct. Introduce a suitable wording variation and a test. Co-authored-by: Yugo Nagata <nagata@sraoss.co.jp> Discussion: https://postgr.es/m/20180628182803.e4632d5a.nagata@sraoss.co.jp Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org>
1 parent e353389 commit cb9db2a

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

src/backend/commands/tablecmds.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6504,14 +6504,21 @@ ATExecSetStatistics(Relation rel, const char *colName, int16 colNum, Node *newVa
65046504
errmsg("cannot alter system column \"%s\"",
65056505
colName)));
65066506

6507-
if ((rel->rd_rel->relkind == RELKIND_INDEX ||
6508-
rel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
6509-
rel->rd_index->indkey.values[attnum - 1] != 0)
6510-
ereport(ERROR,
6511-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6512-
errmsg("cannot alter statistics on non-expression column \"%s\" of index \"%s\"",
6513-
NameStr(attrtuple->attname), RelationGetRelationName(rel)),
6514-
errhint("Alter statistics on table column instead.")));
6507+
if (rel->rd_rel->relkind == RELKIND_INDEX ||
6508+
rel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
6509+
{
6510+
if (attnum > rel->rd_index->indnkeyatts)
6511+
ereport(ERROR,
6512+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6513+
errmsg("cannot alter statistics on included column \"%s\" of index \"%s\"",
6514+
NameStr(attrtuple->attname), RelationGetRelationName(rel))));
6515+
else if (rel->rd_index->indkey.values[attnum - 1] != 0)
6516+
ereport(ERROR,
6517+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6518+
errmsg("cannot alter statistics on non-expression column \"%s\" of index \"%s\"",
6519+
NameStr(attrtuple->attname), RelationGetRelationName(rel)),
6520+
errhint("Alter statistics on table column instead.")));
6521+
}
65156522

65166523
attrtuple->attstattarget = newtarget;
65176524

src/test/regress/expected/index_including.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,20 @@ SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl' ORDER BY indexname;
243243
----------
244244
(0 rows)
245245

246+
DROP TABLE tbl;
247+
/*
248+
* 3.3 Test ALTER TABLE SET STATISTICS
249+
*/
250+
CREATE TABLE tbl (c1 int, c2 int);
251+
CREATE INDEX tbl_idx ON tbl (c1, (c1+0)) INCLUDE (c2);
252+
ALTER INDEX tbl_idx ALTER COLUMN 1 SET STATISTICS 1000;
253+
ERROR: cannot alter statistics on non-expression column "c1" of index "tbl_idx"
254+
HINT: Alter statistics on table column instead.
255+
ALTER INDEX tbl_idx ALTER COLUMN 2 SET STATISTICS 1000;
256+
ALTER INDEX tbl_idx ALTER COLUMN 3 SET STATISTICS 1000;
257+
ERROR: cannot alter statistics on included column "c2" of index "tbl_idx"
258+
ALTER INDEX tbl_idx ALTER COLUMN 4 SET STATISTICS 1000;
259+
ERROR: column number 4 of relation "tbl_idx" does not exist
246260
DROP TABLE tbl;
247261
/*
248262
* 4. CREATE INDEX CONCURRENTLY

src/test/regress/sql/index_including.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ ALTER TABLE tbl DROP COLUMN c1;
137137
SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl' ORDER BY indexname;
138138
DROP TABLE tbl;
139139

140+
/*
141+
* 3.3 Test ALTER TABLE SET STATISTICS
142+
*/
143+
CREATE TABLE tbl (c1 int, c2 int);
144+
CREATE INDEX tbl_idx ON tbl (c1, (c1+0)) INCLUDE (c2);
145+
ALTER INDEX tbl_idx ALTER COLUMN 1 SET STATISTICS 1000;
146+
ALTER INDEX tbl_idx ALTER COLUMN 2 SET STATISTICS 1000;
147+
ALTER INDEX tbl_idx ALTER COLUMN 3 SET STATISTICS 1000;
148+
ALTER INDEX tbl_idx ALTER COLUMN 4 SET STATISTICS 1000;
149+
DROP TABLE tbl;
140150

141151
/*
142152
* 4. CREATE INDEX CONCURRENTLY

0 commit comments

Comments
 (0)