Many ORs instead of IN
Severity: Warning
Three or more OR conditions comparing the same column to different values can be simplified to an IN list. While PostgreSQL’s planner can sometimes collapse OR chains into equivalent IN lookups, an explicit IN is clearer, shorter, and gives the planner the simplest possible input.
Example
Section titled “Example”-- Verbose and harder to maintainSELECT * FROM ordersWHERE status = 'pending' OR status = 'processing' OR status = 'review';Why it matters
Section titled “Why it matters”Long OR chains are harder to read and maintain. Each time a new value is added, another OR condition = 'value' clause has to be appended. The planner typically handles both forms identically, but the IN syntax is unambiguous — it states exactly what you mean with no room for precedence confusion when combined with AND.
When the OR conditions involve different columns, the planner may not be able to merge them, resulting in multiple index scans or a sequential scan with a complex filter. Rewriting to IN where possible helps keep plans simple.
How to fix it
Section titled “How to fix it”Replace the OR chain with IN:
SELECT * FROM ordersWHERE status IN ('pending', 'processing', 'review');If the values come from application code, parameterize with = ANY($1):
-- Using a parameterized arraySELECT * FROM orders WHERE status = ANY($1);Detection
Section titled “Detection”Query Doctor flags this nudge when it finds a WHERE clause with three or more OR branches. The nudge marker points to the OR expression in the formatted SQL view.