Subquery pull-up: SubPlan vs semi-join

The cost of an EXISTS subquery depends on whether it runs once per row (SubPlan) or is turned into a semi-join. The join-then-DISTINCT antipattern is covered too.

SQL

Situation

A list of Busan customers who had a canceled order in 2025. A subquery was copied as is from another screen with OFFSET 0 attached, and because of it the subquery runs separately for each customer.

Tables

TableColumnsDescription
customerscust_id, region100,000 customers, about 20,000 in Busan
orderscust_id, status, ord_dtabout 16,000 canceled orders in 2025
IndexColumns
orders_x1cust_id, ord_dt
orders_x2status, ord_dt, ord_id

Busan customers with a canceled order in 2025

BEFORE · Repeated subquery (OFFSET 0)

SELECT c.*
FROM customers c
WHERE c.region = 'BUSAN'
  AND EXISTS (SELECT 1
              FROM orders o
              WHERE o.cust_id = c.cust_id
                AND o.status = 'CANCELED'
                AND o.ord_dt >= DATE '2025-01-01'
              OFFSET 0)

OFFSET 0 stops PostgreSQL from pulling the subquery up into the outer query. It has the same effect as Oracle's NO_UNNEST hint. Run it and check the Starts of SubPlan.

Approach

1. Look at how many times the subquery runs

If the Starts of SubPlan equals the number of Busan customers (about 20,000), each customer visited orders_x1 and scanned that customer's 2025 orders to check for a canceled one.

2. See whether the question can become a set

"Customers with a canceled order" is the cust_id set of the 16,000 canceled orders in 2025. Build that set once and match it to Busan customers, and there is no repetition. That is a semi-join.

3. Find what blocks the transformation

PostgreSQL normally turns EXISTS into a semi-join by itself. It cannot when OFFSET 0, LIMIT, an aggregate, or a volatile function is inside the subquery.

Explanation

Remove what blocks the transformation → hash semi-join

AFTER · Semi-join (default)

SELECT c.*
FROM customers c
WHERE c.region = 'BUSAN'
  AND EXISTS (SELECT 1
              FROM orders o
              WHERE o.cust_id = c.cust_id
                AND o.status = 'CANCELED'
                AND o.ord_dt >= DATE '2025-01-01')

Why it got faster

  • In Before, SubPlan runs 19,893 times, and per customer it finds 2025 orders with orders_x1 and filters for canceled ones. Buffers about 151,700.
  • After reads the 15,989 canceled orders of 2025 once with orders_x2, groups them per customer (HashAggregate), and hash-joins with Busan customers. Buffers about 5,800. To keep "once per customer", the planner chose deduplication followed by a plain join (unique-ify) instead of a semi-join.

Variation: forcing an NL semi-join

TRY · Forced NL join

SET LOCAL enable_hashjoin = off;
SET LOCAL enable_mergejoin = off;
SELECT c.*
FROM customers c
WHERE c.region = 'BUSAN'
  AND EXISTS (SELECT 1
              FROM orders o
              WHERE o.cust_id = c.cust_id
                AND o.status = 'CANCELED'
                AND o.ord_dt >= DATE '2025-01-01')
  • It first builds the 13,463 customers with canceled orders, then checks per customer through customers_pk whether they are in Busan (Starts 13,463). Buffers about 45,300. Less repetition than SubPlan, but more expensive than the hash join.

Variation: join then DISTINCT

TRY · Join then DISTINCT

SELECT DISTINCT c.*
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE c.region = 'BUSAN'
  AND o.status = 'CANCELED'
  AND o.ord_dt >= DATE '2025-01-01'
  • The result is the same, but the join builds 3,356 rows and then removes duplicates with Sort and Unique. Buffers is about 5,800, the same as After, but the sort costs extra. Write "does it exist" questions with EXISTS.

Why this is optimal

  • It builds the canceled-order set once through the index (about 4,900 blocks) and reads the customer table once (about 830 blocks). There are no repeated lookups.

Key points

  • EXISTS is expensive when run per row (SubPlan) and cheap when turned into a set (semi-join, or deduplicate then join).
  • In PostgreSQL, OFFSET 0 is an optimization fence that blocks subquery transformation. Unless it is deliberate, suspect OFFSET and LIMIT inside a subquery.
  • DB difference: Oracle decides with the UNNEST/NO_UNNEST and NL_SJ/HASH_SJ hints (Oracle scenario 7); in PostgreSQL the planner decides. Oracle's FILTER caches subquery results, but PostgreSQL's SubPlan does not.