{"id":21356,"date":"2024-01-02T08:01:26","date_gmt":"2024-01-02T08:01:26","guid":{"rendered":"https:\/\/server.hk\/blog\/21356\/"},"modified":"2024-11-14T10:36:04","modified_gmt":"2024-11-14T02:36:04","slug":"how-to-fix-postgresql-error-code-25005-no_active_sql_transaction_for_branch_transaction","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21356\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25005 &#8211; no_active_sql_transaction_for_branch_transaction"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-25005-no_active_sql_transaction_for_branch_transaction-pwAGAvWdfz\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25005 &#8211; no_active_sql_transaction_for_branch_transaction<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u5f0f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u3002\u7136\u800c\uff0c\u8207\u4efb\u4f55\u8edf\u4ef6\u4e00\u6a23\uff0c\u5b83\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u932f\u8aa4\uff0c\u5f9e\u800c\u5f71\u97ff\u5176\u6b63\u5e38\u904b\u884c\u3002\u5176\u4e2d\u4e4b\u4e00\u4fbf\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25005 &#8211; no_active_sql_transaction_for_branch_transaction\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u539f\u56e0\uff0c\u4e26\u63d0\u4f9b\u9010\u6b65\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"understanding-the-error-pwAGAvWdfz\">\u7406\u89e3\u932f\u8aa4<\/h2>\n<p>\u7576\u5206\u652f\u4e8b\u52d9\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u6c92\u6709\u6d3b\u52d5 SQL \u4e8b\u52d9\u7684\u6578\u64da\u5eab\u6642\uff0c\u6703\u51fa\u73fe PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25005 &#8211; no_active_sql_transaction_for_branch_transaction\u3002\u9019\u500b\u932f\u8aa4\u901a\u5e38\u767c\u751f\u5728\u5206\u652f\u4e8b\u52d9\u8207\u4e3b\u4e8b\u52d9\u4e4b\u9593\u4e0d\u5339\u914d\u7684\u60c5\u6cc1\u4e0b\u3002<\/p>\n<h2 id=\"possible-causes-pwAGAvWdfz\">\u53ef\u80fd\u7684\u539f\u56e0<\/h2>\n<p>\u6b64\u932f\u8aa4\u53ef\u80fd\u7531\u4ee5\u4e0b\u5e7e\u500b\u539f\u56e0\u5f15\u8d77\uff1a<\/p>\n<ul>\n<li><strong>\u7f3a\u5c11 BEGIN \u8a9e\u53e5\uff1a<\/strong>\u5982\u679c\u5206\u652f\u4e8b\u52d9\u6c92\u6709\u4ee5 BEGIN \u8a9e\u53e5\u958b\u59cb\uff0c\u5b83\u5c07\u7121\u6cd5\u64c1\u6709\u6d3b\u52d5\u7684 SQL \u4e8b\u52d9\u3002<\/li>\n<li><strong>\u4e8b\u52d9\u5d4c\u5957\u4e0d\u6b63\u78ba\uff1a<\/strong>\u5982\u679c\u5206\u652f\u4e8b\u52d9\u5728\u4e3b\u4e8b\u52d9\u5167\u5d4c\u5957\u4e0d\u7576\uff0c\u5b83\u53ef\u80fd\u7121\u6cd5\u8a2a\u554f\u6d3b\u52d5\u7684 SQL \u4e8b\u52d9\u3002<\/li>\n<li><strong>\u56de\u6efe\u6216\u63d0\u4ea4\u554f\u984c\uff1a<\/strong>\u5982\u679c\u4e3b\u4e8b\u52d9\u5728\u5206\u652f\u4e8b\u52d9\u5b8c\u6210\u4e4b\u524d\u88ab\u56de\u6efe\u6216\u63d0\u4ea4\uff0c\u5247\u53ef\u80fd\u6703\u5c0e\u81f4\u6b64\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h2 id=\"fixing-the-error-pwAGAvWdfz\">\u89e3\u6c7a\u932f\u8aa4\u7684\u65b9\u6cd5<\/h2>\n<p>\u8981\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25005 &#8211; no_active_sql_transaction_for_branch_transaction\uff0c\u8acb\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u64cd\u4f5c\uff1a<\/p>\n<h3 id=\"1-check-transaction-nesting-pwAGAvWdfz\">1. \u6aa2\u67e5\u4e8b\u52d9\u5d4c\u5957<\/h3>\n<p>\u78ba\u4fdd\u5206\u652f\u4e8b\u52d9\u6b63\u78ba\u5d4c\u5957\u65bc\u4e3b\u4e8b\u52d9\u5167\u3002\u5982\u679c\u5d4c\u5957\u4e0d\u6b63\u78ba\uff0c\u8acb\u76f8\u61c9\u8abf\u6574\u4e8b\u52d9\u7d50\u69cb\u3002<\/p>\n<pre><code>BEGIN;\r\n  -- \u4e3b\u4e8b\u52d9\u8a9e\u53e5\r\n  \r\n  BEGIN;\r\n    -- \u5206\u652f\u4e8b\u52d9\u8a9e\u53e5\r\n  COMMIT;\r\n  \r\n  -- \u66f4\u591a\u4e3b\u4e8b\u52d9\u8a9e\u53e5\r\nCOMMIT;\r\n<\/code><\/pre>\n<h3 id=\"2-verify-begin-statement-pwAGAvWdfz\">2. \u78ba\u8a8d BEGIN \u8a9e\u53e5<\/h3>\n<p>\u78ba\u4fdd\u5206\u652f\u4e8b\u52d9\u4ee5 BEGIN \u8a9e\u53e5\u958b\u59cb\u3002\u8a72\u8a9e\u53e5\u521d\u59cb\u5316\u4e8b\u52d9\uff0c\u78ba\u4fdd\u5b83\u64c1\u6709\u6d3b\u52d5\u7684 SQL \u4e8b\u52d9\u3002<\/p>\n<pre><code>BEGIN;\r\n  -- \u5206\u652f\u4e8b\u52d9\u8a9e\u53e5\r\nCOMMIT;\r\n<\/code><\/pre>\n<h3 id=\"3-avoid-premature-rollback-or-commit-pwAGAvWdfz\">3. \u907f\u514d\u904e\u65e9\u56de\u6efe\u6216\u63d0\u4ea4<\/h3>\n<p>\u78ba\u4fdd\u4e3b\u4e8b\u52d9\u5728\u5206\u652f\u4e8b\u52d9\u5b8c\u6210\u4e4b\u524d\u4e0d\u6703\u56de\u6efe\u6216\u63d0\u4ea4\u3002\u904e\u65e9\u7684\u56de\u6efe\u6216\u63d0\u4ea4\u6703\u5c0e\u81f4\u5206\u652f\u4e8b\u52d9\u5931\u53bb\u5c0d\u6d3b\u52d5 SQL \u4e8b\u52d9\u7684\u8a2a\u554f\u6b0a\u9650\u3002<\/p>\n<pre><code>BEGIN;\r\n  -- \u4e3b\u4e8b\u52d9\u8a9e\u53e5\r\n  \r\n  SAVEPOINT sp1;\r\n  \r\n  BEGIN;\r\n    -- \u5206\u652f\u4e8b\u52d9\u8a9e\u53e5\r\n  COMMIT;\r\n  \r\n  -- \u66f4\u591a\u4e3b\u4e8b\u52d9\u8a9e\u53e5\r\n  \r\n  ROLLBACK TO SAVEPOINT sp1;\r\n  \r\n  -- \u66f4\u591a\u4e3b\u4e8b\u52d9\u8a9e\u53e5\r\nCOMMIT;\r\n<\/code><\/pre>\n<h2 id=\"summary-pwAGAvWdfz\">\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u800c\u8a00\u4e4b\uff0c\u901a\u904e\u78ba\u4fdd\u6b63\u78ba\u7684\u4e8b\u52d9\u5d4c\u5957\uff0c\u5728\u5206\u652f\u4e8b\u52d9\u4e2d\u5305\u62ec BEGIN \u8a9e\u53e5\uff0c\u4e26\u907f\u514d\u904e\u65e9\u7684\u56de\u6efe\u6216\u63d0\u4ea4\uff0c\u53ef\u4ee5\u89e3\u6c7a PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25005 &#8211; no_active_sql_transaction_for_branch_transaction\u3002\u6309\u7167\u9019\u4e9b\u6b65\u9a5f\u64cd\u4f5c\uff0c\u4f60\u53ef\u4ee5\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u78ba\u4fdd\u4f60\u7684 PostgreSQL \u6578\u64da\u5eab\u9806\u66a2\u904b\u884c\u3002<\/p>\n<p>\u5982\u9700\u53ef\u9760\u4e14\u9ad8\u6548\u80fd\u7684 VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8003\u616e <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u6211\u5011\u7684\u9999\u6e2f VPS \u8a17\u7ba1\u670d\u52d9\u70ba\u60a8\u7684 PostgreSQL \u548c\u5176\u4ed6\u6578\u64da\u5eab\u9700\u6c42\u63d0\u4f9b\u9802\u7d1a\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4fee\u5fa9 PostgreSQL &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-21356","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=21356"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21356\/revisions"}],"predecessor-version":[{"id":24142,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21356\/revisions\/24142"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}