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 1riZaN-000MwH-3a for pgsql-hackers@arkaria.postgresql.org; Fri, 08 Mar 2024 12:44:28 +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 1riZaL-00Ca4Q-77 for pgsql-hackers@arkaria.postgresql.org; Fri, 08 Mar 2024 12:44:25 +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 1riZaK-00Ca4H-A5 for pgsql-hackers@lists.postgresql.org; Fri, 08 Mar 2024 12:44:25 +0000 Received: from m16.mail.163.com ([220.197.31.2]) by magus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1riZaF-003RBF-3N for pgsql-hackers@lists.postgresql.org; Fri, 08 Mar 2024 12:44:22 +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=SkuHH66vmaOiCvVNDfRyoXaNGAkqF21JpvUnVqAZi8U=; b=en7S88FtY3FvybC7imzA6GwSrjg/FePxRRQX6tRl0YQxkWa+H+i+h0bfBe+gYk 1dVUO5Ez46cKBxnuSt5dv/2onocA2KAX7J00nAj/GLeYXjuifHtbWd6G01xWtPkz Iz5EF39CpGDxJuqUIkimDX8G2fOCuOYOQvi1TFoQ2Mwu4= Received: from 8235eee8a2a0 (unknown [140.205.118.182]) by gzga-smtp-mta-g2-2 (Coremail) with SMTP id _____wDnT9AZCOtlbPb9Aw--.17205S3; Fri, 08 Mar 2024 20:44:09 +0800 (CST) References: <878r2zeqsp.fsf@163.com> <87r0gqcjno.fsf@163.com> <87le6xb5j9.fsf@163.com> <87bk7sbzaj.fsf@163.com> <87msra9vgo.fsf@163.com> <87edcl9hlu.fsf@163.com> User-agent: mu4e 1.10.7; emacs 29.1 From: Andy Fan To: Andy Fan Cc: David Rowley , PostgreSQL Hackers , pgsql-hackers@lists.postgresql.org Subject: Re: a wrong index choose when statistics is out of date Date: Fri, 08 Mar 2024 19:53:14 +0800 In-reply-to: <87edcl9hlu.fsf@163.com> Message-ID: <877cicao6e.fsf@163.com> MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID: _____wDnT9AZCOtlbPb9Aw--.17205S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7Ar15CF1UtryfKryUGFW8Crg_yoW8Zr45pr WSg3Wjk3WDCr42yr18C3WvgryFyrn5Gr43AFs2k34DtrsxWryI9r17Kw4j9F9IgFW8X3Wq qr40gryDZFWrZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JUhXorUUUUU= X-Originating-IP: [140.205.118.182] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/1tbiVAWbU2VOBzHlTgABsu List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk After some more thoughts about the diference of the two ideas, then I find we are resolving two different issues, just that in the wrong index choose cases, both of them should work generally. Your idea actually adding some rule based logic named certainty_factor, just the implemenation is very grace. for the example in this case, it take effects *when the both indexes has the same cost*. I believe that can resolve the index choose here, but how about the rows estimation? issue due to the fact that the design will not fudge the cost anyway, I assume you will not fudge the rows or selectivity as well. Then if the optimizer statistics is missing, what can we do for both index choosing and rows estimation? I think that's where my idea comes out. Due to the fact that optimizer statistics can't be up to date by design, and assume we have a sistuation where the customer's queries needs that statistcs often, how about doing the predication with the history statistics? it can cover for both index choose and rows estimation. Then the following arguments may be arised. a). we can't decide when the missed optimizer statistics is wanted *automatically*, b). if we predicate the esitmiation with the history statistics, the value of MCV information is missed. The answer for them is a). It is controlled by human with the "alter table t alter column a set (force_generic=on)". b). it can't be resolved I think, and it only take effects when the real Const is so different from the ones in history. generic plan has the same issue I think. I just reviewed the bad queries plan for the past half years internally, I found many queries used the Nested loop which is the direct cause. now I think I find out a new reason for this, because the missed optimizer statistics cause the rows in outer relation to be 1, which make the Nest loop is choosed. I'm not sure your idea could help on this or can help on this than mine at this aspect. -- Best Regards Andy Fan