What is the RETURNING clause in PostgreSQL?
Answer
The RETURNING clause is a PostgreSQL extension to INSERT, UPDATE, and DELETE statements that returns data from the affected rows — avoiding a separate SELECT query. Example: INSERT INTO users (name, email) VALUES ('Alice', 'a@x.com') RETURNING id; returns the auto-generated ID immediately. UPDATE products SET price = price * 1.1 WHERE id = 5 RETURNING id, price; returns the new price. DELETE FROM sessions WHERE expires_at < NOW() RETURNING session_id;. RETURNING can return any column or expression from the affected rows. This is particularly useful in ORMs, application code, and CTEs (with data-modifying CTEs using WITH ... INSERT ... RETURNING).
Previous
What is TRUNCATE and how does it differ from DELETE?
Next
What is a transaction in PostgreSQL?