pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Kristoffer Gustafsson <kristoffer.gustafsson@yves-rocher.se>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #13592: Optimizer throws out join constraint causing incorrect result
Date: Wed, 26 Aug 2015 18:47:13 -0400
Message-ID: <20001.1440629233@sss.pgh.pa.us> (raw)
In-Reply-To: <DB5PR03MB112634A7F5EA93E31DD75178C5600@DB5PR03MB1126.eurprd03.prod.outlook.com>
References: <20150826195031.2091.40681@wrigleys.postgresql.org>
	<CAKFQuwZMJg_PDyOdxYovTTVDV9zpuC1y57iWrJoTa6=i4i-g2A@mail.gmail.com>
	<DB5PR03MB112634A7F5EA93E31DD75178C5600@DB5PR03MB1126.eurprd03.prod.outlook.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Kristoffer Gustafsson <kristoffer.gustafsson@yves-rocher.se> writes:
> The FROM has set A which is joined with set B which links set C.
> There is a path with keys going from A to B to C.
> There is no link from A to C.

Actually, there is: the WHERE clause involving A and C is itself a join
clause, since it allows filtering out some pairs of A and C rows, albeit
only after performing a nestloop join.  It seems somewhat unlikely that
the optimizer would choose that approach in preference to equijoins, but
since you've not shown us any concrete details, it can't be ruled out.
For example, if both A and C are small and both could usefully be used in
an indexscan on a large B table, joining A to C first would make perfect
sense.

> Of course you can write the WHERE criteria to not contain expressions/calculations and duplicate the constraint expressed by the FROM section, but doesn't that defeat the purpose of declaring the joins in FROM?

There is *no* semantic difference between writing a join clause in WHERE
and writing it in an (inner) JOIN/ON clause.  There is certainly no
promise about the execution order.  See
http://www.postgresql.org/docs/9.4/static/sql-expressions.html#SYNTAX-EXPRESS-EVAL

Personally I'd dodge the whole problem by reformulating the WHERE to avoid
division, that is

    (
        ((a.A_VAL01_DP + a.A_VAL02_DP) > (:THRESHOLD_01 * c.C_VAL01_DP))
    or
        ((a.A_VAL03_DP - a.A_VAL02_DP) > (:THRESHOLD_02 * c.C_VAL01_DP))
    )

If you can't fix it in that sort of way, the traditional approach to
forcing the join order in Postgres is to put the desired innermost join
in a sub-SELECT with OFFSET 0, which works as an optimization fence.
(The optimizer can't push joins or WHERE clauses past a LIMIT/OFFSET
for fear of changing the set of rows returned.)  But JOIN/ON is most
certainly not an optimization fence.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (5+ messages)  latest in thread

Message-ID: <20001.1440629233@sss.pgh.pa.us>
Permalink:  ../20001.1440629233@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/20001.1440629233@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, kristoffer.gustafsson@yves-rocher.se, david.g.johnston@gmail.com
  Subject: Re: BUG #13592: Optimizer throws out join constraint causing incorrect result
  In-Reply-To: <20001.1440629233@sss.pgh.pa.us>
  References: <20150826195031.2091.40681@wrigleys.postgresql.org> <CAKFQuwZMJg_PDyOdxYovTTVDV9zpuC1y57iWrJoTa6=i4i-g2A@mail.gmail.com> <DB5PR03MB112634A7F5EA93E31DD75178C5600@DB5PR03MB1126.eurprd03.prod.outlook.com> <20001.1440629233@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox