Sub topic 4
Enforcing Uniqueness So the Match Is Reliable
This chapter assumes you know basic SQL (INSERT, UPDATE, SELECT), primary keys, and table constraints. It also assumes you have read the earlier chapters on external IDs and on keeping your own primary key next to them. The examples use PostgreSQL.
Why the match needs a constraint
An upsert asks the database one question: does a row for this record already exist? The database can answer reliably only if the match key is unique. If two rows can share the same external ID, "update the existing row" has no single target. A load would then create duplicates, or update the wrong row.
PostgreSQL enforces this. INSERT ... ON CONFLICT needs a unique constraint or unique index to detect the conflict. Without one, the statement fails with there is no unique or exclusion constraint matching the ON CONFLICT specification.
Declaring the constraint
The table keeps its own primary key and adds a unique constraint on the external ID, scoped by the system it came from.
CREATE TABLE customers (
id bigserial PRIMARY KEY,
source_system text NOT NULL,
external_id text NOT NULL,
name text NOT NULL,
email text,
UNIQUE (source_system, external_id)
);The constraint covers two columns together. An ID such as C-1001 is only unique inside the system that issued it. The CRM and the billing system may both have a C-1001 for different people. The pair (source_system, external_id) must be unique, so both rows can exist.
What the database does with it
The constraint is backed by a unique index. Each insert looks up the key in that index, and the outcome decides what happens next.
With a plain INSERT, a duplicate fails loudly with SQLSTATE 23505, and the message names the constraint (customers_source_system_external_id_key by default). The next chapter turns that failure into an update. Until then, the error is useful, because it shows the constraint is working.
Common errors
- Unique on
external_idalone. Two systems that reuse the same ID values will collide. Include the source system in the constraint. - Nullable key columns. In PostgreSQL,
NULLvalues are treated as distinct in a unique constraint. Rows with aNULLexternal ID never conflict, so every load adds another copy. Declare key columnsNOT NULL. - Variants of the same value.
C-1001,c-1001, andC-1001(with a trailing space) are three different keys to the constraint. Normalize values before matching. A later chapter covers this. - Adding the constraint to a table that already has duplicates. The
ALTER TABLEfails until you clean the data. Find the offenders first:
SELECT source_system, external_id, count(*)
FROM customers
GROUP BY source_system, external_id
HAVING count(*) > 1;After you merge or delete the duplicates, add the constraint:
ALTER TABLE customers
ADD CONSTRAINT customers_source_external_key
UNIQUE (source_system, external_id);Naming the constraint yourself keeps error messages and migrations predictable.
Summary of the design
- The internal
ididentifies the row inside your database. - The unique pair
(source_system, external_id)identifies the record in the outside world. - Both are enforced by the database, so every upsert has exactly one possible target.
Exercise
A table has UNIQUE (external_id) with external_id nullable. A nightly load inserts rows whose external_id is NULL because the source field was empty. After three runs, how many copies of each such row exist, and how do you fix the design?
Show answer
Three copies. PostgreSQL treats NULLs as distinct in unique constraints, so they never conflict. Make external_id NOT NULL, reject or quarantine source rows without an ID, and scope the constraint with the source system: UNIQUE (sourcesystem, externalid).