Re: Allowing join removals for more join types

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Allowing join removals for more join types
Date: 2014-05-18 11:08:23
Message-ID: CAApHDvog9sxF+zFM_yLuG6cWvu132JeabjhdJyVv7Upb8fDriw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, May 17, 2014 at 8:57 PM, David Rowley <dgrowleyml(at)gmail(dot)com> wrote:

> I'm currently in the early stages of looking into expanding join removals.
>
> Currently left outer joins can be removed if none of the columns of the
> table are required for anything and the table being joined is a base table
> that contains a unique index on all columns in the join clause.
>
> The case I would like to work on is to allow sub queries where the query
> is grouped by or distinct on all of the join columns.
>
> Take the following as an example:
>
> CREATE TABLE products (productid integer NOT NULL, code character
> varying(32) NOT NULL);
> CREATE TABLE sales (saleid integer NOT NULL, productid integer NOT NULL,
> qty integer NOT NULL);
>
> CREATE VIEW product_sales AS
> SELECT p.productid,
> p.code,
> s.qty
> FROM (products p
> LEFT JOIN ( SELECT sales.productid,
> sum(sales.qty) AS qty
> FROM sales
> GROUP BY sales.productid) s ON ((p.productid = s.productid)));
>
> If a user does:
> SELECT productid,code FROM product_sales;
> Then, if I'm correct, the join on sales can be removed.
>
>
Attached is a patch which implements this. It's still a bit rough around
the edges and some names could likely do with being improved, but it at
least seems to work with all of the test cases that I've thrown at it so
far.

Comments are welcome, but the main purpose of the email is so I can
register the patch for the June commitfest.

Regards

David Rowley

Attachment Content-Type Size
subquery_leftjoin_removal_v0.5.patch application/octet-stream 14.2 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Munro 2014-05-18 13:16:30 Re: SKIP LOCKED DATA (work in progress)
Previous Message Andres Freund 2014-05-18 09:14:45 Re: 9.4 beta1 crash on Debian sid/i386