![运行四个单元测试时 Postgres 发生死锁](https://linux22.com/image/794706/%E8%BF%90%E8%A1%8C%E5%9B%9B%E4%B8%AA%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E6%97%B6%20Postgres%20%E5%8F%91%E7%94%9F%E6%AD%BB%E9%94%81.png)
我正在针对 Windows 上的 PostgresSQL 14.11 build 1937 运行单元测试。每个测试都执行以下操作:
在服务器上创建临时数据库。
通过运行一组 SQL 文件来创建表。
执行测试
如果我单独运行这些文件,它们会运行正常。当我同时运行三个文件时,所有文件也会通过。但是每次我运行四个文件时,测试都会死锁。如果我删除第二步,测试会失败,但不会发生锁定。最大连接数设置为 100。
什么原因导致了这个问题?
当发生锁定时,我在 psql 中看到的内容如下:
se4-unit-test=# SELECT pid, datname, usename, query, state from pg_stat_activity;
pid | datname | usename | query | state
-------+--------------------+----------+-------------------------------------------------------------------+--------
4868 | | | |
4864 | | postgres | |
6972 | se4-unit-test | postgres | SELECT pid, datname, usename, query, state from pg_stat_activity; | active
15364 | unit-test-64df13c0 | postgres | CREATE TABLE IF NOT EXISTS Organizations(\r +| idle
| | | Id serial PRIMARY KEY NOT NULL, \r +|
| | | Name VARCHAR(255) NOT NULL,\r +|
| | | Created timestamptz not null DEFAULT CURRENT_TIMESTAMP) |
11692 | unit-test-733a213e | postgres | SELECT MAX(Version) FROM Version | idle
14620 | unit-test-290fca6b | postgres | SELECT MAX(Version) FROM Version | idle
15436 | unit-test-d85a84ce | postgres | SELECT MAX(Version) FROM Version | idle
772 | | | |
5032 | | | |
4816 | | | |
(10 rows)
se4-unit-test=# select * from pg_locks;
locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted | fastpath | waitstart
------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+------+-----------------+---------+----------+-----------
relation | 16394 | 12290 | | | | | | | | 4/1250 | 6972 | AccessShareLock | t | t |
virtualxid | | | | | 4/1250 | | | | | 4/1250 | 6972 | ExclusiveLock | t | t |
(2 rows)