Transformed join columns: join conditions that keep the NL join inner index usable

Joining 10 numeric order numbers sent by the settlement system with varchar order numbers while converting the orders-side column makes the inner index unusable, so 2 million rows are hash-joined. The same happens when a calendar is joined to orders with date_trunc. Moving the conversion to the outer value, or rewriting it as a range join, lets the NL join use the index.

SQL

Situation

Two reports are slow.

  1. Settlement reconciliation: 10 order numbers sent by the settlement system as numbers (bigint) are matched against orders. orders.ord_no is a varchar made only of digits, so a plain comparison raises an error; the developer converted the orders side with ::bigint in the join.
  2. Daily payment status: paid orders are attached per date to a list of the last 7 days (generate_series). Days without orders must still show 0, so it is a LEFT JOIN, and the join uses date_trunc('day', ord_dt) to match dates.

The two queries return 10 rows and 7 rows.

Tables

TableColumnsDescription
ordersord_no'YYYYMMDD' + 8-digit serial number, varchar
ordersstatus, ord_dt140,577 paid orders, about 136 per day
IndexColumns
orders_x3ord_no
orders_x2status, ord_dt, ord_id

Query

BEFORE · Settlement reconciliation: cast on the orders side

WITH settle(ord_no, settle_amt) AS (
  VALUES (2025081301910987, 351200), (2025081801921098, 120400), (2025082401932109, 98000),
         (2025082901943210, 455000), (2025090401954321, 17300),  (2025090901965432, 230000),
         (2025091501976543, 64000),  (2025092001987654, 410500), (2025092601998123, 77700),
         (2025092601999001, 128800)
)
SELECT s.ord_no, s.settle_amt, o.ord_id, o.amt, o.status
FROM settle s
JOIN orders o ON o.ord_no::bigint = s.ord_no

BEFORE · Daily status: date_trunc join

SELECT d.dt::date AS dt, COUNT(o.ord_id) AS paid_cnt, COALESCE(SUM(o.amt), 0) AS paid_amt
FROM generate_series(TIMESTAMP '2025-09-20', TIMESTAMP '2025-09-26', INTERVAL '1 day') AS d(dt)
LEFT JOIN orders o ON o.status = 'PAID'
                  AND date_trunc('day', o.ord_dt) = d.dt
GROUP BY d.dt
ORDER BY d.dt

In both execution plans, look at the A-Rows of the node that reads orders, and whether the join condition is in Index Cond or in Hash Cond/Merge Cond.

Approach

1. Write down what the inner side of the NL join looks up

When one value comes from the outer side, the inner side must find the matching rows through an index. The join condition becomes an index access condition only in the form inner column = outer value. With f(inner column) = outer value, the index holds no f(column) order, so it cannot be searched (the join version of scenario 1).

2. See what the planner picks instead

If the index cannot be used for lookups, an NL join would have to read the whole inner side for every outer row. The planner instead reads the inner side once in full and picks a hash join or merge join. Even with a 10-row result, it reads the whole inner table.

3. See which side the conversion can move to

  • Cast: is converting the number to text the same comparison as converting the text to a number? In this data order numbers are always 16 digits and never start with 0, so it is the same.
  • Date transformation: date_trunc('day', ord_dt) = date is the same as "at or after 00:00 of that day and before 00:00 of the next day". It can be rewritten as a range condition that does not transform the column.

Explanation

1. Move the cast to the outer value

AFTER · Convert the outer value to text

WITH settle(ord_no, settle_amt) AS (
  VALUES (2025081301910987, 351200), (2025081801921098, 120400), (2025082401932109, 98000),
         (2025082901943210, 455000), (2025090401954321, 17300),  (2025090901965432, 230000),
         (2025091501976543, 64000),  (2025092001987654, 410500), (2025092601998123, 77700),
         (2025092601999001, 128800)
)
SELECT s.ord_no, s.settle_amt, o.ord_id, o.amt, o.status
FROM settle s
JOIN orders o ON o.ord_no = s.ord_no::text

Why it got faster

  • Before has Hash Cond: ((o.ord_no)::bigint = "*VALUES*".column1). It reads 2 million orders with a Seq Scan (18,752 blocks), converts each row to a number, and compares it with the 10 rows in the hash table. Note also that E-Rows is 100,000, far from the actual 10. A transformed column has no statistics, so the planner uses a default estimate.
  • In After, the inner side of Nested Loop is Index Scan using orders_x3 with Index Cond: ((ord_no)::text = ("*VALUES*".column1)::text). It descends the index once for each of the 10 order numbers (Starts 10, 4 blocks per iteration). Buffers 40.

2. Rewrite the date transformation as a range join

AFTER · Range join

SELECT d.dt::date AS dt, COUNT(o.ord_id) AS paid_cnt, COALESCE(SUM(o.amt), 0) AS paid_amt
FROM generate_series(TIMESTAMP '2025-09-20', TIMESTAMP '2025-09-26', INTERVAL '1 day') AS d(dt)
LEFT JOIN orders o ON o.status = 'PAID'
                  AND o.ord_dt >= d.dt
                  AND o.ord_dt <  d.dt + INTERVAL '1 day'
GROUP BY d.dt
ORDER BY d.dt

Why it got faster

  • Before reads all 140,577 paid orders (Bitmap Heap Scan, 19,417 blocks), sorts them by the date_trunc value, and does a Merge Left Join with the 7 dates. Only 953 orders are actually joined.
  • In After, the inner side of Nested Loop Left Join has Index Cond: ((status = 'PAID') AND (ord_dt >= d.dt) AND (ord_dt < (d.dt + '1 day'))). For each date it reads only that day's range of orders_x2 (Starts 7, about 136 rows per iteration). Buffers 164.
  • Dates without orders remain with 0 rows because of the LEFT JOIN. The result is the same as Before.

Why this is optimal

  • Settlement reconciliation reads only 10 orders × index height, and the daily status reads only the blocks holding the 953 paid orders of the 7 days. Only the rows the result needs were read.
  • The daily status reads table blocks because amt is not in the index. A report that only counts rows would drop further with an Index Only Scan.

Note: comparing without a cast

WITH settle(ord_no) AS (VALUES (2025081301910987), (2025081801921098))
SELECT o.ord_id, o.ord_no
FROM settle s
JOIN orders o ON o.ord_no = s.ord_no
-- ERROR: operator does not exist: character varying = bigint
  • PostgreSQL raises the error above. It does not implicitly convert between text and numbers, so the developer writes the conversion explicitly, and which side gets converted then decides performance. Oracle converts the text side with TO_NUMBER without an error, so the same problem occurs silently (Oracle scenario 1).

Key points

  • Join conditions follow the same rules as WHERE conditions. Transforming the inner column of an NL join makes the inner index unusable, and the planner switches to a join that reads the whole inner side.
  • Move the conversion to the outer side (the side with fewer values). Converting 10 outer values is cheaper than converting 2 million inner rows, and the inner index stays usable.
  • When matching a date/timestamp column to dates, write a range join >= date AND < date + 1 day instead of date_trunc or ::date.
  • Changing the conversion direction can change the meaning of the comparison ('0012' vs 12, decimal points, spaces). First confirm that the data format is fixed.