The NULL trap of NOT IN → NOT EXISTS
A single NULL in a NOT IN subquery makes the result 0 rows. In PostgreSQL there is also a performance trap: NOT IN is not turned into an anti-join.
SQL
Situation
A marketing report picks customers with no orders in 2025. The result is correct today. But the query is built so that a single row of data can wipe out the whole result.
Tables
| Table | Column | Type | Description |
|---|---|---|---|
customers | cust_id | integer | Customer ID (PK) |
orders | cust_id | integer | ID of the ordering customer. No NOT NULL constraint |
orders | ord_dt | timestamp | Order date and time |
Customers with no orders in 2025
BEFORE · NOT IN
SELECT *
FROM customers
WHERE cust_id NOT IN (SELECT cust_id
FROM orders
WHERE ord_dt >= DATE '2025-01-01')
Run it and check the result row count and the shape of the execution plan. Then consider "what happens when a NULL enters cust_id on the subquery side".
Approach
1. Check correctness before speed
orders.cust_id has no NOT NULL constraint. Whether today's correct result stays correct depends on NULLs.
2. Expand NOT IN into a condition
x NOT IN (a, b, NULL) is x <> a AND x <> b AND x <> NULL. x <> NULL is UNKNOWN, neither true nor false, so the whole expression can never be true.
3. Look at the shape of the execution plan
PostgreSQL does not turn NOT IN into an anti-join, because the NULL handling rules differ. Instead it builds a hash table from the subquery result and checks each customer against it (hashed SubPlan). If this hash table does not fit in work_mem, the plan switches to re-running the subquery per customer.
Explanation
NOT IN → NOT EXISTS
AFTER · NOT EXISTS
SELECT *
FROM customers c
WHERE NOT EXISTS (SELECT 1
FROM orders o
WHERE o.cust_id = c.cust_id
AND o.ord_dt >= DATE '2025-01-01')
Why the result is correct
- Today both queries return 743 rows, and Buffers is the same at about 19,600.
- Before checks the
hashed SubPlan(thecust_idof 538,000 orders in 2025) as the filter ofSeq Scan customers. After is aHash Join Right Anti(anti-join). - NOT EXISTS only checks, per customer, "is there an order row matching the condition". An order whose
cust_idis NULL matches no customer, so it does not affect the result.
Mixing in one NULL
The sandbox is read-only, so the subquery mimics it by turning the cust_id of the last order (ord_id = 2000000) into NULL with CASE.
TRY · NOT IN with one NULL mixed in
SELECT *
FROM customers
WHERE cust_id NOT IN (SELECT CASE WHEN ord_id = 2000000 THEN NULL ELSE cust_id END
FROM orders
WHERE ord_dt >= DATE '2025-01-01')
TRY · NOT EXISTS with one NULL mixed in
SELECT *
FROM customers c
WHERE NOT EXISTS (SELECT 1
FROM (SELECT CASE WHEN ord_id = 2000000 THEN NULL ELSE cust_id END AS cust_id, ord_dt
FROM orders) o
WHERE o.cust_id = c.cust_id
AND o.ord_dt >= DATE '2025-01-01')
- NOT IN returns 0 rows. NOT EXISTS returns the same 743 rows as before.
Why this is optimal
- NOT EXISTS returns the result the question means, even when a NULL comes in.
- It executes as an anti-join, reading all 2025 orders and all customers once each to compare. The report returns "every customer without an order", so the cost of reading both sides is unavoidable.
Key points
- If a
NOT INlist has even one NULL, the result is 0 rows. Before performance, this is a bug that gives a wrong result. - PostgreSQL performance trap:
NOT IN (subquery)does not become an anti-join. When the subquery result exceedswork_mem, the plan rescans the subquery per customer and slows down sharply.NOT EXISTShas no such concern. - DB difference: Oracle handles
NOT INquickly too, with a NULL-aware anti-join (HASH JOIN ANTI NA) (Oracle scenario 8). The wrong-result problem is common to every DB.