Wrong results from self-join elimination with inheritance and ONLY
Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.
This thread has been committed, so CI has stopped here. Anything below is the last result it produced.
You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:
docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t254147psql -h localhost -U postgresBuilt from patchset v1 (message #1), October 14, 2026 at 01:36 AM.
Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:
git clone --branch t254147_1 https://github.com/hackorum-dev/postgres.gitIn a checkout you already have, add the fork once:
git remote add hackorum https://github.com/hackorum-dev/postgres.gitthen, for this patchset and every later one:
git fetch hackorum t254147_1 && git checkout t254147_1Patchset v1 (message #1) is on t254147_1
Hi,
I found a wrong-result case in self-join elimination involving an
inheritance scan and an ONLY scan.
Here is a reproducer:
CREATE TABLE r1 (id int, v int);
INSERT INTO r1 VALUES (1, 1);
CREATE TABLE r1c () INHERITS (r1);
INSERT INTO r1c VALUES (1, 100);
CREATE UNIQUE INDEX r1_id ON r1 (id);
ANALYZE r1, r1c;
SELECT * FROM r1 p1 JOIN ONLY r1 p2 ON p1.id = p2.id;
With enable_self_join_elimination enabled, this returns:
postgres=# show enable_self_join_elimination ;
enable_self_join_elimination
------------------------------
on
(1 row)
postgres=# SELECT * FROM r1 p1 JOIN ONLY r1 p2 ON p1.id = p2.id;
id | v | id | v
----+---+----+---
1 | 1 | 1 | 1
(1 row)
With it disabled, the query correctly returns:
postgres=# set enable_self_join_elimination = off;
SET
postgres=# SELECT * FROM r1 p1 JOIN ONLY r1 p2 ON p1.id = p2.id;
id | v | id | v
----+-----+----+---
1 | 1 | 1 | 1
1 | 100 | 1 | 1
(2 rows)
remove_self_joins_recurse() groups candidates by relation OID without
checking rte->inh.
In this case, the two references have the same OID but different scan scopes.
The unique index on r1 does not prove that a matching row from r1c is
the same physical row as the matching row in r1.
The attached patch excludes RTEs with inh set from SJE candidates.
Partitioned tables are already excluded by the relkind check, while
references to ordinary tables without children have their inh flag
cleared during preprocessing.
The patch also adds regression tests for both reference orders and
verifies that a join between two ONLY scans can still be eliminated.
--
Thanks,
Tender Wang
Tender Wang <tndrwang@gmail.com> writes:
I found a wrong-result case in self-join elimination involving an
inheritance scan and an ONLY scan.
Pushed, thanks for the report.
(I left out the proposed regression tests, which seemed much too
heavyweight for a one-line change.)
regards, tom lane