Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fYTDq-0006Z4-V2 for pgsql-hackers@arkaria.postgresql.org; Thu, 28 Jun 2018 09:28:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fYTDn-00038S-AE for pgsql-hackers@arkaria.postgresql.org; Thu, 28 Jun 2018 09:28:11 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fYTDm-00038L-WA for pgsql-hackers@lists.postgresql.org; Thu, 28 Jun 2018 09:28:11 +0000 Received: from sraihb2.sra.co.jp ([202.32.10.6]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fYTDi-0000ND-Mi for pgsql-hackers@postgresql.org; Thu, 28 Jun 2018 09:28:09 +0000 Received: from srascf.sra.co.jp (srascf [133.137.8.80]) by sraihb2.sra.co.jp (Postfix) with ESMTP id 0DA052A15BF for ; Thu, 28 Jun 2018 18:28:03 +0900 (JST) Received: from srascb.sra.co.jp (unknown [133.137.8.65]) by srascf.sra.co.jp with smtp id 0426_118c_bb4ec20b_76be_4f0a_8754_ac718a66bdb0; Thu, 28 Jun 2018 18:28:03 +0900 Received: from sranhm.sra.co.jp (osspc25 [133.137.174.97]) by srascb.sra.co.jp (Postfix) with ESMTP id E651E2D6942 for ; Thu, 28 Jun 2018 18:28:03 +0900 (JST) Received: from yugo-n-ubuntu (dhcp-175-181.sra.co.jp [133.137.175.181]) by sranhm.sra.co.jp (Postfix) with SMTP id BBCF9A0D48 for ; Thu, 28 Jun 2018 18:28:03 +0900 (JST) Date: Thu, 28 Jun 2018 18:28:03 +0900 From: Yugo Nagata To: pgsql-hackers@postgresql.org Subject: Fix error message when trying to alter statistics on included column Message-Id: <20180628182803.e4632d5a.nagata@sraoss.co.jp> X-Mailer: Sylpheed Pro 2.1.4 (GTK+ 2.24.23; x86_64-unknown-linux-gnu; Pro 2.1.4) Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="Multipart=_Thu__28_Jun_2018_18_28_03_+0900_uL8UhAw3p_wLwIBu" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --Multipart=_Thu__28_Jun_2018_18_28_03_+0900_uL8UhAw3p_wLwIBu Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Hi, According to the error message, it is not allowed to alter statistics on included column because this is "non-expression column". postgres=# create table test (i int, d int); CREATE TABLE postgres=# create index idx on test(i) include (d); CREATE INDEX postgres=# alter index idx alter column 2 set statistics 10; ERROR: cannot alter statistics on non-expression column "d" of index "idx" HINT: Alter statistics on table column instead. However, I think this should be forbidded in that this is not a key column but a included column. Even if we decide to support expressions in included columns in future, it is meaningless to do this because any statistics on included column is never used by the planner. Attached is the patch to fix the error message. In this fix, column number is checked first. After applying this, the message is changed as below; postgres=# alter index idx alter column 2 set statistics 10; ERROR: cannot alter statistics on included column "d" of index "idx" Regards, -- Yugo Nagata --Multipart=_Thu__28_Jun_2018_18_28_03_+0900_uL8UhAw3p_wLwIBu Content-Type: text/x-diff; name="set_statistics_error_message.patch" Content-Disposition: attachment; filename="set_statistics_error_message.patch" Content-Transfer-Encoding: 7bit diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 7c0cf0d..4beb160 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -6504,14 +6504,21 @@ ATExecSetStatistics(Relation rel, const char *colName, int16 colNum, Node *newVa errmsg("cannot alter system column \"%s\"", colName))); - if ((rel->rd_rel->relkind == RELKIND_INDEX || - rel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) && - rel->rd_index->indkey.values[attnum - 1] != 0) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("cannot alter statistics on non-expression column \"%s\" of index \"%s\"", - NameStr(attrtuple->attname), RelationGetRelationName(rel)), - errhint("Alter statistics on table column instead."))); + if (rel->rd_rel->relkind == RELKIND_INDEX || + rel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) + { + if (attnum > rel->rd_index->indnkeyatts) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot alter statistics on included column \"%s\" of index \"%s\"", + NameStr(attrtuple->attname), RelationGetRelationName(rel)))); + else if (rel->rd_index->indkey.values[attnum - 1] != 0) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot alter statistics on non-expression column \"%s\" of index \"%s\"", + NameStr(attrtuple->attname), RelationGetRelationName(rel)), + errhint("Alter statistics on table column instead."))); + } attrtuple->attstattarget = newtarget; --Multipart=_Thu__28_Jun_2018_18_28_03_+0900_uL8UhAw3p_wLwIBu--