Questions tagged [locking]

A mechanism for managing concurrent access to shared data or resources by granting temporarily exclusive access to the processes requesting the lock.

Any operation that involves more than a single CPU instruction is inherently non-atomic. In other words, it can be interrupted before it is finished. Interruting such an operation can leave data in an inconsistent state.

If another process starts using the inconsistent data

  • that data can be corrupted
  • the inconsistent data can cause the process to work incorrectly or crash.

Locking allows one process to request temporary exclusive access to the data so it can make a change and complete that change consistently before it relinquishes the lock.

Row-Level Locking

A lock can be shared or exclusive. A shared lock allows multiple processes to read the data, but will block any process requesting exclusive access to the data until the processes holding shared (read-only) locks on the data release those locks.

An exclusive (write) lock blocks any process requesting a lock (either shared or exclusive) until the exclusive lock is released. The process holding the exclusive lock can complete its update before it releases the lock. If other processes hold read locks the process requesting the exclusive lock will block until the read locks are released.

Some database systems provide a facility called Multi-Version Concurrency Control (MVCC) for database writes. A system that supports MVCC will write the new version of the data item (holding a lock on that record) and then dispose of the old record. Other processes can read the old record while the new one is being created, but processes requesting a write lock on the record will block until the transaction holding the lock has committed.

MVCC allows better concurrent access, as a non-MVCC system will block reads until the write as committed, whereas readers can see the previous version of the record in a MVCC system while the write operation is still in progress.

Table-Level Locking

At times, there may be queries that

  • update or delete a large number of rows
  • alter a tables structure
  • come in such sheer numbers that the table is essentially treated as a FIFO queue

Some databases provide storage engines that does this by default, such as the MyISAM storage engine for MySQL. More robust databases may require perform explicit table locks. In a highly transactional, doing full table locking should be the exception rather than the norm.

889 questions
69
votes
1 answer

How do I swap tables in MySQL?

Suppose, I have a table foo, which contains some statistics that are computed every now and then. It is heavily used by other queries. That's why I want to compute more recent statistics in foo_new and swap them when computation is ready. I could…
Ben
  • 887
  • 1
  • 7
  • 9
63
votes
4 answers

What is lock escalation?

I was asked this question at an interview and had no answer. Can anyone here explain?
Kilhoffer
48
votes
7 answers

How to determine if an Oracle table is locked or not?

We've been using BI software and a repository database that are installed on Oracle Enterprise 11gR2. Some of these batch reports will try to access a database table which may still be locked. How can I find out if an Oracle table is locked or not?…
Selahattin
  • 621
  • 1
  • 6
  • 6
42
votes
5 answers

Locking issue with concurrent DELETE / INSERT in PostgreSQL

This is pretty simple, but I'm baffled by what PG does (v9.0). We start with a simple table: CREATE TABLE test (id INT PRIMARY KEY); and a few rows: INSERT INTO TEST VALUES (1); INSERT INTO TEST VALUES (2); Using my favorite JDBC query tool…
DaveyBob
  • 561
  • 2
  • 5
  • 4
35
votes
4 answers

Is NOLOCK always bad?

I am a Report Developer who wants to make my queries as efficient as possible. I used to work with a DBA who told me - I believe because I was always dealing with reports on a Production Server - to use NOLOCK in every single query. Now, I work…
DataGirl
  • 475
  • 4
  • 6
31
votes
4 answers

Adding columns to production tables

What's the best way to add columns to large production tables on SQL Server 2008 R2? According to Microsoft's books online: The changes specified in ALTER TABLE are implemented immediately. If the changes require modifications of the rows in the…
sh-beta
  • 609
  • 1
  • 5
  • 11
30
votes
6 answers

Can I rely on reading SQL Server Identity values in order?

TL;DR: The question below boils down to: When inserting a row, is there a window of opportunity between the generation of a new Identity value and the locking of the corresponding row key in the clustered index, where an external observer could see…
Fabian Schmied
  • 597
  • 1
  • 5
  • 10
29
votes
3 answers

Managing concurrency when using SELECT-UPDATE pattern

Let's say you have the following code (please ignore that it's awful): BEGIN TRAN; DECLARE @id int SELECT @id = id + 1 FROM TableA; UPDATE TableA SET id = @id; --TableA must have only one row, apparently! COMMIT TRAN; -- @id is returned to the…
ErikE
  • 4,355
  • 4
  • 29
  • 39
25
votes
3 answers

How do I cancel an app lock request in SQL Server?

The sp_getapplock stored procedure has the following return values: 0: The lock was successfully granted synchronously. 1: The lock was granted successfully after waiting for other incompatible locks to be released. -1: The lock request timed…
Heinzi
  • 3,210
  • 2
  • 32
  • 43
23
votes
3 answers

Justify NOT using (nolock) hint in every query

Have you ever had to justify NOT using a query hint? I am seeing WITH (NOLOCK) in every single query that hits a very busy server. It is to the point that the developers think it should just be on by default because they hate seeing it in their…
datagod
  • 7,141
  • 4
  • 38
  • 58
22
votes
2 answers

Locking in Postgres for UPDATE / INSERT combination

I have two tables. One is a log table; another contains, essentially, coupon codes that can only be used once. The user needs to be able to redeem a coupon, which will insert a row into the log table and mark the coupon as used (by updating the used…
Rob Miller
  • 323
  • 1
  • 2
  • 6
21
votes
2 answers

What is blocking and how does it happen?

I tried to find some information about blocking in SQL Server, but I could not find a concise explanation for what it is and how it happens. Could you please enlighten me?
jrara
  • 5,393
  • 20
  • 58
  • 65
21
votes
6 answers

In Sql Server, is there a way to check if a selected group of rows are locked or not?

We are attempting to update/delete a large number of records in a multi-billion row table. Since this is a popular table, there is a lot of activity in different sections of this table. Any large update/delete activity is being blocked for extended…
ToC
  • 727
  • 1
  • 8
  • 20
20
votes
2 answers

Detecting the locked table or row in SQL Server

I'm trying to understand/learn how to track down the details of a blocked session. So I created the following setup: create table foo (id integer not null primary key, some_data varchar(20)); insert into foo values (1, 'foo'); commit; Now I…
user1822
20
votes
5 answers

What is the exact relationship between a database transaction and locking?

This is a humble question asked in the spirit of increasing my knowledge; kindly be gentle in your response. As a long-time application developer, I know at some level what a transaction is (I use them all the time). Leaving aside transaction…
Laird Nelson
  • 303
  • 2
  • 6
1
2 3
59 60