Sub topic 8
Reading Upsert Results with `RETURNING`
Assume you know basic INSERT, UPDATE, primary keys, and unique constraints. This chapter uses PostgreSQL's INSERT ... ON CONFLICT syntax.
An upsert can insert a new row, update an existing row, or skip a conflicting row. The RETURNING clause lets you read rows affected by the statement immediately. This is useful when the application needs the stored values, such as a generated ID, the final quantity, or a timestamp.
What `RETURNING` returns
Consider this table:
CREATE TABLE product_stock (
sku text PRIMARY KEY,
quantity integer NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now()
);An upsert can return the row that it inserted or updated:
INSERT INTO product_stock (sku, quantity)
VALUES ('A-100', 5)
ON CONFLICT (sku) DO UPDATE
SET quantity = EXCLUDED.quantity,
updated_at = now()
RETURNING sku, quantity, updated_at;RETURNING uses the final values stored in the table. If A-100 did not exist, the statement inserts it and returns the inserted row. If it already existed, the statement updates it and returns the updated row.
You can return any expression that is valid in a SELECT list:
INSERT INTO product_stock (sku, quantity)
VALUES ('A-100', 3)
ON CONFLICT (sku) DO UPDATE
SET quantity = product_stock.quantity + EXCLUDED.quantity
RETURNING sku, quantity, quantity * 2 AS double_quantity;The result contains the new combined quantity and the calculated expression.
`RETURNING` and `DO NOTHING`
DO NOTHING does not affect a row when a conflict occurs. Therefore, it returns no row for that proposed value:
INSERT INTO product_stock (sku, quantity)
VALUES ('A-100', 8)
ON CONFLICT (sku) DO NOTHING
RETURNING sku, quantity;If A-100 already exists, the result set is empty. The statement succeeds, but the existing row is not returned.
This difference matters to application code. A client should not assume that a successful upsert always produces one result row.
| Conflict action | Row inserted or updated | Row returned |
|---|---|---|
| `DO NOTHING`, no conflict | Yes | Yes |
| `DO NOTHING`, conflict | No | No |
| `DO UPDATE`, update allowed | Yes, as an update | Yes |
| `DO UPDATE`, `WHERE` condition false | No | No |
Conditional updates and empty results
A WHERE clause on DO UPDATE can prevent an update. PostgreSQL then does not return the locked but unchanged row:
INSERT INTO product_stock (sku, quantity)
VALUES ('A-100', 5)
ON CONFLICT (sku) DO UPDATE
SET quantity = EXCLUDED.quantity,
updated_at = now()
WHERE product_stock.quantity IS DISTINCT FROM EXCLUDED.quantity
RETURNING sku, quantity;If the stored quantity is already 5, the condition is false. PostgreSQL does not update the row, and RETURNING produces an empty result.
This is different from returning a row whose values happen to be unchanged. The row was not affected by the INSERT ... ON CONFLICT action, so it is absent from the result.
Understanding the result flow
Reading generated values
RETURNING is especially useful for generated columns and defaults. The database, rather than the client, chooses the final value:
CREATE TABLE account (
account_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text UNIQUE NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO account (email)
VALUES ('[email protected]')
ON CONFLICT (email) DO UPDATE
SET email = EXCLUDED.email
RETURNING account_id, email, created_at;The client receives the stored identity value and timestamp without issuing a separate SELECT.
A separate query can observe a different state if another transaction changes the row between statements. RETURNING reports the result of this statement itself.
Exercise
An upsert uses ON CONFLICT DO NOTHING RETURNING sku. What does an empty result mean when the statement succeeds?
Show answer
The proposed row conflicted with an existing unique key, so PostgreSQL skipped it. DO NOTHING did not affect or return the existing row.