OR across two tables: filter after join → UNION ALL

When one side of an OR is a customer column and the other an order column, as in "orders of VIP customers or canceled orders", neither table can be filtered first, so all 14,000 orders of the last 7 days are joined and a Join Filter picks 559 rows. Splitting the OR into two UNION ALL branches and removing the overlapping rows with IS NOT TRUE cuts Buffers from 19,586 to 5,345.

SQL

Situation

Every morning the customer management team checks, among the orders of the last 7 days, "orders of VIP customers or canceled orders". The result is just over 500 rows, yet the whole orders table is read.

Tables

TableColumnsDescription
ordersord_dt, status14,000 orders since 2025-09-20, of which 407 canceled
customersgrade1,000 VIPs (1%)
IndexColumns
customers_x1grade
orders_x1cust_id, ord_dt
orders_x2status, ord_dt, ord_id

There is no index that starts with ord_dt alone.

Query

BEFORE · OR filter after join

SELECT o.ord_id, o.ord_dt, o.status, o.amt, c.cust_nm, c.grade
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.ord_dt >= TIMESTAMP '2025-09-20'
  AND (c.grade = 'VIP' OR o.status = 'CANCELED')

Look at the Join Filter and Rows Removed by Join Filter of Hash Join.

Approach

1. See at which node the OR condition is evaluated

c.grade = 'VIP' can be decided from the customer alone, and o.status = 'CANCELED' from the order alone. Combined with OR, both tables are needed to decide true or false, so the condition becomes the Join Filter of the join node. The only condition usable before the join is o.ord_dt >= ….

2. Consider how each OR branch would be read on its own

"Recent orders of VIP customers" is a matter of finding 1,000 customers through the customer-side index and attaching orders. "Recent canceled orders" is a matter of a single orders_x2 range. The two branches have different driving tables.

3. Work out the conditions under which splitting gives the same result

Combining the two branches with UNION ALL makes canceled orders of VIP customers appear in both. Removing duplicates with UNION can also remove rows with identical values that were in the original result. Remove from the second branch only the rows for which the first branch's condition is true, while keeping the rows where grade is NULL.

Explanation

OR → two UNION ALL branches

AFTER · UNION ALL + IS NOT TRUE

SELECT o.ord_id, o.ord_dt, o.status, o.amt, c.cust_nm, c.grade
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE c.grade = 'VIP'
  AND o.ord_dt >= TIMESTAMP '2025-09-20'
UNION ALL
SELECT o.ord_id, o.ord_dt, o.status, o.amt, c.cust_nm, c.grade
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.status = 'CANCELED'
  AND o.ord_dt >= TIMESTAMP '2025-09-20'
  AND (c.grade = 'VIP') IS NOT TRUE

Why it got faster

  • Before reads 2 million orders with Seq Scan on orders to pick the 14,000 of the last 7 days (18,752 blocks) and hash-joins them with all 100,000 customers (834 blocks). Join Filter discards 13,441 rows and keeps 559. Buffers 19,586.
  • After runs as two branches under Append. The first branch finds the 1,000 VIPs through customers_x1 (837 blocks) and finds the orders of the last 7 days for each customer through orders_x1 (1,000 times, 3,154 blocks, 155 rows).
  • The second branch reads only the 407 canceled orders of the last 7 days through orders_x2, looks up customers 407 times, and keeps 404 rows after removing VIPs (1,354 blocks). Total Buffers 5,345, with 559 result rows, the same as Before.
  • PostgreSQL keeps the value distribution of grade (MCV) in its statistics and estimates VIPs at 970. So it picks the right plan for each branch without hints.

Why IS NOT TRUE is needed

c.gradeFirst branch grade = 'VIP'Second branch (grade = 'VIP') IS NOT TRUEIf the second branch were grade <> 'VIP'
'VIP'true → includedfalse → excludedexcluded
'GOLD'falsetrue → includedincluded
NULLNULL → excludedtrue → includedNULL → excluded from both

In the original OR, canceled orders of customers with a NULL grade also appear in the result through o.status = 'CANCELED' alone. The current data has no NULL grades, but grade has no NOT NULL constraint. IS NOT TRUE means the same as Oracle's LNNVL.

Why this is optimal

  • The second branch reads only the 407 canceled orders that become the result.
  • The remaining cost of the first branch is the orders_x1 probes for the 1,000 VIPs. Most VIPs have no orders within 7 days, but confirming that requires one index descent per customer.

Key points

  • Combining conditions on different tables with OR pushes that condition to the Join Filter of the join node. Write down separately which conditions can filter before the join.
  • PostgreSQL has no transformation that splits an OR into branches. An OR within one table is handled with BitmapOr, but an OR across two tables is split into UNION ALL by hand.
  • Remove overlapping rows with IS NOT TRUE, not UNION. A <> that ignores NULL changes the result.