Removing LEFT JOINs in more cases

From: David Rowley <david(dot)rowley(at)2ndquadrant(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Removing LEFT JOINs in more cases
Date: 2017-11-01 00:09:13
Views: Raw Message | Whole Thread | Download mbox
Lists: pgsql-hackers


Normally we'll only ever remove a LEFT JOIN relation if it's unused
and there's no possibility that the join would cause row duplication.
To check that the join wouldn't cause row duplicate we make use of
proofs, such as unique indexes, or for sub-queries, we make use of
DISTINCT and GROUP BY clauses.

There's another case that we don't handle, and it's VERY simple to test for.

Quite simply, it seems we could remove the join in cases such as:

create table t1 (id int primary key);
create table t2 (id int primary key, b int not null);

insert into t2 values(1,1),(2,1);
insert into t1 values(1);

select distinct t1.* from t1 left join t2 on;


select from t1 left join t2 on GROUP BY;

but not:

select,count(*) from t1 left join t2 on GROUP BY;

In this case, the join *can* cause row duplicates, but the distinct or
group by would filter these out again anyway, so in these cases, we'd
not only get the benefit of not joining but also not having to remove
the duplicate rows caused by the join.

Given how simple the code is to support this, it seems to me to be
worth handling.

A patch to do this is attached.

David Rowley
PostgreSQL Development, 24x7 Support, Training & Services

Attachment Content-Type Size
0001-Support-removing-LEFT-JOINs-with-DISTINCT-GROUP-BY.patch application/octet-stream 7.5 KB


Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Langote 2017-11-01 00:35:50 Re: Adding column_constraint description in ALTER TABLE synopsis
Previous Message Tsunakawa, Takayuki 2017-11-01 00:07:44 Re: [bug fix] postgres.exe crashes with access violation on Windows while starting up