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.96) (envelope-from ) id 1wpT2d-001fE4-2b for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Jul 2026 15:51:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wpT1c-00CQIw-2n for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Jul 2026 15:50:24 +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.96) (envelope-from ) id 1wpT1c-00CQId-1d for pgsql-hackers@lists.postgresql.org; Thu, 30 Jul 2026 15:50:24 +0000 Received: from 10.mo533.mail-out.ovh.net ([46.105.72.194]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wpT1Z-000000017Xp-11yQ for pgsql-hackers@postgresql.org; Thu, 30 Jul 2026 15:50:23 +0000 Received: from director1.derp.mail-out.ovh.net (director1.derp.mail-out.ovh.net [51.210.158.128]) by mo533.mail-out.ovh.net (Postfix) with ESMTPS id 4h9tvH0yL2z621V; Thu, 30 Jul 2026 15:50:19 +0000 (UTC) Received: from director1.derp.mail-out.ovh.net (director1.derp.mail-out.ovh.net. [127.0.0.1]) by director1.derp.mail-out.ovh.net (inspect_sender_mail_agent) with SMTP for ; Thu, 30 Jul 2026 15:50:18 +0000 (UTC) Received: from mta7.priv.ovhmail-u1.ea.mail.ovh.net (unknown [10.110.0.227]) by director1.derp.mail-out.ovh.net (Postfix) with ESMTPS id 4h9tvG66YCz6JdL; Thu, 30 Jul 2026 15:50:18 +0000 (UTC) Received: from mailstore90.priv.ovhmail-u1.ea.mail.ovh.net (unknown [10.2.8.90]) by mta7.priv.ovhmail-u1.ea.mail.ovh.net (Postfix) with ESMTP id 66D5AB81A24; Thu, 30 Jul 2026 15:50:18 +0000 (UTC) Date: Thu, 30 Jul 2026 15:50:18 +0000 (UTC) From: Ayoub Kazar To: Ashutosh Bapat Cc: pgsql-hackers@postgresql.org Message-ID: <81409240.63339175.1785426618268.JavaMail.zimbra@data-bene.io> In-Reply-To: References: <4ab664c3585d3c2da2d4c1d1bbed430e@data-bene.io> Subject: Re: [PATCH] Rewrite undirected edge patterns in GRAPH_TABLE using UNION ALL MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_a5786766-b0e3-49a9-9d29-1b3cd4e0c958" X-Originating-IP: [37.67.99.163] X-Authenticated-User: ayoub.kazar@data-bene.io Thread-Topic: Rewrite undirected edge patterns in GRAPH_TABLE using UNION ALL Thread-Index: tqqtKMcBUlXUM02ADBShY1TV99ByMg== x-ovh-tracer-id: 8266920066748481838 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: 0 X-VR-SPAMCAUSE: dmFkZTG1U+mU5QV3yyELyROpNex1BIGXPA8m8bCkbZ3BBDIfEB2JGtS+R+TJUvpK+zFQS3yI5fUst6IE78CrgdQG1H3eZxYm30emuUup32UyX6N5hCEmLIRRFp/+u8ByJFDhkREHLpOYcHZZ/v0JR6GWva1hJn4g9LvVL1KSLnCS0SslOSOPZcWsDXdQaO+gSHZwyVBx6RSjA6BkC1Bu/fwt8SXCxN8i86ljRQaMDfvW3UJctlRrtFg3SLI8Bv47n7AfAOhd5Y9q55WFVjHek2YMZ/05qCwWZH93Ce/RBc7T03AHGzI8ZpBotTO7mfWiMY8rKzEKkw/VUWhyrout5HvMmxLRQ0RChvjfqksQdmOsd9hERx6woHUzsH6CwcQ1GtCzNNbhn9JLKMhqSeVOx/3w8u5CrbtqR8FRZ1EqoxGGVl0hol8MhuVaGbGIjqzri9xk8b/tBfWYNzZ8rjymP6RgIJK/XMlqZSiiZxjptjmrI7uyjYXBrcstdr+Jk/gaaXxwxTpW5ImINYC5xBDfVzsZCrSDiL86norO8prLflBdFvTAVGIprlIFEDeNqG8HQwNhGrJL1Dg4IP+dfMeunAQFYrz+rlKj7JyvZ4OWtGgwACrHm+nWgkRG0d6E+pAbj3Rb6IY1Hyg4E9IOXWQeNg2iiJMDUcn+6hYaatW/J/g3ggi1PA List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=_a5786766-b0e3-49a9-9d29-1b3cd4e0c958 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Hello Ashutosh, Thanks for the review From: Ashutosh Bapat To: ayoub kazar Cc: pgsql-hackers Date: Thursday, 30 July 2026 2:21 PM CEST Subject: Re: [PATCH] Rewrite undirected edge patterns in GRAPH_TABLE using UNION ALL Hi Ayoub, I spent some time researching this problem. Here's what I found. On Thu, Jul 16, 2026 at 7:16 PM < [ mailto:ayoub.kazar@data-bene.io | ayoub.kazar@data-bene.io ] > wrote: BQ_BEGIN On 2026-07-16 14:27, Ashutosh Bapat wrote: > On Thu, Jul 16, 2026 at 2:46 PM < [ mailto:ayoub.kazar@data-bene.io | ayoub.kazar@data-bene.io ] > wrote: >> The attached patch changes how GRAPH_TABLE rewrites undirected edge >> patterns >> (-[e]-) to UNION ALL subquery instead of emitting an OR on both >> directions quals. >> >> Background >> ---------- >> >> When an undirected edge pattern matches an edge table whose source and >> destination vertex table are the same, the current implementation >> combines the equi-join >> conditions for both traversal directions into a single OR qual: >> >> WHERE (e.src = [ http://v1.id/ | v1.id ] AND e.dst = [ http://v2.id/ | v2.id ] ) >> OR (e.src = [ http://v2.id/ | v2.id ] AND e.dst = [ http://v1.id/ | v1.id ] ) >> >> This generally implies using BitmapOr or a full OR evaluation which is >> very unefficient in queries with large intermediate results (see >> benchmarks below). >> JOIN with OR was discussed previously at [1]. I think optimizing this query pattern is better since it will help such queries generally not just when they are generated by graph query rewrite. BQ_END BQ_BEGIN BQ_BEGIN Q2 is the equivalent of what Oracle does, its makes a UNION ALL between two queries that each work on a single direction. BQ_END Oracle implements OR-expansion tranformation as a planner optimization [2]. I guess that it is writing graph query as JOIN with OR but the optimizer is turning it into UNION instead of graph query rewriter turning it into a UNION. Have you checked at what stage does Oracle introduce UNION? BQ_END Its using the cost based OR expansion from the plans i saw, so yes i suppose it is originally writing the graph query as JOIN with OR. From many query plans that i saw from test queries, the only time the query was planned with an OR instead of UNION ALL was when stats weren't gathered and had very few rows, in all other tries it was all UNION ALL plans. I just saw that the idea is patented [3] ; granted in 2018, i don't know much about patents but i see from the claims that we ONLY can't do a cost based optimization of this same idea (if we decided to) ? BQ_BEGIN The discussion in [1] seems to have stalled because there was no clear proof of the transform being correct. Given that other products have implemented the transformation, there is possibility that the proof of correctness exists. It might be better to find that proof and resurrect the old thread. BQ_END The only proof which is in the last message of thread: for a query: SELECT * FROM any_v WHERE (A OR B OR C). we do: SELECT * FROM any_v WHERE A UNION ALL SELECT * FROM any_v WHERE B AND !A UNION ALL SELECT * FROM any_v WHERE C AND !A AND !(B AND !A); where !(Expr) means (NOT (expr) OR (EXPR) IS NULL) This is the only thing i thought of which guarantees correctness when i was working on the patch. I just check with the patent now, it discusses the same proof for de-duplicating results. BQ_BEGIN Also, given that the OR expansion was later replaced by cost based OR expansion, the transformation may not always yield optimal results. So, we should not always rewrite the undirected edge as UNION as the patch does. BQ_END As mentioned above from what i noticed from some tests on Oracle query plans nearly everytime an OR expansion was applied, given both this patch's idea (UNION ALL only on edge table to generate two directions data -> then join separately) is a special case of the OR expansion, i find it interesting to see if we can try to prove that in all cases we would want this instead of the normal OR that mostly implies the bitmap scan ; which might suggest handling this "easier" case in rewrite for graph queries. BQ_BEGIN [1] [ https://www.postgresql.org/message-id/CAKJS1f9OPfZ4n9ozTeDMLpq6y10Ms3dVgQnepZ8xC0LyEcLNdQ%40mail.gmail.com | https://www.postgresql.org/message-id/CAKJS1f9OPfZ4n9ozTeDMLpq6y10Ms3dVgQnepZ8xC0LyEcLNdQ%40mail.gmail.com ] [2] [ https://blogs.oracle.com/optimizer/optimizer-transformations-or-expansion | https://blogs.oracle.com/optimizer/optimizer-transformations-or-expansion ] -- Best Wishes, Ashutosh Bapat BQ_END [3] [ https://patents.justia.com/patent/20150234888 | https://patents.justia.com/patent/20150234888 ] Regards, Ayoub BQ_BEGIN BQ_END --=_a5786766-b0e3-49a9-9d29-1b3cd4e0c958 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Hello Ashutosh,
 
Thanks for the review

<= strong>From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>To: ayoub kazar <ayoub.kazar@data-bene.io>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: Thursday, 30 July 2026 2:21 PM CEST
Subject: = Re: [PATCH] Rewrite undirected edge patterns in GRAPH_TABLE using = UNION ALL

Hi Ayoub,
 
I spent some time researching this problem. Here's what I found.

On Thu, Jul 16, 2026 at 7:16=E2=80=AF= PM <ayoub.kazar@data-bene.io= > wrote:
On 2026-07-16 14:27= , Ashutosh Bapat wrote:
> On Thu, Jul 16, 2026 at 2:46=E2=80=AFPM <= ;ayoub.kazar@data-bene.io&g= t; wrote:

>> The attached patch changes how GRAPH_TABLE rewrit= es undirected edge
>> patterns
>> (-[e]-) to UNION ALL su= bquery instead of emitting an OR on both
>> directions quals.
&= gt;>
>> Background
>> ----------
>>
>= > When an undirected edge pattern matches an edge table whose source and=
>> destination vertex table are the same, the current implementat= ion
>> combines the equi-join
>> conditions for both trav= ersal directions into a single OR qual:
>>
>>  &nbs= p;   WHERE (e.src =3D v1.id AND e.dst =3D= v2.id)
>>       =  OR (e.src =3D v2.id AND e.dst =3D v1.id)
>>
>> This generally impl= ies using BitmapOr or a full OR evaluation which is
>> very uneffi= cient in queries with large intermediate results (see
>> benchmark= s below).
>>
 
JOIN with OR was discussed previously at [1]. I think optimizing this = query pattern is better since it will help such queries generally not just = when they are generated by graph query rewrite.

Q2 is the equiv= alent of what Oracle does, its makes a UNION ALL between
two queries th= at each work on a single direction.
 
Oracle implements OR-expansion tranformation as a planner optimization= [2]. I guess that it is writing graph query as JOIN with OR but the optimi= zer is turning it into UNION instead of graph query rewriter turning it int= o a UNION. Have you checked at what stage does Oracle introduce UNION?
Its using the cost based OR expansion from the pl= ans i saw, so yes i suppose it is originally writing the graph query as JOI= N with OR.
From many query plans that i saw from test queries, th= e only time the query was planned with an OR instead of UNION ALL was when = stats weren't gathered and had very few rows, in all other tries it was all= UNION ALL plans.

I just saw = that the idea is patented [3] ; granted in 2018, i don't know much about pa= tents but i see from the claims that we ONLY can't do a cost based optimiza= tion of this same idea (if we decided to) ? 
 
The discussion in [1] seems to have stalled because there was no clear= proof of the transform being correct. Given that other products have imple= mented the transformation, there is possibility that the proof of correctne= ss exists. It might be better to find that proof and resurrect the old thre= ad.
The only proof which is in the last messa= ge of thread:

for a query:
SELECT * FROM any_v WHERE (A OR B OR C).

we do:

SELECT * FROM any_v WHERE A
UNION ALL
SELECT * FROM any_v WHERE B = AND !A
UNION ALL
SELECT * FROM any_v WHERE C AND !A AND !(B AND !A);<= br>where !(Expr) means (NOT (expr) OR (EXPR) IS NULL)

This is the only thing i thought of which guarantees co= rrectness when i was working on the patch.
I just check with= the patent now, it discusses the same proof for de-duplicating results.
 
Also, given that the OR expansion was later replaced by cost based OR = expansion, the transformation may not always yield optimal results. So, we = should not always rewrite the undirected edge as UNION as the patch does.
As mentioned above from what i noticed from so= me tests on Oracle query plans nearly everytime an OR expansion was applied= , given both this patch's idea (UNION ALL only on edge table to generate tw= o directions data -> then join separately) is a special case of the OR e= xpansion, i find it interesting to see if we can try to prove that in all c= ases we would want this instead of the normal OR that mostly implies the bi= tmap scan ; which might suggest handling this "easier" case in rewrite for = graph queries.



Regards,
Ayoub

--=_a5786766-b0e3-49a9-9d29-1b3cd4e0c958--