Lock size

The size (sometimes scope or level) of a lock on data in a table describes the amount of data that is controlled by the lock. The same piece of data can be controlled by locks of different sizes.

Begin program-specific programming interface information.
DB2® uses locks of the following sizes:

  • Table space
  • Table
  • Partition
  • Page
  • Row
  • LOB
  • XML

A table space lock (the largest size) controls the most data, all the data in an entire table space. A page or row lock controls only the data in a single page or row.

As the following figure suggests, row locks and page locks occupy an equal place in the hierarchy of lock sizes.
Figure 1. Sizes of objects locked
Begin figure description. The figure uses five hierarchies to show the relative sizes of objects that are locked. End figure description.
Locking larger or smaller amounts of data allows you to trade performance for concurrency. Using page or row locks instead of table or table space locks has the following effects:
  • Concurrency usually improves, meaning better response times and higher throughput rates for many users.
  • Processing time and use of storage increases. That is especially evident in batch processes that scan or update a large number of rows.
Using only table or table space locks has the following effects:
  • Processing time and storage usage is reduced.
  • Concurrency can be reduced, meaning longer response times for some users but better throughput for one user.

Lock sizes and table space type

DB2 uses different lock sizes depending on the type of table spaces where the locks are acquired.

Partitioned and universal table space
In a partitioned table space or universal table space, locks are obtained at the partition level. Individual partitions are locked when necessary, as they are accessed. Gross locks (S, U, or X) can be obtained on individual partitions instead of on the entire partitioned table space.
Restriction: If one of the following conditions is true, DB2 must lock all partitions:
  • The table space is defined with LOCKSIZE TABLESPACE.
  • The LOCK TABLE statement is used without the PART option.
Segmented table space
In a segmented table space without partitions, rows from different tables are contained in different pages. Locking a page does not lock data from more than one table. Also, DB2 can acquire a table lock, which locks only the data from one specific table. Because a single row, of course, contains data from only one table, the effect of a row lock is the same as for a simple or partitioned table space: it locks one row of data from one table.
Simple table space
DB2 no longer supports the creation of simple table spaces. However, an existing simple table space can contain more than one table. A lock on the table space locks all the data in every table. A single page of the table space can contain rows from every table. A lock on a page locks every row in the page, no matter what tables the data belongs to. Thus, a lock needed to access data from one table can make data from other tables temporarily unavailable. That effect can be partly undone by using row locks instead of page locks.
LOB table space
In a LOB table space, pages are not locked. Because the concept of rows does not occur in a LOB table space, rows are not locked. Instead, LOBs are locked.
XML table space
In an XML table space, XML locks are acquired.

Example: simple versus segmented table spaces

Suppose that tables T1 and T2 reside in table space TS1. In a simple table space, a single page can contain rows from both T1 and T2. If User 1 and User 2 acquire incompatible locks on different pages, such as exclusive locks for updating data, neither can access all the rows in T1 and T2 until one of the locks is released. (User 1 and User 2 can both hold a page lock on the same page when the mode of the locks are compatible, such as locks for reading data.)

As the figure also shows, in a segmented table space, a table lock applies only to segments assigned to a single table. Thus, User 1 can lock all pages assigned to the segments of T1 while User 2 locks all pages assigned to segments of T2. Similarly, User 1 can lock a page of T1 without locking any data in T2.

Figure 2. Page locking for simple and segmented table spaces
Begin figure description. Four diagrams that compare the locking of data pages in simple and segmented table spaces. End figure description.
End program-specific programming interface information.