PostgreSQL - TRANSACTIONS



In PostgreSQL, a transaction is a logical unit of work that uses SQL queries to combine one or more database operations. These operations shows the sequences like INSERT, UPDATE, DELETE, or SELECT. When executing multiple queries as a transaction, proper error handling is required for maintaining data integrity.

postgresql_transaction

In this tutorial, you will learn how to group multiple PostgreSQL queries and execute them as part of a transaction.

Transactions Properties in PostgreSQL

Transactions follow four standard properties which is referred by the acronym ACID −

  • Atomicity − Ensures that all operations within the work unit are completed successfully; otherwise, the transaction is aborted at the point of failure and previous operations are rolled back to their former state.
  • Consistency − Ensures that the database properly changes states upon a successfully committed transaction.
  • Isolation − Enables transactions to operate independently of and transparent to each other.
  • Durability − Ensures that the result or effect of a committed transaction persists in case of a system failure.

Transaction Control

Following are the few commands that control transactions −

  • BEGIN TRANSACTION − To start a transaction.

  • COMMIT − To save the changes, alternatively you can use END TRANSACTION command.

  • ROLLBACK − To rollback the changes.

Transactional control commands are only used with the DML commands INSERT, UPDATE and DELETE only. They cannot be used while creating tables or dropping them because these operations are automatically committed in the database.

The BEGIN TRANSACTION Command

Transactions can be started using BEGIN TRANSACTION or simply BEGIN command. Such transactions usually persist until the next COMMIT or ROLLBACK command is encountered. But a transaction will also ROLLBACK if the database is closed or if an error occurs.

Syntax

The following is the simple syntax to start a transaction −

BEGIN;

or

BEGIN TRANSACTION;

The COMMIT Command

The COMMIT command is the transactional command used to save changes invoked by a transaction to the database.

The COMMIT command saves all transactions to the database since the last COMMIT or ROLLBACK command.

Syntax

The syntax for COMMIT command is as follows −

COMMIT;

or

END TRANSACTION;

The ROLLBACK Command

The ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database.

The ROLLBACK command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.

Syntax

The syntax for ROLLBACK command is as follows −

ROLLBACK;

Example of Transaction

Consider the COMPANY table is having the following records −

id name age address salary
1 Paul 32 California 20000
2 Allen 25 Texas 15000
3 Teddy 23 Norway 20000
4 Mark 25 Rich-Mond 65000
5 David 27 Texas 85000
6 Kim 22 South-Hall 45000
7 James 24 Houston 10000

Now, let us start a transaction and delete records from the table having age = 25 and finally we use ROLLBACK command to undo all the changes.

testdb=# BEGIN;
DELETE FROM COMPANY WHERE AGE = 25;
ROLLBACK;

If you will check COMPANY table is still having the following records −

id name age address salary
1 Paul 32 California 20000
2 Allen 25 Texas 15000
3 Teddy 23 Norway 20000
4 Mark 25 Rich-Mond 65000
5 David 27 Texas 85000
6 Kim 22 South-Hall 45000
7 James 24 Houston 10000

Now, let us start another transaction and delete records from the table having age = 25 and finally we use COMMIT command to commit all the changes.

testdb=# BEGIN;
DELETE FROM COMPANY WHERE AGE = 25;
COMMIT;

If you will check the COMPANY table, it still has the following records −

id name age address salary
1 Paul 32 California 20000
3 Teddy 23 Norway 20000
5 David 27 Texas 85000
6 Kim 22 South-Hall 45000
7 James 24 Houston 10000
(5 rows)
Advertisements