BUG #19765: Wrong result from a chain of LEFT JOINs since v16

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: leis(at)in(dot)tum(dot)de
Subject: BUG #19765: Wrong result from a chain of LEFT JOINs since v16
Date: 2026-10-16 05:35:05
Message-ID: 19765-5778b18918040583@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: 19765
Logged by: Viktor Leis
Email address: leis(at)in(dot)tum(dot)de
PostgreSQL version: 19rc1
Operating system: Ubuntu 26.04.1
Description:

Hi,

The following self-contained query returns a wrong answer on every release
since PostgreSQL 16 (verified on 16.0, 17.9, 18.6 and current master at
3c157ad1167; PostgreSQL 15.0 and 14.7 return the correct answer):

select a.a, b.b, c.c, d.d from
(values (1),(2)) as a(a)
left join (values (1),(2)) as b(b) on a.a = b.b
left join (values (7),(2)) as c(c) on b.b = c.c
left join (values (1),(2)) as d(d) on c.c = d.d
where a.a = 1;

a | b | c | d
---+---+---+---
1 | 1 | | 1
(1 row)

c.c is correctly null-extended: b.b is 1 and c contains only 7 and 2, so
the third join has no match. But then "c.c = d.d" is NULL = d.d, which is
never true, so d.d must be NULL as well:

a | b | c | d
---+---+---+---
1 | 1 | |
(1 row)

Ordinary tables behave the same way; the VALUES lists are only for brevity.

EXPLAIN shows that all three join conditions have disappeared, leaving
three unqualified nested-loop left joins:

Nested Loop Left Join
-> Values Scan on "*VALUES*"
Filter: (column1 = 1)
-> Nested Loop Left Join
-> Nested Loop Left Join
-> Values Scan on "*VALUES*_1"
Filter: (column1 = 1)
-> Values Scan on "*VALUES*_2"
Filter: (column1 = 1)
-> Values Scan on "*VALUES*_3"
Filter: (column1 = 1)

Best regards,
Viktor Leis

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Staroverov Ilja 2026-10-16 07:58:47 SQL/JSON PASSING fails for char(n), name and domains over text
Previous Message Michael Paquier 2026-10-16 04:00:42 Re: BUG #19764: ManualBug3 Assert failure in pgstat_setup_shared_refs crashes initdb post-bootstrap subprocess