subquery plan rows = 1, but it's merge joined instead of index lookup

From: ilya Basin <basinilya(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: subquery plan rows = 1, but it's merge joined instead of index lookup
Date: 2024-04-11 12:57:34
Message-ID: 620cfada-683e-45b7-8513-122b2ffab4d8@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi List.
I have a form with filter inputs and this form produces SQL like:
select * from t where id in( select id from t join filter1... join filter2... )

Usually the query plan has the "in" subquery at the beginning and the result table at the end.
The subquery plan rows is usually 1, actual rows is usually 8. The result table is usually joined using the index scan.

When using more filters than usual at once, the query plan turns upside down and the result table is fully scanned without any filter and before all joins despite its plan rows =200,000 and the subquery plan rows is still =1.

I'm trying to understand why. Is there some complexity limit after which the planner starts acting dumb?

Also, I tried to force the desired plan (only to see if its cost is really higher than the cost of the default plan). However, pg_hint_plan just prints "not used hints" for some of my NestLoop() hints while accepting the others. And set enable_mergejoin = OFF and such only change the join method, but don't fix the order.

Attachment Content-Type Size
2024-04-11-2.sql text/plain 11.1 KB
bad.json application/json 75.7 KB
bad.svg image/svg+xml 161.0 KB
good.json application/json 77.4 KB
good.svg image/svg+xml 154.9 KB

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2024-04-11 14:07:34 Re: (When) can a single SQL statement return multiple result sets?
Previous Message Patrick FICHE 2024-04-11 07:41:56 RE: (When) can a single SQL statement return multiple result sets?