The locking system controls locking by doing the following:
When you either issue a statement or a command or perform the equivalent operation using VDBA, implicit requests for locks are made.
In addition, the locking system considers the following factors to determine what mode and level of lock, if any, to take:
When the system administrator configures the logging and locking system, the total number of available locks is set. As each lock is used, a counter is decremented to reflect the number of locks still available. If a lock request is received after all available locks have been used, the request cannot be satisfied until a lock is freed.
If this happens frequently, your system administrator can reconfigure the maximum number of locks. For details, see the System Administrator Guide.
Whether the locking system can grant a lock depends on whether another transaction holds a lock on the resource requested and, if so, what mode of lock that other transaction holds. If another transaction already holds an exclusive lock on the resource in question, a new lock cannot be used. The second request must wait.
The locking system uses intended shared and intended exclusive locks on a table to determine quickly whether a table-level lock can be used on that table, as follows:
The following table shows which granted lock modes are compatible with the requested mode:
|
Granted Mode |
|||||||
|---|---|---|---|---|---|---|---|---|
Req. Mode |
NL |
IS |
IX |
S |
SIX |
U |
X |
|
NL |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
|
IS |
Yes |
Yes |
Yes |
Yes |
Yes |
No |
No |
|
IX |
Yes |
Yes |
Yes |
No |
No |
No |
No |
|
S |
Yes |
Yes |
No |
Yes |
No |
No |
No |
|
SIX |
Yes |
Yes |
No |
No |
No |
No |
No |
|
U |
Yes |
No |
No |
Yes |
No |
No |
No |
|
X |
Yes |
No |
No |
No |
No |
No |
No |
|
For meaning of the lock mode abbreviations, see Lock Modes.
The locking system determines the default lock mode as follows:
The default state of the locking system ensures that no user can read data being changed and no user can change data being read. However, users can read data that is being read by other users. This means that the locking system can grant an:
This default strategy is adequate for most situations. When it is not, you can establish a different strategy using the set lockmode statement. For details, see User-Controlled Locking.
By default, Ingres determines the best locking level to use. Ingres selects the locking level as page or table, depending on the optimizer's estimates of the number of pages to be read.
If the estimated number of pages to be read is greater than the session maxlocks limit, or if the entire table is to be read, Ingres uses table level locking. Otherwise, Ingres uses page level locking, and then escalates to table level locking if the number of page locks requested exceeds the session maxlocks per table, per query limit.
In evaluating the query on which the lock is being requested, the Query Optimizer determines the level of lock as follows:
This strategy saves the overhead of accumulating multiple page-level locks and prevents the contention caused by lock escalation. For example, on a query that is not restrictive or does not use a key to locate affected records, the locking system grants a table-level lock at the beginning of query execution.
When page locking, if the number of pages in a table on which locks are held reaches maxlocks during a query, the locking system escalates to table-level locks. To do this it:
The locking system also escalates to table-level locks in an attempt to complete a transaction if it exceeds the maximum number of locks allowed or the installation has run out of locks. If this occurs, an error is issued and the transaction is backed out. To avoid this situation in the future, the system administrator can bring down the installation and reconfigure the locking system.
Note: The issuing of lock escalation messages is configurable.
The following methods can be used to change how Ingres handles locking:
By default, system_lock_level is set to DEFAULT, in which Ingres decides the locking level. For details on the default behavior, see How the Locking Level is Determined. Other valid values for system_lock_level are ROW, PAGE, and TABLE. Each of the default lock levels is subject to escalation. For example, if system_lock_level is set to PAGE, the default locking level is page, and then Ingres escalates to a table-level lock if the number of page locks requested exceeds the session maxlocks per table, per query limit.
Note: The set lockmode statement cannot be issued in a transaction.
For details, see User-Controlled Locking.
The following table describes what mode and level of lock is invoked by default when a query is issued.
Statement or Command |
Comment |
Mode |
Level |
|---|---|---|---|
create index |
On base table: |
X |
Table lock |
create rule |
On base table: |
X |
Table lock |
create table |
On table: |
X |
Table lock |
create view |
On view: On base table: |
X X |
Table lock Table lock |
drop |
On table: |
X |
Table lock |
grant |
On base table: |
X |
Table lock |
modify |
On table: |
X |
Table lock |
select |
For each table involved in the select:
If query touches 50 (or maxlocks) pages: |
IS S S |
Table lock Control lock Table lock |
sysmod |
On database: |
X |
Database lock |
update, insert, or delete |
On table involved in update, insert, or delete: If query touches 50 (or maxlocks) pages: On other tables used in query but not being changed: |
IX X S |
Table lock Table lock See lock for select statement |
A transaction accumulates locks on resources until you commit or roll back. When a transaction is committed, the results are written to the database, and all the locks accumulated during the transaction are released. A rollback aborts the transaction and releases accumulated locks.
You commit transactions during a session by doing one of the following:
For details on the set statement using the autocommit parameter, see the SQL Reference Guide.
After a commit is executed, the current transaction is terminated and you are in a new transaction as soon as the next SQL statement is issued.
All open transactions are automatically committed when you end your session.
Important! If you do not issue the commit statement during a session when the set autocommit is off, all locks requested on the resources affected by your queries are held until your session ends. Your entire session is treated like one transaction and can cause concurrency problems.