Re: Eliminating unnecessary left joins

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Jim Nasby <decibel(at)decibel(dot)org>
Cc: Ottó Havasvölgyi <havasvolgyi(dot)otto(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Eliminating unnecessary left joins
Date: 2007-04-12 15:09:21
Message-ID: 20070412074253.W22381@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, 11 Apr 2007, Jim Nasby wrote:

> I agree with others that the way that query is constructed is a bit
> odd, but it does bring another optimization to mind: when doing an
> inner-join between a parent and child table when RI is defined
> between them, if the query only refers to the child table you can
> drop the parent table from the join, because each row in the child
> table must have one and only one row in the parent.

I don't think that's quite true without qualifications. First, I think it
needs to be an immediate constraint (and I don't remember how we handle
set constraints inside functions that might be called from a statement, so
it might need to be not deferrable). Second, I think you also need to take
care of NULLs since child rows with NULLs in the key pass the constraint
but have no rows in the parent and would get culled by the inner join.

Also, there's a possible issue that constraints do not actually guarantee
that they always hold true, merely that they hold true at particular
times. I don't know if it's possible to get a statement executed such that
it would see the table state between the action and constraint check or
if such is allowed by spec.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2007-04-12 15:12:30 Re: [HACKERS] Fix mdsync never-ending loop problem
Previous Message Gregory Stark 2007-04-12 15:08:28 Re: Makefile patch to make gcov work on Postgres contrib modules