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 1gjkFR-0005jw-SH for pgsql-general@arkaria.postgresql.org; Wed, 16 Jan 2019 12:24: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 1gjkFQ-0000fB-DA for pgsql-general@arkaria.postgresql.org; Wed, 16 Jan 2019 12:24:44 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjkFQ-0000eY-4d for pgsql-general@lists.postgresql.org; Wed, 16 Jan 2019 12:24:44 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjkFN-0002Og-Pc for pgsql-general@postgresql.org; Wed, 16 Jan 2019 12:24:43 +0000 Received: from [192.168.127.1] (port=34001 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gjkFK-0006Is-Qe; Wed, 16 Jan 2019 12:24:38 +0000 Received: from [127.0.0.1] (port=17020 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gjkFJ-0009f7-Vg; Wed, 16 Jan 2019 12:24:38 +0000 From: Andrew Gierth To: =?utf-8?B?0J7Qu9C10LMg0KHQsNC80L7QudC70L7Qsg==?= Cc: pgsql-general@postgresql.org Subject: Re: lost "left join" In-Reply-To: <4BE8DD10-C364-4A45-AD93-5B1773130EA3@ya.ru> (=?utf-8?B?ItCe?= =?utf-8?B?0LvQtdCzINCh0LDQvNC+0LnQu9C+0LIiJ3M=?= message of "Wed, 16 Jan 2019 13:54:21 +0300") Message-ID: <87h8e8ls2y.fsf@news-spur.riddles.org.uk> References: <4BE8DD10-C364-4A45-AD93-5B1773130EA3@ya.ru> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Wed, 16 Jan 2019 12:24:37 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "=D0=9E=D0=BB=D0=B5=D0=B3" =3D=3D =D0=9E=D0=BB=D0=B5=D0=B3 =D0=A1=D0= =B0=D0=BC=D0=BE=D0=B9=D0=BB=D0=BE=D0=B2 writes: =D0=9E=D0=BB=D0=B5=D0=B3> Hi, all. =D0=9E=D0=BB=D0=B5=D0=B3> I got some mystic behaviour of PostgreSQL, perha= ps this is a bug. Feature, actually. =D0=9E=D0=BB=D0=B5=D0=B3> But things begin be strange if I add validation = by time. =D0=9E=D0=BB=D0=B5=D0=B3> =3D> explain select * from node as parent left j= oin link on =D0=9E=D0=BB=D0=B5=D0=B3> parent.node_id=3Dlink.parent left join node as c= hild on =D0=9E=D0=BB=D0=B5=D0=B3> link.child=3Dchild.node_id where parent.node_id= =3D1 and current_date =D0=9E=D0=BB=D0=B5=D0=B3> <@ parent.valid and current_date <@ link.valid a= nd current_date =D0=9E=D0=BB=D0=B5=D0=B3> <@ 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. --=20 Andrew (irc:RhodiumToad)