Postgres:如何记录由 Spring JPA 创建的事务的锁
Postgres: How to log down the locks for a transaction that is created by Spring JPA
我有一个从 Java Spring 应用程序进行的操作,它生成如下所示的日志,其中 LOG 之前的数字:是事务 ID (%x) https://www.postgresql.org/docs/13/runtime-config-logging.html
2021-11-08 06:55:26.834 UTC [134] 0 LOG: execute S_4: BEGIN
2021-11-08 06:55:26.836 UTC [134] 0 LOG: execute <unnamed>: /* insert ENTITY1 */
RETURNING *
2021-11-08 06:55:26.881 UTC [134] 19573 LOG: execute <unnamed>: /* insert ENTITY2 */
RETURNING *
2021-11-08 06:55:28.165 UTC [134] 19573 LOG: execute <unnamed>: /* update ENTITY1 */
--Some more things--
2021-11-08 06:55:30.357 UTC [155] 0 LOG: execute <unnamed>: BEGIN
2021-11-08 06:55:30.357 UTC [155] 0 LOG: execute <unnamed>:
RETURNING *
2021-11-08 06:55:30.400 UTC [155] 19576 LOG: execute S_1: COMMIT
2021-11-08 06:55:30.452 UTC [134] 19573 LOG: execute <unnamed>: /* update ENTITY1 */ update ...
2021-11-08 06:55:32.395 UTC [155] 0 LOG: execute <unnamed>: BEGIN READ ONLY
2021-11-08 06:55:32.396 UTC [155] 0 LOG: execute <unnamed>: select ...
2021-11-08 06:55:32.438 UTC [155] 0 LOG: execute S_1: COMMIT
2021-11-08 06:55:32.564 UTC [155] 0 LOG: execute S_2: BEGIN READ ONLY
2021-11-08 06:55:32.565 UTC [155] 0 LOG: execute S_3: select ...
2021-11-08 06:55:32.606 UTC [155] 0 LOG: execute S_1: COMMIT
2021-11-08 06:55:32.648 UTC [155] 0 LOG: execute <unnamed>: select
2021-11-08 06:55:32.864 UTC [134] 19573 LOG: execute S_1: COMMIT
我看到由于某种原因交易 运行 很长时间(几秒钟)
我的问题:
- 鉴于我只插入单个实体并始终使用 WHERE 进行更新,这会导致 table 级别锁定吗?
- 有什么方法可以让我从 Postgres 端或 Java 端注销此操作何时获取哪种类型的锁 - 以及多长时间?
Given that I only do insert single entity and update always with a
WHERE, can this cause table level lock?
如果 where 列中使用的列上没有索引,则以 where 为原因的查询或更新可能需要时间。你也可以试试explain来弄明白。
Is there a way I can log out either from Postgres side or Java side
when and which type of locks is acquired by this operation - and for
how long?
由于您的代码看起来不像是显式获取任何数据库锁 - 锁将在 SQL 数据库引擎执行期间隐式获取。 Postgres 提供了不同的视图来查询锁级别信息。可以参考this and official docs不同的查询从Postgres端获取锁信息。
我有一个从 Java Spring 应用程序进行的操作,它生成如下所示的日志,其中 LOG 之前的数字:是事务 ID (%x) https://www.postgresql.org/docs/13/runtime-config-logging.html
2021-11-08 06:55:26.834 UTC [134] 0 LOG: execute S_4: BEGIN
2021-11-08 06:55:26.836 UTC [134] 0 LOG: execute <unnamed>: /* insert ENTITY1 */
RETURNING *
2021-11-08 06:55:26.881 UTC [134] 19573 LOG: execute <unnamed>: /* insert ENTITY2 */
RETURNING *
2021-11-08 06:55:28.165 UTC [134] 19573 LOG: execute <unnamed>: /* update ENTITY1 */
--Some more things--
2021-11-08 06:55:30.357 UTC [155] 0 LOG: execute <unnamed>: BEGIN
2021-11-08 06:55:30.357 UTC [155] 0 LOG: execute <unnamed>:
RETURNING *
2021-11-08 06:55:30.400 UTC [155] 19576 LOG: execute S_1: COMMIT
2021-11-08 06:55:30.452 UTC [134] 19573 LOG: execute <unnamed>: /* update ENTITY1 */ update ...
2021-11-08 06:55:32.395 UTC [155] 0 LOG: execute <unnamed>: BEGIN READ ONLY
2021-11-08 06:55:32.396 UTC [155] 0 LOG: execute <unnamed>: select ...
2021-11-08 06:55:32.438 UTC [155] 0 LOG: execute S_1: COMMIT
2021-11-08 06:55:32.564 UTC [155] 0 LOG: execute S_2: BEGIN READ ONLY
2021-11-08 06:55:32.565 UTC [155] 0 LOG: execute S_3: select ...
2021-11-08 06:55:32.606 UTC [155] 0 LOG: execute S_1: COMMIT
2021-11-08 06:55:32.648 UTC [155] 0 LOG: execute <unnamed>: select
2021-11-08 06:55:32.864 UTC [134] 19573 LOG: execute S_1: COMMIT
我看到由于某种原因交易 运行 很长时间(几秒钟)
我的问题:
- 鉴于我只插入单个实体并始终使用 WHERE 进行更新,这会导致 table 级别锁定吗?
- 有什么方法可以让我从 Postgres 端或 Java 端注销此操作何时获取哪种类型的锁 - 以及多长时间?
Given that I only do insert single entity and update always with a WHERE, can this cause table level lock?
如果 where 列中使用的列上没有索引,则以 where 为原因的查询或更新可能需要时间。你也可以试试explain来弄明白。
Is there a way I can log out either from Postgres side or Java side when and which type of locks is acquired by this operation - and for how long?
由于您的代码看起来不像是显式获取任何数据库锁 - 锁将在 SQL 数据库引擎执行期间隐式获取。 Postgres 提供了不同的视图来查询锁级别信息。可以参考this and official docs不同的查询从Postgres端获取锁信息。