Re: BUG #17540: Prepared statement: PG switches to a generic query plan which is consistently much slower

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: william(dot)duclot(at)gmail(dot)com, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17540: Prepared statement: PG switches to a generic query plan which is consistently much slower
Date: 2022-07-06 22:07:46
Message-ID: CAKFQuwZDyjnCRGfvbQJrhfOiCx5z5=khZr4QJWUMEcEcwx4s9A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

On Wed, Jul 6, 2022 at 2:41 PM PG Bug reporting form <noreply(at)postgresql(dot)org>
wrote:

> The following bug has been logged on the website:
>
> Bug reference: 17540
> Logged by: William Duclot
> Email address: william(dot)duclot(at)gmail(dot)com
> PostgreSQL version: 14.4
> Operating system: GNU/Linux (Red Hat 8.5.0)
> Description:
>

> This means that the query planner does not realise that the actual
> parameter value matters a lot, and that the parameters used _in practice_
> result in a faster plan than the generic plan (100% of the first 5
> executions), and that therefore it shouldn't stick to the generic plan.
>

I mean, it is the planner and so, no, it doesn't understand that the
executor encountered an issue.

> It is particularly insidious as actually I wasn't even aware I was using
> prepared statements. Like most applications I use a database driver (pgx,
> in
> Go) which I learnt uses `PQexecPrepared` under the hood, which creates a
> sort of "unnamed prepared statement" behaving the same as this minimal
> reproduction without me ever being aware that prepared statements are
> involved anywhere between my code and the database.

Yep, and the core project pretty much says that if you don't like this you
need to complain to the driver writer and ask them to provide you an
interface to the unnamed parse-bind-execute API which lets you perform
parameterization without memory, just safety.

PostgreSQL has built the needed tools to make this less problematic, and
has made solid attempts to improve matters in the current state of things.
There doesn't seem to be a bug here. There is potentially room for
improvement but no one presently is working on things in this area.

David J.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message David Rowley 2022-07-07 00:23:01 Re: BUG #17540: Prepared statement: PG switches to a generic query plan which is consistently much slower
Previous Message Ronan Dunklau 2022-07-06 15:42:28 Re: index cost estimation

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2022-07-06 22:47:27 pg_parameter_aclcheck() and trusted extensions
Previous Message Robert Haas 2022-07-06 21:49:43 Re: tuplesort Generation memory contexts don't play nicely with index builds