简体   繁体   English

使用Postgres和GO强制“锁定”

[英]Force a “lock” with Postgres and GO

I am new to Postgres so this may be obvious (or very difficult, I am not sure). 我是Postgres的新手,所以这可能很明显(或者非常困难,我不确定)。

I would like to force a table or row to be "locked" for at least a few seconds at a time. 我想强制一次将表或行“锁定”至少几秒钟。 Which will cause a second operation to "wait". 这将导致第二个操作“等待”。

I am using golang with "github.com/lib/pq" to interact with the database. 我正在将golang与“ github.com/lib/pq”一起使用来与数据库进行交互。

The reason I need this is because I am working on a project that monitors postgresql. 我需要这个的原因是因为我正在研究一个监视postgresql的项目。 Thanks for any help. 谢谢你的帮助。

You can also use select ... for update to lock a row or rows for the length of the transaction. 您也可以使用select ...进行更新,以锁定事务长度的一行或多行。

Basically, it's like: 基本上,它就像:

begin;
select * from foo where quatloos = 100 for update;
update foo set feens = feens + 1 where quatloos = 100;
commit;

This will execute an exclusive row-level lock on foo table rows where quatloos = 100. Any other transaction attempting to access those rows will be blocked until commit or rollback has been issued once the select for update has run. 这将在quatloos = 100的foo表行上执行排他级锁。尝试访问这些行的任何其他事务将被阻塞,直到选择更新运行后发出了提交或回滚。

Ideally, these locks should live as short as possible. 理想情况下,这些锁的寿命应尽可能短。

See: https://www.postgresql.org/docs/current/static/explicit-locking.html 参见: https : //www.postgresql.org/docs/current/static/explicit-locking.html

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

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