pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Nicklas Avén <nicklas.aven@jordogskog.no>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Problems pushing down WHERE-clause to underlying view
Date: Sat, 16 Feb 2019 00:02:50 -0500
Message-ID: <32758.1550293370@sss.pgh.pa.us> (raw)
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>

=?UTF-8?Q?Nicklas_Av=c3=a9n?= <nicklas.aven@jordogskog.no> 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 = '887655635442600'

and there is also a join condition

	l.machine_key=ci.machine_key

From these two things the planner can deduce

	l.machine_key='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

<query 1 as written, up to the WHERE>
where ci.machine_key = (select machine_key from contractor_access
                        where t4e_contractor_id = 'nicklas.aven@jordogskog.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 =
'nicklas.aven@jordogskog.no', but you can probably adapt the idea
to make it work.

			regards, tom lane




view thread (8+ messages)  latest in thread

Message-ID: <32758.1550293370@sss.pgh.pa.us>
Permalink:  ../32758.1550293370@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/32758.1550293370@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-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, nicklas.aven@jordogskog.no
  Subject: Re: Problems pushing down WHERE-clause to underlying view
  In-Reply-To: <32758.1550293370@sss.pgh.pa.us>
  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> <32758.1550293370@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