如何在PostgreSQL中锁定多个表行?
我有两个桌子,我将在下面留下创建表语句。
我想知道,当某人在 Currency 上修改一行时,是否有可能将相应的行锁定在 Currency_details 中。
CREATE TABLE IF NOT EXISTS currency (
id VARCHAR(8),
name VARCHAR(20),
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS currency_details (
id VARCHAR(8) REFERENCES currency ON DELETE CASCADE,
info VARCHAR(80),
PRIMARY KEY (id)
);
I have two tables, i will leave the create table statements for both of them below.
I would like to know if it is possible to lock the corresponding rows in currency_details when someone is modifying a row on currency and vice versa.
CREATE TABLE IF NOT EXISTS currency (
id VARCHAR(8),
name VARCHAR(20),
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS currency_details (
id VARCHAR(8) REFERENCES currency ON DELETE CASCADE,
info VARCHAR(80),
PRIMARY KEY (id)
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自动捕获的唯一锁是键共享 在
货币
上的行,如果将行插入crurine> Currency> Currency_details
。除此之外,您还可以编写一个触发器,该触发器在另一个表上执行适当的
SELECT ... for Update
。但这是一个不寻常的要求;也许真的不需要锁定这些行。
The only locks that are taken automatically is a
FOR KEY SHARE
row lock oncurrency
if you insert rows intocurrency_details
.Other than that, you could write a trigger that performs an appropriate
SELECT ... FOR UPDATE
on the other table.But that is an unusual requirement; perhaps it is not really necessary to lock those rows.