next up previous contents index
Next: Multistatement Transactions Up: Transactions and Locks Previous: Transactions and Locks

Transactions

Although you may not have heard the term transaction before, you have already used transactions. Every SQL query is executed in a transaction. Transactions give databases an all-or-nothing capability when making modifications.

For example, suppose the query UPDATE trans_test SET col = 3 is in the process of modifying 700 rows. After it has modified 200 rows, the user presses control-C or hits the computer reset button. When the user looks at trans_test, he will see that none of the rows has been updated.

This result might surprise you. Because 200 of the 700 rows had already updated, you might suspect that 200 rows would show as modified. However, POSTGRESQL uses transactions to guarantee that queries are either fully completed or have no effect.

This feature is valuable. Suppose you were executing a query to add $500 to everyone's salary and accidentally kicked the power cord out of the wall during the update procedure. Without transactions, the query may have updated half the salaries, but not the rest. It would be difficult to know where the UPDATE stopped. You would wonder, ``Which rows were updated, and which ones were not?'' You cannot simply re-execute the query, because some people would have already received their $500 increase. With transactions, you can check to see if any of the rows were updated. If one was updated, then all were updated. If not, you can simply re-execute the query.


next up previous contents index
Next: Multistatement Transactions Up: Transactions and Locks Previous: Transactions and Locks
Bruce Momjian
2001-05-09