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 1r8cui-00At21-BB for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Nov 2023 09:00:52 +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 1r8cug-00Fw12-Lu for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Nov 2023 09:00:50 +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 1r8cug-00Fw0u-Bw for pgsql-hackers@lists.postgresql.org; Thu, 30 Nov 2023 09:00:50 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r8cuZ-009K4i-RO for pgsql-hackers@postgresql.org; Thu, 30 Nov 2023 09:00:49 +0000 Received: from [10.4.14.67] (unknown [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: a.rybakina@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id 2BC90E21019; Thu, 30 Nov 2023 12:00:42 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1701334842; bh=FMWWWCHJDeQElJwXKzQv4txlO/QU88RmbLQ1hqq4hgs=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=U8iI6GA0BPT1vHyzo7h4FADJPWL5OViz++pPNstCE5tYsc7N8l8ZwPVTuimgKcseW wRkqChwDNuuC17qV8dNTZAwJVgdob847DhnJWXsdfhOIz4+2pOh2Dt5PBqvanqIFcj qbno9qwH4nWgNKAQ7MNat2xQ5oExmeTR5H08wnRB0as3KVBSk2IAGUVaM3YsE6m3lB UCB3r7bO5gFZ7kn3r1j1DQSU9ELyOsmCzksB5klROCnFwY6W2k/vIkW9ggM4KjDcYl SC6NfHaXn0+P/eQTvVlUBUcl/rzAyJ6ravuslNWU4UfPoigCCZG09TP5qZmvGf+x7B 7+2Xb04xdcBzg== Message-ID: <26d8828b-a939-463d-9617-514d50f27a69@postgrespro.ru> Date: Thu, 30 Nov 2023 12:00:41 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: POC, WIP: OR-clause support for indexes Content-Language: en-US To: Andrei Lepikhov , Robert Haas , pgsql-hackers@postgresql.org, Alexander Korotkov Cc: Peter Geoghegan , "Finnerty, Jim" , Marcos Pegoraro , teodor@sigaev.ru, Ranier Vilela , Tomas Vondra , Peter Eisentraut References: <567ED6CA.2040504@sigaev.ru> <16a7e3aa-24c0-4986-8820-ea2857bd7b6b@postgrespro.ru> <421656a9-5df7-41bb-9d58-1c1edbd5dc3e@postgrespro.ru> <28c727ba-5260-42a8-88b0-4ecdeeca6ef8@postgrespro.ru> <4c1c2c0b-802f-45ae-a7cd-007bfd8957cf@postgrespro.ru> <52f0d302-7963-452f-8c5a-f66d261514eb@postgrespro.ru> <43ad8a48-b980-410d-a83c-5beebf82a4ed@postgrespro.ru> <04ef16eb-46c1-4ce7-9f68-d1c80ef0be81@postgrespro.ru> <59e67a40-95a8-4d74-ae4e-027ea0f59084@postgrespro.ru> <8ff70ada-4ec7-436a-8bf8-d1c7d8551ff1@postgrespro.ru> From: Alena Rybakina In-Reply-To: <8ff70ada-4ec7-436a-8bf8-d1c7d8551ff1@postgrespro.ru> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 30.11.2023 11:30, Andrei Lepikhov wrote: > On 30/11/2023 15:00, Alena Rybakina wrote: >> 2. The second patch is my patch version when I moved the OR >> transformation in the s index formation stage: >> >> So, I got the best query plan despite the possible OR to ANY >> transformation: > > If the user uses a clause like "x IN (1,2) AND y=100", it will break > your 'good' solution. No, unfortunately I still see the plan with Seq scan node: postgres=# explain analyze select * from test where x in (1,2) and y = 100;                                                      QUERY PLAN --------------------------------------------------------------------------------------------------------------------  Gather  (cost=1000.00..12690.10 rows=1 width=12) (actual time=72.985..74.832 rows=0 loops=1)    Workers Planned: 2    Workers Launched: 2    ->  Parallel Seq Scan on test  (cost=0.00..11690.00 rows=1 width=12) (actual time=68.573..68.573 rows=0 loops=3)          Filter: ((x = ANY ('{1,2}'::integer[])) AND (y = '100'::double precision))          Rows Removed by Filter: 333333  Planning Time: 0.264 ms  Execution Time: 74.887 ms (8 rows) > In my opinion, the general approach here is to stay with OR->ANY > transformation at the parsing stage and invent one more way for > picking an index by looking into the array and attempting to find a > compound index. > Having a shorter list of expressions, where uniform ORs are grouped > into arrays, the optimizer will do such work with less overhead. Looking at the current index generation code, implementing this approach will require a lot of refactoring so that functions starting with get_indexes do not rely on the current baserestrictinfo, but use only the indexrestrictinfo, which is a copy of baserestrictinfo. And I think, potentially, there may be complexity also with the equivalences that we can get from OR expressions. All interesting transformations are available only for OR expressions, not for ANY, that is, it makes sense to try the last chance to find a suitable plan with the available OR expressions and if that plan turns out to be better, use it. -- Regards, Alena Rybakina Postgres Professional