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.
- Settlement reconciliation: 10 order numbers sent by the settlement system as numbers (
bigint) are matched against orders.orders.ord_nois avarcharmade only of digits, so a plain comparison raises an error; the developer converted the orders side with::bigintin the join. - 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 aLEFT JOIN, and the join usesdate_trunc('day', ord_dt)to match dates.
The two queries return 10 rows and 7 rows.
Tables
| Table | Columns | Description |
|---|---|---|
orders | ord_no | 'YYYYMMDD' + 8-digit serial number, varchar |
orders | status, ord_dt | 140,577 paid orders, about 136 per day |
| Index | Columns |
|---|---|
orders_x3 | ord_no |
orders_x2 | status, 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) = dateis 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 aSeq 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 LoopisIndex Scan using orders_x3withIndex 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 thedate_truncvalue, and does aMerge Left Joinwith the 7 dates. Only 953 orders are actually joined. - In After, the inner side of
Nested Loop Left JoinhasIndex 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 oforders_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
amtis not in the index. A report that only counts rows would drop further with anIndex 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_NUMBERwithout 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 dayinstead ofdate_truncor::date. - Changing the conversion direction can change the meaning of the comparison (
'0012'vs12, decimal points, spaces). First confirm that the data format is fixed.