Re: postgres chooses objectively wrong index

From: Andrei Lepikhov <lepihov(at)gmail(dot)com>
To: Merlin Moncure <mmoncure(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-24 09:21:15
Message-ID: 217AB71B-F368-4DA1-A3B5-ABED94E1BD0A@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-performance

> On 23 Mar 2026, at 22:58, Merlin Moncure <mmoncure(at)gmail(dot)com> wrote:
> On Thu, Mar 19, 2026 at 1:09 AM Andrei Lepikhov <lepihov(at)gmail(dot)com> wrote:
> 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.

We already have plan-freezing and plan-hinting extensions. If I understand you correctly, it makes sense to invent a statistics-freezing module right now. I think such a module will be quite simple - is it a good crutch for you?
Also, we have some stuff already to work out your case someday:
1. Postgres already scans indexes during planning to improve estimations of inequality clauses (get_actual_variable_range). Here may be a way to estimate the bloat effect. Not sure how to do it, but allowing index AM to read the page number of the returned tuple, you might, in principle, detect anomalies in the index.
2. We are quite close to vacuum statistics and detailed index statistics. This is also a way to estimate issues of stale statistics/bloated indexes and decide on the scan type.

So, keep the community posted and provide more real-life examples to build up a proper solution.

Andrei,
pgEdge

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Merlin Moncure 2026-03-25 16:53:42 Re: postgres chooses objectively wrong index
Previous Message Merlin Moncure 2026-03-23 21:58:40 Re: postgres chooses objectively wrong index