Sub topic 13
Edge Cases and Common Mistakes
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, and that you can run SQL through psql or another PostgreSQL client. This chapter focuses on mistakes that occur after you understand the basic RETURNING syntax.
No matching row means no returned row
RETURNING does not guarantee one result. It returns one row for each row that the statement actually affects.
UPDATE tasks
SET status = 'done'
WHERE id = 999
RETURNING id, status;If task 999 does not exist, this statement succeeds but returns zero rows. Application code must distinguish “the command ran” from “a row was changed.”
The same rule applies to DELETE. A WHERE clause that matches nothing produces an empty result set.
A multi-row statement can return many rows:
DELETE FROM tasks
WHERE status = 'done'
RETURNING id, title;Do not assume the returned rows arrive in table order. Unless the statement provides an ordering mechanism, the order is unspecified.
`RETURNING *` can return more than you intended
The * in RETURNING * means all columns of the affected table. It can expose internal fields, audit data, large values, or information that the caller does not need.
Prefer an explicit list at an application boundary:
INSERT INTO tasks (title)
VALUES ('Write report')
RETURNING id, title, status;Explicit columns also make client code less fragile when the table later gains a column.
Defaults and triggers can change the returned values
RETURNING reports the row produced by the modification. That includes values supplied by defaults and values changed by applicable row-level triggers.
INSERT INTO tasks (title)
VALUES ('Write report')
RETURNING id, status, updated_at;This returns the generated identity key, the default status, and the timestamp generated by now().
A BEFORE trigger can replace a value before PostgreSQL returns the row. Therefore, do not assume that the value sent in INSERT or UPDATE is the value returned. If an AFTER trigger writes to another table, that separate table change does not turn into extra rows in the original statement's RETURNING result.
Generated columns are also calculated values. Return them when the client needs the database's result instead of attempting to reproduce the calculation in application code.
Upsert paths do not all return a row
With ON CONFLICT DO NOTHING, a conflicting input produces no returned row:
INSERT INTO tasks (title)
VALUES ('Review PR')
ON CONFLICT (title) DO NOTHING
RETURNING id, title;The client must handle both an inserted row and an empty result.
With ON CONFLICT DO UPDATE, the update path returns the affected row. However, a conflict action with a WHERE condition may skip the update. That row is then not returned.
INSERT INTO tasks (title, status)
VALUES ('Review PR', 'done')
ON CONFLICT (title) DO UPDATE
SET status = EXCLUDED.status
WHERE tasks.status <> EXCLUDED.status
RETURNING id, title, status;If the existing status is already done, the condition is false and the result is empty.
CTEs are atomic, but their row order is not
A data-modifying CTE can pass RETURNING rows into another operation. This is useful for moving data, but do not depend on the textual order of sibling data-modifying CTEs. They run as one statement under the statement's snapshot, and trying to modify the same row more than once in that statement can produce unpredictable results.
Version-dependent syntax needs a guard
MERGE ... RETURNING and merge_action() require PostgreSQL 17 or later. RETURNING old.column and RETURNING new.column require PostgreSQL 18 or later. Check the server version before deploying these forms:
SELECT current_setting('server_version_num');A client connected to an older server will receive a syntax error, even if the query worked in development.
Exercise
An UPDATE ... RETURNING statement succeeds but the client receives zero rows. Name two possible reasons.
Show answer
The WHERE clause matched no rows, or an upsert-like condition prevented the modification. In either case, no row was affected.