如何在PostgreSQL中锁定多个表行?

发布于 2025-01-24 16:39:12 字数 551 浏览 0 评论 0原文

我有两个桌子,我将在下面留下创建表语句。

我想知道,当某人在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倥絔 2025-01-31 16:39:12

自动捕获的唯一锁是键共享 在货币上的行,如果将行插入crurine> Currency> Currency_details

除此之外,您还可以编写一个触发器,该触发器在另一个表上执行适当的SELECT ... for Update

但这是一个不寻常的要求;也许真的不需要锁定这些行。

The only locks that are taken automatically is a FOR KEY SHARE row lock on currency if you insert rows into currency_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文