gpt4 book ai didi

ruby-on-rails-3 - 如何在更新时锁定PostgreSQL中的某些行并让其他行读取?

转载 作者:行者123 更新时间:2023-11-29 12:47:20 25 4
gpt4 key购买 nike

我想更新表中的一些行,我想锁定它们,这样其他进程就不会同时更新它们。但我希望其他进程能够读取它们。

我尝试实现它的方式是这样的:

MyTable.where(mycolumn: "some value").lock(true).update_all(mycolumn: "other value")

我认为 lock(true) 正在锁定选定的行(这是文档所说的),但我不知道它们是否可同时用于其他选择。所以我认为我应该强制使用“共享模式”。

我正在尝试这个:

MyTable.where(mycolumn: "some value").lock("LOCK IN SHARE MODE").update_all(mycolumn: "other value")

但它引发了这个错误:

STATEMENT:  SELECT  "my_table".* FROM "my_table"  WHERE "my_table"."mycolumn" = $1 LIMIT 1 LOCK IN SHARE MODE
ERROR: syntax error at or near "LOCK" at character 74

虽然它适用于 SQLite。

最佳答案

I want to update some rows in a table, and I want to lock them so other processes don't update them as well at the same time. But I'd like other processes to read them.

这是自动保证的。看起来您正在尝试解决不存在的问题:-)

在第一次更新完成之前,其他进程不会更新。

在 postgres 中,读取永远不会被写入阻塞。

关于ruby-on-rails-3 - 如何在更新时锁定PostgreSQL中的某些行并让其他行读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12960132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com