何时在PostgreSQL中选择“ Query Aquire exclusivelock和RowexClusivelock”?

发布于 2025-02-10 17:57:01 字数 548 浏览 2 评论 0原文

根据官方文档,选择ShareLock只需要ShareLock ,但我发现我的选择查询获得了独家锁。它是怎么发生的?这是我的选择查询:

select gc.id 
from group_access_strategy ga
  left outer join person_group pg on gp.person_group_id=pg.id 
where gp.id=3

官方文档是我添加了左加入。

According to official documentation, select query only need sharelock, but I found my select query acquired Exclusive lock. How did it happen? Here is my select query:

select gc.id 
from group_access_strategy ga
  left outer join person_group pg on gp.person_group_id=pg.id 
where gp.id=3

what is different from official documentation is that I added left join.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无人问我粥可暖 2025-02-17 17:57:01

您很可能会运行另一个命令,例如Alter Table Person_Group ...(访问独家)或update> update/insert/delete delete (row exclusive)在相同的 Transaction 。锁将一直持续到交易完成或中止。

因此,如果您运行:

BEGIN; --BEGIN starts the transaction
UPDATE group_access_strategy SET column = 'some data' where id = 1;
SELECT
  gc.id,
FROM
  group_access_strategy ga
  LEFT OUTER JOIN person_group pg ON (gp.person_group_id = pg.id)
WHERE
  pg.id = 3

update语句将创建一个行的独家锁定,该锁将在您结束交易之前不会发布:

通过以下方式保存所有更改,因为begin

COMMIT;

将语句的任何效果删除,因为开始

ROLLBACK;

如果您是Postgres的新手,并且通常在PG Admin或DataGrip等IDE中运行查询,则begin> begin/<代码>提示 回滚命令在您单击相应的UI按钮时为您发出。

Most likely you ran another command like ALTER TABLE person_group ... (Access Exclusive) or an UPDATE/INSERT/DELETE (Row exclusive) in the same transaction. Locks will persist until a transaction is completed or aborted.

So if you ran:

BEGIN; --BEGIN starts the transaction
UPDATE group_access_strategy SET column = 'some data' where id = 1;
SELECT
  gc.id,
FROM
  group_access_strategy ga
  LEFT OUTER JOIN person_group pg ON (gp.person_group_id = pg.id)
WHERE
  pg.id = 3

The UPDATE statement would have created a Row Exclusive Lock that will not be released until you end the transaction by:

Saving all of the changes made since BEGIN:

COMMIT;

OR

nullifying any of the effects of statements since BEGIN with

ROLLBACK;

If you're new to Postgres and typically run your queries in an IDE like PG Admin or DataGrip, the BEGIN / COMMIT ROLLBACK commands are issued behind the scenes for you when you click the corresponding UI buttons.

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