Re: Predicate migration on complex self joins

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Simon Riggs <simon(at)2ndquadrant(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Predicate migration on complex self joins
Date: 2009-07-13 18:33:41
Message-ID: 603c8f070907131133q327fff51p92e17600a80e48a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Jul 13, 2009 at 1:33 PM, Tom Lane<tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Simon Riggs <simon(at)2ndQuadrant(dot)com> writes:
>> In some cases, we have SQL being submitted that has superfluous
>> self-joins. An example would be
>
>> select count(*)
>> from foo1 a, foo1 b
>> where a.c1 = b.c1 /* PK join */
>> and a.c2 = 5
>> and b.c2 = 10;
>
>> You may well ask who would be stupid enough to write SQL like that. The
>> answer is of course that it is automatically generated by an ORM.
>
> Seems like the right answer is "fix the damn ORM".  It's hard to believe
> this sort of case comes up often enough to justify the cycles that would
> be expended (on *every* join query) to try to recognize it.

I think it's more common than you might think. It's been requested on
-performance within recent memory, and I've had cases where I needed
to deal with it as well. You can't write:

DELETE FROM table AS alias LEFT JOIN othertable ...

so you end up writing:

DELETE FROM table AS alias USING sametable LEFT JOIN othertable ...

or sometimes:

DELETE FROM table AS alias USING viewthatconstainstable ...

...Robert

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2009-07-13 18:57:38 [RFC] obtaining the function call stack
Previous Message Simon Riggs 2009-07-13 18:06:40 Re: Predicate migration on complex self joins