简体   繁体   English

在SELECT中嵌入pg_advisory_lock与显式使用另一个语句有什么好处?

[英]What is the benefit of embedding pg_advisory_lock inside SELECT vs explicitly using another statement?

I cannot really understand the example from https://www.postgresql.org/docs/9.1/static/explicit-locking.html 我无法真正理解来自https://www.postgresql.org/docs/9.1/static/explicit-locking.html的示例

where they embed the lock along with the SELECT clause SELECT pg_advisory_lock(id) FROM foo WHERE id = 12345; -- ok 它们与SELECT子句一起嵌入锁定SELECT pg_advisory_lock(id) FROM foo WHERE id = 12345; -- ok SELECT pg_advisory_lock(id) FROM foo WHERE id = 12345; -- ok

What is it selecting from FOO ? FOO选择什么? I would understand better if it is like 如果是这样的话,我会更好理解

SELECT pg_advisory_lock(123); //lock
SELECT * FROM foo WHERE id = 12345;

where it is explicitly locking the block. 它显式锁定块的位置。 I can't seem to find explanation on how to really use advisory lock anywhere that explains the differnece between both Embedding and Explicitly on its own statement. 我似乎无法找到解释如何在任何地方真正使用咨询锁定来解释嵌入和明确地在其自己的陈述之间的差异。

Doing SELECT pg_advisory_lock(123); 执行SELECT pg_advisory_lock(123); will create a lock on 123 , whether it is a valid value or not. 将在123上创建一个锁,无论它是否是有效值。 Doing SELECT pg_advisory_lock(id) FROM foo WHERE id = 123; 执行SELECT pg_advisory_lock(id) FROM foo WHERE id = 123; will create the lock only if there is an entry for ID 123 in table foo. 只有在表foo中有ID 123的条目时才会创建锁。

Let's note the line found in the pg_locks doc : 让我们注意pg_locks文档中的行

The actual meaning of the keys is up to the user 密钥的实际含义取决于用户

which tends to imply that the select/from/where syntax is for associating the lock to an existing row, while the lone select syntax is for a broader meaning, like an application-wide lock. 这倾向于暗示select / from / where语法用于将锁与现有行相关联 ,而单独选择语法用于更广泛的含义,例如应用程序范围的锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM