5.39. PostgreSQL LOCK (lock)

发布时间 : 2025-10-25 13:32:24 UTC      

Page Views: 9 views

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.

5.39.1. LOCK command syntax

The basic syntax for LOCK commands is as follows:

LOCK [ TABLE ] name IN lock_mode 
  • 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.

5.39.2. Deadlock

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.

5.39.3. Consulting lock

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.

5.39.4. Example

Create 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) 

The following example sets the runoobdb In the database COMPANY The table is locked in ACCESS EXCLUSIVE mode.

The LOCK statement only works in transactional mode.

runoobdb=#BEGIN; LOCK TABLE company1 IN ACCESS EXCLUSIVE MODE; 

The above action will result in the following result:

LOCK TABLE 

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.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。