使用 Postgres 和 GO 强制“锁定”

jsi*_*nni 3 postgresql locking go load-generator

我是 Postgres 新手,所以这可能是显而易见的(或者非常困难,我不确定)。

我想强制一次将表或行“锁定”至少几秒钟。这将导致第二个操作“等待”。

我使用 golang 和“github.com/lib/pq”来与数据库交互。

我需要这个的原因是因为我正在开发一个监视 postgresql 的项目。谢谢你的帮助。

小智 7

您还可以使用 select ... for update 在事务长度内锁定一行或多行。

基本上,就像:

begin;
select * from foo where quatloos = 100 for update;
update foo set feens = feens + 1 where quatloos = 100;
commit;
Run Code Online (Sandbox Code Playgroud)

这将对 foo 表行(其中 quatloos = 100)执行独占行级锁。尝试访问这些行的任何其他事务都将被阻止,直到运行 select for update 后发出提交或回滚。

理想情况下,这些锁的寿命应尽可能短。

请参阅: https: //www.postgresql.org/docs/current/static/explicit-locking.html