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

ε祈祈猫儿з 提交于 2019-12-02 01:16:50
Erwin Brandstetter

This seems to be a misunderstanding. Consider my bold emphasis:

[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

The WARNING obviously belongs to a different concurrent transaction, which did not start an explicit transaction. The commit is misplaced there, since it's operating in autocommit mode.

If you keep reading your log, you will probably find an entry for the commit further down:

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

Debug

If you find neither that nor a rollback entry nor an error, I would check for problems in your app leaving uncommitted zombi transactions, which would be a bad thing.

Start the investigation by checking the system view pg_stat_activity while connected to your database:

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%';

The state value idle is not necessarily suspicious - just a session that's waiting for input.
But idle in transaction and idle in transaction (aborted) are.

More in the manual here or these related answers:

This is caused by the sails-postgresql node module I am using. It is causing a new session to be created in the middle of my transaction queries and so the commit query was being issued by a different session. That was causing the warning.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!