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 1twhC1-00GhxF-1X for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Mar 2025 12:46:13 +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 1twhBz-002zSg-JY for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Mar 2025 12:46:11 +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 1twhBz-002zPg-2T for pgsql-hackers@lists.postgresql.org; Mon, 24 Mar 2025 12:46:11 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1twhBu-000s2p-12 for pgsql-hackers@lists.postgresql.org; Mon, 24 Mar 2025 12:46:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1742820364; bh=wSBF7upv1OvUYX3Xx67H+owS2LofU3qwU1jLf4qL060=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=2zfy/dDiDyue6ga++aEf0WJAMUF5G+JumqDt9+ux7HQZCC/FuJOLpu2tasbPfQCeZ BTwTJB7MGeuPCcuDY9lHDxxFE5++2AHztzhWFJ56ke9WJSNl9yU93kYZNcD6MUoLa8 UUG9LRxbXB4miF50lSsol62QRDEh+uSzPpQLazGWCoIRl7oDSeg2/n+9gkPcPufLyh u7p2320FEyJZasRl/hbbsLg7/mPQt0nTvRHKYdkDsRuV9MgU1duMk1R4Gvf4jaWy1Q E4vcjGgCf3Jkuiw8Oj3lqVORVD/AmIBL0bETCJAK6NB7yp4ADyKAQw5z8L11ajDv4T qdwar61jnpNew== Received: from [10.4.12.74] (unknown [93.174.131.141]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: a.rybakina@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id 47EE260721; Mon, 24 Mar 2025 15:46:04 +0300 (MSK) Content-Type: multipart/alternative; boundary="------------9Ryb9fx9hjdJRTXYGpzNQwfc" Message-ID: <72eab2bf-7f94-4f58-84bc-2b80d74b6443@postgrespro.ru> Date: Mon, 24 Mar 2025 15:46:03 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: POC, WIP: OR-clause support for indexes To: Pavel Borisov , Andrei Lepikhov Cc: Alexander Korotkov , Alexander Lakhin , pgsql-hackers@lists.postgresql.org References: <567ED6CA.2040504@sigaev.ru> <43db6a40-5c5f-4909-ad62-3060b297e686@postgrespro.ru> <41ba3d47-2a48-476c-88d4-6ebd889a7af2@gmail.com> <036d64e6-4d47-485c-b047-6664d452b3e8@postgrespro.ru> <1a4f1631-86c1-4185-a520-03b24a389449@postgrespro.ru> <3ac7c436-81e1-4191-9caf-b0dd70b51511@gmail.com> Content-Language: en-US From: Alena Rybakina In-Reply-To: X-KSMG-AntiPhishing: NotDetected, bases: 2025/03/24 12:09:00 X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/03/24 08:46:00 #27817850 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------9Ryb9fx9hjdJRTXYGpzNQwfc Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 24.03.2025 13:46, Pavel Borisov wrote: > Hi, Andrei! > > On Mon, 24 Mar 2025 at 14:10, Andrei Lepikhov wrote: >> Hi, >> >> Playing with the feature, I found a slightly irritating permutation - >> even if this code doesn't group any clauses, it may permute positions of >> the quals. See: >> >> DROP TABLE IF EXISTS main_tbl; >> CREATE TABLE main_tbl(id bigint, hundred int, thousand int); >> CREATE INDEX mt_hundred_ix ON main_tbl(hundred); >> CREATE INDEX mt_thousand_ix ON main_tbl(thousand); >> VACUUM (ANALYZE) main_tbl; >> >> SET enable_seqscan = off; >> EXPLAIN (COSTS OFF) >> SELECT m.id, m.hundred, m.thousand >> FROM main_tbl m WHERE (m.hundred < 2 OR m.thousand < 3); >> >> Bitmap Heap Scan on public.main_tbl m >> Output: id, hundred, thousand >> Recheck Cond: ((m.thousand < 3) OR (m.hundred < 2)) >> -> BitmapOr >> -> Bitmap Index Scan on mt_thousand_ix >> Index Cond: (m.thousand < 3) >> -> Bitmap Index Scan on mt_hundred_ix >> Index Cond: (m.hundred < 2) >> >> Conditions on the columns "thousand" and "hundred" changed their places >> according to the initial positions defined in the user's SQL. >> It isn't okay. I see that users often use the trick of "OR order" to >> avoid unnecessary calculations - most frequently, Subplan evaluations. >> So, it makes sense to fix. >> In the attachment, I have included a quick fix for this issue. Although >> many tests returned to their initial (pre-18) state, I added some tests >> specifically related to this issue to make it clearer. > I looked at your patch and have no objections to it. > > However it's clearly stated in PostgreSQL manual that nothing about > the OR order is warranted [1]. So changing OR order was (and is) ok > and any users query tricks about OR order may work and may not work. > > [1]https://www.postgresql.org/docs/17/sql-expressions.html#SYNTAX-EXPRESS-EVAL > I agree with Andrey's changes and think we should fix this, because otherwise it might be inconvenient. For example, without this changes we will have to have different test output files for the same query for different versions of Postres in extensions if the whole change is only related to the order of column output for a transformation that was not applied. -- Regards, Alena Rybakina Postgres Professional --------------9Ryb9fx9hjdJRTXYGpzNQwfc Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit
On 24.03.2025 13:46, Pavel Borisov wrote:
Hi, Andrei!

On Mon, 24 Mar 2025 at 14:10, Andrei Lepikhov <lepihov@gmail.com> wrote:
Hi,

Playing with the feature, I found a slightly irritating permutation -
even if this code doesn't group any clauses, it may permute positions of
the quals. See:

DROP TABLE IF EXISTS main_tbl;
CREATE TABLE main_tbl(id bigint, hundred int, thousand int);
CREATE INDEX mt_hundred_ix ON main_tbl(hundred);
CREATE INDEX mt_thousand_ix ON main_tbl(thousand);
VACUUM (ANALYZE) main_tbl;

SET enable_seqscan = off;
EXPLAIN (COSTS OFF)
SELECT m.id, m.hundred, m.thousand
FROM main_tbl m WHERE (m.hundred < 2 OR m.thousand < 3);

  Bitmap Heap Scan on public.main_tbl m
    Output: id, hundred, thousand
    Recheck Cond: ((m.thousand < 3) OR (m.hundred < 2))
    ->  BitmapOr
          ->  Bitmap Index Scan on mt_thousand_ix
                Index Cond: (m.thousand < 3)
          ->  Bitmap Index Scan on mt_hundred_ix
                Index Cond: (m.hundred < 2)

Conditions on the columns "thousand" and "hundred" changed their places
according to the initial positions defined in the user's SQL.
It isn't okay. I see that users often use the trick of "OR order" to
avoid unnecessary calculations - most frequently, Subplan evaluations.
So, it makes sense to fix.
In the attachment, I have included a quick fix for this issue. Although
many tests returned to their initial (pre-18) state, I added some tests
specifically related to this issue to make it clearer.
I looked at your patch and have no objections to it.

However it's clearly stated in PostgreSQL manual that nothing about
the OR order is warranted [1]. So changing OR order was (and is) ok
and any users query tricks about OR order may work and may not work.

[1] https://www.postgresql.org/docs/17/sql-expressions.html#SYNTAX-EXPRESS-EVAL

I agree with Andrey's changes and think we should fix this, because otherwise it might be inconvenient.
For example, without this changes we will have to have different test output files for the same query for different versions of Postres in extensions if the whole change is only related to the order of column output for a transformation that was not applied.
-- 
Regards,
Alena Rybakina
Postgres Professional
--------------9Ryb9fx9hjdJRTXYGpzNQwfc--