Re: How do I check for NULL - Mailing list pgsql-general

From Ron Johnson
Subject Re: How do I check for NULL
Date
Msg-id CANzqJaAx6RYPzEpgHhioHm0xCQ=OhZjbs7aNHJD-Dex3LyfcpA@mail.gmail.com
Whole thread Raw
In response to How do I check for NULL  (Igor Korot <ikorot01@gmail.com>)
Responses Re: How do I check for NULL
List pgsql-general
On Mon, Dec 8, 2025 at 9:40 PM Igor Korot <ikorot01@gmail.com> wrote:
Hi, ALL,
Consider the following scenario:

CREATE TABLE test(a INT, b VARCHAR(256), c INT, d VARCHAR(256), /*
more fields follows*/);
CREATE UNIQUE INDEX test_x( b, c, d );

Now I try to do:

INSERT INTO test VALUES( 0, 'abc', 12345, (SELECT foo FROM bar),
/*more data follow*/);

My problem is:

The SELECT can either return data or NULL.
Everything is good when the data is returned, but the insert fails
when the NULL is returned, because the field "d" is a part of UNIQUE
INDEX.

However,, I'd like to still insert the record and I'd like to do something like:

INSERT INTO test VALUES( 0, 'abc', 12345, IF( (SELECT foo FROM bar) ==
NULL, "postgres", <select_result>), /*more data follow*/);

What would be the best way to achieve this?

"Specifies whether for a unique index, null values should be considered distinct (not equal). The default is that they are distinct, so that a unique index could contain multiple null values in a column."

That seems to mean multiple rows can have NULL in column "d".

--
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!

pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: How do I check for NULL
Next
From: Ron Johnson
Date:
Subject: Re: How do I check for NULL