Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1riBAT-00FZ9v-Tn for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Mar 2024 10:40:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1riBAS-004EuG-C9 for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Mar 2024 10:40:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1riBAR-004Eu8-Gt for pgsql-hackers@lists.postgresql.org; Thu, 07 Mar 2024 10:40:04 +0000 Received: from m16.mail.163.com ([220.197.31.3]) by magus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1riBAL-003FMA-J9 for pgsql-hackers@lists.postgresql.org; Thu, 07 Mar 2024 10:40:02 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version: Content-Type; bh=2P0xWiz6Ah4EARht0OZ7ERnIGnWRFg04Df8OzrdsLUc=; b=cC0aBhSJvqe271AqCjh7iQa4MpE0MM4vb1zpkAAEoX792TUOVVFMcu1Eemcenv d0Nti+1n6+gjwZbbci9yKKCviWUFTN1sUzSuRmtbYSSDyCobj2hExcE/l4t+TVSQ haXAk+6iMPotDUjhh3FHHili2NvPKoT3plpM/4gUrEkIk= Received: from 8235eee8a2a0 (unknown [140.205.118.50]) by gzga-smtp-mta-g1-3 (Coremail) with SMTP id _____wDnV9F3mellWksfAw--.12471S3; Thu, 07 Mar 2024 18:39:52 +0800 (CST) References: <878r2zeqsp.fsf@163.com> <87r0gqcjno.fsf@163.com> <87le6xb5j9.fsf@163.com> <87bk7sbzaj.fsf@163.com> User-agent: mu4e 1.10.7; emacs 29.1 From: Andy Fan To: David Rowley Cc: PostgreSQL Hackers , pgsql-hackers@lists.postgresql.org Subject: Re: a wrong index choose when statistics is out of date Date: Thu, 07 Mar 2024 18:16:52 +0800 In-reply-to: Message-ID: <87msra9vgo.fsf@163.com> MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID: _____wDnV9F3mellWksfAw--.12471S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7WFWftF48JFyxAF4xAr1DZFb_yoW8Zw13pr WSgF17KFykJFyxZrn7Gw1qkrn5Cws7Jry7Jrn8J340vr45Zry7ArsF93y8CFW3CFWxCw1j yF45ta9rAr4DAFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRKsjbUUUUU= X-Originating-IP: [140.205.118.50] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/1tbiQA2aU2VOBx2qagADs2 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > On Wed, 6 Mar 2024 at 02:09, Andy Fan wrote: >> This patch introduces a new attoptions like this: >> >> ALTER TABLE t ALTER COLUMN col set (force_generic=true); >> >> Then selfunc.c realizes this and ignore the special Const value, then >> average selectivity is chosen. This fall into the weakness of generic >> plan, but this patch doesn't introduce any new weakness and we leave the >> decision to user which could resolve some problem. Also this logic only >> apply to eqsel since the ineqsel have the get_actual_variable_range >> mechanism which is helpful for index choose case at least. > > If you don't want the planner to use the statistics for the column why > not just do the following? Acutally I didn't want the planner to ignore the statistics totally, I want the planner to treat the "Const" which probably miss optimizer part average, which is just like what we did for generic plan for the blow query. prepare s as SELECT * FROM t WHERE a = $1 and b = $2; explain (costs off) execute s(109, 8); QUERY PLAN --------------------------------- Index Scan using t_a_c_idx on t Index Cond: (a = 109) Filter: (b = 8) (3 rows) custom plan, Wrong index due to we have a bad estimation for a = 109. set plan_cache_mode to force_generic_plan ; explain (costs off) execute s(109, 8); QUERY PLAN --------------------------------------- Index Scan using t_a_b_idx on t Index Cond: ((a = $1) AND (b = $2)) -- Correct index. (2 rows) Generic plan - we use the average estimation for the missed optimizer statistics part and *if the new value is not so different from existing ones*, we can get a disired result. It is true that the "generic" way is not as exactly accurate as the "custom" way since the later one can use the data in MCV, but that is the cost we have to pay to make the missed optimizer statistics less imporant and generic plan has the same issue as well. As for this aspect, I think the way you proposed probably have a wider use case. -- Best Regards Andy Fan