pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedDoes PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner?
5+ messages / 3 participants
[nested] [flat]
* Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner?
@ 2022-01-21 16:38 Dominique Devienne <ddevienne@gmail.com>
2022-01-21 16:44 ` Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? David G. Johnston <david.g.johnston@gmail.com>
2022-01-21 16:47 ` Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 5+ messages in thread
From: Dominique Devienne @ 2022-01-21 16:38 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi,
I just saw some code of ours that takes 4 strings are arguments,
and wants to do optional filtering on those, in a SELECT statement.
Something like:
```
void foo(string arg1, string arg2, ...) {
... = exec(
conn, "SELECT * from tab where col1 like $1 and col2 like $2 and ...",
arg1.empty()? "%": arg1, arg2.empty()? "%": arg2, ...
);
}
```
where the exec() helper does proper binding of the argN strings.
Will the query planner be able to *peek* into the args, and turn the
`colN like $N`
into a no-op? Note that in this case, this is *not* a prepared
statement at the moment,
but it could be in the future.
So I guess my question can also be viewed as whether it's worth
preparing several statements
for the various cases of empty argN strings, or does the planner do
*bind-peeking*, and thus a single prepared statement would do the job,
and still have different plans used depending on the actual binds?
I'm assuming PostgreSQL does bind-peeking like Oracle, but I don't
know, and I've never read anything yet about that.
Thanks, --DD
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner?
2022-01-21 16:38 Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Dominique Devienne <ddevienne@gmail.com>
@ 2022-01-21 16:44 ` David G. Johnston <david.g.johnston@gmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: David G. Johnston @ 2022-01-21 16:44 UTC (permalink / raw)
To: Dominique Devienne <ddevienne@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Fri, Jan 21, 2022 at 9:36 AM Dominique Devienne <ddevienne@gmail.com>
wrote:
>
> for the various cases of empty argN strings, or does the planner do
> *bind-peeking*, and thus a single prepared statement would do the job,
> and still have different plans used depending on the actual binds?
>
> I'm assuming PostgreSQL does bind-peeking like Oracle, but I don't
> know, and I've never read anything yet about that.
>
>
A prepared statement either generates a custom plan and, as a side-effect,
does bind-peeking, or it uses the single prepared plan it has established
and executes that. It doesn't use bind-peeking to decide among multiple
saved prepared plans. There is more to it than that, like a 5 custom plan
threshold before abandoning bind-peeking (I think I got that right...).
David J.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner?
2022-01-21 16:38 Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Dominique Devienne <ddevienne@gmail.com>
@ 2022-01-21 16:47 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-01-21 17:08 ` Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Dominique Devienne <ddevienne@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Tom Lane @ 2022-01-21 16:47 UTC (permalink / raw)
To: Dominique Devienne <ddevienne@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
Dominique Devienne <ddevienne@gmail.com> writes:
> Will the query planner be able to *peek* into the args, and turn the
> `colN like $N`
> into a no-op?
No. It would not do that even if the pattern were constant '%';
it doesn't know that much about that particular function.
There is a notion of "custom plans" in which parameter values are
inserted as constants, precisely to allow simplifications based on
known constant values. But this particular case isn't implemented.
I am entirely unfamiliar with the terminology "bind-peeking", so
I can't say whether that's effectively the same thing as our
custom plans.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner?
2022-01-21 16:38 Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Dominique Devienne <ddevienne@gmail.com>
2022-01-21 16:47 ` Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Tom Lane <tgl@sss.pgh.pa.us>
@ 2022-01-21 17:08 ` Dominique Devienne <ddevienne@gmail.com>
2022-01-21 17:22 ` Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 5+ messages in thread
From: Dominique Devienne @ 2022-01-21 17:08 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org
On Fri, Jan 21, 2022 at 5:47 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Dominique Devienne <ddevienne@gmail.com> writes:
> > Will the query planner be able to *peek* into the args, and turn `colN like $N` into a no-op?
Thanks for the replies, David and Tom.
> No. It would not do that even if the pattern were constant '%';
> it doesn't know that much about that particular function.
Interesting. Thanks.
> There is a notion of "custom plans" in which parameter values are
> inserted as constants, precisely to allow simplifications based on
> known constant values. But this particular case isn't implemented.
Where can I read more about this? And is it something the client has
any influence on?
> I am entirely unfamiliar with the terminology "bind-peeking"
I thought it was "standard speak" in the SQL world :)
I don't recall where I picked that up, to be honest.
Here's what seems like an official use in the Oracle PL/SQL doc:
https://oracle.readthedocs.io/en/latest/plsql/bind/bind-peeking.html
But that link does imply that what I thought could happen in Oracle,
i.e. that it could keep several plans for the same prepared statement,
that it would select at runtime based on the actual binds, is NOT
happening in Oracle either...
Thanks, --DD
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner?
2022-01-21 16:38 Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Dominique Devienne <ddevienne@gmail.com>
2022-01-21 16:47 ` Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Tom Lane <tgl@sss.pgh.pa.us>
2022-01-21 17:08 ` Re: Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Dominique Devienne <ddevienne@gmail.com>
@ 2022-01-21 17:22 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2022-01-21 17:22 UTC (permalink / raw)
To: Dominique Devienne <ddevienne@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
Dominique Devienne <ddevienne@gmail.com> writes:
> On Fri, Jan 21, 2022 at 5:47 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> There is a notion of "custom plans" in which parameter values are
>> inserted as constants, precisely to allow simplifications based on
>> known constant values. But this particular case isn't implemented.
> Where can I read more about this? And is it something the client has
> any influence on?
Start here:
https://www.postgresql.org/docs/current/sql-prepare.html
https://www.postgresql.org/docs/current/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2022-01-21 17:22 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-01-21 16:38 Does PostgreSQL do bind-peeking? Is `col like '%'` optimized-away by the planner? Dominique Devienne <ddevienne@gmail.com>
2022-01-21 16:44 ` David G. Johnston <david.g.johnston@gmail.com>
2022-01-21 16:47 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-01-21 17:08 ` Dominique Devienne <ddevienne@gmail.com>
2022-01-21 17:22 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox