Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fmMTR-0000FR-8r for pgsql-general@arkaria.postgresql.org; Sun, 05 Aug 2018 17:05:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fmMTP-0007rj-PA for pgsql-general@arkaria.postgresql.org; Sun, 05 Aug 2018 17:05:43 +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.89) (envelope-from ) id 1fmMTO-0007rR-2s for pgsql-general@lists.postgresql.org; Sun, 05 Aug 2018 17:05:43 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fmMTH-0000Nh-Nn for pgsql-general@lists.postgresql.org; Sun, 05 Aug 2018 17:05:40 +0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id 500AD44D for ; Sun, 5 Aug 2018 13:05:32 -0400 (EDT) Received: from web4 ([10.202.2.214]) by compute7.internal (MEProxy); Sun, 05 Aug 2018 13:05:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=duodecim.org; h= content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= mesmtp; bh=QbB8ipsNWVOuRHh2l6TsapxsIxmn9lxTj3VjbNy7QCc=; b=CkLhL GUGgq3bin0NV5ehxX3g1e2lo9uquE2Fmpl+XSf2CcbCeyEuOryZha5r8LY+AtWLU Z4VJY3FaXR/AMSngE0+TGAlFz2ltZM9zMJQ81zOsqB0IXdFAFJIuQWz8Kg3UDm4r OA/3Urj2/ydqorKum82Q80dfmsHm6HXEzLfLRg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:message-id:mime-version:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=QbB8ipsNWVOuRHh2l6TsapxsIxmn9 lxTj3VjbNy7QCc=; b=QQE1HlEOtG9M7d5f14bZRrb7tpuX12swMk8rMdHKZnnl6 bGBFxJgxij/6GzCM9s2QLOiELAggfmCBltXOmTA7sLRNAxhgPQ2jDOtj/IkhcWXk w3eDxEEDq1DACB+jgtfCbuzlCMDu1cMpkIHFE7Akybbg55vpH0f2hWzkDJMKGXI+ /6B3Olkx2+/jvNQKwT6jX4NpB4wFYsfWoQC5/P2OJ5LQ7BcvwFCWdpRmDd/90zr8 /js9iYrAUpB6MikN8EjBwbreA8GzTCpJnr96HojQLDOmef6OKbtS/4FjrPXLJt+e bnTxhu/XfQd2Qk5jj9peZ8g8Fcmtr5qAe2AmfAY7w== X-ME-Proxy: X-ME-Sender: Received: by mailuser.nyi.internal (Postfix, from userid 99) id C3F6FBA50C; Sun, 5 Aug 2018 13:05:31 -0400 (EDT) Message-Id: <1533488731.48072.1464077088.225906DE@webmail.messagingengine.com> From: wouter-postgresql@publica.duodecim.org To: pgsql-general@lists.postgresql.org MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" X-Mailer: MessagingEngine.com Webmail Interface - ajax-2be8cd1b Subject: Conditional JOINs to optimise expensive views Date: Sun, 05 Aug 2018 20:05:31 +0300 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello all, I'm trying to write queries with conditional JOINs. The goal is to guard expensive views by checking some conditions first and not execute the view at all if any of the conditions fail. Think along the lines of REST, like Not Authorized or Not Modified. My problem is that the Postgresql (9.5 and 9.6) query planner executes LEFT and LATERAL JOINed queries even when the JOIN conditions are false. I don't understand why. I could work around some of this by adding a (dirty-ish?) CASE statement or a slower and more complex CTE construct or even create functions for each of these queries, but I'd really like to know why the most simple solution doesn't work. I tried many variations, but in my use cases, Postgresql always executes JOINs if the conditions are not constants. What I'm really looking for is a general pattern with good performance to guard expensive views. Here is an example query (is_owner is false): SELECT is_owner, is_newer, json FROM ( SELECT id, owner = '053bffbc-c41e-dad4-853b-ea91fc42ea17' "is_owner" , modified >= created "is_newer" FROM datasets WHERE id = '056e4eed-ee63-2add-e981-0c86b8b6a66f' ) cond LEFT JOIN view_dataset view ON view.id = cond.id AND cond.is_owner AND cond.is_newer; Which returns (correctly, but executing the slow JOINed view): is_owner: false is_newer: true json: (null) I don't want that query to execute the JOINed view, that is the whole point of those conditions, but it does. Here is a working sql fiddle: http://sqlfiddle.com/#!17/6882c6/5 And here is the stackoverflow thread: https://dba.stackexchange.com/questions/211642/optimising-expensive-join-subquery-by-filtering-with-conditionals (Note that in the fiddle example there is a 0.5 second delay in the view to simulate it being expensive, so it should be pretty clear if the query planner executes the view or not.) Thanks for any insight!