{"id":21465,"date":"2024-01-02T09:50:26","date_gmt":"2024-01-02T09:50:26","guid":{"rendered":"https:\/\/server.hk\/blog\/21465\/"},"modified":"2024-11-14T10:33:51","modified_gmt":"2024-11-14T02:33:51","slug":"how-to-fix-postgresql-error-code-72000-snapshot_too_old","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21465\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a72000 &#8211; snapshot_too_old"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-72000-snapshot_too_old-AfUYGuQAtw\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a72000 &#8211; snapshot_too_old<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5168\u7403\u7684\u958b\u767c\u8005\u548c\u4f01\u696d\u3002\u7136\u800c\uff0c\u50cf\u6240\u6709\u8edf\u4ef6\u4e00\u6a23\uff0cPostgreSQL \u4e5f\u53ef\u80fd\u6703\u9047\u5230\u932f\u8aa4\uff0c\u9019\u4e9b\u932f\u8aa4\u53ef\u80fd\u6703\u5e72\u64fe\u5176\u6b63\u5e38\u904b\u884c\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a72000 &#8211; snapshot_too_old\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u9019\u500b\u932f\u8aa4\u7684\u542b\u7fa9\u4ee5\u53ca\u5982\u4f55\u4fee\u5fa9\u5b83\u3002<\/p>\n<h2 id=\"understanding-postgresql-error-code-72000-snapshot_too_old-AfUYGuQAtw\">\u7406\u89e3 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a72000 &#8211; snapshot_too_old<\/h2>\n<p>PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a72000 &#8211; snapshot_too_old \u767c\u751f\u5728 PostgreSQL \u4e2d\u7684\u67d0\u500b\u4e8b\u52d9\u5617\u8a66\u8a2a\u554f\u5728\u7576\u524d\u4e8b\u52d9\u958b\u59cb\u5f8c\u88ab\u53e6\u4e00\u500b\u4e8b\u52d9\u4fee\u6539\u7684\u884c\u6642\u3002\u9019\u500b\u932f\u8aa4\u8207\u300c\u5feb\u7167\u904e\u820a\u300d\u7684\u6982\u5ff5\u6709\u95dc\uff0c\u9019\u610f\u5473\u8457\u4e8b\u52d9\u6b63\u5728\u8a2a\u554f\u7684\u6578\u64da\u5728\u6578\u64da\u5eab\u4e2d\u4e0d\u518d\u53ef\u7528\uff0c\u56e0\u70ba\u5b83\u5df2\u88ab\u53e6\u4e00\u500b\u4e8b\u52d9\u4fee\u6539\u6216\u522a\u9664\u3002<\/p>\n<p>\u7576\u6b64\u932f\u8aa4\u767c\u751f\u6642\uff0cPostgreSQL \u6703\u56de\u6efe\u7576\u524d\u4e8b\u52d9\uff0c\u4ee5\u7dad\u6301\u6578\u64da\u7684\u4e00\u81f4\u6027\u4e26\u9632\u6b62\u6578\u64da\u640d\u58de\u3002\u96d6\u7136\u9019\u7a2e\u884c\u70ba\u5c0d\u65bc\u4fdd\u6301\u6578\u64da\u5eab\u7684\u5b8c\u6574\u6027\u81f3\u95dc\u91cd\u8981\uff0c\u4f46\u5c0d\u65bc\u9047\u5230\u6b64\u932f\u8aa4\u4e26\u9700\u8981\u627e\u5230\u89e3\u6c7a\u65b9\u6848\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u9019\u53ef\u80fd\u6703\u4ee4\u4eba\u6cae\u55aa\u3002<\/p>\n<h2 id=\"fixing-postgresql-error-code-72000-snapshot_too_old-AfUYGuQAtw\">\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a72000 &#8211; snapshot_too_old<\/h2>\n<p>\u60a8\u53ef\u4ee5\u63a1\u53d6\u5e7e\u7a2e\u65b9\u6cd5\u4f86\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a72000 &#8211; snapshot_too_old\uff1a<\/p>\n<h3 id=\"1-retry-the-transaction-AfUYGuQAtw\">1. \u91cd\u8a66\u4e8b\u52d9<\/h3>\n<p>\u8655\u7406\u6b64\u932f\u8aa4\u7684\u4e00\u7a2e\u65b9\u6cd5\u662f\u91cd\u8a66\u9047\u5230 snapshot_too_old \u932f\u8aa4\u7684\u4e8b\u52d9\u3002\u901a\u904e\u91cd\u8a66\u4e8b\u52d9\uff0c\u60a8\u7d66 PostgreSQL \u4e00\u500b\u6a5f\u6703\u4f86\u7372\u53d6\u6578\u64da\u5eab\u7684\u65b0\u5feb\u7167\uff0c\u9019\u53ef\u80fd\u5305\u62ec\u5176\u4ed6\u4e8b\u52d9\u6240\u505a\u7684\u66f4\u6539\u3002\u60a8\u53ef\u4ee5\u5728\u61c9\u7528\u7a0b\u5e8f\u4ee3\u78bc\u4e2d\u5be6\u73fe\u91cd\u8a66\u908f\u8f2f\uff0c\u81ea\u52d5\u91cd\u8a66\u4e8b\u52d9\u82e5\u5e72\u6b21\uff0c\u7136\u5f8c\u518d\u653e\u68c4\u3002<\/p>\n<h3 id=\"2-increase-the-transaction-isolation-level-AfUYGuQAtw\">2. \u63d0\u9ad8\u4e8b\u52d9\u9694\u96e2\u7d1a\u5225<\/h3>\n<p>\u53e6\u4e00\u7a2e\u65b9\u6cd5\u662f\u63d0\u9ad8\u4e8b\u52d9\u9694\u96e2\u7d1a\u5225\u3002PostgreSQL \u7684\u9ed8\u8a8d\u9694\u96e2\u7d1a\u5225\u662f\u300c\u5df2\u63d0\u4ea4\u8b80\u53d6\u300d\uff0c\u9019\u610f\u5473\u8457\u6bcf\u500b\u4e8b\u52d9\u770b\u5230\u7684\u6578\u64da\u5eab\u5feb\u7167\u662f\u4e8b\u52d9\u958b\u59cb\u6642\u7684\u72c0\u614b\u3002\u901a\u904e\u5c07\u9694\u96e2\u7d1a\u5225\u63d0\u9ad8\u5230\u300c\u53ef\u91cd\u8907\u8b80\u53d6\u300d\u6216\u300c\u53ef\u5e8f\u5217\u5316\u300d\uff0c\u60a8\u53ef\u4ee5\u6e1b\u5c11\u9047\u5230 snapshot_too_old \u932f\u8aa4\u7684\u6a5f\u6703\u3002\u7136\u800c\uff0c\u8acb\u6ce8\u610f\uff0c\u63d0\u9ad8\u9694\u96e2\u7d1a\u5225\u4e5f\u53ef\u80fd\u589e\u52a0\u5176\u4ed6\u8207\u4e26\u767c\u76f8\u95dc\u7684\u554f\u984c\u7684\u98a8\u96aa\uff0c\u4f8b\u5982\u6b7b\u9396\u3002<\/p>\n<h3 id=\"3-optimize-database-queries-AfUYGuQAtw\">3. \u512a\u5316\u6578\u64da\u5eab\u67e5\u8a62<\/h3>\n<p>\u512a\u5316\u4e0d\u826f\u7684\u6578\u64da\u5eab\u67e5\u8a62\u53ef\u80fd\u6703\u4fc3\u6210 snapshot_too_old \u932f\u8aa4\u7684\u767c\u751f\u3002\u901a\u904e\u5206\u6790\u548c\u512a\u5316\u60a8\u7684\u6578\u64da\u5eab\u67e5\u8a62\uff0c\u60a8\u53ef\u4ee5\u6e1b\u5c11\u4e8b\u52d9\u5b8c\u6210\u6240\u9700\u7684\u6642\u9593\uff0c\u5f9e\u800c\u6700\u5c0f\u5316\u9047\u5230\u6b64\u932f\u8aa4\u7684\u6a5f\u6703\u3002\u6dfb\u52a0\u9069\u7576\u7684\u7d22\u5f15\u3001\u91cd\u5beb\u8907\u96dc\u67e5\u8a62\u4ee5\u53ca\u4f7f\u7528\u67e5\u8a62\u512a\u5316\u5de5\u5177\u7b49\u6280\u8853\u53ef\u4ee5\u5e6b\u52a9\u63d0\u9ad8\u67e5\u8a62\u7684\u6027\u80fd\uff0c\u6e1b\u5c11 snapshot_too_old \u932f\u8aa4\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<h3 id=\"4-adjust-postgresql-configuration-AfUYGuQAtw\">4. \u8abf\u6574 PostgreSQL \u914d\u7f6e<\/h3>\n<p>\u60a8\u9084\u53ef\u4ee5\u8abf\u6574\u67d0\u4e9b PostgreSQL \u914d\u7f6e\u53c3\u6578\uff0c\u4ee5\u6e1b\u8f15 snapshot_too_old \u932f\u8aa4\u3002\u4f8b\u5982\uff0c\u589e\u52a0\u300cmax_standby_streaming_delay\u300d\u53c3\u6578\u7684\u503c\u53ef\u4ee5\u4f7f\u5099\u7528\u4f3a\u670d\u5668\u66f4\u5feb\u5730\u61c9\u7528\u4f86\u81ea\u4e3b\u4f3a\u670d\u5668\u7684\u66f4\u6539\uff0c\u5f9e\u800c\u6e1b\u5c11\u9047\u5230\u6b64\u932f\u8aa4\u7684\u6a5f\u6703\u3002\u7136\u800c\uff0c\u5728\u4fee\u6539\u914d\u7f6e\u53c3\u6578\u6642\u8981\u8b39\u614e\uff0c\u4e26\u67e5\u95b1 PostgreSQL \u6587\u6a94\u4ee5\u7372\u53d6\u6307\u5c0e\u3002<\/p>\n<h2 id=\"summary-AfUYGuQAtw\">\u7e3d\u7d50<\/h2>\n<p>PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a72000 &#8211; snapshot_too_old \u53ef\u80fd\u662f\u4e00\u500b\u4ee4\u4eba\u6cae\u55aa\u7684\u554f\u984c\uff0c\u4f46\u901a\u904e\u6b63\u78ba\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u89e3\u6c7a\u5b83\u3002\u901a\u904e\u91cd\u8a66\u4e8b\u52d9\u3001\u63d0\u9ad8\u4e8b\u52d9\u9694\u96e2\u7d1a\u5225\u3001\u512a\u5316\u6578\u64da\u5eab\u67e5\u8a62\u548c\u8abf\u6574 PostgreSQL \u914d\u7f6e\uff0c\u60a8\u53ef\u4ee5\u6700\u5c0f\u5316\u6b64\u932f\u8aa4\u7684\u767c\u751f\uff0c\u78ba\u4fdd PostgreSQL \u6578\u64da\u5eab\u7684\u9806\u5229\u904b\u884c\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9047\u5230 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a72000 &#8211; snapshot_too_old \u6216\u4efb\u4f55\u5176\u4ed6\u8207 PostgreSQL \u76f8\u95dc\u7684\u554f\u984c\uff0c\u8003\u616e\u806f\u7e6b\u53ef\u9760\u7684 VPS \u8a17\u7ba1\u63d0\u4f9b\u5546\uff0c\u5982 <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u6191\u85c9\u4ed6\u5011\u5728\u8a17\u7ba1\u548c\u7ba1\u7406 PostgreSQL \u6578\u64da\u5eab\u65b9\u9762\u7684\u5c08\u696d\u77e5\u8b58\uff0c\u4ed6\u5011\u53ef\u4ee5\u63d0\u4f9b\u5fc5\u8981\u7684\u652f\u6301\u548c\u6307\u5c0e\uff0c\u4ee5\u89e3\u6c7a\u4efb\u4f55\u8207\u6578\u64da\u5eab\u76f8\u95dc\u7684\u554f\u984c\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-21465","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21465","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=21465"}],"version-history":[{"count":2,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21465\/revisions"}],"predecessor-version":[{"id":24011,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21465\/revisions\/24011"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}