SAVEPOINT(7)

πŸ“¦ version 14.24, in opensuse-leap 16.0

✨ 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 TO to roll back to a savepoint πŸ”™
  • use RELEASE SAVEPOINT to 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 🌟

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