Re: Replace IN VALUES with ANY in WHERE clauses during optimization

From: Ivan Kush <ivan(dot)kush(at)tantorlabs(dot)com>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, Alena Rybakina <a(dot)rybakina(at)postgrespro(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Replace IN VALUES with ANY in WHERE clauses during optimization
Date: 2024-10-04 08:48:04
Message-ID: 42f7fba2-c2e5-4425-a836-6700a4e58ee5@tantorlabs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Some ORMs or proprietary software may write it mistakenly. In these
cases this idea may be helpful.

This patch contains GUC to enable/disable this optimization

On 10/3/24 23:19, Laurenz Albe wrote:
> On Thu, 2024-10-03 at 23:10 +0300, Alena Rybakina wrote:
>> On 03.10.2024 22:52, Ivan Kush wrote:
>>> Hello, hackers! I with my friends propose the patch to replace IN
>>> VALUES to ANY in WHERE clauses.
>>>
>>> # Intro
>>>
>>> The `VALUES` in the `IN VALUES` construct is replaced with with an
>>> array of values when `VALUES` contains 1 column. In the end it will be
>>> replaced with ANY by the existing function makeA_Expr
>>> (src/backend/nodes/makefuncs.c)
>>>
>>> This improves performance, especially if the values are small.
>> Anlrey Lepikhov and I recently described this in an article [0] here and
>> the implementation already exists, but for now it was posted a binary
>> application for testing. The acceleration is significant I agree.
>>
>> [0] https://danolivo.substack.com/p/7456653e-9716-4e91-ad09-83737784c665
> I believe that the speed improvement is significant, but who writes a
> query like
>
> ... WHERE col IN (VALUES (1), (2), (3))
>
> when they could write the much shorter
>
> ... WHERE col IN (1, 2, 3)
>
> which is already converted to "= ANY"?
>
> I wonder if it is worth the extra planning time to detect and improve
> such queries.
>
> Yours,
> Laurenz Albe

--
Best wishes,
Ivan Kush
Tantor Labs LLC

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrei Lepikhov 2024-10-04 09:05:01 Re: Replace IN VALUES with ANY in WHERE clauses during optimization
Previous Message Ivan Kush 2024-10-04 08:43:28 Re: Replace IN VALUES with ANY in WHERE clauses during optimization