Posts

Showing posts from November, 2026

PostgreSQL Transactions: COMMIT, ROLLBACK and SAVEPOINT – Practical SQL Guide

  Transactions are an important feature of PostgreSQL that help maintain data accuracy and consistency. They allow multiple SQL statements to be executed as a single logical unit. If all operations are successful, we can save the changes using COMMIT. If something goes wrong, we can undo the changes using ROLLBACK. Transactions are commonly used in banking systems, e-commerce applications, payment processing, and database management. What Is a Transaction in PostgreSQL? A transaction is a group of SQL operations executed as a single unit. For example, imagine transferring ₹1,000 from one bank account to another. Two operations are required: Deduct ₹1,000 from the sender's account. Add ₹1,000 to the receiver's account. Both operations should succeed together. If one operation fails, the transaction should be rolled back to avoid incorrect account balances. PostgreSQL provides commands such as BEGIN, COMMIT, ROLLBACK, and SAVEPOINT to manage transactions. 1. BEGIN – Starting a Tr...