pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Олег Самойлов <splarv@ya.ru>
Cc: pgsql-general@postgresql.org
Subject: Re: lost "left join"
Date: Wed, 16 Jan 2019 12:24:37 +0000
Message-ID: <87h8e8ls2y.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <4BE8DD10-C364-4A45-AD93-5B1773130EA3@ya.ru>
References: <4BE8DD10-C364-4A45-AD93-5B1773130EA3@ya.ru>

>>>>> "Олег" == Олег Самойлов <splarv@ya.ru> writes:

 Олег> Hi, all.
 Олег> I got some mystic behaviour of PostgreSQL, perhaps this is a bug.

Feature, actually.

 Олег> But things begin be strange if I add validation by time.

 Олег> => explain select * from node as parent left join link on
 Олег> parent.node_id=link.parent left join node as child on
 Олег> link.child=child.node_id where parent.node_id=1 and current_date
 Олег> <@ parent.valid and current_date <@ link.valid and current_date
 Олег> <@ child.valid;

The problem here is that (for example) child.valid is null if there was
no matching child row in the join, and the planner knows that x <@ NULL
is not true (since the chosen <@ operator is defined as strict), and
therefore it knows that the left join is unnecessary and can be reduced
to an inner join.

At least 90% of the time when you refer to values from the nullable side
of a join in a WHERE clause, you're making a mistake (e.g. the condition
should have been in the ON clause instead). The other 10% or less of the
time, you have to make sure you use non-strict conditions, i.e. take
account of the fact that the values might be null.

-- 
Andrew (irc:RhodiumToad)




view thread (2+ messages)

Message-ID: <87h8e8ls2y.fsf@news-spur.riddles.org.uk>
Permalink:  ../87h8e8ls2y.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87h8e8ls2y.fsf@news-spur.riddles.org.uk

 · 

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: andrew@tao11.riddles.org.uk, splarv@ya.ru
  Subject: Re: lost "left join"
  In-Reply-To: <87h8e8ls2y.fsf@news-spur.riddles.org.uk>
  References: <4BE8DD10-C364-4A45-AD93-5B1773130EA3@ya.ru> <87h8e8ls2y.fsf@news-spur.riddles.org.uk>

* 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