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 1ty8kF-002vou-3t for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Mar 2025 12:23:31 +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 1ty8kD-0098t1-QT for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Mar 2025 12:23:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ty8kD-0098sr-D4 for pgsql-hackers@lists.postgresql.org; Fri, 28 Mar 2025 12:23:29 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1ty8kB-001cWh-0l for pgsql-hackers@lists.postgresql.org; Fri, 28 Mar 2025 12:23:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1743164604; bh=Rx9/g3X702ZUY2DfnVsFa7y1RXEGUn3lGETTVo+ipeU=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=4nZgJ1uqBQ3QN+IgT2fBGy60mI7HuzMp6lATqVAsm4PgJcAD01l7rOLCWgrQL5JRf GNJhFOUM0zgudoRo8bLgGKC/2z+gtwNRTWQqNBNcCdObGtD3wURuexuhRGXUsZ0wro ddhIpNmVQe0PwdoqWddDZnwdHELUmzvNUEUJF0pw+MKl3sxQrNAiZ1qSk/pASqMwFC x/ACgkDYcAf7+A1T02wKGHrPOIphKizGj9lOgduWvvL0afBnBdJP0lCRSVYfhNAwip IjEJZkOLFno3wCDPudfDLVA3EUcq/5AmYa3bvPJzfAhApzWCF/ecioFny9nWFLwfkl /5fcAVJkdKOTg== 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 885106084A; Fri, 28 Mar 2025 15:23:24 +0300 (MSK) Content-Type: multipart/alternative; boundary="------------ozdQhQRlcPY4UGDDdG5Dm81T" Message-ID: Date: Fri, 28 Mar 2025 15:23:24 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: POC, WIP: OR-clause support for indexes To: Alexander Korotkov Cc: Pavel Borisov , Andrei Lepikhov , Alexander Lakhin , pgsql-hackers@lists.postgresql.org References: <567ED6CA.2040504@sigaev.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> <72eab2bf-7f94-4f58-84bc-2b80d74b6443@postgrespro.ru> Content-Language: en-US From: Alena Rybakina In-Reply-To: X-KSMG-AntiPhishing: NotDetected, bases: 2025/03/28 10:28: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/28 11:27:00 #27851261 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. --------------ozdQhQRlcPY4UGDDdG5Dm81T Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 28.03.2025 02:18, Alexander Korotkov wrote: > Hi! > > On Mon, Mar 24, 2025 at 2:46 PM Alena Rybakina > wrote: >> 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. > I agree with problem spotted by Andrei: it should be preferred to > preserve original order of clauses as much as possible. The approach > implemented in Andrei's patch seems fragile for me. Original order is > preserved if we didn't find any group. But once we find a single > group original order might be destroyed completely. > > The attached patch changes the reordering algorithm of > group_similar_or_args() in the following way. We reorder each group > of similar clauses so that the first item of the group stays in place, > but all the other items are moved after it. So, if there are no > similar clauses, the order of clauses stays the same. When there are > some groups, only required reordering happens while the rest of the > clauses remain in their places. > I agree with your code in general, but to be honest, double qsort confused me a little. I understood why it is needed - we need to sort the elements so that they stand next to each other if they can be assigned to the same group, and then sort the groups themselves according to the set identifier. I may be missing something, but in the worst case we can get the complexity of qsort O(n^2), right? And I saw the letter where you mentioned this, but it is possible to use mergesort algorithm instead of qsort, which in the worst case gives n * O(n) complexity? -- Regards, Alena Rybakina Postgres Professional --------------ozdQhQRlcPY4UGDDdG5Dm81T Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 28.03.2025 02:18, Alexander Korotkov wrote:
Hi!

On Mon, Mar 24, 2025 at 2:46 PM Alena Rybakina
<a.rybakina@postgrespro.ru> wrote:
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.
I agree with problem spotted by Andrei: it should be preferred to
preserve original order of clauses as much as possible.  The approach
implemented in Andrei's patch seems fragile for me.  Original order is
preserved if we didn't find any group.  But once we find a single
group original order might be destroyed completely.

The attached patch changes the reordering algorithm of
group_similar_or_args() in the following way.  We reorder each group
of similar clauses so that the first item of the group stays in place,
but all the other items are moved after it.  So, if there are no
similar clauses, the order of clauses stays the same.  When there are
some groups, only required reordering happens while the rest of the
clauses remain in their places.


I agree with your code in general, but to be honest, double qsort confused me a little. 

I understood why it is needed - we need to sort the elements so that they stand next to each other if they can be assigned to the same group, and then sort the groups themselves according to the set identifier. 

I may be missing something, but in the worst case we can get the complexity of qsort O(n^2), right? And I saw the letter where you mentioned this, but it is possible to use mergesort algorithm  instead of qsort, which in the worst case gives n * O(n) complexity?

-- 
Regards,
Alena Rybakina
Postgres Professional
--------------ozdQhQRlcPY4UGDDdG5Dm81T--