{"id":21351,"date":"2024-01-02T07:56:26","date_gmt":"2024-01-02T07:56:26","guid":{"rendered":"https:\/\/server.hk\/blog\/21351\/"},"modified":"2024-11-14T10:36:11","modified_gmt":"2024-11-14T02:36:11","slug":"how-to-fix-postgresql-error-code-25001-active_sql_transaction","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21351\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25001 &#8211; active_sql_transaction"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-25001-active_sql_transaction-sgbddKYlFz\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25001 &#8211; active_sql_transaction<\/h1>\n<p>PostgreSQL \u662f\u4e00\u6b3e\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u5f0f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5f0f\u3002\u7136\u800c\uff0c\u6b63\u5982\u4efb\u4f55\u8edf\u9ad4\u4e00\u6a23\uff0c\u5b83\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u932f\u8aa4\uff0c\u9019\u4e9b\u932f\u8aa4\u53ef\u80fd\u6703\u5e72\u64fe\u5176\u6b63\u5e38\u904b\u4f5c\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25001 &#8211; active_sql_transaction\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u6210\u56e0\u4e26\u63d0\u4f9b\u9010\u6b65\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"understanding-postgresql-error-code-25001-active_sql_transaction-sgbddKYlFz\">\u7406\u89e3 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25001 &#8211; active_sql_transaction<\/h2>\n<p>\u7576\u5b58\u5728\u4e00\u500b\u6d3b\u8e8d\u7684 SQL \u4ea4\u6613\u6642\uff0c\u6703\u51fa\u73fe PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25001 &#8211; active_sql_transaction\uff0c\u9019\u6703\u963b\u6b62\u67d0\u4e9b\u64cd\u4f5c\u7684\u57f7\u884c\u3002\u4ea4\u6613\u662f\u4e00\u7cfb\u5217\u88ab\u8996\u70ba\u55ae\u4e00\u5de5\u4f5c\u55ae\u4f4d\u7684 SQL \u8a9e\u53e5\u3002\u5b83\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u63d0\u4f9b\u4e86\u539f\u5b50\u6027\u3001\u4e00\u81f4\u6027\u3001\u9694\u96e2\u6027\u548c\u6301\u4e45\u6027\uff08ACID\uff09\u7279\u6027\u3002<\/p>\n<p>\u7576\u9019\u500b\u932f\u8aa4\u767c\u751f\u6642\uff0c\u610f\u5473\u8457\u6709\u4e00\u500b\u6301\u7e8c\u4e2d\u7684\u4ea4\u6613\u6b63\u5728\u963b\u6b62\u67d0\u4e9b\u547d\u4ee4\u7684\u57f7\u884c\u3002\u9019\u53ef\u80fd\u7531\u65bc\u591a\u7a2e\u539f\u56e0\u9020\u6210\uff0c\u4f8b\u5982\u9577\u6642\u9593\u904b\u884c\u7684\u4ea4\u6613\u3001\u6b7b\u9396\u60c5\u6cc1\u6216\u4ea4\u6613\u672c\u8eab\u7684\u932f\u8aa4\u3002<\/p>\n<h2 id=\"steps-to-fix-postgresql-error-code-25001-active_sql_transaction-sgbddKYlFz\">\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25001 &#8211; active_sql_transaction \u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-identify-the-active-transaction-sgbddKYlFz\">1. \u78ba\u8a8d\u6d3b\u8e8d\u7684\u4ea4\u6613<\/h3>\n<p>\u4fee\u5fa9\u6b64\u932f\u8aa4\u7684\u7b2c\u4e00\u6b65\u662f\u78ba\u8a8d\u9020\u6210\u554f\u984c\u7684\u6d3b\u8e8d\u4ea4\u6613\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\u4f86\u6aa2\u67e5\u6d3b\u8e8d\u7684\u4ea4\u6613\uff1a<\/p>\n<pre><code>SELECT * FROM pg_stat_activity;<\/code><\/pre>\n<p>\u9019\u500b\u67e5\u8a62\u5c07\u986f\u793a\u6240\u6709\u6d3b\u8e8d\u7684\u9023\u63a5\u53ca\u5176\u76f8\u95dc\u4ea4\u6613\u3002\u67e5\u770b\u4efb\u4f55\u904b\u884c\u7570\u5e38\u9577\u6642\u9593\u6216\u9020\u6210\u885d\u7a81\u7684\u4ea4\u6613\u3002<\/p>\n<h3 id=\"2-rollback-or-commit-the-active-transaction-sgbddKYlFz\">2. \u56de\u6efe\u6216\u63d0\u4ea4\u6d3b\u8e8d\u4ea4\u6613<\/h3>\n<p>\u4e00\u65e6\u60a8\u8b58\u5225\u4e86\u9020\u6210\u932f\u8aa4\u7684\u6d3b\u8e8d\u4ea4\u6613\uff0c\u60a8\u53ef\u4ee5\u9078\u64c7\u56de\u6efe\u6216\u63d0\u4ea4\u8a72\u4ea4\u6613\u4f86\u89e3\u6c7a\u554f\u984c\u3002<\/p>\n<p>\u5982\u679c\u8a72\u4ea4\u6613\u4e0d\u662f\u95dc\u9375\u4e14\u53ef\u4ee5\u88ab\u4e1f\u68c4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u56de\u6efe\u8a72\u4ea4\u6613\uff1a<\/p>\n<pre><code>ROLLBACK;<\/code><\/pre>\n<p>\u6b64\u547d\u4ee4\u5c07\u64a4\u92b7\u4ea4\u6613\u6240\u505a\u7684\u6240\u6709\u66f4\u6539\uff0c\u4e26\u91cb\u653e\u5176\u6301\u6709\u7684\u9396\u3002<\/p>\n<p>\u5982\u679c\u8a72\u4ea4\u6613\u662f\u95dc\u9375\u4e14\u9700\u8981\u5b8c\u6210\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u63d0\u4ea4\u8a72\u4ea4\u6613\uff1a<\/p>\n<pre><code>COMMIT;<\/code><\/pre>\n<p>\u6b64\u547d\u4ee4\u5c07\u78ba\u8a8d\u4ea4\u6613\u4e26\u4f7f\u5176\u66f4\u6539\u6c38\u4e45\u751f\u6548\u3002<\/p>\n<h3 id=\"3-resolve-deadlock-situations-sgbddKYlFz\">3. \u89e3\u6c7a\u6b7b\u9396\u60c5\u6cc1<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0cPostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25001 &#8211; active_sql_transaction \u53ef\u80fd\u662f\u7531\u65bc\u6b7b\u9396\u60c5\u6cc1\u9020\u6210\u7684\u3002\u6b7b\u9396\u767c\u751f\u5728\u5169\u500b\u6216\u591a\u500b\u4ea4\u6613\u76f8\u4e92\u7b49\u5f85\u91cb\u653e\u8cc7\u6e90\u7684\u60c5\u6cc1\uff0c\u5c0e\u81f4\u6b7b\u9396\u3002\u8981\u89e3\u6c7a\u6b7b\u9396\u60c5\u6cc1\uff0c\u60a8\u53ef\u4ee5\u63a1\u53d6\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>pg_stat_activity<\/code> \u6aa2\u8996\u4f86\u8b58\u5225\u6d89\u53ca\u7684\u4ea4\u6613\u3002<\/li>\n<li>\u4f7f\u7528 <code>pg_terminate_backend<\/code> \u51fd\u6578\u624b\u52d5\u7d42\u6b62\u5176\u4e2d\u4e00\u500b\u4ea4\u6613\u3002<\/li>\n<li>\u8abf\u6574\u61c9\u7528\u908f\u8f2f\u4ee5\u9632\u6b62\u672a\u4f86\u7684\u6b7b\u9396\u3002<\/li>\n<\/ul>\n<h3 id=\"4-check-for-errors-in-the-transaction-sgbddKYlFz\">4. \u6aa2\u67e5\u4ea4\u6613\u4e2d\u7684\u932f\u8aa4<\/h3>\n<p>\u5982\u679c\u4ee5\u4e0a\u6b65\u9a5f\u5747\u672a\u80fd\u89e3\u6c7a PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25001 &#8211; active_sql_transaction\uff0c\u5247\u53ef\u80fd\u662f\u4ea4\u6613\u672c\u8eab\u5b58\u5728\u932f\u8aa4\u3002\u6aa2\u67e5\u4ea4\u6613\u4e2d\u7684 SQL \u8a9e\u53e5\uff0c\u67e5\u770b\u662f\u5426\u5b58\u5728\u8a9e\u6cd5\u932f\u8aa4\u6216\u908f\u8f2f\u554f\u984c\u3002\u4fee\u6b63\u9019\u4e9b\u932f\u8aa4\u61c9\u8a72\u80fd\u89e3\u6c7a\u554f\u984c\u3002<\/p>\n<h2 id=\"summary-sgbddKYlFz\">\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u4e4b\uff0cPostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a25001 &#8211; active_sql_transaction \u53ef\u80fd\u7531\u65bc\u6d3b\u8e8d\u7684\u4ea4\u6613\u963b\u6b62\u67d0\u4e9b\u64cd\u4f5c\u7684\u57f7\u884c\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u78ba\u8a8d\u6d3b\u8e8d\u4ea4\u6613\u3001\u56de\u6efe\u6216\u63d0\u4ea4\u8a72\u4ea4\u6613\u3001\u89e3\u6c7a\u4efb\u4f55\u6b7b\u9396\u60c5\u6cc1\uff0c\u4e26\u6aa2\u67e5\u4ea4\u6613\u4e2d\u7684\u932f\u8aa4\u3002\u9075\u5faa\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u89e3\u6c7a\u932f\u8aa4\uff0c\u78ba\u4fdd\u60a8\u7684 PostgreSQL \u6578\u64da\u5eab\u9806\u5229\u904b\u884c\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f <a href=\"https:\/\/server.hk\">Server.HK<\/a>\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-21351","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21351","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=21351"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21351\/revisions"}],"predecessor-version":[{"id":24147,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21351\/revisions\/24147"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}