SAVEPOINT(7)
β¨ SAVEPOINT(7) β¨
okay so bestie, welcome to the PostgreSQL 14.24 Documentation page for SAVEPOINT π
this whole thing lives in section 7 of the manual and it's about making lil checkpoints inside your transaction, no cap π
πΈ NAME πΈ
SAVEPOINT β define a new savepoint within the current transaction π«Ά
π¦ SYNOPSIS π¦
SAVEPOINT savepoint_name
that's it. that's the whole vibe. one line, iconic π
π DESCRIPTION π
SAVEPOINT establishes a new savepoint within the current transaction. literally that's its whole job and it does it slayfully β¨
so like, a savepoint is a special mark inside a transaction β think of it as a little bookmark π that lets all the commands executed after it was established get rolled back, restoring the transaction state to exactly what it was at the time of the savepoint. it's giving "undo button but make it surgical" ππ
π PARAMETERS π
savepoint_nameβ the name you give to the new savepoint π« careful though: if savepoints with the same name already exist, they will be inaccessible until newer identically-named savepoints are released. yes queen, name collisions are real π
π NOTES π
- use
ROLLBACK TOto roll back to a savepoint π - use
RELEASE SAVEPOINTto destroy a savepoint, keeping the effects of commands executed after it was established ποΈβ¨ - savepoints can only be established when inside a transaction block β no transaction, no savepoint, sorry babe π
- there can be multiple savepoints defined within a transaction. stack 'em, collect 'em, it's fine π
π₯ EXAMPLES π₯
π establish a savepoint and later undo everything after it
to establish a savepoint and later undo the effects of all commands executed after it was established, you go:
BEGIN;
INSERT INTO table1 VALUES (1);
SAVEPOINT my_savepoint;
INSERT INTO table1 VALUES (2);
ROLLBACK TO SAVEPOINT my_savepoint;
INSERT INTO table1 VALUES (3);
COMMIT;
the above transaction will insert the values 1 and 3, but not 2. the 2? cancelled. never happened. we don't know her ππΈ
π§ establish and later destroy 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 β releasing keeps the effects, remember? ate and left no crumbs π
π use a single savepoint name
now this one is the main character example, the way it stacks same-named savepoints is so good π
BEGIN;
INSERT INTO table1 VALUES (1);
SAVEPOINT my_savepoint;
INSERT INTO table1 VALUES (2);
SAVEPOINT my_savepoint;
INSERT INTO table1 VALUES (3);
-- rollback to the second savepoint
ROLLBACK TO SAVEPOINT my_savepoint;
SELECT * FROM table1; -- shows rows 1 and 2
-- release the second savepoint
RELEASE SAVEPOINT my_savepoint;
-- rollback to the first savepoint
ROLLBACK TO SAVEPOINT my_savepoint;
SELECT * FROM table1; -- shows only row 1
COMMIT;
the above transaction shows row 3 being rolled back first, then row 2. layers, bestie. layers π
β COMPATIBILITY β
okay so here's where PostgreSQL lowkey does its own thing π€
SQL requires a savepoint to be destroyed automatically when another savepoint with the same name is established. in PostgreSQL, the old savepoint is kept, though only the more recent one will be used when rolling back or releasing. (releasing the newer savepoint with RELEASE SAVEPOINT will cause the older one to again become accessible to ROLLBACK TO SAVEPOINT and RELEASE SAVEPOINT.) it's giving hidden lore π
otherwise, SAVEPOINT is fully SQL conforming β so no drama there, periodt π
ngl this is the one lil spot where PG says "we do it our way" and honestly? respect π«Ά
π SEE ALSO π
BEGIN(7)COMMIT(7)RELEASE SAVEPOINT(RELEASE_SAVEPOINT(7))ROLLBACK(7)ROLLBACK TO SAVEPOINT(ROLLBACK_TO_SAVEPOINT(7))
go read those too, they're the friend group π
and that's the page!! brought to you by PostgreSQL 14.24, dated 2026, filed under SAVEPOINT(7) β mwah, go forth and checkpoint responsibly bestie π
β¨ππ¦
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms