Using NULLS NOT DISTINCT in PostgreSQL Unique Constraints
PostgreSQL's NULLS NOT DISTINCT makes unique constraints treat NULLs as equal, rejecting duplicate NULLs; default treats them as distinct. Portability varies.
On this page
Key idea
In PostgreSQL, a unique constraint by default treats two NULL values as distinct, so multiple rows with NULL in a constrained column are allowed. Adding NULLS NOT DISTINCT changes this so that NULLs are considered equal, and only one row with NULL is permitted. This clause is available from PostgreSQL 15. The SQL standard leaves the default null treatment implementation-defined, so other databases may behave differently. Use explicit NULLS DISTINCT or NULLS NOT DISTINCT to make intent clear. Primary keys are not affected because they require NOT NULL.
Default unique constraint behavior with NULLs
In PostgreSQL, a unique constraint ensures that no two rows have the same non-null values in the constrained columns. However, by default, two NULL values are not considered equal in this comparison. This means that even with a unique constraint, you can store multiple rows that contain NULL in at least one of the constrained columns. For example, a table with a unique constraint on a nullable column can have several rows where that column is NULL.
In PostgreSQL, the default behavior is that NULL values are not considered equal to each other for the purpose of a unique constraint. As a result, you can insert multiple rows that have NULL in the constrained column, even though the constraint would reject duplicate non-null values. This is the default unless you explicitly specify a different null treatment.
Enforcing uniqueness including NULLs with NULLS NOT DISTINCT
To treat NULLs as equal and enforce uniqueness across all rows, including those with NULL, add the NULLS NOT DISTINCT clause to the unique constraint. This clause is supported in PostgreSQL 15 and later. The syntax can be used as a column constraint or a table constraint. With this clause, only one row with NULL in the constrained column is allowed; any additional row with NULL violates the constraint and is rejected.
You can specify NULLS NOT DISTINCT either as a column constraint, for example UNIQUE NULLS NOT DISTINCT on a column, or as a table constraint, such as UNIQUE NULLS NOT DISTINCT (product_no). To make the default behavior explicit, you can use NULLS DISTINCT.
CREATE TABLE products ( product_no integer UNIQUE NULLS NOT DISTINCT, name text);INSERT INTO products VALUES (1, 'a'), (NULL, 'b'), (NULL, 'c');-- The second NULL insert fails because NULLS NOT DISTINCT treats NULLs as equal.Portability and SQL standard implications
The SQL standard leaves the default null treatment in unique constraints implementation-defined, so different database systems may behave differently. PostgreSQL's default is to treat NULLs as distinct, but some other systems treat them as equal or offer different options. For example, some databases allow only one NULL in a unique column by default.
To make your intent explicit and avoid surprises when moving between systems, use NULLS DISTINCT or NULLS NOT DISTINCT explicitly. However, note that NULLS NOT DISTINCT is a PostgreSQL extension; other databases may not support this syntax, so you may need to use alternative approaches such as partial indexes or triggers for portability.
Choosing between NULLS DISTINCT and NULLS NOT DISTINCT
The choice depends on what NULL represents in your data. If NULL means 'missing' or 'unknown' and you want to allow multiple rows with missing values, keep the default NULLS DISTINCT behavior. If NULL represents a meaningful value that should be unique, such as a placeholder for 'no product number', use NULLS NOT DISTINCT to enforce that only one such row exists.
Primary keys are not affected because they require NOT NULL. A primary key constraint requires that the values be both unique and not null, so it always enforces uniqueness without considering NULLs.
Consider the application logic: changing to NULLS NOT DISTINCT may cause inserts that previously succeeded to fail, so review existing data and queries before applying it.
Applicability
- Is your PostgreSQL version 15 or later? NULLS NOT DISTINCT is not available in earlier versions.
- Does your data model treat NULL as a meaningful value that should be unique, or as missing data that can repeat?
- Are you planning to migrate to another database system? If so, check how that system handles NULLs in unique constraints.
- Do you have existing rows with duplicate NULLs? Adding NULLS NOT DISTINCT will fail if duplicates exist.
Where this applies
NULLS NOT DISTINCT is a PostgreSQL extension; other databases may not support it or may have different default behavior. The clause applies only to unique constraints, not to primary keys, which already enforce NOT NULL. Using NULLS NOT DISTINCT can change application logic if NULL is used to represent missing data rather than a distinct value.