pgsql: Fix self-join elimination to avoid inheritance scans.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix self-join elimination to avoid inheritance scans.
Date: 2026-10-14 20:38:24
Message-ID: E1xH5k0-00000001dnx-1Wzh@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix self-join elimination to avoid inheritance scans.

Self-join elimination failed to check for inheritance cases,
which has two big problems. First, a unique index on the parent
table isn't sufficient to prove uniqueness across its children too.
Second, since we matched RTEs by OID without considering rte->inh,
we could try to merge "mytable" with "mytable ONLY", which doesn't
even represent the same set of rows.

We can fix both issues by just skipping RTEs that have inh set.
preprocess_relation_rtes already cleared that flag for tables known
not to have inheritance children, so this doesn't break SJE for the
cases where it's valid.

Author: Tender Wang <tndrwang(at)gmail(dot)com>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/CAHewXNn4EjCQEvanN_FPKQ3OJWaBXq8Pxcj-DZYbeQ6_4xSFgw@mail.gmail.com
Backpatch-through: 18

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/cbcccca71e48fa683f520b1d36108d9b35373f75

Modified Files
--------------
src/backend/optimizer/plan/analyzejoins.c | 30 +++++++++++++++++++++++-------
1 file changed, 23 insertions(+), 7 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Geoghegan 2026-10-14 21:33:14 pgsql: Unset SK_SEARCHARRAY when inequality arrays degenerate.
Previous Message Tom Lane 2026-10-14 19:37:45 pgsql: Clear more substructure of a dead relation during join removal.