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 1rgfxN-007Oux-QT for pgsql-hackers@arkaria.postgresql.org; Sun, 03 Mar 2024 07:08:22 +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 1rgfxL-000Xib-T7 for pgsql-hackers@arkaria.postgresql.org; Sun, 03 Mar 2024 07:08:20 +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 1rgfxL-000Xhk-1W for pgsql-hackers@lists.postgresql.org; Sun, 03 Mar 2024 07:08:20 +0000 Received: from m16.mail.163.com ([220.197.31.2]) by magus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rgfxF-002WMJ-Co for pgsql-hackers@postgresql.org; Sun, 03 Mar 2024 07:08:17 +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=frTmRiE0ifl6HEtI9N/GN61OHVYcITUvEMCP8UxvTIU=; b=WHlbEcX5xSB9Qp0eULdGGtBlkfY7PjwwZj9fqOmVVLzh3rcy1jSWxg9xVx239N dRjVoMm+Z6rmFIxWzM6FMwnij48homrB+x/YRd4FQMWpnneytR3NlJao/HEMNJAI i2dstkkaQ5ZAutLlDxupBRHEoaI5siJLqFVqzvv4Nhnk0= Received: from 8235eee8a2a0 (unknown [140.205.118.200]) by gzga-smtp-mta-g1-0 (Coremail) with SMTP id _____wDX7xzWIeRlgWZiAA--.52859S3; Sun, 03 Mar 2024 15:08:06 +0800 (CST) User-agent: mu4e 1.10.7; emacs 29.1 From: Andy Fan To: PostgreSQL Hackers Subject: a wrong index choose when statistics is out of date Date: Sun, 03 Mar 2024 15:01:23 +0800 Message-ID: <878r2zeqsp.fsf@163.com> MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID:_____wDX7xzWIeRlgWZiAA--.52859S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7ArWftw17uw13Ar1xtw4xZwb_yoW8XFykpa yFgr129Fs3Jw10yrWvkF10g34rKrWDG3yDJr1rGw1Iyw15Cry0kr1Ig3y0yFW7Ga4kG3Wq v3W2gr9xCrySgFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07Uk8n5UUUUU= X-Originating-IP: [140.205.118.200] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/1tbiQACWU2VOBtookwAAsJ List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The issue can be reproduced with the following steps: create table x_events (.., created_at timestamp, a int, b int); create index idx_1 on t(created_at, a); create index idx_2 on t(created_at, b); query: select * from t where create_at = current_timestamp and b = 1; index (created_at, a) rather than (created_at, b) may be chosen for the above query if the statistics think "create_at = current_timestamp" has no rows, then both index are OK, actually it is true just because statistics is out of date. I just run into this again recently and have two new idea this time, I'd like gather some feedback on this. 1. We can let the user define the column as the value is increased day by day. the syntax may be: ALTER TABLE x_events ALTER COLUMN created_at ALWAYS_INCREASED. then when a query like 'create_at op const', the statistics module can treat it as 'created_at = $1'. so the missing statistics doesn't make difference. Then I think the above issue can be avoided. This is different from letting user using a PreparedStmt directly because it is possible that we always choose a custom plan, the easiest way to make this happen is we do a planning time partition prune. 2. Use some AI approach to forecast the data it doesn't gather yet. The training stage may happen at analyze stage, take the above case for example, it may get a model like 'there are 100 rows per second for the time during 9:00 to 18:00 and there are 2 rows per seconds for other time range. For now, I think option 1 may be easier to happen. -- Best Regards Andy Fan