Postgresql 中文操作指南
Synopsis
LOCK [ TABLE ] [ ONLY ] name [ * ] [, ...] [ IN lockmode MODE ] [ NOWAIT ]
where lockmode is one of:
ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE
| SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE
Description
LOCK TABLE 获取表级锁定,在必要时等待任何冲突锁释放。如果指定了 NOWAIT , LOCK TABLE 不会等待获取所需的锁:如果不能立即获取,则会中止命令并发出错误。获取后,锁将保持在当前事务的剩余时间内。(没有 UNLOCK TABLE 命令;锁总是会在事务结束时释放。)
LOCK TABLE obtains a table-level lock, waiting if necessary for any conflicting locks to be released. If NOWAIT is specified, LOCK TABLE does not wait to acquire the desired lock: if it cannot be acquired immediately, the command is aborted and an error is emitted. Once obtained, the lock is held for the remainder of the current transaction. (There is no UNLOCK TABLE command; locks are always released at transaction end.)
锁定视图时,出现在视图定义查询中的所有关系也会以相同的锁定模式递归锁定。
When a view is locked, all relations appearing in the view definition query are also locked recursively with the same lock mode.
为引用表的命令自动获取锁定时,PostgreSQL 始终使用最小限制的锁模式。当需要进行更加严格的锁定时, LOCK TABLE 会提供案例。例如,假设应用程序在 READ COMMITTED 隔离级别运行,并且需要确保表中的数据在事务期间保持稳定。要实现此目的,可以在查询之前获取表上的 SHARE 锁定模式。这将阻止并发数据更改,并确保随后对表的读取看到已提交数据的稳定视图,因为 SHARE 锁定模式与写入者获取的 ROW EXCLUSIVE 锁定冲突,并且您的 LOCK TABLE _name IN SHARE MODE_ 语句将等待任何持有 ROW EXCLUSIVE 模式锁定的并发持有者提交或回滚。因此,一旦获得锁,就没有未提交的写操作待处理;此外,在您释放锁之前,也不能开始任何操作。
When acquiring locks automatically for commands that reference tables, PostgreSQL always uses the least restrictive lock mode possible. LOCK TABLE provides for cases when you might need more restrictive locking. For example, suppose an application runs a transaction at the READ COMMITTED isolation level and needs to ensure that data in a table remains stable for the duration of the transaction. To achieve this you could obtain SHARE lock mode over the table before querying. This will prevent concurrent data changes and ensure subsequent reads of the table see a stable view of committed data, because SHARE lock mode conflicts with the ROW EXCLUSIVE lock acquired by writers, and your LOCK TABLE _name IN SHARE MODE_ statement will wait until any concurrent holders of ROW EXCLUSIVE mode locks commit or roll back. Thus, once you obtain the lock, there are no uncommitted writes outstanding; furthermore none can begin until you release the lock.
要获得与在 REPEATABLE READ 或 SERIALIZABLE 隔离级别下运行事务相同的成效,您必须在执行任何 SELECT 或数据修改语句之前执行 LOCK TABLE 语句。 REPEATABLE READ 或 SERIALIZABLE 事务对数据所获得的视图会在其第一个 SELECT 或数据修改语句开始时冻结。 LOCK TABLE 的事务稍后仍将阻止并发写入,但它不能确保事务所读取的内容与最新的已提交值相符。
To achieve a similar effect when running a transaction at the REPEATABLE READ or SERIALIZABLE isolation level, you have to execute the LOCK TABLE statement before executing any SELECT or data modification statement. A REPEATABLE READ or SERIALIZABLE transaction’s view of data will be frozen when its first SELECT or data modification statement begins. A LOCK TABLE later in the transaction will still prevent concurrent writes — but it won’t ensure that what the transaction reads corresponds to the latest committed values.
如果此类事务要更改表中的数据,那么它应该使用 SHARE ROW EXCLUSIVE 锁模式,而不是 SHARE 模式。这确保了同一时间只会运行一次此类事务。不进行此操作,则可能导致死锁:两个事务都可能获取 SHARE 模式,然后都无法获取 ROW EXCLUSIVE 模式以实际执行更新。(请注意,事务自身锁之间不会冲突,因此当事务持有 SHARE 模式时,它可以获取 ROW EXCLUSIVE 模式,但如果其他人持有 SHARE 模式,则不行。)为了避免死锁,请确保所有事务按照相同的顺序获取对相同对象的锁,并且如果涉及一个对象有多个锁模式,那么事务应首先总是获取最严格的模式。
If a transaction of this sort is going to change the data in the table, then it should use SHARE ROW EXCLUSIVE lock mode instead of SHARE mode. This ensures that only one transaction of this type runs at a time. Without this, a deadlock is possible: two transactions might both acquire SHARE mode, and then be unable to also acquire ROW EXCLUSIVE mode to actually perform their updates. (Note that a transaction’s own locks never conflict, so a transaction can acquire ROW EXCLUSIVE mode when it holds SHARE mode — but not if anyone else holds SHARE mode.) To avoid deadlocks, make sure all transactions acquire locks on the same objects in the same order, and if multiple lock modes are involved for a single object, then transactions should always acquire the most restrictive mode first.
有关锁模式和锁策略的更多信息,请参见 Section 13.3 。
More information about the lock modes and locking strategies can be found in Section 13.3.
Parameters
-
name
-
The name (optionally schema-qualified) of an existing table to lock. If ONLY is specified before the table name, only that table is locked. If ONLY is not specified, the table and all its descendant tables (if any) are locked. Optionally, * can be specified after the table name to explicitly indicate that descendant tables are included.
-
The command LOCK TABLE a, b; is equivalent to LOCK TABLE a; LOCK TABLE b;. The tables are locked one-by-one in the order specified in the LOCK TABLE command.
-
-
lockmode
-
The lock mode specifies which locks this lock conflicts with. Lock modes are described in Section 13.3.
-
If no lock mode is specified, then ACCESS EXCLUSIVE, the most restrictive mode, is used.
-
-
NOWAIT
-
Specifies that LOCK TABLE should not wait for any conflicting locks to be released: if the specified lock(s) cannot be acquired immediately without waiting, the transaction is aborted.
-
Notes
若要锁定表,用户必须具有指定 lockmode 的正确权限,或者作为该表的拥有者或超级用户。如果用户对该表拥有 UPDATE 、 DELETE 或 TRUNCATE 权限,则允许任何 lockmode 。如果用户对该表拥有 INSERT 权限,则允许 ROW EXCLUSIVE MODE (或 Section 13.3 中描述的非冲突模式)。如果用户对该表拥有 SELECT 权限,则允许 ACCESS SHARE MODE 。
To lock a table, the user must have the right privilege for the specified lockmode, or be the table’s owner or a superuser. If the user has UPDATE, DELETE, or TRUNCATE privileges on the table, any lockmode is permitted. If the user has INSERT privileges on the table, ROW EXCLUSIVE MODE (or a less-conflicting mode as described in Section 13.3) is permitted. If a user has SELECT privileges on the table, ACCESS SHARE MODE is permitted.
在视图上执行锁定操作的用户必须对该视图拥有相应的权限。此外,默认情况下,视图的拥有者必须对底层基关系拥有相关权限,而执行锁定操作的用户无需对底层基关系有任何权限。但是,如果视图已将 security_invoker 设为 true (请参见 CREATE VIEW ),则执行锁定操作的用户,而不是视图的拥有者,必须对底层基关系拥有相关权限。
The user performing the lock on the view must have the corresponding privilege on the view. In addition, by default, the view’s owner must have the relevant privileges on the underlying base relations, whereas the user performing the lock does not need any permissions on the underlying base relations. However, if the view has security_invoker set to true (see CREATE VIEW), the user performing the lock, rather than the view owner, must have the relevant privileges on the underlying base relations.
LOCK TABLE 在事务块外部无用:锁只会在语句完成之前一直保持。因此,如果 LOCK 在事务块外部使用,PostgreSQL 将报告错误。使用 BEGIN 和 COMMIT (或 ROLLBACK )定义事务块。
LOCK TABLE is useless outside a transaction block: the lock would remain held only to the completion of the statement. Therefore PostgreSQL reports an error if LOCK is used outside a transaction block. Use BEGIN and COMMIT (or ROLLBACK) to define a transaction block.
LOCK TABLE 只处理表级锁,因此 涉及 ROW 的模式名都是错误的。一般应将这些模式名视为表明用户意在获取所锁表中的行级锁。另外, ROW EXCLUSIVE 模式是一种可共享表锁。请注意,就 LOCK TABLE 而言,所有锁模式的语义都相同,只有关于哪些模式与哪些模式冲突的规则不同。有关如何获取实际行级锁的信息,请参见 SELECT 文档中的 Section 13.3.2 和 The Locking Clause 。
LOCK TABLE only deals with table-level locks, and so the mode names involving ROW are all misnomers. These mode names should generally be read as indicating the intention of the user to acquire row-level locks within the locked table. Also, ROW EXCLUSIVE mode is a shareable table lock. Keep in mind that all the lock modes have identical semantics so far as LOCK TABLE is concerned, differing only in the rules about which modes conflict with which. For information on how to acquire an actual row-level lock, see Section 13.3.2 and The Locking Clause in the SELECT documentation.
Examples
当要对外部键表执行插入操作时,获取主键表的 SHARE 锁:
Obtain a SHARE lock on a primary key table when going to perform inserts into a foreign key table:
BEGIN WORK;
LOCK TABLE films IN SHARE MODE;
SELECT id FROM films
WHERE name = 'Star Wars: Episode I - The Phantom Menace';
-- Do ROLLBACK if record was not returned
INSERT INTO films_user_comments VALUES
(_id_, 'GREAT! I was waiting for it for so long!');
COMMIT WORK;
当要执行删除操作时,获取主键表的 SHARE ROW EXCLUSIVE 锁:
Take a SHARE ROW EXCLUSIVE lock on a primary key table when going to perform a delete operation:
BEGIN WORK;
LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;
DELETE FROM films_user_comments WHERE id IN
(SELECT id FROM films WHERE rating < 5);
DELETE FROM films WHERE rating < 5;
COMMIT WORK;
Compatibility
SQL 标准中没有 LOCK TABLE ,而是使用 SET TRANSACTION 指定事务的并发级别。PostgreSQL 也支持该标准;请参见 SET TRANSACTION 了解更多详情。
There is no LOCK TABLE in the SQL standard, which instead uses SET TRANSACTION to specify concurrency levels on transactions. PostgreSQL supports that too; see SET TRANSACTION for details.
除了 ACCESS SHARE 、 ACCESS EXCLUSIVE 和 SHARE UPDATE EXCLUSIVE 锁模式,PostgreSQL 锁模式和 LOCK TABLE 语法与 Oracle 中的锁模式和语法兼容。
Except for ACCESS SHARE, ACCESS EXCLUSIVE, and SHARE UPDATE EXCLUSIVE lock modes, the PostgreSQL lock modes and the LOCK TABLE syntax are compatible with those present in Oracle.