Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZUjUA-0007sH-1Y for pgsql-bugs@arkaria.postgresql.org; Wed, 26 Aug 2015 22:48:02 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZUjU9-0006DE-J9 for pgsql-bugs@arkaria.postgresql.org; Wed, 26 Aug 2015 22:48:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZUjTX-0005WL-Tc for pgsql-bugs@postgresql.org; Wed, 26 Aug 2015 22:47:23 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZUjTQ-0003An-6T for pgsql-bugs@postgresql.org; Wed, 26 Aug 2015 22:47:23 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t7QMlDju020002; Wed, 26 Aug 2015 18:47:13 -0400 From: Tom Lane To: Kristoffer Gustafsson cc: "David G. Johnston" , "pgsql-bugs@postgresql.org" Subject: Re: BUG #13592: Optimizer throws out join constraint causing incorrect result In-reply-to: References: <20150826195031.2091.40681@wrigleys.postgresql.org>, Comments: In-reply-to Kristoffer Gustafsson message dated "Wed, 26 Aug 2015 21:57:10 -0000" Date: Wed, 26 Aug 2015 18:47:13 -0400 Message-ID: <20001.1440629233@sss.pgh.pa.us> X-Pg-Spam-Score: -3.3 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Kristoffer Gustafsson 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