The main purpose of locking is to maintain the consistency of database data, which can prevent users from modifying a row or the whole table, which is generally used in databases with high concurrency.
When multiple users visit the database, if there is no control over concurrent operations, incorrect data may be read and stored, and the consistency of the database will be destroyed.
There are two basic types of locks in the database: exclusive locks (Exclusive Locks) and shared locks (Share Locks). If an exclusive lock is added to the data object, it cannot be read or modified by other transactions.
If a shared lock is added, the database object can be read by other transactions but cannot be modified. The basic syntax for LOCK commands is as follows: Name: the name of the existing table to lock (optional schema qualification). If specified only before the table name, only the table is locked. If not specified, the table and all its child tables, if any, are locked. Lock_mode: the locking mode specifies which lock the lock conflicts with. If no locking mode is specified, the most restricted access exclusive mode is used. Possible values are: ACCESS SHARE,ROW SHARE,ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE,SHARE ROW EXCLUSIVE,EXCLUSIVE,ACCESS EXCLUSIVE. Once the lock is acquired, the lock will be held for the rest of the current transaction. There is no unlock table command; the lock is always released at the end of the transaction. A deadlock may occur when two transactions wait for each other to complete their operation. Although PostgreSQL can detect them and end them with a rollback, deadlocks are still inconvenient. To prevent your application from experiencing this problem, make sure that your application is designed to lock objects in the same order. PostgreSQL provides a way to create locks with the meaning of an application definition. These are called consulting locks. Because the system does not force them to be used, it is up to the application to use them correctly. Consulting locks are useful for locking strategies that do not fit the MVCC model. For example, a common use of consulting locks is to simulate the typical pessimistic locking strategy in so-called “flat file” data management systems. Although flags stored in the table can be used for the same purpose, notification locks are faster, avoid table ballooning, and are automatically cleaned up by the server at the end of the session. Create The following example sets the The LOCK statement only works in transactional mode. The above action will result in the following result: The above message indicates that the table is locked until the transaction ends, and to complete the transaction, you must roll back or commit the transaction. 5.39.1. LOCK command syntax ¶
LOCK [ TABLE ]
name
IN
lock_mode
5.39.2. Deadlock ¶
5.39.3. Consulting lock ¶
5.39.4. Example ¶
COMPANY
表( 下载 COMPANY SQL 文件 ), the data are as follows:runoobdb# select * from COMPANY;
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall| 45000
7 | James | 24 | Houston | 10000
(7 rows)
runoobdb
In the database
COMPANY
The table is locked in ACCESS EXCLUSIVE mode.runoobdb=#BEGIN;
LOCK TABLE company1 IN ACCESS EXCLUSIVE MODE;
LOCK TABLE