Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gus8Z-0001n6-DK for pgsql-general@arkaria.postgresql.org; Sat, 16 Feb 2019 05:03:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gus8Y-00055A-1s for pgsql-general@arkaria.postgresql.org; Sat, 16 Feb 2019 05:03:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gus8X-000553-Ll for pgsql-general@lists.postgresql.org; Sat, 16 Feb 2019 05:03:37 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gus8V-0004VS-5E for pgsql-general@lists.postgresql.org; Sat, 16 Feb 2019 05:03:36 +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 x1G52ocC032759; Sat, 16 Feb 2019 00:02:50 -0500 From: Tom Lane To: =?UTF-8?Q?Nicklas_Av=c3=a9n?= cc: pgsql-general@lists.postgresql.org Subject: Re: Problems pushing down WHERE-clause to underlying view In-reply-to: <232283a6-4422-1efd-ec80-3dda8b2f5496@jordogskog.no> References: <4d8a05b6-39de-9a06-7e86-85754fd94716@jordogskog.no> <9cd9e5c1-1909-6f36-1473-f3d2a773762e@aklaver.com> <9770a1ec-c7be-7527-66d7-0c95b8277508@jordogskog.no> <473540ab-5933-04e8-ce55-074f4faaab82@aklaver.com> <232283a6-4422-1efd-ec80-3dda8b2f5496@jordogskog.no> Comments: In-reply-to =?UTF-8?Q?Nicklas_Av=c3=a9n?= message dated "Fri, 15 Feb 2019 21:43:21 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <32757.1550293370.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 16 Feb 2019 00:02:50 -0500 Message-ID: <32758.1550293370@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =3D?UTF-8?Q?Nicklas_Av=3Dc3=3Da9n?=3D writes: > I also, in the first query, changed the where clause to filter on = > machine_key in table contractor _access. Just to illustrate the problem = > better. > Both queries filter on the same table which is joined the same way. But = > in the second example the where clause is not pushed to the subquery The filters are totally different though. In one case you provide where ci.machine_key =3D '887655635442600' and there is also a join condition l.machine_key=3Dci.machine_key =46rom these two things the planner can deduce l.machine_key=3D'887655635442600' which is a restriction condition that it knows how to push down into the "l" subquery. Furthermore, it can also deduce that it can restrict all of the left-joined tables to consider only that value of their join keys. In query #2 you have no constant value for machine_key so none of that happens. IIRC, the propagated value doesn't have to be a constant, exactly, just a fixed expression. So you might consider something like where ci.machine_key =3D (select machine_key from contractor_access where t4e_contractor_id =3D 'nicklas.aven@jordogsk= og.no'); when you need to drive the lookup from something other than raw machine_key. This'll fail, as-is, if there's more than one contractor_access row with t4e_contractor_id =3D 'nicklas.aven@jordogskog.no', but you can probably adapt the idea to make it work. regards, tom lane