简体   繁体   中英

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. I know that postgres provide some lock on table, but i didn't find any docs how to setup it.

Possible some standard solution already exist for that?

Yes, Postgres provides locking mechanism at table level. You can lock the table using SQL and then unlock it when your job is done.

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. When one machine uses it, you can make it TRUE and then toggle as per your requirement. 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>
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>

More seriously: Table-level locks are done with LOCK TABLE statements; see Ved's answer. There are also row-level locks ( SELECT ... FOR [KEY] UPDATE|SHARE ) and optimistic predicate locking in SERIALIZABLE isolation. You can also implement optimistic concurrency control (see 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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