Sub topic 6
Using EXCLUDED to Reference the Proposed Row
Assume you know basic SELECT, INSERT, and UPDATE statements, along with primary keys and unique constraints. This chapter focuses on PostgreSQL's EXCLUDED row inside INSERT ... ON CONFLICT DO UPDATE.
Two rows are involved
An upsert can find an existing row that conflicts with the new row. During DO UPDATE, PostgreSQL gives you access to both rows:
- The target table contains the existing row.
EXCLUDEDcontains the row that theINSERTtried to add.
EXCLUDED is a special name available only inside the DO UPDATE clause. It does not mean that the row was removed. It means that PostgreSQL excluded the proposed row from the normal insert because it conflicted with a unique constraint or index.
Consider this table:
CREATE TABLE product_stock (
sku text PRIMARY KEY,
quantity integer NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO product_stock (sku, quantity)
VALUES ('A-100', 5);A later upsert proposes another row with the same sku:
INSERT INTO product_stock (sku, quantity)
VALUES ('A-100', 8)
ON CONFLICT (sku) DO UPDATE
SET quantity = EXCLUDED.quantity,
updated_at = now();The primary key detects the conflict. PostgreSQL keeps the existing row but replaces its quantity with 8. The value 8 comes from EXCLUDED.quantity.
Name the existing row with an alias
You can give the target table an alias to make the two sources clear:
INSERT INTO product_stock AS current (sku, quantity)
VALUES ('A-100', 3)
ON CONFLICT (sku) DO UPDATE
SET quantity = current.quantity + EXCLUDED.quantity;Here, current.quantity is the value already stored in the table, and EXCLUDED.quantity is the newly proposed value. If the stored quantity was 8, the result becomes 11.
The left side of SET names the column to change. Qualify the source values on the right side when that makes their origin clearer.
| Reference | Meaning |
|---|---|
| `current.quantity` | Quantity in the existing conflicting row |
| `EXCLUDED.quantity` | Quantity from the proposed insert row |
| `quantity` in `SET` | Target column being updated |
This distinction lets an upsert either overwrite a value or combine it with the old value.
Choose an update rule
The correct expression depends on what the incoming value means.
If the incoming row is the latest complete state, overwrite the existing value:
INSERT INTO product_stock AS current (sku, quantity)
VALUES ('A-100', 20)
ON CONFLICT (sku) DO UPDATE
SET quantity = EXCLUDED.quantity,
updated_at = now();If the incoming row represents an increment, combine the two values:
INSERT INTO product_stock AS current (sku, quantity)
VALUES ('A-100', 4)
ON CONFLICT (sku) DO UPDATE
SET quantity = current.quantity + EXCLUDED.quantity,
updated_at = now();The same pattern works for several columns. For example, an incoming profile record might replace name and email:
INSERT INTO customer AS current (customer_id, name, email)
VALUES (42, 'Ada Lovelace', '[email protected]')
ON CONFLICT (customer_id) DO UPDATE
SET name = EXCLUDED.name,
email = EXCLUDED.email;Only columns listed in SET change. Other columns keep their existing values unless a database trigger changes them.
See the proposed and resulting values
You can use EXCLUDED in the update expression and use RETURNING to inspect the result:
INSERT INTO product_stock AS current (sku, quantity)
VALUES ('A-100', 2)
ON CONFLICT (sku) DO UPDATE
SET quantity = current.quantity + EXCLUDED.quantity
RETURNING current.sku, current.quantity;RETURNING reports the row after the insert or update. In application code, this is useful when the database calculates timestamps, generated values, or the final combined quantity.
The database checks the unique conflict and performs the update as one statement. That is safer than reading a quantity in one statement and writing a calculated quantity in another, because another transaction could change the row between those operations.
Exercise
The existing quantity is 10, and an upsert proposes quantity 3. Which expression makes the final quantity 13?
Show answer
Use quantity = current.quantity + EXCLUDED.quantity after aliasing the target table as current.