Postgresql – Implementation of Rollback in PostgreSQL

postgresqlrollback

How is the rollback command implemented within the PostgreSQL database?

I wanted to know the implementation details and what exactly happens underneath when a rollback is executed, i.e. state of database storage, changes in write-ahead logs (WAL).

Scenario: I have a table, called employees, having disk size of, say, 4GB on the database. When I open a transaction on this table to update a single row, and then rollback the changes, what would happen inside the database? Would a new version of the table created on the disk but remain inactive due to rollback?

Best Answer

Apart from some side tasks like releasing locks, deleting files and similar, the main activity is to set the two bits that correspond to the transaction in the commit log (in pg_xact) to “aborted” and sync the file to disk.

A rollback does not touch the table at all, and it should always be fast. The newly created row version simply remain in the table (to be cleaned up later by autovacuum), they just become invisible because the transaction that created them has been aborted. Similar with deleted data: the marker that the row was deleted by the transaction simply becomes invalid.