Skip to content
Advertisement

Postgres changing the query from index Only scan to bit map scan when data set increases

I have two same queries but with different where condition values

When I ran this two queries they both producing different result

for first query the result

and for the 2nd one

Can anyone explain Me why Postgres is making a BitMap Scan instead of Index Only scan ?

Advertisement

Answer

The short version is that Postgres has a cost-based approach, so it has estimated that the cost of doing so is less in the second case, based on the statistics it has.

In your case, the total cost (estimated) of each of these queries is 8.32 and 11.12 respectively. You may be able to see the cost of the index-only scan for the second query by running set enable_bitmapscan = off.

Note that, based on its statistics, Postgres estimated that the first query would return 1 row (actually 4), and that the second would return 2 rows (actually 0).

There are several ways to get better statistics, but if analyze (or autovacuum) hasn’t been run on that table for a while, that is a common cause of bad estimates. Another tell-tale that vacuum may not have been run recently (at least on this table) is the Heap Fetches: 5 you can see in the first query plan.

I’m confused by the “when data set increases” part of your question, please do add more context on that front if relevant.

Finally, if you’re not already planning a PostgreSQL upgrade, I highly recommend doing so soon. 9.6 is nearly out of support, and versions 10, 11, 12, and 13 each contained a host of performance focussed features.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement