SAVEPOINT(7)
โจ SAVEPOINT โจ
okay bestie, pull up a chair, because we're talking about SAVEPOINT โ straight out of the PostgreSQL 18.6 Documentation, manual page SAVEPOINT(7). it's giving "undo button but make it transactional" and honestly? iconic. ๐
๐ธ NAME ๐ธ
SAVEPOINT โ define a new savepoint within the current transaction ๐
๐ฆ SYNOPSIS ๐ฆ
SAVEPOINT savepoint_name
๐ DESCRIPTION ๐
SAVEPOINT establishes a new savepoint within the current transaction. no cap, that's the whole vibe.
so likeโฆ a savepoint is a special mark inside a transaction. and what it does is let you roll back all the commands that got executed after it was established, restoring the transaction state back to exactly what it was at the time of the savepoint. it's literally a checkpoint in your little SQL adventure. โญ
๐ PARAMETERS ๐
savepoint_nameโ the name you're giving to the new savepoint. heads up tho: if savepoints with the same name already exist, they become inaccessible until the newer identically-named savepoints get released. so like, the old ones are still there, they're just in the back of the group chat not being answered. ๐
๐ NOTES ๐
- use
ROLLBACK TOto rollback to a savepoint. ๐ซถ - use
RELEASE SAVEPOINTto destroy a savepoint โ and this one keeps the effects of the commands executed after it was established. so you're deleting the bookmark, not the reading you did after it. - savepoints can only be established when you're inside a transaction block. not optional, bestie, it's a whole requirement.
- you can have multiple savepoints defined within a single transaction. go off, stack them. ๐
๐ EXAMPLES ๐
establishing one and later undoing everything after it
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. bye bye row 2, you will not be missed. ๐
establishing one and later destroying it
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. we love a happy ending. ๐ธ
using a single savepoint name
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. the way that it peels back one layer at a time?? it's giving onion. it's giving archaeology. ๐ง
๐ COMPATIBILITY ๐
okay so here's where PostgreSQL does its own thing and honestly we respect it. โจ
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. (and releasing the newer savepoint with RELEASE SAVEPOINT will cause the older one to again become accessible to ROLLBACK TO SAVEPOINT and RELEASE SAVEPOINT.) so the old one's just napping, not gone. ๐ค
other than that whole situation? SAVEPOINT is fully SQL conforming. periodt.
๐ฅ SEE ALSO ๐ฅ
BEGIN(7)COMMIT(7)RELEASE SAVEPOINT (RELEASE_SAVEPOINT(7))ROLLBACK(7)ROLLBACK TO SAVEPOINT (ROLLBACK_TO_SAVEPOINT(7))
lowkey important: savepoints only work inside a transaction block. if you're not in one, this whole thing is not happening, bestie. ๐ซถ
and that's the page!! this was SAVEPOINT(7) from the PostgreSQL 18.6 documentation, dated 2026, and it absolutely ate. go forth and roll back responsibly. mwah. ๐๐ฆ๐โจ
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms