YogeshChauhan.com
PostgreSQL transactions using the BEGIN, COMMIT, and ROLLBACK statements.
June 19, 2020


Transactions are a fundamental concept of all database systems.

The essential point of a transaction is that it bundles multiple steps into a single, all-or-nothing operation.

The intermediate states between the steps are not visible to other concurrent transactions, and if some failure occurs that prevents the transaction from completing, then none of the steps affect the database at all.

For example, consider a bank database that contains balances for various customer accounts, as well as total deposit balances for branches.

Suppose that we want to record a payment of $100.00 from Alice's account to Bob's account.

Simplifying outrageously, the SQL commands for this might look like:


UPDATE accounts SET balance = balance - 100.00
    WHERE name = 'Alice';
UPDATE branches SET balance = balance - 100.00
    WHERE name = (SELECT branch_name FROM accounts WHERE name = 'Alice');
UPDATE accounts SET balance = balance + 100.00
    WHERE name = 'Bob';
UPDATE branches SET balance = balance + 100.00
    WHERE name = (SELECT branch_name FROM accounts WHERE name = 'Bob');

The details of these commands are not important here; the important point is that there are several separate updates involved to accomplish this rather simple operation.

Our bank's officers will want to be assured that either all these updates happen, or none of them happen.

It would certainly not do for a system failure to result in Bob receiving $100.00 that was not debited from Alice. Nor would Alice long remain a happy customer if she was debited without Bob being credited.

We need a guarantee that if something goes wrong partway through the operation, none of the steps executed so far will take effect. Grouping the updates into a transaction gives us this guarantee.

A transaction is said to be atomic: from the point of view of other transactions, it either happens completely or not at all.

We also want a guarantee that once a transaction is completed and acknowledged by the database system, it has indeed been permanently recorded and won't be lost even if a crash ensues shortly thereafter.

For example, if we are recording a cash withdrawal by Bob, we do not want any chance that the debit to his account will disappear in a crash just after he walks out the bank door.

A transactional database guarantees that all the updates made by a transaction are logged in permanent storage (i.e., on disk) before the transaction is reported complete.

Another important property of transactional databases is closely related to the notion of atomic updates: when multiple transactions are running concurrently, each one should not be able to see the incomplete changes made by others.

For example, if one transaction is busy totaling all the branch balances, it would not do for it to include the debit from Alice's branch but not the credit to Bob's branch, nor vice versa.

So transactions must be all-or-nothing not only in terms of their permanent effect on the database, but also in terms of their visibility as they happen.

The updates made so far by an open transaction are invisible to other transactions until the transaction completes, whereupon all the updates become visible simultaneously.

In PostgreSQL, a transaction is set up by surrounding the SQL commands of the transaction with BEGIN and COMMIT commands.

BEGIN and COMMIT commands

To start a transaction, you use any of the following statements:


BEGIN TRANSACTION;

//or

BEGIN WORK;

//or

BEGIN;

However, if you start a new session after executing a query, you will not see the change.

To make the change become visible to other sessions or users you need to commit the transaction by using any of the following statements:


COMMIT TRANSACTION;

//or

COMMIT WORK;

//or

COMMIT;

So our banking transaction would actually look like:


BEGIN;
UPDATE accounts SET balance = balance - 100.00
    WHERE name = 'Alice';
-- etc etc
COMMIT;

If, partway through the transaction, we decide we do not want to commit (perhaps we just noticed that Alice's balance went negative), we can issue the command ROLLBACK instead of COMMIT, and all our updates so far will be canceled.

ROLLBACK Command

We can use any of the following statements to ROLLBACK changes:


ROLLBACK TRANSACTION;

//or

ROLLBACK WORK;

//or

ROLLBACK;

PostgreSQL actually treats every SQL statement as being executed within a transaction. If you do not issue a BEGIN command, then each individual statement has an implicit BEGIN and (if successful) COMMIT wrapped around it.

A group of statements surrounded by BEGIN and COMMIT is sometimes called a transaction block.

Note: Some libraries issue BEGIN and COMMIT commands automatically, so that you might get the effect of transaction blocks without asking.

Savepoints

It's possible to control the statements in a transaction in a more granular fashion through the use of savepoints.

Savepoints allow you to selectively discard parts of the transaction, while committing the rest.

After defining a savepoint with SAVEPOINT, you can if needed roll back to the savepoint with ROLLBACK TO.

All the transaction's database changes between defining the savepoint and rolling back to it are discarded, but changes earlier than the savepoint are kept.

After rolling back to a savepoint, it continues to be defined, so you can roll back to it several times.

Conversely, if you are sure you won't need to roll back to a particular savepoint again, it can be released, so the system can free some resources.

Keep in mind that either releasing or rolling back to a savepoint will automatically release all savepoints that were defined after it.

All this is happening within the transaction block, so none of it is visible to other database sessions.

When and if you commit the transaction block, the committed actions become visible as a unit to other sessions, while the rolled-back actions never become visible at all.

Remembering the bank database, suppose we debit $100.00 from Alice's account, and credit Bob's account, only to find later that we should have credited Wally's account.

We could do it using savepoints like this:


BEGIN;
UPDATE accounts SET balance = balance - 100.00
    WHERE name = 'Alice';
SAVEPOINT my_savepoint;
UPDATE accounts SET balance = balance + 100.00
    WHERE name = 'Bob';
-- oops ... forget that and use Wally's account
ROLLBACK TO my_savepoint;
UPDATE accounts SET balance = balance + 100.00
    WHERE name = 'Wally';
COMMIT;

This example is, of course, oversimplified, but there's a lot of control to be had over a transaction block through the use of savepoints.

Moreover, ROLLBACK TO is the only way to regain control of a transaction block that was put in aborted state by the system due to an error, short of rolling it back completely and starting again.

Credit: PostgreSQL

dreamhost

Leave a Reply

Most Read

#1 How to set opacity or transparency using CSS? #2 Pagination in CSS with multiple examples #3 How to check if radio button is checked or not using JavaScript? #4 How to make HTML form interactive and using CSS? #5 How to uninstall Cocoapods from the Mac OS? #6 How to add Read More Read Less Button using JavaScript?



Recently Posted

Feb 24 How to modify the latest post array using get_posts() in WordPress? Feb 22 WordPress: How to get ACF field values from another post? Feb 22 WordPress: How to print ACF repeater field values? Feb 22 WordPress: How to print ACF array field values? Feb 21 WordPress: How to get field values in Advanced Custom Fields? Feb 21 WordPress: How to add a Search Icon in Menus with toggle effect using jQuery?



You might also like these

Learn to create your skill bar using CSSCSSHow to use HTML Quotation and Citation?HTMLHow to Clone Objects in PHP?MiscGlobal JavaScript methods to convert variables to numbersJavaScriptHow to get query string values in JavaScript? (URL Manipulation using URLSearchParams)JavaScriptThe order Property in Flex Items in CSSCSS