Re: BUG #19385: Normal SELECT generates an ineffecifient query plan compare to the prepared SELECT.

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: 798604270(at)qq(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19385: Normal SELECT generates an ineffecifient query plan compare to the prepared SELECT.
Date: 2026-01-22 01:31:35
Message-ID: CAApHDvqoKO7jTDoftvzWd2VBouCsYDHwa690xk8xMXPErs8Oqw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wed, 21 Jan 2026 at 23:44, PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
> In the following test case, there are two equivalent simple SELECTs with
> DISTINCT, however, the normal SELECT is slower than the prepared SELECT.
> Given that prepared SELECT statements typically generate suboptimal query
> plans due to the presence of unknown literals, one would expect prepared
> SELECT to be slower than normal SELECT. However, in this example, the
> prepared SELECT executes faster, suggesting that there may still be room for
> optimization in the query plan generation for normal SELECT.

Ultimately, the SeqScan -> NestLoop -> Hash Agg plan is only winning
over the Index Only Scan -> NestLoop -> Unique due to the planner's
estimated costs for the Nested Loop's inner scan. If you find the cost
balance between Seq Scan vs Index [Only] Scan isn't accurate for your
hardware, then adjust random_page_cost.

The planner not choosing the fastest to execute plan all the time does
not constitute a bug. You may want to consult the documentation in
[1]. [2] may also be useful to you.

David

[1] https://www.postgresql.org/docs/18/runtime-config-query.html#GUC-RANDOM-PAGE-COST
[2] https://www.postgresql.org/docs/18/runtime-config-query.html#GUC-EFFECTIVE-CACHE-SIZE

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message surya poondla 2026-01-23 01:18:08 Re: BUG #19382: Server crash at __nss_database_lookup
Previous Message Tom Lane 2026-01-21 16:57:01 Re: Revoke Connect Privilege from Database not working