RefLib

Reference library for AI

Back

postgresql_16:7.1.162.4 Examples

PostgreSQL 16 > Reference > SQL Commands > RELEASE SAVEPOINT > Examples

To establish and later release a savepoint: BEGIN; INSERT INTO table1 VALUES (3); SAVEPOINT my_savepoint; INSERT INTO table1 VALUES (4); RELEASE SAVEPOINT my_savepoint; COMMIT; The above transaction will insert both 3 and 4.

A more complex example with multiple nested subtransactions: BEGIN; INSERT INTO table1 VALUES (1); SAVEPOINT sp1; INSERT INTO table1 VALUES (2); SAVEPOINT sp2; INSERT INTO table1 VALUES (3); RELEASE SAVEPOINT sp2; INSERT INTO table1 VALUES (4))); -- generates an error In this example, the application requests the release of the savepoint sp2, which inserted 3. This changes the insert's transaction context to sp1. When the statement attempting to insert value 4 generates an error, the insertion of 2 and 4 are lost because they are in the same, now-rolled back savepoint, and value 3 is in the same transaction context. The application can now only choose one of these two commands, since all other commands will be ignored: ROLLBACK; ROLLBACK TO SAVEPOINT sp1; Choosing ROLLBACK will abort everything, including value 1, whereas ROLLBACK TO SAVEPOINT sp1 will retain value 1 and allow the transaction to continue.

Back