{"id":21384,"date":"2024-01-02T08:29:24","date_gmt":"2024-01-02T08:29:24","guid":{"rendered":"https:\/\/server.hk\/blog\/21384\/"},"modified":"2024-11-14T10:35:28","modified_gmt":"2024-11-14T02:35:28","slug":"how-to-fix-postgresql-error-code-39p02-srf_protocol_violated","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21384\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a39P02 &#8211; srf_protocol_violated"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-39p02-srf_protocol_violated-GjiLblsjNc\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a39P02 &#8211; srf_protocol_violated<\/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\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u3002\u7136\u800c\uff0c\u50cf\u4efb\u4f55\u8edf\u4ef6\u4e00\u6a23\uff0c\u5b83\u4e5f\u53ef\u80fd\u9047\u5230\u6703\u5e72\u64fe\u6b63\u5e38\u904b\u884c\u7684\u932f\u8aa4\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a39P02 &#8211; srf_protocol_violated\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u539f\u56e0\u4e26\u63d0\u4f9b\u89e3\u6c7a\u65b9\u6848\u4f86\u4fee\u5fa9\u5b83\u3002<\/p>\n<h2 id=\"understanding-the-error-GjiLblsjNc\">\u4e86\u89e3\u932f\u8aa4<\/h2>\n<p>PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a39P02 &#8211; srf_protocol_violated \u7576\u5b58\u5728\u5c0d\u8fd4\u56de\u96c6\u5408\u7684\u51fd\u6578\uff08SRFs\uff09\u5354\u8b70\u7684\u9055\u53cd\u6642\u6703\u767c\u751f\u3002SRFs \u662f\u8fd4\u56de\u4e00\u7d44\u884c\u7684\u51fd\u6578\uff0c\u9019\u500b\u932f\u8aa4\u901a\u5e38\u767c\u751f\u5728\u9810\u671f\u7684\u5217\u6578\u8207\u51fd\u6578\u5be6\u969b\u8fd4\u56de\u7684\u5217\u6578\u4e0d\u5339\u914d\u6642\u3002<\/p>\n<h2 id=\"possible-causes-GjiLblsjNc\">\u53ef\u80fd\u7684\u539f\u56e0<\/h2>\n<p>\u6b64\u932f\u8aa4\u53ef\u80fd\u6709\u5e7e\u500b\u6f5b\u5728\u539f\u56e0\uff1a<\/p>\n<ul>\n<li><strong>\u51fd\u6578\u5b9a\u7fa9\u4e0d\u6b63\u78ba\uff1a<\/strong>\u5982\u679c\u51fd\u6578\u5b9a\u7fa9\u8207\u5be6\u969b\u8fd4\u56de\u7684\u5217\u6578\u4e0d\u5339\u914d\uff0c\u5247\u53ef\u80fd\u6703\u767c\u751f\u932f\u8aa4\u3002\u78ba\u4fdd\u51fd\u6578\u6b63\u78ba\u5b9a\u7fa9\u975e\u5e38\u91cd\u8981\u3002<\/li>\n<li><strong>SRFs \u4f7f\u7528\u4e0d\u7576\uff1a<\/strong>\u5982\u679c\u5728\u67e5\u8a62\u4e2d\u672a\u6b63\u78ba\u4f7f\u7528 SRFs\uff0c\u5247\u53ef\u80fd\u5c0e\u81f4\u5354\u8b70\u9055\u53cd\u932f\u8aa4\u3002\u78ba\u4fdd\u5728\u67e5\u8a62\u4e2d\u6b63\u78ba\u4f7f\u7528 SRFs\u3002<\/li>\n<li><strong>\u6578\u64da\u5eab\u640d\u58de\uff1a<\/strong>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u6578\u64da\u5eab\u640d\u58de\u53ef\u80fd\u6703\u5c0e\u81f4\u6b64\u932f\u8aa4\u3002\u5982\u679c\u6578\u64da\u5eab\u640d\u58de\uff0c\u5247\u9700\u8981\u4fee\u5fa9\u4ee5\u89e3\u6c7a\u554f\u984c\u3002<\/li>\n<\/ul>\n<h2 id=\"fixing-the-error-GjiLblsjNc\">\u4fee\u5fa9\u932f\u8aa4<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u4ee5\u63a1\u53d6\u7684\u6b65\u9a5f\u4f86\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a39P02 &#8211; srf_protocol_violated\uff1a<\/p>\n<h3 id=\"1-verify-function-definition-GjiLblsjNc\">1. \u9a57\u8b49\u51fd\u6578\u5b9a\u7fa9<\/h3>\n<p>\u6aa2\u67e5\u51fd\u6578\u5b9a\u7fa9\u4ee5\u78ba\u4fdd\u5176\u8207\u5be6\u969b\u8fd4\u56de\u7684\u5217\u6578\u5339\u914d\u3002\u5982\u679c\u5b58\u5728\u4e0d\u5339\u914d\uff0c\u5247\u76f8\u61c9\u5730\u4fee\u6539\u51fd\u6578\u5b9a\u7fa9\u3002<\/p>\n<pre><code>CREATE OR REPLACE FUNCTION my_function()\r\n  RETURNS TABLE (column1 datatype, column2 datatype, ...)\r\n  AS $\r\n  BEGIN\r\n    -- \u51fd\u6578\u908f\u8f2f\u5728\u6b64\r\n  END;\r\n  $ LANGUAGE plpgsql;<\/code><\/pre>\n<h3 id=\"2-review-query-usage-GjiLblsjNc\">2. \u6aa2\u67e5\u67e5\u8a62\u4f7f\u7528\u60c5\u6cc1<\/h3>\n<p>\u6aa2\u67e5\u67e5\u8a62\u4e2d SRFs \u7684\u4f7f\u7528\u60c5\u6cc1\u3002\u78ba\u4fdd\u6b63\u78ba\u4f7f\u7528\u5b83\u5011\uff0c\u4e26\u4e14\u9810\u671f\u7684\u5217\u6578\u8207\u51fd\u6578\u5be6\u969b\u8fd4\u56de\u7684\u5217\u6578\u5339\u914d\u3002<\/p>\n<pre><code>SELECT *\r\nFROM my_function();<\/code><\/pre>\n<h3 id=\"3-check-for-database-corruption-GjiLblsjNc\">3. \u6aa2\u67e5\u6578\u64da\u5eab\u640d\u58de<\/h3>\n<p>\u5982\u679c\u60a8\u61f7\u7591\u932f\u8aa4\u662f\u7531\u6578\u64da\u5eab\u640d\u58de\u5f15\u8d77\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 PostgreSQL \u5de5\u5177 <code>pg_dump<\/code> \u5275\u5efa\u6578\u64da\u5eab\u7684\u5099\u4efd\uff0c\u7136\u5f8c\u5c07\u5176\u6062\u5fa9\u5230\u65b0\u6578\u64da\u5eab\u3002\u9019\u500b\u904e\u7a0b\u53ef\u4ee5\u5e6b\u52a9\u89e3\u6c7a\u4efb\u4f55\u640d\u58de\u554f\u984c\u3002<\/p>\n<pre><code>pg_dump -U username -h hostname -p port -F c -b -v -f backup_file.dump database_name\r\ncreatedb -U username -h hostname -p port new_database_name\r\npg_restore -U username -h hostname -p port -d new_database_name backup_file.dump<\/code><\/pre>\n<h2 id=\"summary-GjiLblsjNc\">\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u4e4b\uff0cPostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a39P02 &#8211; srf_protocol_violated \u53ef\u80fd\u7531\u65bc\u591a\u7a2e\u539f\u56e0\u5f15\u8d77\uff0c\u4f8b\u5982\u51fd\u6578\u5b9a\u7fa9\u4e0d\u6b63\u78ba\u3001SRFs \u4f7f\u7528\u4e0d\u7576\u6216\u6578\u64da\u5eab\u640d\u58de\u3002\u901a\u904e\u9a57\u8b49\u51fd\u6578\u5b9a\u7fa9\u3001\u6aa2\u67e5\u67e5\u8a62\u4f7f\u7528\u60c5\u6cc1\u548c\u6aa2\u67e5\u6578\u64da\u5eab\u640d\u58de\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u78ba\u4fdd PostgreSQL \u6578\u64da\u5eab\u7684\u9806\u5229\u904b\u884c\u3002<\/p>\n<p>\u5c0d\u65bc\u53ef\u9760\u4e14\u9ad8\u6027\u80fd\u7684 VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u901a\u904e\u6211\u5011\u4e00\u6d41\u7684 VPS \u8a17\u7ba1\u670d\u52d9\uff0c\u60a8\u53ef\u4ee5\u9ad4\u9a57\u865b\u64ec\u79c1\u6709\u670d\u52d9\u5668\u7684\u5f37\u5927\u548c\u9748\u6d3b\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-21384","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21384","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=21384"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21384\/revisions"}],"predecessor-version":[{"id":24157,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21384\/revisions\/24157"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}