Re: PostgreSQL domains and NOT NULL constraint

From: Erki Eessaar <erki(dot)eessaar(at)taltech(dot)ee>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: PostgreSQL domains and NOT NULL constraint
Date: 2023-10-14 07:31:24
Message-ID: AM9PR01MB8268087307B06FF1AEDC6A1BFED1A@AM9PR01MB8268.eurprd01.prod.exchangelabs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>I doubt we'd consider doing anything about that.
>The whole business of domains with NOT NULL constraints
>is arguably a defect of the SQL standard, because
>there are multiple ways to produce a value that
>is NULL and yet must be considered to be of the domain type.

In my opinion it is inconsistent and illogical if a type sometimes contains a value and sometimes not.

CREATE DOMAIN d_int INTEGER NOT NULL;

All the following statements fail (and correctly so in my opinion).

SELECT (NULL)::d_int;
/*ERROR: domain d_int does not allow null values*/

SELECT Cast(NULL AS d_int);
/*ERROR: domain d_int does not allow null values*/

WITH val (v) AS (VALUES (1), (NULL))
SELECT Cast(v AS d_int) AS v
FROM Val;
/*ERROR: domain d_int does not allow null values*/

In my opinion the confusion and related problems arise from the widespread practice of sometimes treating a domain as a type (which it is not) and sometimes treating NULL as a value (which it is not).

Best regards
Erki Eessaar

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2023-10-14 11:53:18 Re: logical decoding and replication of sequences, take 2
Previous Message Erik Wienhold 2023-10-14 05:55:53 Re: [Doc] Glossary Term Definitions Edits