| From: | PG Bug reporting form <noreply(at)postgresql(dot)org> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Cc: | vectorplanck(at)gmail(dot)com |
| Subject: | BUG #19963: INNER JOIN ON qual evaluated as scan filter even when the inner input is empty |
| Date: | 2026-12-01 09:35:50 |
| Message-ID: | 19963-ecd0101ea3856293@postgresql.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
The following bug has been logged on the website:
Bug reference: 19963
Logged by: Pisces Mar.
Email address: vectorplanck(at)gmail(dot)com
PostgreSQL version: 19.0
Operating system: Ubuntu (24.04)
Description:
## 1. Summary
For an INNER JOIN whose ON condition references only the outer relation,
PostgreSQL pushes
the ON qual into the outer scan's `Filter` and evaluates it **once per outer
row**, even when
the inner input is **empty**. Since the join can produce no rows at all, the
correct answer is
an empty result and the condition never needs to be evaluated — yet
PostgreSQL raises a
runtime error (22012 / 22003) from it.
The same logical query answers differently depending on plan shape:
| # | Query | Result |
|---|-------|--------|
| P1 | `t INNER JOIN e ON (t-only expr)` (e empty, t outer) | **ERROR
22012** |
| P4 | `e INNER JOIN t ON (same expr)` (operand order swapped) | 0 rows |
| P5 | inner wrapped as derived `(SELECT x FROM e OFFSET …)` (no ORDER BY) |
**ERROR 22012** |
| B2 | inner wrapped as derived `(SELECT x FROM e ORDER BY x OFFSET …)` | 0
rows |
| P3 | `t LEFT JOIN e ON (same expr)` | 3 rows, no error |
P1 vs P4 is the sharpest pair: INNER JOIN is commutative, operand order
cannot change the
answer, yet one form errors and the other returns empty.
## 2. Minimal reproduction
```sql
CREATE TABLE t (c1 INT);
INSERT INTO t VALUES (2147483647), (100), (7);
CREATE TABLE e (x INT); -- empty
ANALYZE t; ANALYZE e;
SELECT t.c1
FROM t INNER JOIN e ON (t.c1 / (t.c1 - t.c1)) > 0;
-- ERROR: division by zero
-- SQLSTATE: 22012
-- Expected: empty set
```
The join result must be empty (no rows in `e`), so the ON expression can
never affect the
result. PostgreSQL nevertheless evaluates it per row of `t`:
```
EXPLAIN SELECT t.c1 FROM t INNER JOIN e ON (t.c1 / (t.c1 - t.c1)) > 0;
QUERY PLAN
----------------------------------------------------
Nested Loop (cost=0.00..1.06 rows=1 width=4)
-> Seq Scan on t (cost=0.00..1.05 rows=1 width=4)
Filter: ((c1 / (c1 - c1)) > 0)
-> Seq Scan on e (cost=0.00..0.00 rows=1 width=0)
```
## 3. Evidence matrix (PostgreSQL 19.0)
Setup: `t(c1 INT)` with rows `2147483647, 100, 7`; `e(x INT)` empty; both
ANALYZEd.
| Case | Query | Result |
|------|-------|--------|
| P1 | `SELECT t.c1 FROM t INNER JOIN e ON (t.c1/(t.c1-t.c1)) > 0` | **ERROR
22012** |
| P2 | `SELECT t.c1 FROM t, e WHERE (t.c1/(t.c1-t.c1)) > 0` | **ERROR
22012** (consistent with ON≡WHERE pushdown) |
| P3 | `SELECT t.c1 FROM t LEFT JOIN e ON (t.c1/(t.c1-t.c1)) > 0` | OK, 3
rows — pair semantics respected for outer joins |
| P4 | `SELECT t.c1 FROM e INNER JOIN t ON (t.c1/(t.c1-t.c1)) > 0` | OK, 0
rows (empty side becomes loop outer) |
| P5 | `SELECT t.c1 FROM t INNER JOIN (SELECT x FROM e OFFSET 1000000000) e2
ON (t.c1/(t.c1-t.c1)) > 0` | **ERROR 22012** |
| B2 | `SELECT t.c1 FROM t INNER JOIN (SELECT x FROM e ORDER BY x OFFSET
1000000000) e2 ON (t.c1/(t.c1-t.c1)) > 0` | OK, 0 rows (Sort+Limit becomes
loop outer; the filtered t scan never runs) |
| P6 | `SELECT t.c1 FROM t INNER JOIN e ON ((t.c1 ^ t.c1) <= ALL (SELECT 1
AS c2 FROM t t2))` | **ERROR 22003** (int `^` overflows after cast to double
precision) — the original AQC-discovered shape |
| P7 | `SELECT t.c1 FROM t WHERE (t.c1/(t.c1-t.c1)) > 0` | ERROR 22012 —
sanity: here the rows ARE candidates, evaluation is required |
| P8 | P1 without ANALYZE on fresh tables | OK, 0 rows — plan flips without
stats; finding is plan-shape dependent |
P6's EXPLAIN confirms the subquery is an initplan `SubPlan all_1` and the
whole `ALL(...)`
predicate sits in the scan filter of `t` — evaluated for every row of `t`
although `e` is empty.
## 4. The inconsistency argument
1. **Operand order (P1 vs P4).** `A ⋈ B ON p` ≡ `B ⋈ A ON p` for INNER JOIN.
PG answers
"error 22012" for one and "empty set" for the other. Whatever
evaluation-order freedom an
engine claims, the same logical query must not flip between error and
result.
2. **Syntactic wrapping (P5/B2 vs P1/P4).** Wrapping the *empty* input in
`ORDER BY + OFFSET`
(which cannot change an empty set) flips the observable outcome from
error to 0 rows —
purely because the planner then iterates the empty side first.
3. **Join type (P1 vs P3).** For LEFT JOIN, PG correctly defers the same
qual to pair
formation (3 rows, no error). The pair-based evaluation model is
otherwise respected —
it is violated only for inner joins, where a one-relation ON qual is
hoisted into a scan
filter.
4. **Cross-engine.** MariaDB 12.0.2 and TiDB v7.5.1 both return **0 rows, no
error** for the
analogous division-by-zero ON quals (both the explicit INNER JOIN and the
comma/WHERE form).
MySQL 8.0.43 agrees, and its division-by-zero is a warning rather than an
error for SELECT,
so the sharper probe there is an integer-overflow ON qual (`BIGINT +
BIGINT`, which MySQL
*does* raise as error 1690 when evaluation is required). With `t(c1
BIGINT)` holding
`9223372036854775807, 100, 7` and empty `e(x INT)`:
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Kirill Reshke | 2026-12-01 10:40:49 | Re: BUG #19962: pg_dumpall can't produce restorable circular grants with ADMIN OPTION |
| Previous Message | Laurenz Albe | 2026-12-01 08:20:46 | Re: SIMILAR TO expressions translate wildcards where they shouldn't |