pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: pgsql-bugs@lists.postgresql.org
To: cdalxndr@yahoo.com
Subject: Re: BUG #15166: PL/PGSQL default rowtype variable value is null but also not null
Date: Sat, 21 Apr 2018 05:45:40 +0100
Message-ID: <87wox1chvh.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <152426142167.459.1224353227133000998@wrigleys.postgresql.org>
References: <152426142167.459.1224353227133000998@wrigleys.postgresql.org>

>>>>> "PG" == PG Bug reporting form <noreply@postgresql.org> writes:

 PG> Running the following script will report that 'var' is null but
 PG> also not null:

 PG> create table test_table();

This creates a table with no columns. Such an object isn't valid in the
SQL spec, but if you extend the spec's definition of how IS NULL and IS
NOT NULL are required to work to the zero-column case, it turns out that
a value of such a table's rowtype can be simultaneously NULL and NOT
NULL:

  - a composite value IS NULL either if it is the null value, or every
    one of its column values is the null value. By long-standing
    convention in logic, "every" is vacuously true when applied to the
    empty set, so a composite value of degree 0 always passes the IS
    NULL test.

  - a composite value IS NOT NULL if it is not the null value and none
    of its column values is the null value. "None of" is once more
    vacuously true when there are no columns, so a composite value of
    degree 0 passes IS NOT NULL if and only if it is not the null value.

The spec is quite explicit that IS NULL and IS NOT NULL are inverses of
each other only if the operand is of degree exactly 1 (i.e. is a scalar
or a row value with exactly 1 column).

 PG> By documentation, it should be null: "If the DEFAULT clause is not
 PG> given then the variable is initialized to the SQL null value."

That could be considered a slight bug in the documentation, because what
actually happens for rowtype variables is that they are initialized to a
row value with all null columns, NOT to the null value. In most cases
the difference is somewhat hard to spot.

Also, it looks like this behavior changes in pg11, probably not
intentionally. Relying on it either way seems dangerous (in fact relying
on anything at all to do with zero-column tables seems dangerous).

-- 
Andrew (irc:RhodiumToad)




view thread (4+ messages)  latest in thread

Message-ID: <87wox1chvh.fsf@news-spur.riddles.org.uk>
Permalink:  ../87wox1chvh.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87wox1chvh.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-bugs@postgresql.org
  Cc: andrew@tao11.riddles.org.uk, cdalxndr@yahoo.com
  Subject: Re: BUG #15166: PL/PGSQL default rowtype variable value is null but also not null
  In-Reply-To: <87wox1chvh.fsf@news-spur.riddles.org.uk>
  References: <152426142167.459.1224353227133000998@wrigleys.postgresql.org> <87wox1chvh.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