Re: BUG #16993: Query Planner does not use index for EXISTS-query on a large table with all equal foreign key values

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: a(dot)schnabl(at)synedra(dot)com, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #16993: Query Planner does not use index for EXISTS-query on a large table with all equal foreign key values
Date: 2021-05-06 03:28:35
Message-ID: 4113744.1620271715@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

David Rowley <dgrowleyml(at)gmail(dot)com> writes:
> On Thu, 6 May 2021 at 05:47, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> A narrower fix would be to hack var_eq_non_const so that it doesn't
>> assume that the comparison value must be one of the entries in the
>> column. But it seems like whatever change we made in that line would
>> be a very unprincipled hack, because what are you going to assume
>> instead?

> Yeah, this is the same problem as I was mentioning in [1]
> My solution was to go for that "unprincipled hack" in var_eq_non_const().
> I'm not sure I 100% agree that it's a complete hack, you don't really
> have to change the n_distinct by much to get the good plan. It's a
> massive risk to assume that the given value will *always* be the
> single distinct value that's indexed.

Yeah, I agree that it doesn't seem great to let var_eq_non_const return
1.0 when it has no idea what the comparison value is. However, that
doesn't translate to having much confidence in any other value either.
The actual number-of-rows-fetched, given that we know the column
contents are all the same value, is either zero or the whole table.
It's hard to do much with that; and biasing it towards believing the
optimistic value over the pessimistic value seems dangerous.

In any case, I think the real issue here is that we know that *in use*,
the indexscan will fetch either zero or one row, and be pretty quick
either way. The problem is that that knowledge is being applied
at the join level, which is too late to save the path from losing.
How could we move that knowledge down to the scan path costs?

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message David Rowley 2021-05-06 05:53:02 Re: BUG #16993: Query Planner does not use index for EXISTS-query on a large table with all equal foreign key values
Previous Message David Rowley 2021-05-06 02:13:04 Re: BUG #16993: Query Planner does not use index for EXISTS-query on a large table with all equal foreign key values