Using Transaction
Introduction A transaction is a collection of one or more SQL statements grouped together into a single unit of work. The goal is to ensure atomicity in ACID compliance, meaning either all statements within the transaction succeed and are recorded to the database (COMMIT), or if a single statement fails, all previous statements are aborted and rolled back as if nothing ever happened (ROLLBACK). Example: You transfer money at a bank. The system must perform two tasks: Deduct money from your account. Credit money to the recipient's account. These two statements must reside in the same transaction. If the system loses power while running the first statement and the second statement cannot execute, the system will perform a ROLLBACK to return your money, preventing you from losing money when the transaction fails. Transaction and Lock Regarding the internal operation of PostgreSQL, a transaction must depend on and use locks. The relationship between them is an inseparable symbiotic rel...