SAVEPOINT(7)

๐Ÿ“ฆ version 18.6, in opensuse-leap 16.0

โœจ 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 TO to rollback to a savepoint. ๐Ÿซถ
  • use RELEASE SAVEPOINT to 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 ๐Ÿ”ฅ


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