Bad query plan when you add many OR conditions - Mailing list pgsql-performance

From Marco Colli
Subject Bad query plan when you add many OR conditions
Date
Msg-id CAFvCgN5N+C1BZdri5t2gnP15UFNBGtg6Ve0nJ7WALr4XwWYorg@mail.gmail.com
Whole thread Raw
Responses Re: Bad query plan when you add many OR conditions  (Justin Pryzby <pryzby@telsasoft.com>)
Re: Bad query plan when you add many OR conditions  (Jeff Janes <jeff.janes@gmail.com>)
Re: Bad query plan when you add many OR conditions  (Thomas Kellerer <spam_eater@gmx.net>)
List pgsql-performance
Hello!

I have a query on a large table that is very fast (0s):

Basically the query matches the rows that have a tag1 OR tag2 OR tag3 OR tag4 OR tag5... 

However if you increase the number of OR at some point PostgreSQL makes the bad decision to change its query plan! And the new plan makes the query terribly slow:

Instead of this (which is fast):
  Bitmap Index Scan on index_subscriptions_on_project_id_and_tags
It starts using this (which is slow):
  Parallel Index Scan using index_subscriptions_on_project_id_and_created_at
The choice seems quite stupid since it doesn't have the tags on the new index... and indeed the query takes about 1 minute instead of a few milliseconds. Here's a list of the available indexes:

How can I encourage PostgreSQL to use the Bitmap Index Scan even when there are many OR conditions? I have tried with VACUUM ANALYZE subscriptions but it doesn't help.

Note: the query is generated dynamically by customers of a SaaS, so I don't have full control on it


Thank you very much for any advice!
Marco Colli


pgsql-performance by date:

Previous
From: Justin Pryzby
Date:
Subject: Re: distinguish index cost component from table component
Next
From: Justin Pryzby
Date:
Subject: Re: Bad query plan when you add many OR conditions