DEV Community

JackTT
JackTT

Posted on • Updated on

Understanding the "SELECT FOR UPDATE" SQL Statement

What is "SELECT FOR UPDATE"?

SELECT FOR UPDATE is a clause in SQL that is appended to a SELECT statement. It locks the selected rows, preventing other transactions from modifying them until the current transaction is committed or rolled back. This ensures that the data being read and potentially updated remains consistent throughout the transaction.

Syntax of "SELECT FOR UPDATE"

The basic syntax for using "SELECT FOR UPDATE" is:

BEGIN;
SELECT stock 
FROM items 
WHERE item_id = ? 
FOR UPDATE;
-- Check stock and update if available
UPDATE items 
SET stock = stock - 1 
WHERE item_id = ?;
COMMIT;

Enter fullscreen mode Exit fullscreen mode

Here, the FOR UPDATE clause at the end indicates that the selected rows should be locked for the duration of the transaction.

How Does "SELECT FOR UPDATE" Work?

When a transaction issues a "SELECT FOR UPDATE" statement, the following steps occur:

  1. Row Selection: The database engine identifies the rows that match the specified condition.
  2. Row Locking: The selected rows are locked, preventing other transactions from acquiring locks that would conflict with the current transaction. This typically means other transactions cannot update or delete these rows until the lock is released. The affected transactions include:
    • UPDATE
    • DELETE
    • SELECT FOR UPDATE
    • SELECT FOR NO KEY UPDATE
    • SELECT FOR SHARE
    • SELECT FOR KEY SHARE

That means the read only queries (SELECT) are not locked.

  1. Data Access: The transaction can read and potentially update the locked rows.
  2. Transaction Completion: Once the transaction is committed or rolled back, the locks are released, making the rows available for other transactions.

Postgres: Row-level locks do not affect data querying; they block only writers and lockers to the same row. (ref)

Benefits of Using "SELECT FOR UPDATE"

  1. Data Consistency: By locking the rows during a transaction, "SELECT FOR UPDATE" ensures that the data being read is not changed by other transactions, maintaining consistency.
  2. Avoiding Deadlocks: Proper use of "SELECT FOR UPDATE" can help avoid deadlocks by managing locks in a controlled manner.
  3. Concurrency Control: It provides a way to handle concurrent data access and modification efficiently, particularly in applications with high transaction volumes.

Use Cases of "SELECT FOR UPDATE"

  1. Banking Systems: Ensuring that account balances are accurate by locking rows representing accounts during transactions.
  2. Inventory Management: Preventing the same item from being sold to multiple customers simultaneously by locking the inventory records.
  3. Reservation Systems: Managing bookings by locking the rows representing available slots or seats during the reservation process.

Conclusion

"SELECT FOR UPDATE" is a vital tool for managing data integrity and consistency in relational databases. By understanding its functionality and applying it thoughtfully, developers can handle concurrent transactions effectively, ensuring reliable and accurate data manipulation. However, it is equally important to be aware of its implications on performance and system architecture to make the best use of this feature.

References

Top comments (0)