WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PostgreSQL · Intermediate · question 27 of 100

What are the different types of locking mechanisms in PostgreSQL and how do they work?

📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

PostgreSQL provides several types of locking mechanisms that allow for safe and concurrent access to a database. Let’s dive into each type of lock and how they work in detail.

1. Row-level locking:
As the name suggests, this type of locking mechanism works at the row level. It grants exclusive access to a particular row in a table and locks it for other transactions. This lock is only released when the transaction that placed the lock is committed or rolled back.

For example, let’s say that a table ‘books‘ has multiple rows, and two transactions started executing simultaneously, and the first transaction executed the following SQL statement "`SELECT * FROM books WHERE id = 1 FOR UPDATE‘." This statement will lock the row with ‘id = 1‘ for the first transaction, and the second transaction that tries to access this row will be blocked until the first transaction releases the lock.

Here is how this would look like in Java code:

try (Connection conn = DriverManager.getConnection(url, user, password);
       PreparedStatement stmt = conn.prepareStatement("SELECT * FROM books WHERE id = ? FOR UPDATE")) {
       stmt.setInt(1, 1);
       ResultSet rs = stmt.executeQuery();
       while (rs.next()) {
           // your code goes here
       }
} catch (SQLException e) {
       // handle SQL exception
}

2. Table-level locking:
This type of locking mechanism locks the entire table, and no other transaction can access the table until the lock is released. This lock only releases when the transaction that holds the lock is committed or rolled back.

For example, let’s say there are two transactions that try to access a table ‘books‘, and the first transaction executes "`LOCK TABLE books IN SHARE MODE;‘" statement. This statement locks the entire table in shared mode, allowing other transactions to read data but not modify it. The second transaction that tries to perform any operation on the ‘books‘ table will be blocked until the first transaction releases the lock.

Here is how this would look like in Java code:

try (Connection conn = DriverManager.getConnection(url, user, password);
       PreparedStatement stmt = conn.prepareStatement("LOCK TABLE books IN SHARE MODE")) {
       stmt.executeUpdate();
} catch (SQLException e) {
       // handle SQL exception
}

3. Predicate Locking:
This type of locking mechanism allows you to lock specific rows based on a specific condition mentioned in the SQL statement. PostgreSQL uses the WHERE clause to determine the rows to be locked, and this lock only lasts until the next row in the database is fetched.

For example, let’s say that two transactions are trying to update a table ‘books‘. Transaction A is executing the SQL statement "`UPDATE books SET price = 800 WHERE author = ’JK Rowling’;‘" while transaction B executes "`UPDATE books SET price = 900 WHERE author = ’JK Rowling’;‘." Both the transactions will acquire a predicate lock on all rows with ‘author = ’JK Rowling’‘.

Here is how this would look like in Java code:

try (Connection conn = DriverManager.getConnection(url, user, password);
       PreparedStatement stmt = conn.prepareStatement("UPDATE books SET price = ? WHERE author = ?")) {
       stmt.setInt(1, 800);
       stmt.setString(2, "JK Rowling");
       stmt.executeUpdate();
} catch (SQLException e) {
       // handle SQL exception
}

In conclusion, PostgreSQL provides several types of locking mechanisms to ensure safe and concurrent access to the database, and understanding these locking mechanisms is essential for designing robust and scalable applications.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PostgreSQL interview — then scores it.
📞 Practice PostgreSQL — free 15 min
📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

All 100 PostgreSQL questions · All topics