Re: Clarification on interactions between query parameters and partial indexes

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: samuel(dot)hodge(at)live(dot)com
Cc: pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: Clarification on interactions between query parameters and partial indexes
Date: 2020-02-14 16:42:34
Message-ID: 12809.1581698554@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

PG Doc comments form <noreply(at)postgresql(dot)org> writes:
> In section "11.8 Partial Indexes" it states the following:

> "Matching takes place at query planning time, not at run time. As a result,
> parameterized query clauses do not work with a partial index. For example a
> prepared query with a parameter might specify “x < ?” which will never imply
> “x < 2” for all possible values of the parameter."

> We decided to run some tests to verify this statement, as we use both
> partial indexes and parameterized queries on some very large tables (100mil+
> rows). However, we are not able to replicate the stated behavior. It seems
> like the query planner is able to make use of the partial index for both
> parameterized and manually interpolated values.

> Have we misunderstood what the documentation is trying to say or has this
> limitation been fixed?

The statement is true as far as it goes: "x < $1" will never be considered
to imply "x < 2". However, there's a lot of context that's going unstated
there. In some code paths, higher-level code such as the plan cache may
try substituting the concrete value of a parameter as a constant, to see
if it can get a better (but less general) plan that way. I think that's
probably what happened in your experiment, but you didn't provide enough
details to be sure.

regards, tom lane

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Dave Cramer 2020-02-14 16:43:00 Re: documenting divergence from the SQL spec
Previous Message Tom Lane 2020-02-14 16:26:13 Re: documenting divergence from the SQL spec