Re: postgres chooses objectively wrong index

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Andrei Lepikhov <lepihov(at)gmail(dot)com>
Cc: Alexey Ermakov <alexius(dot)work(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-performance(at)lists(dot)postgresql(dot)org
Subject: Re: postgres chooses objectively wrong index
Date: 2026-03-23 21:58:40
Message-ID: CAHyXU0yAibBKPaJ-A59inJa+iv1Q1bOfMp+VPZUyNjvquBNVSQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Thu, Mar 19, 2026 at 1:09 AM Andrei Lepikhov <lepihov(at)gmail(dot)com> wrote:

> On 18/3/26 19:38, Merlin Moncure wrote:
> > On Tue, Mar 17, 2026 at 11:27 PM Alexey Ermakov <alexius(dot)work(at)gmail(dot)com
> > I think the planner is not giving enough bonus for an exact match versus
> > an inexact match on partial index mathcing, (A=A should be better than
> > A IN(A,B,C)), and it's unclear why the planner things bitmap heap + sort
> > is outperforming a raw read off the index base on marginal estimated row
> > counts. Lowering random_page_cost definitely biases the plan I like,
> > but it skews both estimates.
>
> One ongoing shortcoming is that cardinality estimation takes place early
> in the optimisation process and uses all filter conditions. This can be
> frustrating because a partial index covers just part of the table and
> could give the optimiser better statistics. If we ignored the index
> condition, we might get a more accurate estimate.
>

Thanks. I understand the challenge with estimation around partial
indexes. Something deeper seems to be at play here.

Poking around more, I see that the bad plans are related to bloat. A
simple REINDEX of one of the indexes made the problem disappear; however,
what's odd is that the estimates didn't really change although the net plan
cost certainly did. It's also worth noting ANALYZE doesn't help, only
REINDEX does.

I keep coming back to this: the bitmap scan noted above makes no sense. I'm
trying to figure out what is steering the planner in that direction and
eliminate it.

This problem reliably reproduces about once a month (taking down
production). I'll wait for it to recur and look at it with fresh eyes.

merlin

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Andrei Lepikhov 2026-03-24 09:21:15 Re: postgres chooses objectively wrong index
Previous Message Andrei Lepikhov 2026-03-19 07:09:33 Re: postgres chooses objectively wrong index