Re: Proposal: Add a UNIQUE NOT ENFORCED constraint - Mailing list pgsql-hackers

From Jacob Jackson
Subject Re: Proposal: Add a UNIQUE NOT ENFORCED constraint
Date
Msg-id CAAiQw3zzFy+Rf-wjkeHpDbxNAmTOuNOU18wVZ6uYjUwv6p+tJA@mail.gmail.com
Whole thread Raw
In response to Re: Proposal: Add a UNIQUE NOT ENFORCED constraint  (Neil Chen <carpenter.nail.cz@gmail.com>)
Responses Re: Proposal: Add a UNIQUE NOT ENFORCED constraint
List pgsql-hackers
On Wed, Jan 7, 2026 at 5:22 AM Neil Chen <carpenter.nail.cz@gmail.com> wrote:
> If we want the query planner to generate an execution plan as if a column were unique, would setting n_distinct = -1
inthe table statistics achieve the same effect? 

Setting n_distinct is less clear in describing the data (it isn't tied
to the table schema itself and can be ambiguous in whether values are
actually totally unique or just close enough, which can complicate
things), and, because it only impacts statistics estimations, it still
doesn't help in queries where uniqueness is required. Postgres will
still add a node to ensure uniqueness in that case. There are also
some other limitations with n_distinct (e.g. extended stats are
required for multi-column pairs, which further complicates
documentation and adds complexity/overhead).



pgsql-hackers by date:

Previous
From: Chao Li
Date:
Subject: Re: DOC: fixes multiple errors in alter table doc
Next
From: Japin Li
Date:
Subject: Re: [PATCH] Add pg_get_role_ddl() functions for role recreation