Aborts the current transaction and discards all changes it made.
Syntax
ROLLBACK [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ]Parameters
| Parameter | Description |
|---|---|
WORK | TRANSACTION | Optional keywords with no effect on behavior. The following statements are equivalent: ROLLBACK, ROLLBACK WORK, ROLLBACK TRANSACTION. |
AND CHAIN | Immediately starts a new transaction after the current one ends. The new transaction inherits the same transaction characteristics (see SET TRANSACTION) as the one just finished. |
AND NO CHAIN | Default behavior. Ends the transaction without starting a new one. Equivalent to omitting the clause entirely. The following statements are equivalent: ROLLBACK, ROLLBACK WORK, ROLLBACK AND NO CHAIN, ROLLBACK WORK AND NO CHAIN. |
Usage notes
To end a transaction and persist its changes instead of discarding them, use COMMIT.
Issuing
ROLLBACKoutside a transaction block emits a warning and has no effect.Issuing
ROLLBACK AND CHAINoutside a transaction block returns an error.
Examples
Abort all changes in a transaction
BEGIN;
INSERT INTO orders (product_id, quantity) VALUES (101, 5);
UPDATE inventory SET stock = stock - 5 WHERE product_id = 101;
ROLLBACK;
-- All changes are discarded; the database is unchanged.Chain transactions without a gap
Use AND CHAIN when you want to start a new transaction immediately after rolling back, with the same transaction characteristics as the one just ended.
BEGIN;
DELETE FROM staging_data WHERE processed = true;
ROLLBACK AND CHAIN;
-- The DELETE is discarded. A new transaction starts immediately,
-- inheriting the same transaction characteristics.
INSERT INTO audit_log (event, ts) VALUES ('rollback_test', NOW());
COMMIT;What's next
COMMIT — end a transaction and persist its changes
SET TRANSACTION — configure transaction characteristics such as isolation level and access mode