Re: BUG #19963: INNER JOIN ON qual evaluated as scan filter even when the inner input is empty

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: "vectorplanck(at)gmail(dot)com" <vectorplanck(at)gmail(dot)com>, "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #19963: INNER JOIN ON qual evaluated as scan filter even when the inner input is empty
Date: 2026-12-01 13:41:59
Message-ID: CAKFQuwb8ZGPwXNcj6Y3y6_zhOmVuie0zDDVhye-VCcJn384rRA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tuesday, December 1, 2026, PG Bug reporting form <noreply(at)postgresql(dot)org>
wrote:

> 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.
>
> ## 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)
> ```
>

This seems more like a missed optimization chance than a bug. The question
is whether any sanely written query is actually impacted by this missed
optimization or is it just this degenerate PoC form that doesn’t matter in
reality. If the latter, the priority of this seems quite low. Especially
if the reporter isn’t supplying an agent-suggested initial solution.

David J.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Fujii Masao 2026-12-01 14:19:45 Re: Lost walsender wakeup stalls synchronous commits until the standby's next reply (16+)
Previous Message Kirill Reshke 2026-12-01 13:33:45 Re: BUG #19963: INNER JOIN ON qual evaluated as scan filter even when the inner input is empty