SAVEPOINT(7)
πΎ 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 TOto rollback to a savepoint π - use
RELEASE SAVEPOINTto 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 π
BEGIN(7)COMMIT(7)RELEASE SAVEPOINT(RELEASE_SAVEPOINT(7))ROLLBACK(7)ROLLBACK TO SAVEPOINT(ROLLBACK_TO_SAVEPOINT(7))
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
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms