Skip to content

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.

-- Verbose and harder to maintain
SELECT * FROM orders
WHERE status = 'pending' OR status = 'processing' OR status = 'review';

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.

Replace the OR chain with IN:

SELECT * FROM orders
WHERE status IN ('pending', 'processing', 'review');

If the values come from application code, parameterize with = ANY($1):

-- Using a parameterized array
SELECT * FROM orders WHERE status = ANY($1);

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.