Extending Self-Join Elimination to partitioned tables

Started by Trương Hoàng Quân5 months ago2 messageshackers

Thread notes

1 note#performance#moderate#enhancement
hackorum-bot#performance#moderate#enhancement
hackorum-bot
2 days ago

@global #performance #moderate #enhancement

#performance#moderate#enhancement
Jump to latest
#1Trương Hoàng Quân
truonghoangquan456@gmail.com

Hi hackers,

Please find attached a patch that extends Self-Join Elimination to
partitioned tables. The original SJE commit called this out as future
work, since eliminating redundant self-joins before partition expansion
can be a significant win.

This patch is mostly tests at this point. So far the behavior looks
correct to me and I haven't found any cases where it produces a wrong
plan or wrong results, so I've focused on building out regression tests
covering the cases I could come up with.

I'd appreciate any feedback. If anyone has objections, or can think of a
query pattern where this would break (wrong results, crashes, planner
assertions), please let me know. In the meantime I'll keep expanding the
test coverage for the corner cases I can think of.

Regards,
Quan Truong

Attachments:

t253055_1
v1-0001-Add-correctness-test-for-partition-table-sje.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Add-correctness-test-for-partition-table-sje.patchDownload+579-2
#2Andrei Lepikhov
lepihov@gmail.com
In reply to: Trương Hoàng Quân (#1)
Re: Extending Self-Join Elimination to partitioned tables

On 11/07/2026 01:44, Trương Hoàng Quân wrote:

I'd appreciate any feedback. If anyone has objections, or can think of a
query pattern where this would break (wrong results, crashes, planner
assertions), please let me know. In the meantime I'll keep expanding the
test coverage for the corner cases I can think of.

Your patch is a big deal for partitioned configurations. But it still has some
issues. For example, one direct bug:

create table sjo (id int primary key, note text) partition by range (id);
create table sjo_1 partition of sjo for values from (0) to (100);
insert into sjo values (1,'a'), (2,'b');
select p.id from ONLY sjo p, sjo q where p.id = q.id;

This query shouldn't return anything, but I see the output:

id
----
1
2
(2 rows)

After fixing this, I think you should add a regression test that checks correctness.

Next, in config.sgml for enable_self_join_elimination I see the following:
"Takes into consideration only plain tables." — must be updated.

Further in the code, I see a stale comment:
"We only consider ordinary relations as candidates ..."

Performance evidence. It would be nice to see at least a couple of benchmarks:
1. Overhead in case of unsuccessful SJE operation (no unique index present)
2. How much faster (or slower) planning is if SJE has happened. We know it
should be a clear profit for the executor, but what does it cost for the planner?

--
regards, Andrei Lepikhov,
pgEdge