简体   繁体   English

PostgreSQL中的表锁

[英]Table lock in PostgreSQL

I'm working on a application which should get data from a one table and record a result of processing in another table. 我正在开发一个应从一个表中获取数据并在另一表中记录处理结果的应用程序。 All this in more that one thread and on a few computers. 所有这些都在一个线程和几台计算机上完成。 So I need to use some synchronize mechanism for that. 因此,我需要为此使用一些同步机制。 As far as I need to use more that one computers I have to use some sync mechanism in DB (in my case it's PostgresSQL) instead of use lock in code. 至于我需要使用更多的一台计算机,我必须在数据库中使用某种同步机制(在我的情况下是PostgresSQL),而不是使用代码锁定。 I know that postgres provide some lock on table, but i didn't find any docs how to setup it. 我知道postgres提供了对表的锁定,但是我没有找到任何文档来设置它。

Possible some standard solution already exist for that? 可能已经有一些标准的解决方案了吗?

Yes, Postgres provides locking mechanism at table level. 是的,Postgres在表级别提供了锁定机制。 You can lock the table using SQL and then unlock it when your job is done. 您可以使用SQL锁定表,然后在完成工作后将其解锁。

See the explicit locking chapter of the manual for details. 有关详细信息,请参见手册的显式锁定章节

BUT considering your case, you can achieve the same using some simple mechanism like, adding one more column to table - locking_status boolean. 但是考虑到您的情况,您可以使用一些简单的机制来实现相同的目的,例如,向表中增加一列-locking_status布尔值。 When one machine uses it, you can make it TRUE and then toggle as per your requirement. 一台机器使用它时,可以将其设置为TRUE,然后根据需要进行切换。 If you have more than 1 tables, then however, locking of table is only option. 但是,如果您有多个表,那么锁定表是唯一的选择。

You also need to take care of highly scalable multi-threaded program because, it is possible that, one application has locked the table and then that machine may go down. 您还需要照顾高度可扩展的多线程程序,因为一个应用程序可能已锁定了表,然后该计算机可能会关闭。 In that case, your table remains locked and application simply become non-responsive. 在这种情况下,您的表将保持锁定状态,应用程序将变得无响应。 (depends however on how you have handled this state). (但是取决于您如何处理此状态)。 In this case, some sort of expire mechanism may help which will unlock the table after particular amount of time. 在这种情况下,某种过期机制可能会有所帮助,它将在特定时间后解锁表。

Hope this helps. 希望这可以帮助。

<clippy> <clippy>
It looks like you're trying to write a task queue or message queue . 看来您正在尝试编写任务队列消息队列 These are really hard to get right. 这些真的很难解决。 Would you like me to suggest an existing well-tested implementation for you to use instead? 您是否希望我提出一个经过良好测试的现有实现方案供您使用?
</clippy> </ clippy>

More seriously: Table-level locks are done with LOCK TABLE statements; 更严重的是:表级锁定是通过LOCK TABLE语句完成的; see Ved's answer. 参见韦德的答案。 There are also row-level locks ( SELECT ... FOR [KEY] UPDATE|SHARE ) and optimistic predicate locking in SERIALIZABLE isolation. 还存在行级锁( SELECT ... FOR [KEY] UPDATE|SHARE )和SERIALIZABLE隔离中的乐观谓词锁。 You can also implement optimistic concurrency control (see Wikipedia). 您还可以实现乐观并发控制(请参阅Wikipedia)。

However, it's really hard to actually make a concurrent task or message queue. 但是,实际上很难创建并发任务或消息队列。 Most solutions you come up with will land up actually being serialized so only one task can run at once, or will fail to handle tasks that abort/crash, etc. 您提出的大多数解决方案实际上都会序列化,因此一次只能执行一个任务,否则将无法处理中止/崩溃等任务。

See: 看到:

and look into tools like Celery, ZeroMQ, ActiveMQ, RabbitMQ, Octobot, etc. See http://queues.io/ 并研究诸如Celery,ZeroMQ,ActiveMQ,RabbitMQ,Octobot等工具。请参见http://queues.io/

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

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