{"id":302138,"date":"2020-04-19T15:00:15","date_gmt":"2020-04-19T15:00:15","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=302138"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=302138","title":{"rendered":"PostgeSQL: \u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 (\u0444\u0443\u043d\u043a\u0446\u0438\u0439) \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 \u0421"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/497990\/\">\u042d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0435\u0449\u0435 \u043f\u0430\u0440\u0443 \u043b\u0435\u0442 \u043d\u0430\u0437\u0430\u0434, \u0438 \u043d\u0435 \u0437\u043d\u0430\u043b \u043a\u0443\u0434\u0430 \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0432\u044b\u043b\u043e\u0436\u0438\u0442\u044c, \u0430 \u043f\u043e\u0442\u043e\u043c \u0438 \u0437\u0430\u0431\u044b\u043b.<\/p>\n<p>  \u0421\u043c\u044b\u0441\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u044f\u0437\u044b\u043a\u0430 \u0421 \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u0434\u043b\u044f PostgreSQL \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u043c\u0438 (\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432\u044b\u043c\u0438) \u044f\u0437\u044b\u043a\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u0432\u0435\u0441\u0442\u0438 \u043a \u0434\u0432\u0443\u043c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c: \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u041d\u0443 \u0430 \u043f\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u043c\u0443, \u043a\u043e\u0434 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0421 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0440\u0430\u0437 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c. \u0410 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e, \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 PostgreSQL \u0438 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u0440\u043e\u043c\u0435 \u043a\u0430\u043a \u043d\u0430 \u0421, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0442\u0438\u043f\u044b (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438) <strong>ANYELEMENT<\/strong>, <strong>ANYARRAY<\/strong> \u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432\u0430\u0436\u043d\u044b\u0439 <strong>VARIADIC<\/strong>.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h2>\u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 \u0421<\/h2>\n<p>  \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0434\u0432\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0438 \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0438\u0445. \u042d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043a PostgreSQL, \u043d\u043e \u043c\u044b \u0435\u0433\u043e \u0447\u0443\u0442\u044c \u0443\u043b\u0443\u0447\u0448\u0438\u043c \u0438 \u0441\u043e\u0431\u0435\u0440\u0435\u043c. \u0417\u0430\u0442\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043c \u0432 PostgreSQL \u0438 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0432\u044b\u0437\u043e\u0432 \u0438\u0437 SQL \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<pre><code class=\"cpp\">#include &quot;postgres.h&quot; #include &quot;fmgr.h&quot; PG_MODULE_MAGIC; PG_FUNCTION_INFO_V1(add_ab); Datum add_ab(PG_FUNCTION_ARGS) {     int32 arg_a = PG_GETARG_INT32(0);     int32 arg_b = PG_GETARG_INT32(1);     PG_RETURN_INT32(arg_a + arg_b); } <\/code><\/pre>\n<p>  \u0424\u0430\u0439\u043b <em>add_func.c<\/em> \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430. \u0422\u0430\u043a\u0436\u0435, \u043f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u043f\u043e \u043a\u043e\u0434\u0443<\/p>\n<ul>\n<li>#include \u00abpostgresql.h\u00bb: \u044d\u0442\u043e\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u0412\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0432\u0441\u0435\u0433\u0434\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/li>\n<li>#include \u00abfmgr.h\u00bb: \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 PG_* \u043c\u0430\u043a\u0440\u043e\u0441\u044b.<\/li>\n<li>PG_MODULE_MAGIC: \u043c\u0430\u043a\u0440\u043e\u0441, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0439 \u0447\u0442\u043e \u043c\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u044c \u0434\u043b\u044f PostgreSQL \u0432\u044b\u0448\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 8.2.<\/li>\n<li>PG_FUNC_INFO_V1: \u043c\u0430\u043a\u0440\u043e\u0441, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0439 \u0441\u043e\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u044f \u043e \u0432\u044b\u0437\u043e\u0432\u0430\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432\u043d\u0443\u0442\u0440\u0438 PostgreSQL. \u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0435\u0437 \u043d\u0435\u0433\u043e, \u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u0435 \u043e \u0432\u044b\u0437\u043e\u0432\u0435 \u0412\u0435\u0440\u0441\u0438\u0438 0, \u0438\u043d\u0430\u0447\u0435 \u0412\u0435\u0440\u0441\u0438\u0438 1.<\/li>\n<li>Datum: \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0412\u043e\u043e\u0431\u0449\u0435, \u044d\u0442\u043e \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u0438\u043f \u0432 PostgreSQL. \u0427\u0442\u043e-\u0442\u043e \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 VARIANT \u0443 Microsoft. \u041f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u00ab\u0441\u044b\u0440\u043e\u0439\u00bb \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435. \u041a\u0430\u043a \u0438\u0445 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0440\u0435\u0448\u0430\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445.<\/li>\n<li>add_ab(PG_FUNCTION_ARGS): \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043c\u0430\u043a\u0440\u043e\u0441\u043e\u043c. \u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u044e\u0431\u043e\u0435 \u043a\u043e\u043b-\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/li>\n<li>int32 arg_a = PG_GETARG_INT32(0): \u043c\u0430\u043a\u0440\u043e\u0441 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 (\u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u043d\u0443\u043b\u044f).<\/li>\n<li>PG_RETURN_INT32(arg_a + arg_b): \u043c\u0430\u043a\u0440\u043e\u0441 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/li>\n<\/ul>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u044d\u0442\u043e. \u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c\u0430\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 (*.so). \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <em>Makefile<\/em>. \u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043a\u043b\u044e\u0447\u0438 \u0438 \u043f\u0443\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c, \u043d\u043e \u043c\u044b \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <strong>PGXS<\/strong>. \u042d\u0442\u043e \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439, \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0442\u043e \u0432 \u0412\u0430\u0448\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 <em>-dev<\/em> \u0438 <em>-devel<\/em> \u043f\u0430\u043a\u0435\u0442\u044b \u0434\u043b\u044f PostgreSQL.<\/p>\n<pre><code class=\"bash\">MODULES = add_func PG_CONFIG = pg_config PGXS := $(shell $(PG_CONFIG) --pgxs) include $(PGXS) <\/code><\/pre>\n<p>  <\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c SQL \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/h2>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c SQL \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u0437 \u043d\u0430\u0448\u0435\u0439 \u0440\u0430\u043d\u043d\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f.  <\/p>\n<pre><code class=\"sql\">CREATE FUNCTION add(int, int)   RETURNS int AS '\/usr\/lib\/postgresql\/9.4\/lib\/add_func', 'add_ab' LANGUAGE C STRICT; <\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0451! \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0442\u0430\u043a,<\/p>\n<pre><code class=\"sql\">SELECT add(1, 2); <\/code><\/pre>\n<p>  <\/p>\n<h2>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443<\/h2>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u0435\u0441\u044c\u043c\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u0412\u0430\u043c \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e \u043a\u0430\u043a\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f PostgreSQL \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0438 \u043f\u043e \u043a\u0430\u043a\u043e\u043c\u0443 \u043f\u0443\u0442\u0438 \u043e\u043d \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u0430\u0439\u043b,<\/p>\n<pre><code class=\"sql\">CREATE FUNCTION add(int, int)   RETURNS int AS 'MODULE_PATHNAME', 'add_ab' LANGUAGE C STRICT; <\/code><\/pre>\n<p>  \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 <em>Makefile<\/em> \u0441\u0442\u0440\u043e\u043a\u0443,<\/p>\n<pre><code class=\"bash\">MODULES = add_func DATA_built = add_funcs.sql PG_CONFIG = pg_config PGXS := $(shell $(PG_CONFIG) --pgxs) include $(PGXS) <\/code><\/pre>\n<p>  <\/p>\n<h2>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u043c<\/h2>\n<p>  \u0412\u043d\u0435\u0441\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u043d\u0430\u0448 \u043f\u0440\u0438\u043c\u0435\u0440, \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f.<\/p>\n<h3>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432<\/h3>\n<p>  \u041f\u043e\u043c\u043d\u0438\u0442\u0435 \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 SQL \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <em>add(int,int)<\/em> \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e <strong>STRICT<\/strong>? \u041e\u043d\u043e \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u0438\u043d \u0438\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <em>NULL<\/em>, \u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0435\u0440\u043d\u0435\u0442 <em>NULL<\/em>. \u042d\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 SQL \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0435 <strong>&quot;+&quot;<\/strong> \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u0438\u043d \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <em>NULL<\/em>, \u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u0443\u0434\u0435\u0442 <em>NULL<\/em>.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 SQL \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u0443\u044e\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <em>sum()<\/em>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <em>NULL<\/em> \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c,<\/p>\n<ul>\n<li>\u0414\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0430 \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <em>NULL<\/em><\/li>\n<li>\u0415\u0441\u043b\u0438 \u043e\u0431\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0438\u043c\u0435\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <em>NULL<\/em>, \u0442\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c <em>NULL<\/em><\/li>\n<\/ul>\n<p>  <\/p>\n<pre><code class=\"cpp\">PG_FUNCTION_INFO_V1(add_ab_null); Datum add_ab_null(PG_FUNCTION_ARGS) {     int32 not_null = 0;     int32 sum = 0;     if (!PG_ARGISNULL(0))     {         sum += PG_GETARG_INT32(0);         not_null = 1;     }     if (!PG_ARGISNULL(1))     {         sum += PG_GETARG_INT32(1);         not_null = 1;     }     if (not_null)     {         PG_RETURN_INT32(sum);     }     PG_RETURN_NULL(); } <\/code><\/pre>\n<p>  \u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u044d\u0442\u043e,<\/p>\n<pre><code class=\"sql\">CREATE FUNCTION add(int, int)  RETURNS int AS '$libdir\/add_func', 'add_ab_null' LANGUAGE C;  SELECT add(NULL, NULL) AS must_be_null, add(NULL, 1) AS must_be_one; -[ RECORD 1 ]+-- must_be_null | must_be_one  | 1 <\/code><\/pre>\n<p>  \u0410 \u0432\u043e\u0442 \u043a\u0430\u043a \u044d\u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c\u0438 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 PostgreSQL,  <\/p>\n<pre><code class=\"sql\">SELECT (CASE WHEN (a IS null) AND (b IS null) (THEN null ELSE coalesce(a, 0) + coalesce(b,0) END) FROM (SELECT 1::int AS a, null::int AS b) s; -[ RECORD 1 ]  case | 1 <\/code><\/pre>\n<p>  <\/p>\n<h3>\u041b\u044e\u0431\u043e\u0435 \u043a\u043e\u043b-\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/h3>\n<p>  \u041a\u0430\u043a \u0412\u044b \u0443\u0436\u0435 \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438, \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043c\u0430\u043a\u0440\u043e\u0441\u044b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0435 \u043a\u043e\u043b-\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0432 \u0446\u0438\u043a\u043b\u0435 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f,<\/p>\n<pre><code class=\"cpp\">if (!PG_ARGISNULL(i)) {     sum += PG_GETARG_INT32(i);     not_null = 1; } <\/code><\/pre>\n<p>  \u0423 \u0412\u0430\u0441 \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441, \u043d\u043e \u0432\u0435\u0434\u044c \u043c\u043e\u0436\u043d\u043e \u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u0418 \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0442\u0430\u043a \u0438 \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u043d\u043e \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u0438\u0437-\u0437\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 PostgreSQL, \u044d\u0442\u043e \u043d\u0435 \u0441\u0442\u043e\u043b\u044c \u0443\u0436 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430. \u041d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0435\u0435 \u0440\u0435\u0448\u0438\u0442\u044c. \u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 PostgreSQL \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u0433\u0434\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 text[], \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0446\u0435\u043b\u0435\u0439,<\/p>\n<pre><code class=\"cpp\">#include &quot;utils\/array.h&quot;     \/\/ \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 #include &quot;catalog\/pg_type.h&quot; \/\/ INT4OID PG_MODULE_MAGIC; Datum add_int32_array(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(add_int32_array); Datum add_int32_array(PG_FUNCTION_ARGS) {     \/\/ \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432.     \/\/ \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0446\u0435\u043b\u044b\u0435 int.     ArrayType *input_array;     int32 sum = 0;     bool not_null = false;      Datum *datums;     bool *nulls;     int count;     int i;     input_array = PG_GETARG_ARRAYTYPE_P(0); \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430. \u0421\u0443\u0444\u0444\u0438\u043a\u0441 *_P \u0432 \u043a\u043e\u043d\u0446\u0435                                             \/\/ \u043c\u0430\u043a\u0440\u043e\u0441\u0430 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043e \u0442\u043e\u043c \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c, \u0430 \u043d\u0435 INT32      \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0447\u0442\u043e \u043c\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b INT32 (INT4)     Assert(ARR_ELEMTYPE(input_array) == INT4OID);      \/\/ \u0423\u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u0438\u043c\u0441\u044f \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u0439     if (ARR_NDIM(input_array) &gt; 1)         ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg(&quot;1-dimensional array needed&quot;)));      deconstruct_array(input_array, \/\/ \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432                       INT4OID,     \/\/ \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b                       4,           \/\/ \u0440\u0430\u0437\u043c\u0435\u0440 \u0446\u0435\u043b\u043e\u0433\u043e \u0432 \u0431\u0430\u0439\u0442\u0430\u0445                       true,        \/\/ int4 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e                       'i',         \/\/ \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435 'i'                       &datums, &nulls, &count); \/\/ \u0437\u0434\u0435\u0441\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442       for(i = 0; i &lt; count; i++)     {         \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0438 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043d\u0443\u043b\u0435\u0432\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b         if (nulls[i])             continue;          \/\/ \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u043c, \u043f\u043e\u043c\u043d\u044f \u0447\u0442\u043e \u0442\u0430\u043c \u0431\u044b\u043b\u0438 \u043d\u0435 \u043d\u0443\u043b\u0435\u0432\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b         sum += DatumGetInt32(datums[i]);         not_null = true;     }     if (not_null)         PG_RETURN_INT32(sum);      PG_RETURN_NULL(); } <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430, \u043f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u043f\u043e \u043a\u043e\u0434\u0443,<\/p>\n<ul>\n<li>\u041d\u0435\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0434\u043b\u044f \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f <em>ArrayType<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432<\/li>\n<li>\u0414\u043b\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u0435\u0440\u0432\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u043a\u0440\u043e\u0441 <em>PG_GETARG_ARRAYTYPE_P<\/em><\/li>\n<li>\u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u0439 ARR_NDIM<\/li>\n<li>\u0422\u0438\u043f OID \u0434\u043b\u044f int4 (= 23) \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u043a\u0430\u043a INT4OID. \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0442\u0438\u043f\u043e\u0432, \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f SQL,\n<pre><code class=\"sql\">select oid, typlen, typbyval, typalign from pg_type where typname = 'int4'; -[ RECORD 1 ] oid | 23 typlen | 4 typbyval | t typalign | i <\/code><\/pre>\n<p>  <\/li>\n<\/ul>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043d\u0430\u0443\u0447\u0438\u0442\u044c PostgreSQL \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e, \u043e\u0431\u044a\u044f\u0432\u0438\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 int[],<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION add_arr(int[]) RETURNS int AS '$libdir\/add_func', 'add_int32_array' LANGUAGE C STRICT; <\/code><\/pre>\n<p>  \u0418 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c,<\/p>\n<pre><code class=\"sql\">SELECT add_arr('{1,2,3,4,5,6,7,8,9}'); -[ RECORD 1 ] add_arr | 45 SELECT add_arr(ARRAY[1,2,NULL]); -[ RECORD 1 ] add_arr | 3 SELECT add_arr(ARRAY[NULL::int]); -[ RECORD 1 ] add_arr | <\/code><\/pre>\n<p>  <\/p>\n<h2>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438<\/h2>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u0432\u0435\u0434\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0442\u043e\u0433\u0438, \u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439.<\/p>\n<h3>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h3>\n<p>  \u0410\u0432\u0442\u043e\u0440\u044b \u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u0438 \u0421\u0423\u0411\u0414 PostgreSQL \u0443\u0434\u0435\u043b\u044f\u044e\u0442 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u0438 \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044e \u0443\u0442\u0435\u0447\u0435\u043a. \u042d\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0421\u0423\u0411\u0414 \u0438 \u0435\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439. \u041e\u0441\u043d\u043e\u0432\u0430\u043d\u043e \u044d\u0442\u043e \u043d\u0430 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u2014 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<h4>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c palloc() \u0438 pfree()<\/h4>\n<p>  \u0412\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432 PostgreSQL \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0430 \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0435\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 palloc() \u0438 pfree().<\/p>\n<h4>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440<\/h4>\n<p>  \u0412\u0441\u0435\u0433\u0434\u0430 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b\u0437\u044b\u0432\u0430\u0439\u0442\u0435 memset() \u043f\u043e\u0441\u043b\u0435 palloc().<\/p>\n<h4>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u044b\u0435 \u0444\u0430\u0439\u043b\u044b<\/h4>\n<p>  \u041a\u0430\u0436\u0434\u043e\u0435 \u0432\u0430\u0448\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0434\u0432\u0443\u0445 \u0444\u0430\u0439\u043b\u043e\u0432: postgres.h \u0438 fmgr.h.<\/p>\n<h2>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438<\/h2>\n<p>  <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/spi.html\">PostgreSQL: Server Programming Interface<\/a>.<\/p>\n<p>  <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/xfunc-c.html\">PostgreSQL: User Defined Functions in C<\/a><\/p>\n<p>  <a href=\"http:\/\/www.pgxn.org\/\">PostgreSQL Extension Network<\/a><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/497990\/\"> https:\/\/habr.com\/ru\/post\/497990\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/497990\/\">\u042d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0435\u0449\u0435 \u043f\u0430\u0440\u0443 \u043b\u0435\u0442 \u043d\u0430\u0437\u0430\u0434, \u0438 \u043d\u0435 \u0437\u043d\u0430\u043b \u043a\u0443\u0434\u0430 \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0432\u044b\u043b\u043e\u0436\u0438\u0442\u044c, \u0430 \u043f\u043e\u0442\u043e\u043c \u0438 \u0437\u0430\u0431\u044b\u043b.<\/p>\n<p>  \u0421\u043c\u044b\u0441\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u044f\u0437\u044b\u043a\u0430 \u0421 \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u0434\u043b\u044f PostgreSQL \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u043c\u0438 (\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432\u044b\u043c\u0438) \u044f\u0437\u044b\u043a\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u0432\u0435\u0441\u0442\u0438 \u043a \u0434\u0432\u0443\u043c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c: \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u041d\u0443 \u0430 \u043f\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u043c\u0443, \u043a\u043e\u0434 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0421 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0440\u0430\u0437 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c. \u0410 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e, \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 PostgreSQL \u0438 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u0440\u043e\u043c\u0435 \u043a\u0430\u043a \u043d\u0430 \u0421, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0442\u0438\u043f\u044b (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438) <strong>ANYELEMENT<\/strong>, <strong>ANYARRAY<\/strong> \u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432\u0430\u0436\u043d\u044b\u0439 <strong>VARIADIC<\/strong>.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-302138","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/302138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=302138"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/302138\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=302138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=302138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=302138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}