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 1epn5k-0002rH-Iq for pgsql-bugs@arkaria.postgresql.org; Sun, 25 Feb 2018 03:35:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1epn5j-0003aZ-3b for pgsql-bugs@arkaria.postgresql.org; Sun, 25 Feb 2018 03:35:11 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1epn5i-0003aI-R6 for pgsql-bugs@lists.postgresql.org; Sun, 25 Feb 2018 03:35:10 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1epn5g-0006TY-75 for pgsql-bugs@lists.postgresql.org; Sun, 25 Feb 2018 03:35:09 +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 w1P3Z6CV013060; Sat, 24 Feb 2018 22:35:06 -0500 From: Tom Lane To: "David G. Johnston" cc: "deinspanjer@gmail.com" , "pgsql-bugs@lists.postgresql.org" Subject: Re: BUG #15085: Domain "not null" constraint doesn't detect a null returned from a resultset In-reply-to: References: <151952089752.1463.4091997216675283064@wrigleys.postgresql.org> Comments: In-reply-to "David G. Johnston" message dated "Sat, 24 Feb 2018 19:11:40 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <13058.1519529706.1@sss.pgh.pa.us> Date: Sat, 24 Feb 2018 22:35:06 -0500 Message-ID: <13059.1519529706@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "David G. Johnston" writes: > On Saturday, February 24, 2018, PG Bug reporting form < > noreply@postgresql.org> wrote: >> After some testing, I came across this >> unusual behavior that I believe might be a bug. > This appears to fall within the exceptions for not null noted in the > documentation for create domain. > https://www.postgresql.org/docs/9.6/static/sql-createdomain.html Yeah, the core issue here is that at no point does the query attempt to convert a value of some other type to the domain type. Rather, the appearance of the null result falls out of the semantics of a scalar subquery that returns no rows. Like the case for a "not null" domain column on the right side of a left join, this is a basic inconsistency in the SQL language, and it's not clear that anyone would thank us for throwing errors. (But they *would* complain if we slowed down these fundamental operations to check for such cases :-(.) I've occasionally wondered if we should force domain types that appear in these contexts to decay to their base type, so as to avoid the situation of returning a value that claims to be of a domain type but is actually null. (That is, the reported output column type in an outer join or whatever would be the base type not the domain type.) I'm doubtful that any but pedants would find that to be an improvement, though; and I've not come across anything in the SQL standard that suggests an implementation should do that. regards, tom lane