SAVEPOINT(7)

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

πŸ’– SAVEPOINT(7) πŸ’–

okay so bestie, welcome to the SAVEPOINT page from the PostgreSQL 16.15 Documentation β€” this is the manual page literally called SAVEPOINT(7) and it's about to make your transaction game iconic. ✨


πŸŽ€ NAME πŸŽ€

SAVEPOINT β€” define a new savepoint within the current transaction. that's it. that's the whole vibe. πŸ’…


✨ SYNOPSIS ✨

SAVEPOINT savepoint_name

πŸ¦‹ DESCRIPTION πŸ¦‹

SAVEPOINT establishes a new savepoint within the current transaction. periodt. 🫢

A savepoint is a special mark inside a transaction that lets you roll back all the commands executed after it was established β€” restoring the transaction state to exactly what it was at the time of the savepoint. it's giving undo button but make it SQL. no cap. 😭


🌸 PARAMETERS 🌸

  • savepoint_name β€” the name you're giving to the new savepoint. and listen πŸ‘€ if savepoints with the same name already exist, they will be inaccessible until the newer identically-named savepoints are released. iykyk.

πŸ’œ NOTES πŸ’œ

  • use ROLLBACK TO to rollback to a savepoint. πŸ”₯
  • use RELEASE SAVEPOINT to destroy a savepoint β€” and it keeps the effects of the commands executed after it was established. bestie it's non-destructive, slay. πŸ’…
Savepoints can only be established when you're inside a transaction block. and yes, there can be multiple savepoints defined within a transaction. you can stack them. iconic behaviour honestly. πŸ‘‘

πŸ“ EXAMPLES πŸ“

🧚 establish a savepoint and later undo the effects

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. the way that 2 just… didn't happen πŸ˜­πŸ’€

🧚 establish and later destroy a savepoint

to 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. we love a release that keeps everything 🫢✨

🧚 use a single savepoint name

to use 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 nesting?? the drama?? main character energy. 🌈


⭐ COMPATIBILITY ⭐

okay so this is the part where we're slightly ✨different✨ from the standard, ngl:

  • 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. so mostly a good girl fr fr πŸŽ€


πŸ’« SEE ALSO πŸ’«


and that's the whole page bestie!! πŸ¦‹πŸ’– this was PostgreSQL 16.15, dated 2026 β€” go forth and savepoint responsibly, mwah πŸ’…πŸ‘‘πŸŒΈ

πŸ“¦ where this page comes from