Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iNG5W-0007rh-Sx for pgsql-docs@arkaria.postgresql.org; Wed, 23 Oct 2019 12:50:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iNG5V-0004Wu-Lb for pgsql-docs@arkaria.postgresql.org; Wed, 23 Oct 2019 12:50:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iNEv7-0006l7-NY for pgsql-docs@lists.postgresql.org; Wed, 23 Oct 2019 11:35:17 +0000 Received: from mail-lf1-x143.google.com ([2a00:1450:4864:20::143]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iNEv4-0002MJ-Qd for pgsql-docs@lists.postgresql.org; Wed, 23 Oct 2019 11:35:17 +0000 Received: by mail-lf1-x143.google.com with SMTP id g21so14676099lfh.4 for ; Wed, 23 Oct 2019 04:35:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=PR2HzeN1YhqMsSCGwrFlARyURI5Ku65NA1HoRMmVg8Y=; b=Mt0PuaA+ggGcHn/LUv+l+8E6eV7O7+kctYSyl6q5hXb66BtAa6MXNkZnDIEp1O4BXm ff0s0Hq6SqwbKvXkeh8VLgeE0oPfKwAiEk97LsnrvyC5QKslZu9qm7HzvIr/+tkQKXu9 +UlAkDIoHoHZBpUIcQtt0Wz4QcLs+Ow31BHfxSi9bMDQ8VuEmDzSqWr34W2pI4zvPWKK KOKpxiTe6csBkQHQUw4icBYA9WFUMEZbFlX5c9P8YiFIPRw314gwCkgX7qDV6Lsl/J8g B58QPPtM8zsBhT+rglnJYOL9x+Dxr1cFuqYGGko9E2VWaNe62ncUYsbGWsLOTX+C/Uhz Vx5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=PR2HzeN1YhqMsSCGwrFlARyURI5Ku65NA1HoRMmVg8Y=; b=EhvXkOhZ7sMjQtBnndzwtMxjddEZII7NLHgRBZbZJujOnphxScfcRZuZwRE7rQxsct CMpwu0fOepOp+KsL2CP/4xOPKOw2yCbAyBRwmbZxQUpBKv/LuV9RDgqKuc4IfBSqlxwx Uk9fnoxGoQqUJWnJneILaNVcP+qJYl89OkaC3LuKdDihUjuJ3xHQaQ950E/w6EhakqnA BP4oqdeu8yssHsvPQME3tM34x6fZHtMmqCFTNhTtwhlb72cO/NtrDFyMAIUnlGuwUIxc Vi4Ud1kW0Ado96kn+XeNsFh+asVuNMSx5kOh/Vo3y5H87MIR1jMj3U5qTaHuLOX4F2Ir Vx4Q== X-Gm-Message-State: APjAAAUOOJLIgoN+vfAVNu5wZT7MjcmiK0bUE96FTgjn0+FH5gyEDYTk 5Ebx8lcRce3jRXwdDAZFBRkb08IqZDKc1XIvEqj4ti99 X-Google-Smtp-Source: APXvYqyfT2XTiGvI4ehMCAqqmqWTUXBIuNoO9C1CymUaDJu05jFSD2JTzPzCAyQDEM5s93Q3vkV7crBo7oe5zhv65Do= X-Received: by 2002:a19:6759:: with SMTP id e25mr21265962lfj.80.1571830513365; Wed, 23 Oct 2019 04:35:13 -0700 (PDT) MIME-Version: 1.0 References: <156760275564.1127.12321702656456074572@wrigleys.postgresql.org> <20190927163747.GE31412@momjian.us> In-Reply-To: <20190927163747.GE31412@momjian.us> From: Tuomas Leikola Date: Wed, 23 Oct 2019 14:35:02 +0300 Message-ID: Subject: Re: uniqueness and null could benefit from a hint for dba To: Bruce Momjian Cc: pgsql-docs@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000006578fa0595924fae" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --0000000000006578fa0595924fae Content-Type: text/plain; charset="UTF-8" That is a nice design. You can create a regular unique index where columns(s)s are not null and then filtered index(es) for the cases where some of the unique columns are null. However my point, specifically, was that if the document in question would have offered alternative solutions, I personally would have been saved from some frustration and an exercise in bad index design (I had 5 nullables that need uniqueness for the null as well). Maybe it would help someone else. On Fri, Sep 27, 2019 at 7:37 PM Bruce Momjian wrote: > On Wed, Sep 4, 2019 at 01:12:35PM +0000, PG Doc comments form wrote: > > The following documentation comment has been logged on the website: > > > > Page: https://www.postgresql.org/docs/11/indexes-unique.html > > Description: > > > > Sometimes it is convenient to create an unique index that considers NULL > > values equal. Designating a "zero" value for those rows might not be > > feasible, for example due to a foreign key. > > > > The documentation currently only states that unique indexes do not > consider > > NULLs equal. It might be good to offer workarounds, like indexing a > coalesce > > function, if scans are not the reason for the index, but the uniqueness > > constraint. > > I did write a blog entry about this: > > https://momjian.us/main/blogs/pgblog/2017.html#April_3_2017 > > Does that help? > > -- > Bruce Momjian http://momjian.us > EnterpriseDB http://enterprisedb.com > > + As you are, so once was I. As I am, so you will be. + > + Ancient Roman grave inscription + > -- - Tuomas --0000000000006578fa0595924fae Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
That is a nice design. You can create a regular unique ind= ex where columns(s)s are not null and then filtered index(es) for the cases= where some of the unique columns are null.

However my p= oint, specifically, was that if the document in question would have offered= alternative solutions, I personally would have been saved from some frustr= ation=C2=A0and an exercise in bad index design (I had 5 nullables that need= uniqueness for the null as well). Maybe it would help someone else.
<= /div>
O= n Fri, Sep 27, 2019 at 7:37 PM Bruce Momjian <bruce@momjian.us> wrote:
On Wed, Sep=C2=A0 4, 2019 at 01:12:35PM +0000, PG= Doc comments form wrote:
> The following documentation comment has been logged on the website: >
> Page: https://www.postgresql.org/docs/11/= indexes-unique.html
> Description:
>
> Sometimes it is convenient to create an unique index that considers NU= LL
> values equal. Designating a "zero" value for those rows migh= t not be
> feasible, for example due to a foreign key.
>
> The documentation currently only states that unique indexes do not con= sider
> NULLs equal. It might be good to offer workarounds, like indexing a co= alesce
> function, if scans are not the reason for the index, but the uniquenes= s
> constraint.

I did write a blog entry about this:

=C2=A0 =C2=A0 =C2=A0 =C2=A0 https://momji= an.us/main/blogs/pgblog/2017.html#April_3_2017

Does that help?

--
=C2=A0 Bruce Momjian=C2=A0 <bruce@momjian.us>=C2=A0 =C2=A0 =C2=A0 =C2=A0 http://momjian.us
=C2=A0 EnterpriseDB=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0
http://enterprisedb.com
+ As you are, so once was I.=C2=A0 As I am, so you will be. +
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 Ancient Roman grave inscription +


--
- Tuomas
--0000000000006578fa0595924fae--