A form rejects duplicates, a column has a uniqueness constraint and two unexpected rows still appear. Sometimes the problem is an assumption: PostgreSQL treats NULL values as distinct by default in a UNIQUE constraint.
PostgreSQL 18 documentation also describes NULLS NOT DISTINCT, which changes that behaviour. This practical article was checked on October 29, 2026; it does not present the syntax as a November release.
Define what an absent value means
Missing data may mean unknown information, not applicable or a global object. Those meanings do not necessarily deserve the same uniqueness rule.
Consider an account preference with an optional locale. Allowing several rows without a locale and allowing a single default preference are different contracts.
Write that contract in business terms before choosing the index. A technical control cannot resolve an ambiguous product definition.
Discuss it with whoever consumes the data too. A report may already rely on several missing values remaining possible, even if the main form never intended them.
Observe the difference in an isolated example
The following SQL belongs in a test database. The table is temporary; this is not a production migration.
CREATE TEMP TABLE demo_preferences (
account_id integer NOT NULL,
locale text,
UNIQUE NULLS NOT DISTINCT (account_id, locale)
);
INSERT INTO demo_preferences VALUES (1, NULL);
-- A second identical insertion should fail.
INSERT INTO demo_preferences VALUES (1, NULL);
This rule represents one slot for the pair even when locale is absent. Another locale remains another combination. Adapt the example to what your data actually means.
Application checks do not replace the constraint
Checking that a row does not exist before inserting leaves a gap between operations. Concurrent requests can receive the same initial answer.
The database must carry an integrity rule that needs to survive concurrent writes. The application should then handle rejection appropriately rather than turning it into an unexplained error.
Test updates as well: a row may become conflicting when an existing value changes to NULL. The contract applies beyond creation through the primary form.
Prepare a migration without deleting evidence
Before adding a constraint to existing data, identify groups that would violate the new rule. Have the business owner approve duplicate treatment.
Do not arbitrarily delete every row except the newest. A timestamp does not necessarily identify the record containing the correct information.
Assess locking and duration against representative data, and prepare an appropriate recovery plan. A data-model change needs more than pasting illustrative SQL into a console.
Finally, retain tests for ordinary values, absence, updates and concurrency. The objective is not banning NULL everywhere. It is aligning storage behaviour with the decision the product genuinely needs to guarantee.
Keep that explanation beside the migration so a later maintainer understands why the unusual-looking constraint exists rather than simplifying it away.
Cover visual: generated editorial illustration, not an official screenshot.




Join the discussion
Comments
Loading comments…