Understanding Explicit Locking Types in PostgreSQL

Understanding Explicit Locking Types in PostgreSQL

Daily short news for you
  • Since the Lunar New Year holiday has started, I won't be posting anymore. See you all after the holiday! 😁

    » Read more
  • Continuing about jj. I'm wondering if there are any GUI software made for it yet to make it easier to use. There are already so many similar to git that I can't count them all.

    Luckily, the author has compiled them all together in Community-built tools around Jujutsu 🥳

    » Read more
  • Turso announces that they are rewriting SQLite in Rust. This adds another piece of evidence supporting the notion that Rust is "redefining" many things.

    But the deeper reason is more interesting. Why are they doing this? Everyone knows that SQLite is open source, and anyone can create a fork to modify it as they wish. Does the Turso team dislike or distrust C—the language used to build SQLite?

    Let me share a bit of a story. Turso is a provider of database server services based on SQLite; they have made some customizations to a fork of SQLite to serve their purposes, calling it libSQL. They are "generous" in allowing the community to contribute freely.

    Returning to the point that SQLite is open source but not open contribution. There is only a small group of people behind the maintenance of this source code, and they do not accept pull requests from others. This means that any changes or features are created solely by this group. It seems that SQLite is very popular, but the community cannot do what they want, which is to contribute to its development.

    We know that most open source applications usually come with a "tests" directory that contains very strict tests. This makes collaboration in development much easier. If you want to modify or add a new feature, you first need to ensure that the changes pass all the tests. Many reports suggest that SQLite does not publicly share this testing suite. This inadvertently makes it difficult for those who want to modify the source code, as they are uncertain whether their new implementation is compatible with the existing features.

    tursodatabase/limbo is the project rewriting SQLite in Rust mentioned at the beginning of this article. They claim that it is fully compatible with SQLite and completely open source. Limbo is currently in the final stages of development. Let’s wait and see what the results will be in the future. For a detailed article, visit Introducing Limbo: A complete rewrite of SQLite in Rust.

    » Read more

The Problem

After working with SQL or PostgreSQL for a while, have you ever wondered what would happen if you executed a SELECT and DROP command on the same table at the same time? Which command would run first? What is the priority of these commands, and how does PostgreSQL determine it? In this article, I will explain how PostgreSQL uses Locks to handle data conflicts.

Why Do We Need Locks?

PostgreSQL provides various lock modes to control concurrent access to data in tables. These locks can be used to intervene in Multi Version Concurrency Control (MVCC) situations.

In addition, most PostgreSQL commands acquire appropriate locks to ensure that referenced tables are not deleted or modified in an unintended way while the commands are being executed. For example, the TRUNCATE command cannot be executed concurrently with other operations on the same table, such as SELECT... To make this happen, TRUNCATE is granted an ACCESS EXCLUSIVE lock.

Lock Modes

Postgres has the following lock modes:

  • Table-Level Locks
  • Row-Level Locks
  • Page-Level Locks
  • Advisory Locks

These lock modes describe the conflict domain of PostgreSQL commands at the table, row, or page level.

However, in this article, I will focus on two lock modes: Table and Row. You can learn more about other lock modes at the PostgreSQL documentation.

Table-Level Locks

These locks are table-level locks, and they are either granted automatically or by using the LOCK command.

Automatic means that depending on the SQL command being invoked, you are immediately granted the corresponding lock. Here are some common commands and their corresponding locks when they are invoked.

  • SELECT: ACCESS SHARE
  • UPDATE, DELETE, INSERT: ROW EXCLUSIVE
  • CREATE INDEX: SHARE
  • CREATE TRIGGER: SHARE ROW EXCLUSIVE
  • DROP TABLE, TRUNCATE: ACCESS EXCLUSIVE

For more locks and details, refer to the PostgreSQL documentation.

The table below shows the conflict between locks.

table locking

I can explain it as follows: looking at the table, we can see that the ACCESS SHARE lock conflicts with the ACCESS EXCLUSIVE lock. Therefore, in transactions, if a command is being executed first (holding the ACCESS SHARE lock), the subsequent command (holding the ACCESS EXCLUSIVE lock) has to wait until the transaction is completed, and vice versa.

-- (1)
BEGIN;
-- (2)
SELECT * FROM users; -- ACCESS SHARE LOCKS
-- (4)
COMMIT;
-- (3)
TRUNCATE users; -- ACCESS EXCLUSIVE LOCKS

Try running these commands on two separate processes following the steps (1) (2) (3) (4) to see what happens.

Row-Level Locks

Row-level locks are acquired by specifying the name of the lock in the query. There are 4 locks:

  • FOR UPDATE
  • FOR NO KEY UPDATE
  • FOR SHARE
  • FOR KEY SHARE

row locking

FOR UPDATE causes the rows retrieved by SELECT to be locked as if for an update. This prevents them from being locked, modified, or deleted by other transactions until the current transaction finishes. In other words, other transactions like DELETE or UPDATE on these rows will be blocked until the current transaction finishes.

Similar to Table-Level Locks, the table above describes the conflicts between locks. Conflicting locks have to wait until the preceding lock is released before they can proceed.

For example, you are developing a feature for gift exchange. There is a gift table that contains the remaining quantity of gifts you want to exchange, and a table that tracks the valid points you have for gift exchange. When exchanging a gift, you need to retrieve the remaining quantity and your points, perform multiple complex validations, and it takes a long time before you can proceed with the exchange. Assuming that in the first request, you retrieve the remaining quantity as 1, and in the second request immediately after, you also retrieve the remaining quantity as 1. What would happen if both requests continue with the gift exchange? To prevent this, you can use Row-Level Locks. This means that when one transaction is retrieving the remaining quantity to start validation, the second transaction has to wait until the first transaction finishes.

Deadlock

Deadlock occurs when two or more transactions hold locks that the other needs. For example, the first transaction acquires an ACCESS EXCLUSIVE lock on table A and then wants to acquire that lock on table B, while the second transaction already holds an ACCESS EXCLUSIVE lock on table B and now wants that lock on table A. In this case, no transaction can proceed. PostgreSQL automatically detects this situation and resolves it by aborting one of the related transactions, but it is unpredictable which transaction will be aborted.

Deadlock can also occur with row-level locks when transactions hold each other's locks.

The best defense against deadlocks in general is to avoid having commands acquire locks on multiple objects (tables/rows) in a specified order.

If a transaction operates on multiple related objects, it is best to keep the transaction duration as short as possible. Avoid holding transactions for long periods. For example, a bad practice is to execute a transaction and wait for user input.

Conclusion

In this article, I have presented how PostgreSQL uses locks to manage concurrent access to data in tables. Although specific to PostgreSQL, the concept of using locks still applies to other SQL databases such as MySQL and SQL Server. Understanding how locks work will help you handle situations in application development with SQL more effectively.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (1)

Leave a comment...
Avatar
Trần Ngọc Hải2 years ago
Bài viết quá hay quá chi tiết cảm ơn tác giả
Reply
Scroll or click to go to the next page