关于日志记录:为什么这个PostgreSQL事务会给出“警告:没有正在进行的事务”

Why does this PostgreSQL transaction give “WARNING: there is no transaction in progress”

我运行的事务查询的代码如下(简化):

1
2
3
4
5
6
7
8
9
10
try {
    runQuery("begin");
    runQuery("some query ...");
    runQuery("some other query ...");
    runQuery("some more query ...");
    runQuery("some extra query ...");
    runQuery("commit");
} catch (e){
    runQuery("rollback");
}

这是Postgres日志文件中的语句列表。请注意,第一次开始/提交工作正常,第二次提交出现了主题中提到的错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2015-02-18 20:19:17 UTC [6459-7] vdsr@sails LOG:  statement: BEGIN
2015-02-18 20:19:17 UTC [6459-8] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"groups" ("name","parentGroupRef","isCompany","companyRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4, $5, $6) RETURNING *
2015-02-18 20:19:17 UTC [6459-9] vdsr@sails DETAIL:  parameters: $1 = 'testclient', $2 = '5', $3 = 't', $4 = '1', $5 = '2015-02-18 20:19:17+00', $6 = '2015-02-18 20:19:17+00'
2015-02-18 20:19:17 UTC [6459-10] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"users" ("firstName","lastName","email","companyRef","isMainUser","password","middleName","createdAt","updatedAt") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *
2015-02-18 20:19:17 UTC [6459-11] vdsr@sails DETAIL:  parameters: $1 = 'aa', $2 = 'bb', $3 = '[email protected]', $4 = '18', $5 = 't', $6 = '06a8ec164adcc7db4edfb6ca20c07b20', $7 = '', $8 = '2015-02-18 20:19:17+00', $9 = '2015-02-18 20:19:17+00'
2015-02-18 20:19:17 UTC [6459-12] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"roles" ("name","companyRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4) RETURNING *
2015-02-18 20:19:17 UTC [6459-13] vdsr@sails DETAIL:  parameters: $1 = 'Master', $2 = '18', $3 = '2015-02-18 20:19:17+00', $4 = '2015-02-18 20:19:17+00'
2015-02-18 20:19:17 UTC [6459-14] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"userroles" ("roleRef","userRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4) RETURNING *
2015-02-18 20:19:17 UTC [6459-15] vdsr@sails DETAIL:  parameters: $1 = '11', $2 = '13', $3 = '2015-02-18 20:19:17+00', $4 = '2015-02-18 20:19:17+00'
2015-02-18 20:19:17 UTC [6459-16] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"rolepermissions" ("roleRef","permissionRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4) RETURNING *
2015-02-18 20:19:17 UTC [6459-17] vdsr@sails DETAIL:  parameters: $1 = '11', $2 = '24', $3 = '2015-02-18 20:19:17+00', $4 = '2015-02-18 20:19:17+00'
2015-02-18 20:19:17 UTC [6459-18] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"rolepermissions" ("roleRef","permissionRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4) RETURNING *
2015-02-18 20:19:17 UTC [6459-19] vdsr@sails DETAIL:  parameters: $1 = '11', $2 = '22', $3 = '2015-02-18 20:19:17+00', $4 = '2015-02-18 20:19:17+00'
2015-02-18 20:19:17 UTC [6459-20] vdsr@sails LOG:  EXECUTE <unnamed>: SELECT"groups"."name","groups"."notes","groups"."parentGroupRef","groups"."isCompany","groups"."hierPos","groups"."companyRef","groups"."id","groups"."createdAt","groups"."updatedAt" FROM"groups" AS"groups"  WHERE"groups"."companyRef" = $1
2015-02-18 20:19:17 UTC [6459-21] vdsr@sails DETAIL:  parameters: $1 = '1'
2015-02-18 20:19:17 UTC [6461-1] vdsr@sails LOG:  statement:  UPDATE groups SET"hierPos" = CASE id WHEN 2 THEN '1' WHEN 3 THEN '2' WHEN 4 THEN '3' WHEN 5 THEN '4' WHEN 6 THEN '4.1' WHEN 10 THEN '4.1.1' WHEN 18 THEN '4.2' WHEN 13 THEN '5' WHEN 17 THEN '6' ELSE"hierPos" END  WHERE id IN (2,3,4,5,6,10,18,13,17)
2015-02-18 20:19:17 UTC [6459-22] vdsr@sails LOG:  statement: commit
2015-02-18 20:26:08 UTC [6477-1] vdsr@sails LOG:  EXECUTE <unnamed>: SELECT"users"."firstName","users"."middleName","users"."lastName","users"."email","users"."password","users"."resetKey","users"."resetKeyGeneratedAt","users"."isMainUser","users"."companyRef","users"."id","users"."createdAt","users"."updatedAt" FROM"users" AS"users"  WHERE"users"."id" = $1  LIMIT 1
2015-02-18 20:26:08 UTC [6477-2] vdsr@sails DETAIL:  parameters: $1 = '8'
2015-02-18 20:26:08 UTC [6477-3] vdsr@sails LOG:  EXECUTE <unnamed>: SELECT p.name FROM permissions p  INNER JOIN rolepermissions rp ON rp."permissionRef" = p.id   INNER JOIN roles r ON r.id = rp."roleRef"  INNER JOIN userroles ur ON ur."roleRef" = r.id  WHERE ur."userRef" = $1
2015-02-18 20:26:08 UTC [6477-4] vdsr@sails DETAIL:  parameters: $1 = '8'
2015-02-18 20:26:08 UTC [6477-5] vdsr@sails LOG:  statement: BEGIN
2015-02-18 20:26:08 UTC [6477-6] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"groups" ("name","isCompany","parentGroupRef","companyRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4, $5, $6) RETURNING *
2015-02-18 20:26:08 UTC [6477-7] vdsr@sails DETAIL:  parameters: $1 = 'ddffdfd', $2 = 't', $3 = '1', $4 = '1', $5 = '2015-02-18 20:26:08+00', $6 = '2015-02-18 20:26:08+00'
2015-02-18 20:26:08 UTC [6477-8] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"users" ("firstName","lastName","email","companyRef","isMainUser","password","middleName","createdAt","updatedAt") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *
2015-02-18 20:26:08 UTC [6477-9] vdsr@sails DETAIL:  parameters: $1 = 'fdfd', $2 = 'fdfd', $3 = '[email protected]', $4 = '19', $5 = 't', $6 = '7b9e87b1795e4d96bf9fbb898fa669fe', $7 = '', $8 = '2015-02-18 20:26:08+00', $9 = '2015-02-18 20:26:08+00'
2015-02-18 20:26:08 UTC [6477-10] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"roles" ("name","companyRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4) RETURNING *
2015-02-18 20:26:08 UTC [6477-11] vdsr@sails DETAIL:  parameters: $1 = 'Master', $2 = '19', $3 = '2015-02-18 20:26:08+00', $4 = '2015-02-18 20:26:08+00'
2015-02-18 20:26:08 UTC [6477-12] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"userroles" ("roleRef","userRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4) RETURNING *
2015-02-18 20:26:08 UTC [6477-13] vdsr@sails DETAIL:  parameters: $1 = '12', $2 = '14', $3 = '2015-02-18 20:26:08+00', $4 = '2015-02-18 20:26:08+00'
2015-02-18 20:26:08 UTC [6477-14] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"rolepermissions" ("roleRef","permissionRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4) RETURNING *
2015-02-18 20:26:08 UTC [6477-15] vdsr@sails DETAIL:  parameters: $1 = '12', $2 = '24', $3 = '2015-02-18 20:26:08+00', $4 = '2015-02-18 20:26:08+00'
2015-02-18 20:26:08 UTC [6477-16] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"rolepermissions" ("roleRef","permissionRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4) RETURNING *
2015-02-18 20:26:08 UTC [6477-17] vdsr@sails DETAIL:  parameters: $1 = '12', $2 = '22', $3 = '2015-02-18 20:26:08+00', $4 = '2015-02-18 20:26:08+00'
2015-02-18 20:26:08 UTC [6477-18] vdsr@sails LOG:  EXECUTE <unnamed>: INSERT INTO"rolepermissions" ("roleRef","permissionRef","createdAt","updatedAt") VALUES ($1, $2, $3, $4) RETURNING *
2015-02-18 20:26:08 UTC [6477-19] vdsr@sails DETAIL:  parameters: $1 = '12', $2 = '23', $3 = '2015-02-18 20:26:08+00', $4 = '2015-02-18 20:26:08+00'
2015-02-18 20:26:08 UTC [6477-20] vdsr@sails LOG:  EXECUTE <unnamed>: SELECT"groups"."name","groups"."notes","groups"."parentGroupRef","groups"."isCompany","groups"."hierPos","groups"."companyRef","groups"."id","groups"."createdAt","groups"."updatedAt" FROM"groups" AS"groups"  WHERE"groups"."companyRef" = $1
2015-02-18 20:26:08 UTC [6477-21] vdsr@sails DETAIL:  parameters: $1 = '1'
2015-02-18 20:26:08 UTC [6478-1] vdsr@sails LOG:  statement:  UPDATE groups SET"hierPos" = CASE id WHEN 2 THEN '1' WHEN 3 THEN '2' WHEN 4 THEN '3' WHEN 5 THEN '4' WHEN 6 THEN '4.1' WHEN 10 THEN '4.1.1' WHEN 18 THEN '4.2' WHEN 13 THEN '5' WHEN 17 THEN '6' WHEN 19 THEN '7' ELSE"hierPos" END  WHERE id IN (2,3,4,5,6,10,18,13,17,19)
2015-02-18 20:26:08 UTC [6479-1] vdsr@sails LOG:  statement: commit
2015-02-18 20:26:08 UTC [6479-2] vdsr@sails WARNING:  there IS no TRANSACTION IN progress


这似乎是个误会。想想我大胆的强调:

[6459-7] vdsr@sails LOG: statement: begin
[6459-8] vdsr@sails LOG: execute : INSERT INTO"groups" ("name","parentGroupRef","isCompany","companyRef","createdAt",
"updatedAt") values ($1, $2, $3, $4, $5, $6) RETURNING *
...
[6459-22] vdsr@sails LOG: statement: commit
...
[6477-5] vdsr@sails LOG: statement: begin
[6477-6] vdsr@sails LOG: execute : INSERT INTO"groups" ("name","isCompany","parentGroupRef","companyRef","createdAt",
"updatedAt") values ($1, $2, $3, $4, $5, $6) RETURNING *
...
[6479-1] vdsr@sails LOG: statement: commit
[6479-2] vdsr@sails WARNING: there is no transaction in progress

警告显然属于不同的并发事务,它没有启动显式事务。commit放错了位置,因为它是在自动提交模式下运行的。

如果您继续阅读日志,您可能会在下面找到提交的条目:

[6477-??] vdsr@sails LOG: statement: commit

调试

如果您既没有发现该条目,也没有发现rollback条目,也没有发现错误,那么我将检查您的应用程序中是否存在未提交僵尸事务的问题,这将是一件坏事。

在连接到数据库时,通过检查系统视图pg_stat_activity开始调查:

1
2
3
4
5
SELECT *
FROM   pg_stat_activity
WHERE  datname = current_database()  -- only current database
AND    pid <> pg_backend_pid()       -- except your current session
AND    state LIKE 'idle%';

stateidle不一定可疑——只是一个等待输入的会话。但是idle in transactionidle in transaction (aborted)是。

本手册中的更多内容或这些相关答案:

  • 如果有活动连接,如何删除PostgreSQL数据库?
  • 空闲PostgreSQL连接是否有超时?


这是由我正在使用的帆PostgreSQL节点模块引起的。它导致在事务查询的中间创建一个新的会话,因此提交查询是由另一个会话发出的。这就是引起警告的原因。