2 Comments
User's avatar
Tejeswar Reddy Chinthala's avatar

Hey Nishaanth, that was a good one! But the example you shared could technically be done in SQL as well right?

The real strength of PostgreSQL (PSQL) lies in its advanced transaction management using Savepoints and partial rollbacks. When you’re running a transaction with multiple queries and make a mistake at a certain point, SQL would require a full rollback, undoing everything.

In PostgreSQL, however, you can roll back only to a specific savepoint, keeping the successful operations intact. Here’s a clearer example:

BEGIN;

INSERT INTO accounts (id, name, balance) VALUES (1, 'Alice', 1000);

SAVEPOINT sp1;

INSERT INTO accounts (id, name, balance) VALUES (2, 'Bob', 500);

SAVEPOINT sp2;

-- Oops! Attempt to insert a duplicate ID

INSERT INTO accounts (id, name, balance) VALUES (1, 'Charlie', 300);

-- Instead of losing everything, rollback to the last savepoint

ROLLBACK TO SAVEPOINT sp2;

-- Continue the transaction without affecting previous inserts

INSERT INTO accounts (id, name, balance) VALUES (3, 'David', 700);

COMMIT;

See how PSQL gives you the flexibility to fix mistakes without restarting the whole process? It’s a game changer for large, complex transactions! 😊

Expand full comment
Ramesh Balan's avatar

Nicely done Nishaanth

Expand full comment