SAVEPOINT(7)

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

πŸ’Ύ SAVEPOINT(7) πŸ’Ύ

ok so real talk bestie, this is the SAVEPOINT page from the PostgreSQL 15.19 docs and it's genuinely one of my fave little commands?? like it's giving "undo button for your transaction" and I'm obsessed 🫢

✨ NAME ✨

SAVEPOINT β€” define a new savepoint within the current transaction πŸŽ€

✨ SYNOPSIS ✨

SAVEPOINT savepoint_name

✨ DESCRIPTION ✨

okay so SAVEPOINT establishes a new savepoint within the current transaction. that's the whole vibe πŸ’…

a savepoint is literally a special mark inside a transaction β€” like 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. no cap, it's a rewind button.

✨ PARAMETERS ✨

  • savepoint_name β€” the name you're giving to the new savepoint πŸ’–. and here's the tea: if savepoints with the same name already exist, they will be inaccessible until the newer identically-named savepoints are released. so yeah, names can stack, but the old ones go quiet 😭

🌸 NOTES 🌸

  • use ROLLBACK TO to rollback to a savepoint πŸ”™
  • use RELEASE SAVEPOINT to destroy a savepoint, keeping the effects of the commands executed after it was established ✨
important bestie: savepoints can only be established when you're inside a transaction block. outside one? not happening πŸ™ˆ

and good news β€” there can be multiple savepoints defined within a transaction, so go off, stack as many as you want πŸ‘‘

πŸŽ€ EXAMPLES πŸŽ€

establishing a savepoint and later undoing everything after it

to establish a savepoint and later undo the effects of all commands executed after it was established:

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. iconic, honestly πŸ”₯

establishing and later destroying 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 πŸ’œ

using a single savepoint name (the girlboss move)

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 second savepoint unmasks the first one… main character energy fr fr πŸ‘‘


🌈 COMPATIBILITY 🌈

ok so this is where PostgreSQL does its own thing and I 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.

(releasing the newer savepoint with RELEASE SAVEPOINT will cause the older one to again become accessible to ROLLBACK TO SAVEPOINT and RELEASE SAVEPOINT 😌)

otherwise, SAVEPOINT is fully SQL conforming ✨ slay.

πŸ’– SEE ALSO πŸ’–


ok that's the whole page bestie, go forth and savepoint responsibly 🫢

mwah πŸ’‹ β€” this was the PostgreSQL 15.19 docs edition of SAVEPOINT(7), dated 2026. see u in the next transaction πŸ‘‹

πŸ“¦ where this page comes from