Sub topic 7
Deciding which fields an update overwrites
This chapter assumes you have the customers table from the earlier chapters, with UNIQUE (source_system, external_id). It also assumes you can read a basic INSERT ... ON CONFLICT DO UPDATE statement. The question here is what the SET list should contain.
The SET list is a policy
When the conflict fires, PostgreSQL updates only the columns you name. An upsert that rewrites every column is easy to write, but it often destroys data. Sort each column into one of three groups first.
| Group | Examples | In the SET list? |
|---|---|---|
| Identity | `source_system`, `external_id` | No. They are the match key. |
| Source-owned | `name`, `email` | Yes. The other system is the authority. |
| Locally owned | `id`, `created_at`, `notes` | No. Your system owns them. |
Suppose your team adds a free-text notes column and a created_at timestamp. An upsert that overwrote them would erase notes on every sync and reset the creation date.
ALTER TABLE customers
ADD COLUMN notes text,
ADD COLUMN created_at timestamptz NOT NULL DEFAULT now(),
ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();Name the columns you overwrite
List only the source-owned columns. EXCLUDED is the row you tried to insert. customers is the row already stored.
INSERT INTO customers (source_system, external_id, name, email)
VALUES ('crm', 'C-1001', 'Ada Lovelace', '[email protected]')
ON CONFLICT (source_system, external_id)
DO UPDATE SET name = EXCLUDED.name,
email = EXCLUDED.email,
updated_at = now()
RETURNING id;notes and created_at are not in the list, so an update leaves them alone. A new row gets the column defaults.
Missing values versus empty values
Some sources omit a field when they have no value. If the incoming email is NULL, email = EXCLUDED.email erases a good address. Use COALESCE to keep the stored value when the incoming one is NULL.
INSERT INTO customers (source_system, external_id, name, email)
VALUES ('crm', 'C-1001', 'Ada Lovelace', NULL)
ON CONFLICT (source_system, external_id)
DO UPDATE SET name = EXCLUDED.name,
email = COALESCE(EXCLUDED.email, customers.email),
updated_at = now()
WHERE (customers.name, customers.email)
IS DISTINCT FROM
(EXCLUDED.name, COALESCE(EXCLUDED.email, customers.email))
RETURNING id;This version has two effects:
- A
NULLemail no longer overwrites a stored one. The cost is that the source can no longer clear an email on purpose. If clearing must work, the source needs a way to send an explicit "empty" value that differs from "absent". - The
WHEREclause skips the write when nothing changed. That avoids needless row versions and keepsupdated_atmeaningful.
Choosing the behavior
If you only want to create missing records and never touch existing ones, use DO NOTHING instead. That suits reference data you have already corrected by hand.
Exercise
The source sends a customer with name = 'Ada King' and email = NULL. Your upsert uses name = EXCLUDED.name, email = COALESCE(EXCLUDED.email, customers.email). What happens to a stored row with name 'Ada Lovelace' and email '[email protected]', and what happens to its notes column?
Show answer
The name becomes 'Ada King'. The email stays '[email protected]' because COALESCE keeps the stored value when the incoming one is NULL. The notes column is unchanged because it is not in the SET list.