Sub topic 6
Getting the New ID Back with `RETURNING`
Assume you know basic INSERT, primary keys, and PostgreSQL identity columns. This chapter focuses on reading the ID that PostgreSQL generates when you insert a row.
Why `RETURNING` matters
An identity column gets its value inside PostgreSQL. The application often needs that value immediately so it can:
- Insert related rows that refer to the new row.
- Display a link to the new record.
- Store the database ID in application state.
- Avoid a second query that guesses which row was inserted.
PostgreSQL provides the RETURNING clause for this purpose. It makes an INSERT return selected columns from the rows it inserted.
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer text NOT NULL
);
INSERT INTO orders (customer)
VALUES ('Ada')
RETURNING id, customer;The result is similar to this:
| id | customer |
|---|---|
| 1 | Ada |
The generated id is available in the result returned by the INSERT statement. The application should read that result instead of issuing a separate SELECT.
Returning one generated ID
You can return only the identity column when that is all the application needs.
INSERT INTO orders (customer)
VALUES ('Grace')
RETURNING id;You can also return expressions or several columns:
INSERT INTO orders (customer)
VALUES ('Lin')
RETURNING id, customer, 'created' AS action;RETURNING returns the final row values produced by the statement. This includes generated values and values filled in by defaults.
A database client normally exposes the result as one returned row. The exact method name depends on the programming language, but the SQL pattern stays the same:
- Execute the
INSERT ... RETURNINGstatement. - Read the returned row.
- Store its
id. - Use that ID for related work.
Returning IDs for multiple rows
A single statement can insert several rows and return one result row for each inserted row.
INSERT INTO orders (customer)
VALUES ('Mina'), ('Noah'), ('Omar')
RETURNING id, customer;The result contains three generated IDs. Do not assume that an ID can be predicted from the number of existing rows. Concurrent sessions, rolled-back inserts, and sequence behavior can make predictions incorrect.
If the application needs to match each returned ID to its input, include an input value in the inserted row or perform the operation in a way that preserves a clear application-side association. The returned rows contain database values, not a special client-generated position.
`RETURNING` and transaction errors
RETURNING reports values only when the statement succeeds. If a constraint violation aborts the statement, there is no successful inserted row to return.
The returned ID also does not mean that the transaction has committed. A later error can still roll back the transaction. The ID may then never be visible in the table, even though the sequence value was consumed.
flowchart TD
A[Application sends INSERT] --> B[PostgreSQL generates identity]
B --> C[INSERT succeeds]
C --> D[RETURNING sends generated ID]
D --> E[Application uses ID]
C --> F[Later transaction error]
F --> G[Transaction rolls back]
G --> H[Row is not committed]Exercise
Exercise
Which SQL statement returns the generated identity value after inserting a customer named "Ravi" into orders?
Show answer
INSERT INTO orders (customer) VALUES ('Ravi') RETURNING id;