Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vSnpl-00612n-1P for pgsql-general@arkaria.postgresql.org; Tue, 09 Dec 2025 02:52:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vSnpj-002Qtv-2e for pgsql-general@arkaria.postgresql.org; Tue, 09 Dec 2025 02:52:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vSnpj-002Qtm-1a for pgsql-general@lists.postgresql.org; Tue, 09 Dec 2025 02:52:11 +0000 Received: from mail-oa1-x2d.google.com ([2001:4860:4864:20::2d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vSnph-003xg0-16 for pgsql-general@postgresql.org; Tue, 09 Dec 2025 02:52:11 +0000 Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-3e7e57450ceso1947051fac.2 for ; Mon, 08 Dec 2025 18:52:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765248727; x=1765853527; darn=postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=2QvNRtAlYtMeGQ3teFTDnAnshcCTtIhnH61R28G6ImM=; b=ZobHaH2NJ7fgJ9Wa/br842IWvm7y8QQsgvEA0qjfq9TxwRVkadbdS7OTZN23rFoD32 B1jQcdC9hJEZ1skebDLpao9bbHLbxJynyqPiaqVsECwrlXAKtAhKyVE5Kmmm/vI13Y3A Ff30CiuVDkYK96EKn4kDu6kQC133WrILA41XLyQraUsDBgKTUARERpWlvshWdkzqvtC6 1U3PUffWrUkBkm+78EdhJ8qP85JQ7vRAttAgLAmvUvQMAk4TN1p0mMQT22kwWiLZcByp 1NrCqgxbRQ1ZOsKo3QMhAQvqbUvFK2ppgAHpaCNsHxgNXqPNIP79lIpyYhUn+acPieQD +HuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765248727; x=1765853527; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2QvNRtAlYtMeGQ3teFTDnAnshcCTtIhnH61R28G6ImM=; b=FoTN5bZ/u1MFUsdo7hS50pk8dMsciWvtIZ8x4nW4bVDHteKtwl/XslNr26SWG/ldbo 4F8Enuf1hUHM5iIxWnXCHRZSLk4rm7v0EXDU2JTUFMPHLEJiRVQ27YHmqlMLcN9RhBNY 221/P5fQ5n9zS1VQmByI1kcFL6KCPjT3RdHd10Cd8I3ZIz2xvBXqA5nps51aPpsfYl6h FAOdsG8AmPK1ZBX8p6holRrHaK0VQWWADPLVLa7OeZjLlKjg0ww0sIHjyI5cAojMPqBF XVfSo1HwlJbvM/CQxgsM4ZcBO+JH2QMK8Iea3iUVjv47TTJmaQl/n2rB5dCU1mNBfxsJ 59ww== X-Gm-Message-State: AOJu0YxuAl7Yl5Mj+Du5OPRacNCc48va23243qS21EtVC6fNPNkx6LkT uFF7kJRLubRkDP5bKxG18Ppo3Wi/R28vhXfpB+QE70d1tV3AGdZ2txKiDYV4G5wsAu/rOYGfkIO F/LRcUYQ34dAs0613Bzkxn+arXGdhlV3Gj0fM X-Gm-Gg: ASbGncvhWtscdZfKbLRqRYdngBsmMBPpebp7k/ZT0gBnmgA3Y5vhuHjo7zngGiCRNe/ H5BfSjh+fepNt7DpW0skIeZmNroao16E8DDR6Qnout3CO1IjMRTV1ZXxb1o/Aba+LXFvMKNYidM wUWLME13Q4JCgnvpZJ5I2xBaMD+ShPFihyryaMo9ngmmX/WiTIJ312zuNWc68TUB6CMezQ+EFaT cQ1Sc0TN0StD3NRQR/o6vjmO45jtU4Kes9mH2v0QDvYnnLea/MKB3TFsz/VXW9UBIgQ6qNP X-Google-Smtp-Source: AGHT+IGMTI8xc2lIYpsXRbGVEogklOp2rmNN4/IGq82R6SMQpwnN52EVOK6sPyY4ktyj6ibUjaKGnGKhYN/YZwKHEHI= X-Received: by 2002:a05:6871:64cb:b0:3f5:4172:21f with SMTP id 586e51a60fabf-3f54415a124mr4092374fac.58.1765248726824; Mon, 08 Dec 2025 18:52:06 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Ron Johnson Date: Mon, 8 Dec 2025 21:51:55 -0500 X-Gm-Features: AQt7F2oYRZ2AZyca8Lkt7pGYv6v7XRB-dEHmTt2ZzA5aBkW27lGFg1n66UV33SY Message-ID: Subject: Re: How do I check for NULL To: pgsql-general Content-Type: multipart/alternative; boundary="0000000000004d78e706457bff5c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000004d78e706457bff5c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Dec 8, 2025 at 9:40=E2=80=AFPM Igor Korot wrot= e: > 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 somethin= g > like: > > INSERT INTO test VALUES( 0, 'abc', 12345, IF( (SELECT foo FROM bar) =3D= =3D > NULL, "postgres", ), /*more data follow*/); > > What would be the best way to achieve this? > https://www.postgresql.org/docs/15/sql-createindex.html section on NULLS DISTINCT says "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". --=20 Death to , and butter sauce. Don't boil me, I'm still alive. lobster! --0000000000004d78e706457bff5c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Mon, Dec 8, 2025 at 9:40=E2=80=AFPM Ig= or Korot <ikorot01@gmail.com&g= t; 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 UNI= QUE
INDEX.

However,, I'd like to still insert the record and I'd like to do so= mething like:

INSERT INTO test VALUES( 0, 'abc', 12345, IF( (SELECT foo FROM bar)= =3D=3D
NULL, "postgres", <select_result>), /*more data follow*/);<= br>
What would be the best way to achieve this?

=
ht= tps://www.postgresql.org/docs/15/sql-createindex.html section on=C2=A0<= span style=3D"background-color:rgb(248,249,250);color:rgb(0,0,0);font-famil= y:monospace,monospace;font-size:14.4px">NULLS DISTINCT=C2=A0says=C2= =A0
"Specifies whether for a unique inde= x, 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 row= s can have NULL in column "d".

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