简体   繁体   English

PostgreSQL中的咨询锁范围

[英]Advisory locks scope in postgresql

I am new to postgresql and trying to understand advisory locks. 我是Postgresql的新手,并试图了解咨询锁。 I have the following two scenarios: 我有以下两种情况:

  1. With different databases in two different sessions: (Works in expected manner) 在两个不同的会话中使用不同的数据库:(按预期方式工作)

    Session 1: SELECT pg_advisory_lock(1); 会话1: SELECT pg_advisory_lock(1); Successfully acquires the lock 成功获得锁

    Session 2 (note in different database): SELECT pg_advisory_lock(1); 会话2(在不同数据库中的注释): SELECT pg_advisory_lock(1); Successfully acquires the lock 成功获得锁

  2. With Different Schemas in same database: When I do the same operation, the second 'session' blocks. 在同一数据库中使用不同的架构:当我执行相同的操作时,第二个“会话”将阻塞。

It appears that advisory locks operate at database level rather than (database and schema) combination. 咨询锁似乎在数据库级别而不是(数据库和架构)组合上运行。 Is my assumption correct or is there anything I am missing? 我的假设是正确的还是我缺少什么?

In postgres schema is a namespace. 在postgres模式中是一个名称空间。 More than just a prefix, but less than another database. 不仅仅是前缀,但少于另一个数据库。 in your case two, second session not "blocks", but rather is waiting as per docs : 在您的情况下,第二个会话不是“阻塞”,而是按照docs等待:

If another session already holds a lock on the same resource identifier, this function will wait until the resource becomes available. 如果另一个会话已经在同一资源标识符上持有锁,则此功能将等待直到资源可用为止。

Regarding successful locking on different databases: After you run SELECT pg_advisory_lock(1); 关于成功锁定不同数据库:运行SELECT pg_advisory_lock(1); checkout pg_locks , column objid 检出pg_locks列objid

OID of the lock target within its system catalog, or null if the target is not a general database object 锁定目标在其系统目录中的OID;如果目标不是通用数据库对象,则为null

So this number is per database - you can reference same 1 for many databases - those will be different OIDs. 因此,此数字是每个数据库的-您可以为多个数据库引用相同的1-这些将是不同的OID。

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

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