{"id":375521,"date":"2024-05-21T06:26:39","date_gmt":"2024-05-21T06:26:39","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=375521"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=375521","title":{"rendered":"<span>\u0414\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0432 SQL \u0432 \u043e\u0434\u043d\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0443<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u0442\u043e\u044f\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430: \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u0440\u0430\u0431\u043e\u0442\u0443 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430 \u043f\u0430\u043f\u043e\u043a \u0432 PostgreSQL. \u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u043c\u044b \u043d\u0430\u0442\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0443 \u0440\u0435\u0448\u0430\u043b\u0438, \u043d\u043e\u00a0<s>\u0434\u0435\u043b\u0430\u043b\u0438 \u044d\u0442\u043e \u0431\u0435\u0437 \u0443\u0432\u0430\u0436\u0435\u043d\u0438\u044f<\/s>\u00a0\u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b\u0438 \u043d\u0435 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u043e, \u043d\u0430\u0440\u0443\u0448\u0430\u043b\u0438 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438, \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u0438 \u0431\u041e\u043b\u044c\u0448\u0435\u0433\u043e \u0432\u043e\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0432 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0438 \u0442.\u0434. \u041f\u043e\u0442\u043e\u043c\u0443 \u0437\u0430\u0434\u0430\u043b\u0441\u044f \u0446\u0435\u043b\u044c\u044e: \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 (\u0432 \u043e\u0431\u0449\u0435\u043c \u0432\u0438\u0434\u0435) \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u0432, \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a, \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 (\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439) \u0438 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 PostgreSQL (\u0432\u0435\u0440\u0441\u0438\u0438 16).<\/p>\n<p>\u0421\u043b\u043e\u0432\u0430\u0440\u044c: <br \/>\u0443\u0437\u0435\u043b &#8212; \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0431\u0434, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0443\u0437\u0435\u043b \u0434\u0435\u0440\u0435\u0432\u0430 <br \/>\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c &#8212; \u0443\u0437\u0435\u043b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u0438\u0435 \u0443\u0437\u043b\u044b <br \/>\u0434\u043e\u0447\u043a\u0430 &#8212; \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0443\u0437\u0435\u043b <br \/>\u043a\u043e\u0440\u0435\u043d\u044c &#8212; \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f (\u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u0447\u043a\u043e\u0439)<\/p>\n<ol>\n<li>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0435\u0440\u0435\u0432\u043e \u0432 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e, \u0441\u043e\u0441\u043b\u0430\u0432\u0448\u0438\u0441\u044c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043d\u0430 \u0441\u0430\u043c\u0443 \u0441\u0435\u0431\u044f. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u0430\u0436\u0434\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0431\u043e\u0439 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u043c\u0435\u0435\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u043e\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0438 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043b\u044e\u0431\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u043e\u0447\u0435\u043a. \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint,    foreign key (parent_id)     references node (id) );<\/code><\/pre>\n<p>\u0422\u0438\u043f id \u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u0435\u0433\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u044b\u0431\u0440\u0430\u043d\u044b \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e \u0438 \u043d\u0430 \u043b\u043e\u0433\u0438\u043a\u0443 \u043d\u0435 \u0432\u043b\u0438\u044f\u044e\u0442. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043c\u0435\u043d\u044f\u0442\u044c \u0438\u0445 \u043d\u0430 \u0441\u0432\u043e\u0435 \u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21161: \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430<\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441, \u0430 \u043a\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438\u0437 \u0431\u0430\u0437\u044b? \u041d\u0435\u0443\u0436\u0435\u043b\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u0435\u043d\u044c \u043f\u043e id, \u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c? \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u043d\u0435\u0442. \u0427\u0442\u043e\u0431\u044b \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 (\u0441\u0442\u0440\u043e\u043a\u0430 5):<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint, tree_id   bigint not null,    foreign key (parent_id)     references node (id) );<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0435 \u0434\u0435\u0440\u0435\u0432\u043e, \u043a\u0430\u043a\u0430\u044f \u0431\u044b \u043c\u043d\u043e\u0433\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443 \u043d\u0435\u0433\u043e \u043d\u0438 \u0431\u044b\u043b\u0430, \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043e \u043e\u0434\u043d\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c. \u042d\u0442\u0438\u043c tree_id \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 id: \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043b\u0438 \u043b\u044e\u0431\u0430\u044f \u0434\u0440\u0443\u0433\u0430\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e. \u041c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c id \u043a\u043e\u0440\u043d\u044f. \u041e\u0434\u043d\u0430\u043a\u043e \u0442\u0430\u043a\u043e\u0435 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u0435 \u043d\u0435\u0441\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0434\u0432\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21162: \u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u0440\u043d\u0438<\/p>\n<p>\u0412 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0445\u0435\u043c\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u043e\u0435 tree_id \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043d\u0435 \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u0443\u0437\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430, \u0442.\u0435. \u043d\u0435 \u0438\u043c\u0435\u0442\u044c \u043e\u0431\u0449\u0435\u0433\u043e \u043a\u043e\u0440\u043d\u044f. \u041a\u0430\u043a \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c? \u041d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0431\u044b\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0443\u0437\u0435\u043b \u0441 parent_id == null. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0441 parent_id == null \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0432 \u0431\u0430\u0437\u0435 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438? \u0412\u043d\u043e\u0432\u044c, \u043d\u0435\u0442. \u041f\u043e\u043c\u043e\u0436\u0435\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441. \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043e\u0440\u043d\u044f (\u043a\u043e\u0433\u0434\u0430 parent_id == null) \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 tree_id (\u0441\u0442\u0440\u043e\u043a\u0430 10):<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint, tree_id   bigint not null,    foreign key (parent_id)     references node (id) ); create unique index on node(tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c\u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\u0438 \u0441 \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (\u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 null \u0432 parent_id \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 tree_id). \u041a\u0430\u043a \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u0432 \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0435 \u0443\u0432\u0438\u0434\u0438\u043c \u043f\u043e\u0437\u0436\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21163: \u0421\u043c\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432<\/p>\n<p>\u0412 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0445\u0435\u043c\u0435 \u0443\u0437\u043b\u044b \u043e\u0434\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u043e\u0433\u0443\u0442 \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0443\u0437\u043b\u044b \u0434\u0440\u0443\u0433\u043e\u0433\u043e. \u041d\u0435\u0443\u0436\u0435\u043b\u0438 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0441\u0442\u0430\u0432\u043a\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c tree_id \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f? \u0418 \u0441\u043d\u043e\u0432\u0430, \u043d\u0435\u0442. \u0417\u0434\u0435\u0441\u044c \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0439 \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447. \u0422.\u0435. \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442 parent_id \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0440\u0430\u0432\u0435\u043d id \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f, \u043d\u043e \u0438 tree_id \u0434\u043e\u0447\u0435\u0440\u0438 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0440\u0430\u0432\u0435\u043d tree_id \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u0430\u0440\u0430 id \u0438 tree_id \u0431\u044b\u043b\u0430 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u0430. \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u044d\u0442\u043e \u0443\u0436\u0435 \u0442\u0430\u043a (id \u0443\u043d\u0438\u043a\u0430\u043b\u0435\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u044b), \u043d\u043e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0433\u043e \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430 \u043d\u0443\u0436\u043d\u043e \u044f\u0432\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0430 \u043f\u0430\u0440\u0443 id, tree_id (\u0441\u0442\u0440\u043e\u043a\u0438 7,9,10):<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint, tree_id   bigint not null,    unique (id, tree_id),  foreign key (parent_id, tree_id)     references node (id, tree_id) ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21164: \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430<\/p>\n<p>\u041f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0434\u043e\u0447\u043a\u0438, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u044f \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430. \u041d\u0435\u0443\u0436\u0435\u043b\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u0430\u0442\u044c \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u0435\u043a \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0432\u0441\u0435 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443 id? \u041d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u041d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447 \u043d\u0430 \u043a\u0430\u0441\u043a\u0430\u0434\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 (\u0441\u0442\u0440\u043e\u043a\u0430 11):<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint, tree_id   bigint not null,    unique (id, tree_id),  foreign key (parent_id, tree_id)     references node (id, tree_id)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21165: \u0426\u0438\u043a\u043b\u044b<\/p>\n<p>\u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0432\u044f\u0437\u0435\u0439, \u0447\u0442\u043e \u043d\u0430\u0440\u0443\u0448\u0430\u0435\u0442 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0443\u0442\u0435\u0447\u043a\u0430\u043c \u043f\u0430\u043c\u044f\u0442\u0438, \u043b\u043e\u043c\u0430\u0435\u0442 \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0443 \u0438 \u0442.\u0434. \u0412 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>id<\/p>\n<\/th>\n<th>\n<p>parent_id<\/p>\n<\/th>\n<th>\n<p>tree_id<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0417\u0434\u0435\u0441\u044c \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u0430 \u043d\u0430 \u0441\u0435\u0431\u044f. \u0422\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0440\u0435\u0442\u043e\u043c \u043d\u0430 \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e \u043c\u0435\u0436\u0434\u0443 id \u0438 parent_id \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 (\u0441\u0442\u0440\u043e\u043a\u0430 7):<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint, tree_id   bigint not null,    constraint node_cycle_check check (parent_id != id),  unique (id, tree_id),  foreign key (parent_id, tree_id)     references node (id, tree_id)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u041d\u043e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>id<\/p>\n<\/th>\n<th data-colwidth=\"220\" width=\"220\">\n<p>parent_id<\/p>\n<\/th>\n<th>\n<p>tree_id<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<td data-colwidth=\"220\" width=\"220\">\n<p align=\"left\">2<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">2<\/p>\n<\/td>\n<td data-colwidth=\"220\" width=\"220\">\n<p align=\"left\">1<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u041d\u0435\u0443\u0436\u0435\u043b\u0438 \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0432 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u0439 \u0446\u0435\u043f\u043e\u0447\u043a\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0432\u0448\u0435\u0433\u043e\u0441\u044f \u0446\u0438\u043a\u043b\u0430? \u041d\u0435\u0442, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0440\u0430\u0437 \u0441\u0445\u0438\u0442\u0440\u0438\u0442\u044c. \u041c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u044f\u043c\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438. \u041d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 id \u0432\u0441\u0435\u0445 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0434\u043e \u043a\u043e\u0440\u043d\u044f \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043b\u0438 \u0432 \u043d\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 id. \u041a\u0441\u0442\u0430\u0442\u0438, \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 parent_id \u2260 id \u0443\u0436\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e\u0439 (\u0441\u0442\u0440\u043e\u043a\u0438 6, 8):<\/p>\n<pre><code class=\"sql\">create table node ( id         bigint primary key generated always as identity, parent_id  bigint, tree_id    bigint not null, parent_ids bigint[],    constraint node_cycle_check check (not (parent_ids @> array[id])),  unique (id, tree_id),  foreign key (parent_id, tree_id)     references node (id, tree_id)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u041f\u0440\u0435\u0434\u0432\u043e\u0441\u0445\u0438\u0449\u0430\u044e \u043c\u043d\u043e\u0433\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0442\u0430\u043a\u043e\u043c\u0443 \u0448\u0430\u0433\u0443, \u043d\u043e \u043f\u0440\u043e\u0448\u0443 \u0437\u0430\u043f\u0430\u0441\u0442\u0438\u0441\u044c \u0442\u0435\u0440\u043f\u0435\u043d\u0438\u0435\u043c, \u0434\u0430\u043b\u044c\u0448\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u0441\u0435 \u043e\u0442\u0432\u0435\u0442\u044b. \u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 (@>) \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e parent_ids nullable. \u0410\u0442\u0440\u0438\u0431\u0443\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c null \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f. \u041c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0433\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c, \u0438 \u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 (\u0441\u0435\u0439\u0447\u0430\u0441 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c). \u041e\u0434\u043d\u0430\u043a\u043e, \u0437\u0430\u0431\u0435\u0433\u0430\u044f \u0432\u043f\u0435\u0440\u0435\u0434, \u0441\u043a\u0430\u0436\u0443, \u0447\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u043d\u0430 nullable.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21166: \u041a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c parent_ids<\/p>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u0432 parent_ids \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e. \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0440\u0430\u0436\u0430\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0431\u0430\u0437\u044b? \u0412\u0441\u0435-\u0442\u0430\u043a\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u043c \u043f\u0440\u043e\u0431\u0435\u0433\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f\u043c? \u041d\u0435\u0442. \u0417\u0434\u0435\u0441\u044c \u0441\u043d\u043e\u0432\u0430 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430. \u041d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0441\u044f \u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e, \u0430 \u0437\u0430\u0432\u0438\u0441\u0435\u043b \u043e\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438. \u041d\u043e \u043d\u0430 \u0447\u0442\u043e \u0435\u0433\u043e \u0441\u0441\u044b\u043b\u0430\u0442\u044c? \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c parent_ids \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u043e\u0442 parent_ids \u0434\u043e\u0447\u0435\u0440\u0438 \u043d\u0430 \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u0422\u043e\u0433\u0434\u0430 \u0432\u043d\u043e\u0432\u044c \u0441\u0445\u0438\u0442\u0440\u0438\u043c \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0430\u0442\u0440\u0438\u0431\u0443\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 parent_ids \u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e id. \u0410 parent_ids \u0431\u0443\u0434\u0435\u0442 \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043d\u0435\u0433\u043e. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0433\u043e \u0432 \u0438\u043d\u0434\u0435\u043a\u0441 \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447 (\u0441\u0442\u0440\u043e\u043a\u0438 7, 11, 13, 14):<\/p>\n<pre><code class=\"sql\">create table node ( id                 bigint primary key generated always as identity, parent_id          bigint, tree_id            bigint   not null, parent_ids         bigint[], parent_ids_with_it bigint[] not null,    constraint node_cycle_check check (not (parent_ids @> array [id])),  unique (id, tree_id, parent_ids_with_it),  foreign key (parent_id, tree_id, parent_ids)     references node (id, tree_id, parent_ids_with_it)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u0421\u0442\u043e\u0438\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u043d\u0435 nullable, \u0442.\u043a. \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u044d\u0442\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 == 1 (\u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f).<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21167: \u041a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u0442\u043d\u043e\u0441\u0442\u044c parent_ids_with_it<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c parent_ids_with_it \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e. \u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043d\u0438\u043c? \u041c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c, \u0442\u0443\u0442 \u043f\u0440\u043e\u0431\u0435\u0436\u0438\u043c\u0441\u044f \u043f\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f\u043c? \u041d\u0438 \u0432 \u043a\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 parent_ids. \u0421 \u201cgenerated always as\u201d \u043c\u044b \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u043f\u0440\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 id, \u043d\u043e \u0438 \u0437\u0434\u0435\u0441\u044c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043a\u0441\u0442\u0430\u0442\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 parent_ids \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 id (\u0441\u0442\u0440\u043e\u043a\u0430 7):<\/p>\n<pre><code class=\"sql\">create table node ( id                 bigint primary key generated always as identity, parent_id          bigint, tree_id            bigint   not null, parent_ids         bigint[], parent_ids_with_it bigint[] not null generated always as (parent_ids || id) stored,  constraint node_cycle_check check (not (parent_ids @> array [id])),  unique (id, tree_id, parent_ids_with_it),  foreign key (parent_id, tree_id, parent_ids)     references node (id, tree_id, parent_ids_with_it)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 (parent_ids || id) \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 id. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u044d\u0442\u043e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u043d\u0435\u043b\u044c\u0437\u044f \u0437\u0430\u0434\u0430\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u043e\u043d \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 parent_ids == null (\u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f) \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0432\u0435\u0440\u043d\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043e\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c (\u0442\u0435\u043a\u0443\u0449\u0438\u043c id). <\/p>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 stored. \u042d\u0442\u043e\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u043c \u0438\u043b\u0438 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c (\u0442.\u0435. \u0432\u044b\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438). \u0414\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u043f\u043e\u0434\u043e\u0448\u0435\u043b \u0431\u044b \u0432\u0442\u043e\u0440\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0435\u0435 \u0434\u0438\u0441\u043a\u043e\u0432\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e, \u043e\u0434\u043d\u0430\u043a\u043e PostgreSQL \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 (\u043f\u043e 16 \u0432\u0435\u0440\u0441\u0438\u044e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e) \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u044f\u0432\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u043c.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21168: \u041a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c parent_ids \u0432 \u043a\u043e\u0440\u043d\u0435<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c parent_ids, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d parent_ids_with_it \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f. \u0412\u0441\u0435 \u044d\u0442\u043e \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u0434\u043e\u0447\u0435\u043a. \u0410 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043a\u043e\u0440\u043d\u0435\u043c? \u0412\u0435\u0434\u044c \u0435\u0433\u043e parent_id == null, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0435\u0433\u043e \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 (\u043d\u0435 \u0438\u0449\u0435\u0442 \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441\u0430). \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 parent_ids \u043b\u044e\u0431\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0442\u0435\u043c \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u043a\u0430\u0445. \u0422.\u0435. \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f \u0438 parent_id, \u0438 parent_ids \u0431\u044b\u043b\u0438 null (\u0441\u0442\u0440\u043e\u043a\u0430 10):<\/p>\n<pre><code class=\"sql\">create table node ( id                 bigint primary key generated always as identity, parent_id          bigint, tree_id            bigint   not null, parent_ids         bigint[], parent_ids_with_it bigint[] not null generated always as (parent_ids || id) stored,  constraint node_cycle_check check (not (parent_ids @> array [id])), constraint node_root_check check ((parent_ids is null) = (parent_id is null)),  unique (id, tree_id, parent_ids_with_it),  foreign key (parent_id, tree_id, parent_ids)     references node (id, tree_id, parent_ids_with_it)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21169: \u0410\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c parent_ids<\/p>\n<p>\u041a\u0430\u043a \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438 parent_ids? \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u043b\u0438 \u0443\u0437\u0435\u043b (\u0442.\u0435. \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438 \u0435\u0433\u043e parent_id \u0438 parent_ids). \u041a\u0430\u043a \u0430\u043a\u0442\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0443 \u0432\u0441\u0435\u0445 \u0435\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u0435\u0439? \u0417\u0434\u0435\u0441\u044c \u0437\u0430 \u043d\u0430\u0441 \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u0441\u043a\u0430\u0434\u043d\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435. \u042d\u0442\u0430 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u0430\u043c \u043a\u043b\u044e\u0447 \u0432\u0441\u043b\u0435\u0434 \u0437\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430(\u043e\u0432), \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043b\u044e\u0447 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f (\u0441\u0442\u0440\u043e\u043a\u0430 17):<\/p>\n<pre><code class=\"sql\">create table node ( id                 bigint primary key generated always as identity, parent_id          bigint, tree_id            bigint   not null, parent_ids         bigint[], parent_ids_with_it bigint[] not null generated always as (parent_ids || id) stored,  constraint node_cycle_check check (not (parent_ids @> array [id])), constraint node_root_check check ((parent_ids is null) = (parent_id is null)),  unique (id, tree_id, parent_ids_with_it),  foreign key (parent_id, tree_id, parent_ids)     references node (id, tree_id, parent_ids_with_it)     on delete cascade     on update cascade  ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0443 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0437\u043b\u0430 (\u043c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u0432 \u0434\u0440\u0443\u0433\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e) \u0438 \u0432\u0441\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0442\u044f\u043d\u0443\u0442\u0441\u044f \u0443 \u0432\u0441\u0435\u0445 \u0435\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u0435\u0439. <\/p>\n<p>\u0411\u0443\u0434\u044c\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b! \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443 \u043a\u0430\u0441\u043a\u0430\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0446\u0438\u043a\u043b\u044b. \u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435? \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0435\u0441\u0442\u044c 2 \u0437\u0430\u043f\u0438\u0441\u0438: \u043a\u043e\u0440\u0435\u043d\u044c \u0438 \u0435\u0433\u043e \u0434\u043e\u0447\u043a\u0430:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>id<\/p>\n<\/th>\n<th>\n<p>parent_id<\/p>\n<\/th>\n<th>\n<p>tree_id<\/p>\n<\/th>\n<th>\n<p>parent_ids<\/p>\n<\/th>\n<th>\n<p>parent_ids_with_it<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<td>\n<p align=\"left\">null<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<td>\n<p align=\"left\">null<\/p>\n<\/td>\n<td>\n<p align=\"left\">{1}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">2<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<td>\n<p align=\"left\">{1}<\/p>\n<\/td>\n<td>\n<p align=\"left\">{1,2}<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0421\u0441\u044b\u043b\u0430\u0435\u043c \u043a\u043e\u0440\u0435\u043d\u044c \u043d\u0430 \u0441\u0432\u043e\u044e \u0434\u043e\u0447\u043a\u0443:<\/p>\n<pre><code class=\"sql\">update node set parent_id = 2, parent_ids = [1,2] where id = 1;<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0411\u0414 \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u0430\u0435\u0442 parent_ids_with_it \u0443 \u043a\u043e\u0440\u043d\u044f, \u0438 \u0432\u043c\u0435\u0441\u0442\u043e {1} \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f {1,2,1}. \u0422\u043e\u0433\u0434\u0430 \u0443 \u0434\u043e\u0447\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d parent_ids \u043d\u0430 {1,2,1}, \u0430 parent_ids_with_it \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u0430\u043d \u043d\u0430 {1,2,1,2}. \u0427\u0442\u043e \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c parent_ids \u0443 \u043a\u043e\u0440\u043d\u044f \u043d\u0430 {1,2,1,2} \u0438 \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u0430\u0442\u044c parent_ids_with_it \u043d\u0430 {1,2,1,2,1}\u2026 \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u043a\u0440\u0443\u0433 \u0437\u0430\u043c\u044b\u043a\u0430\u0435\u0442\u0441\u044f, \u0438 \u0411\u0414 \u043f\u0440\u043e\u0434\u0435\u043b\u044b\u0432\u0430\u0435\u0442 \u044d\u0442\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e. \u0417\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u0432\u0438\u0441\u0430\u0435\u0442 \u0434\u043e \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u211610: \u0420\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u043a\u043e\u0440\u0435\u043d\u044c \u043d\u0430 \u0434\u0435\u0440\u0435\u0432\u043e<\/p>\n<p>\u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u0440\u043d\u0435\u0439 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430. \u0420\u0430\u043d\u0435\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u043d\u044f. \u0410 \u043a\u0430\u043a \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u043a\u043e\u0440\u0435\u043d\u044c? \u041d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u044d\u0442\u043e \u0443\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043d\u043e. \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0443 \u0434\u0435\u0440\u0435\u0432\u0430 (\u0442.\u0435. \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 tree_id) \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0431\u044b\u0442\u044c \u043a\u043e\u0440\u043d\u044f (\u0442.\u0435. \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0443\u0437\u0435\u043b \u0441 parent_id==null) \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0434\u0432\u0443\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445: \u043a\u043e\u0440\u0435\u043d\u044c \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0443\u0437\u0435\u043b \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0438\u043b\u0438 \u043a\u043e\u0440\u0435\u043d\u044c \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u0438\u0437 \u0441\u0432\u043e\u0438\u0445 \u0434\u043e\u0447\u0435\u043a (\u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u043d\u0430 \u0441\u0435\u0431\u044f). \u041e\u0431\u0430 \u044d\u0442\u0438\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f \u0443\u0436\u0435 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0432 \u0442\u0435\u043a\u0443\u0449\u0435 \u0441\u0445\u0435\u043c\u0435. \u041c\u043e\u0436\u043d\u043e \u043a\u043e\u043d\u0441\u0442\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u2013 \u0435\u0441\u043b\u0438 \u0432 \u0411\u0414 \u0435\u0441\u0442\u044c \u0434\u0435\u0440\u0435\u0432\u043e, \u043e\u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u0440\u043e\u0432\u043d\u043e 1 \u043a\u043e\u0440\u0435\u043d\u044c.<\/p>\n<\/li>\n<li>\n<p>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f<\/p>\n<p>\u0418\u0442\u0430\u043a, \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432. \u041c\u043e\u0436\u043d\u043e \u0431\u0440\u0430\u0442\u044c \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f. \u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043f\u0430\u0440\u0430 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439.<\/p>\n<ol>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0441\u0445\u0435\u043c\u0443, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f parent_ids, parent_id \u0441\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u0435\u043d. \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u043d \u043d\u0435 \u043d\u0435\u0441\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u044b \u043d\u0435 \u0431\u044b\u043b\u043e \u0432 parent_ids, \u0430 \u043a \u0442\u043e\u043c\u0443 \u0436\u0435 \u0435\u0449\u0435 \u0440\u0430\u0437\u0434\u0443\u0432\u0430\u0435\u0442 \u0438\u043d\u0434\u0435\u043a\u0441, \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447 \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 null \u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f. \u041d\u043e \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0431\u0435\u0437\u0431\u043e\u043b\u0435\u0437\u043d\u0435\u043d\u043d\u043e \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0435\u0433\u043e? \u0412\u043f\u043e\u043b\u043d\u0435. \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c 2 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430: tree_id \u0438 parent_ids_with_it. \u041c\u043e\u0436\u043d\u043e \u043b\u0438 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u043e\u0439 \u043f\u0430\u0440\u044b? \u0414\u0430. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c id \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0437\u0430\u043f\u0438\u0441\u0438. \u0422\u0430\u043a \u043a\u0430\u043a id \u0443\u043d\u0438\u043a\u0430\u043b\u0435\u043d, \u0442\u043e \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u0442\u0441\u044f. \u0417\u043d\u0430\u0447\u0438\u0442, \u0443\u043d\u0438\u043a\u0430\u043b\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432. \u0417\u043d\u0430\u0447\u0438\u0442, \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u0430 \u043f\u0430\u0440\u0430. \u041f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f parent_id \u0441\u0445\u0435\u043c\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">create table node ( id                 bigint primary key generated always as identity, tree_id            bigint   not null, parent_ids         bigint[], parent_ids_with_it bigint[] not null generated always as (parent_ids || id) stored,  constraint node_cycle_check check (not (parent_ids @> array [id])),  unique (tree_id, parent_ids_with_it),  foreign key (tree_id, parent_ids)     references node (tree_id, parent_ids_with_it)     on delete cascade     on update cascade  ); create unique index on node (tree_id) where parent_ids is null;<\/code><\/pre>\n<p>\u0423\u0434\u0430\u043b\u0435\u043d\u043e:<br \/>&#8212; \u0430\u0442\u0440\u0438\u0431\u0443\u0442 parent_id<br \/>&#8212; \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 node_root_check<br \/>&#8212; parent_id \u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u0435 \u0438 \u0432\u043d\u0435\u0448\u043d\u0435\u043c \u043a\u043b\u044e\u0447\u0435<br \/>&#8212; \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0432 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u0435 \u043d\u0430 tree_id \u2013 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 null \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443 parent_ids.\u00a0<\/p>\n<p>\u0412\u043e\u0437\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 \u043e \u0442\u043e\u043c, \u0434\u0435\u043b\u0430\u0442\u044c \u043b\u0438 parent_ids nullable \u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f \u0438\u043b\u0438 \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432, &#8212; \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 parent_ids \u043e\u0431\u044f\u0437\u0430\u043d \u0431\u044b\u0442\u044c null \u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f, \u0430 \u043d\u0435 \u043f\u0443\u0441\u0442\u044b\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c, \u0442.\u043a. \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0411\u0414 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043a\u0430\u0442\u044c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0441 \u043f\u0443\u0441\u0442\u044b\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0432 parent_ids_with_it.<\/p>\n<\/li>\n<li>\n<p>\u041d\u043e \u0438 \u044d\u0442\u043e \u0435\u0449\u0435 \u043d\u0435 \u0432\u0441\u0435. \u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445 \u043c\u043e\u0436\u043d\u043e \u0435\u0449\u0435 \u0441\u0438\u043b\u044c\u043d\u0435\u0435 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0441\u0445\u0435\u043c\u0443. \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442 tree_id \u0432\u0432\u043e\u0434\u0438\u043b\u0441\u044f \u0434\u043b\u044f \u0434\u0432\u0443\u0445 \u0432\u0435\u0449\u0435\u0439: \u201c\u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044f\u0442\u044c\u201d \u0434\u0435\u0440\u0435\u0432\u043e \u043a \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e, \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0442.\u043f.) \u0438 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043e\u0434\u043d\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u043f\u043e tree_id. \u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043c \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c tree_id. \u041d\u043e, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0432 \u043e\u0431\u0435\u0437\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u0438 \u0432\u044b\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u0445 \u043f\u043e id \u043a\u043e\u0440\u043d\u044f. \u0422\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u0431\u0435\u0437 tree_id:<\/p>\n<pre><code class=\"sql\">create table node ( id                 bigint primary key generated always as identity, parent_ids         bigint[], parent_ids_with_it bigint[] not null generated always as (parent_ids || id) stored,  constraint node_cycle_check check (not (parent_ids @> array [id])),  unique (parent_ids_with_it),  foreign key (parent_ids)     references node (parent_ids_with_it)     on delete cascade     on update cascade );<\/code><\/pre>\n<p>\u0423\u0434\u0430\u043b\u0435\u043d\u043e:<br \/>&#8212; \u0430\u0442\u0440\u0438\u0431\u0443\u0442 tree_id<br \/>&#8212; tree_id \u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u0435 \u0438 \u0432\u043d\u0435\u0448\u043d\u0435\u043c \u043a\u043b\u044e\u0447\u0435<br \/>&#8212; \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0430 tree_id<\/p>\n<p>\u041a\u0430\u043a \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0434\u0435\u0440\u0435\u0432\u043e? \u0427\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 parent_ids_with_it. \u0418\u043c\u0435\u043d\u043d\u043e \u043e\u043d \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0432\u0435\u043d \u043a\u043e\u0440\u043d\u044e \u0441\u0432\u043e\u0435\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430. \u0417\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e id \u043a\u043e\u0440\u043d\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">select * from node where parent_ids_with_it[1] = :id;<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 1, \u0430 \u043d\u0435 \u0441 0.<\/p>\n<p>\u0421\u0430\u043c\u0430\u044f \u043a\u0440\u0430\u0441\u0438\u0432\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f. \u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u0442\u044c \u0440\u044f\u0434 \u0437\u0430\u0434\u0430\u0447, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u043e, \u043d\u043e \u043e\u0431 \u044d\u0442\u043e\u043c \u043a\u0430\u043a-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u043e\u0442\u043e\u043c.<\/p>\n<\/li>\n<\/ol>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u044f\u0442\u043d\u044b\u0435 \u0431\u043e\u043d\u0443\u0441\u044b:<\/p>\n<p>\u041d\u0430\u043b\u0438\u0447\u0438\u0435 parent_ids_with_it \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0431\u044b \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0431\u0435\u0437 \u043d\u0435\u0433\u043e.<\/p>\n<ol>\n<li>\n<p>\u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c:<\/p>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 parent_ids_with_it:<\/p>\n<pre><code class=\"sql\">constraint node_depth_check check (cardinality(parent_ids_with_it) &lt;= 100)<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043f\u0443\u0442\u0438 \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u0434\u043e \u0443\u0437\u043b\u0430 (\u0432\u0435\u0440\u0442\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a):<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0443\u0437\u043b\u044b \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u0434\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"sql\">select * from node where id in (select parent_ids_with_it from node where id = :node_id);<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432 \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 (\u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a):<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0443\u0437\u043b\u044b \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 (\u043f\u0443\u0441\u0442\u044c \u0434\u0430\u0436\u0435 \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f\u043c\u0438) \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 parent_ids_with_it:<\/p>\n<pre><code class=\"sql\">select * from node where tree_id = :tree_id and cardinality(parent_ids_with_it) = :depth;<\/code><\/pre>\n<pre><code class=\"sql\">select * from node where parent_ids_with_it[1] = :root_id and cardinality(parent_ids_with_it) = :depth;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430:<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0432 \u043d\u0430\u0448\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u044b\u0435, \u0438 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u0432\u0441\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043d\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442. \u041c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u043e \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043a\u0443\u0441\u043a\u0430\u043c, \u0442.\u0435. \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0443 \u0447\u0430\u0441\u0442\u044c \u0434\u0435\u0440\u0435\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430:<\/p>\n<pre><code class=\"sql\">select * from node where parent_ids_with_it @> array [:node_id];<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0433\u043b\u0443\u0431\u0438\u043d\u043e\u0439:<\/p>\n<p>\u0410 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u044c\u044f \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u043b\u0438\u043a\u0438, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e \u0443\u0436\u0435 \u043d\u0435 \u0441\u043f\u0430\u0441\u0430\u0435\u0442? \u041d\u0443\u0436\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u0443\u044e \u0433\u043b\u0443\u0431\u0438\u043d\u0443. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0443\u0436\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u0430\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0440\u0430\u0437\u043c\u0435\u0440 parent_ids_with_it:<\/p>\n<ol>\n<li>\n<p>\u0414\u043b\u044f \u043a\u043e\u0440\u043d\u044f:<\/p>\n<pre><code class=\"sql\">select * from node where tree_id = :tree_id and cardinality(parent_ids_with_it) &lt;= :depth;<\/code><\/pre>\n<pre><code class=\"sql\">select * from node where parent_ids_with_it[1] = :root_id and cardinality(parent_ids_with_it) &lt;= :depth;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430:<\/p>\n<pre><code class=\"sql\">select * from node where parent_ids_with_it @> array [:node_id] and cardinality(parent_ids_with_it) &lt;= cardinality((select parent_ids_with_it from node where id = :node_id)) + :depth - 1;<\/code><\/pre>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><a href=\"https:\/\/postgresql.org\/docs\/current\/index.html\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0441\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0435\u0441\u0442\u044c \u0432 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a><\/p>\n<p><a href=\"https:\/\/postgrespro.ru\/docs\/postgrespro\/16\/index\" rel=\"noopener noreferrer nofollow\">\u0415\u0441\u043b\u0438 \u0441\u043b\u043e\u0436\u043d\u043e \u0441 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u043c, \u0435\u0441\u0442\u044c \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/alexeyminkin96\/tree-sql\" rel=\"noopener noreferrer nofollow\">Github<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/812601\/\"> https:\/\/habr.com\/ru\/articles\/812601\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u0442\u043e\u044f\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430: \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u0440\u0430\u0431\u043e\u0442\u0443 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430 \u043f\u0430\u043f\u043e\u043a \u0432 PostgreSQL. \u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u043c\u044b \u043d\u0430\u0442\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0443 \u0440\u0435\u0448\u0430\u043b\u0438, \u043d\u043e\u00a0<s>\u0434\u0435\u043b\u0430\u043b\u0438 \u044d\u0442\u043e \u0431\u0435\u0437 \u0443\u0432\u0430\u0436\u0435\u043d\u0438\u044f<\/s>\u00a0\u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b\u0438 \u043d\u0435 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u043e, \u043d\u0430\u0440\u0443\u0448\u0430\u043b\u0438 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438, \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u0438 \u0431\u041e\u043b\u044c\u0448\u0435\u0433\u043e \u0432\u043e\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0432 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0438 \u0442.\u0434. \u041f\u043e\u0442\u043e\u043c\u0443 \u0437\u0430\u0434\u0430\u043b\u0441\u044f \u0446\u0435\u043b\u044c\u044e: \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 (\u0432 \u043e\u0431\u0449\u0435\u043c \u0432\u0438\u0434\u0435) \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u0432, \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a, \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 (\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439) \u0438 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 PostgreSQL (\u0432\u0435\u0440\u0441\u0438\u0438 16).<\/p>\n<p>\u0421\u043b\u043e\u0432\u0430\u0440\u044c: <br \/>\u0443\u0437\u0435\u043b &#8212; \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0431\u0434, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0443\u0437\u0435\u043b \u0434\u0435\u0440\u0435\u0432\u0430 <br \/>\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c &#8212; \u0443\u0437\u0435\u043b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u0438\u0435 \u0443\u0437\u043b\u044b <br \/>\u0434\u043e\u0447\u043a\u0430 &#8212; \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0443\u0437\u0435\u043b <br \/>\u043a\u043e\u0440\u0435\u043d\u044c &#8212; \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f (\u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u0447\u043a\u043e\u0439)<\/p>\n<ol>\n<li>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0435\u0440\u0435\u0432\u043e \u0432 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e, \u0441\u043e\u0441\u043b\u0430\u0432\u0448\u0438\u0441\u044c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043d\u0430 \u0441\u0430\u043c\u0443 \u0441\u0435\u0431\u044f. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u0430\u0436\u0434\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0431\u043e\u0439 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u043c\u0435\u0435\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u043e\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0438 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043b\u044e\u0431\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u043e\u0447\u0435\u043a. \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint,    foreign key (parent_id)     references node (id) );<\/code><\/pre>\n<p>\u0422\u0438\u043f id \u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u0435\u0433\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u044b\u0431\u0440\u0430\u043d\u044b \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e \u0438 \u043d\u0430 \u043b\u043e\u0433\u0438\u043a\u0443 \u043d\u0435 \u0432\u043b\u0438\u044f\u044e\u0442. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043c\u0435\u043d\u044f\u0442\u044c \u0438\u0445 \u043d\u0430 \u0441\u0432\u043e\u0435 \u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21161: \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430<\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441, \u0430 \u043a\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438\u0437 \u0431\u0430\u0437\u044b? \u041d\u0435\u0443\u0436\u0435\u043b\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u0435\u043d\u044c \u043f\u043e id, \u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c? \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u043d\u0435\u0442. \u0427\u0442\u043e\u0431\u044b \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 (\u0441\u0442\u0440\u043e\u043a\u0430 5):<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint, tree_id   bigint not null,    foreign key (parent_id)     references node (id) );<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0435 \u0434\u0435\u0440\u0435\u0432\u043e, \u043a\u0430\u043a\u0430\u044f \u0431\u044b \u043c\u043d\u043e\u0433\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443 \u043d\u0435\u0433\u043e \u043d\u0438 \u0431\u044b\u043b\u0430, \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043e \u043e\u0434\u043d\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c. \u042d\u0442\u0438\u043c tree_id \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 id: \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043b\u0438 \u043b\u044e\u0431\u0430\u044f \u0434\u0440\u0443\u0433\u0430\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e. \u041c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c id \u043a\u043e\u0440\u043d\u044f. \u041e\u0434\u043d\u0430\u043a\u043e \u0442\u0430\u043a\u043e\u0435 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u0435 \u043d\u0435\u0441\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0434\u0432\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21162: \u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u0440\u043d\u0438<\/p>\n<p>\u0412 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0445\u0435\u043c\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u043e\u0435 tree_id \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043d\u0435 \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u0443\u0437\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430, \u0442.\u0435. \u043d\u0435 \u0438\u043c\u0435\u0442\u044c \u043e\u0431\u0449\u0435\u0433\u043e \u043a\u043e\u0440\u043d\u044f. \u041a\u0430\u043a \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c? \u041d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0431\u044b\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0443\u0437\u0435\u043b \u0441 parent_id == null. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0441 parent_id == null \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0432 \u0431\u0430\u0437\u0435 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438? \u0412\u043d\u043e\u0432\u044c, \u043d\u0435\u0442. \u041f\u043e\u043c\u043e\u0436\u0435\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441. \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043e\u0440\u043d\u044f (\u043a\u043e\u0433\u0434\u0430 parent_id == null) \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 tree_id (\u0441\u0442\u0440\u043e\u043a\u0430 10):<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint, tree_id   bigint not null,    foreign key (parent_id)     references node (id) ); create unique index on node(tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c\u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\u0438 \u0441 \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (\u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 null \u0432 parent_id \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 tree_id). \u041a\u0430\u043a \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u0432 \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0435 \u0443\u0432\u0438\u0434\u0438\u043c \u043f\u043e\u0437\u0436\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21163: \u0421\u043c\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432<\/p>\n<p>\u0412 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0445\u0435\u043c\u0435 \u0443\u0437\u043b\u044b \u043e\u0434\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u043e\u0433\u0443\u0442 \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0443\u0437\u043b\u044b \u0434\u0440\u0443\u0433\u043e\u0433\u043e. \u041d\u0435\u0443\u0436\u0435\u043b\u0438 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0441\u0442\u0430\u0432\u043a\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c tree_id \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f? \u0418 \u0441\u043d\u043e\u0432\u0430, \u043d\u0435\u0442. \u0417\u0434\u0435\u0441\u044c \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0439 \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447. \u0422.\u0435. \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442 parent_id \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0440\u0430\u0432\u0435\u043d id \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f, \u043d\u043e \u0438 tree_id \u0434\u043e\u0447\u0435\u0440\u0438 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0440\u0430\u0432\u0435\u043d tree_id \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u0430\u0440\u0430 id \u0438 tree_id \u0431\u044b\u043b\u0430 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u0430. \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u044d\u0442\u043e \u0443\u0436\u0435 \u0442\u0430\u043a (id \u0443\u043d\u0438\u043a\u0430\u043b\u0435\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u044b), \u043d\u043e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0433\u043e \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430 \u043d\u0443\u0436\u043d\u043e \u044f\u0432\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0430 \u043f\u0430\u0440\u0443 id, tree_id (\u0441\u0442\u0440\u043e\u043a\u0438 7,9,10):<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint, tree_id   bigint not null,    unique (id, tree_id),  foreign key (parent_id, tree_id)     references node (id, tree_id) ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21164: \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430<\/p>\n<p>\u041f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0434\u043e\u0447\u043a\u0438, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u044f \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430. \u041d\u0435\u0443\u0436\u0435\u043b\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u0430\u0442\u044c \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u0435\u043a \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0432\u0441\u0435 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443 id? \u041d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u041d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447 \u043d\u0430 \u043a\u0430\u0441\u043a\u0430\u0434\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 (\u0441\u0442\u0440\u043e\u043a\u0430 11):<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint, tree_id   bigint not null,    unique (id, tree_id),  foreign key (parent_id, tree_id)     references node (id, tree_id)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21165: \u0426\u0438\u043a\u043b\u044b<\/p>\n<p>\u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0432\u044f\u0437\u0435\u0439, \u0447\u0442\u043e \u043d\u0430\u0440\u0443\u0448\u0430\u0435\u0442 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0443\u0442\u0435\u0447\u043a\u0430\u043c \u043f\u0430\u043c\u044f\u0442\u0438, \u043b\u043e\u043c\u0430\u0435\u0442 \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0443 \u0438 \u0442.\u0434. \u0412 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>id<\/p>\n<\/th>\n<th>\n<p>parent_id<\/p>\n<\/th>\n<th>\n<p>tree_id<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0417\u0434\u0435\u0441\u044c \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u0430 \u043d\u0430 \u0441\u0435\u0431\u044f. \u0422\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0440\u0435\u0442\u043e\u043c \u043d\u0430 \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e \u043c\u0435\u0436\u0434\u0443 id \u0438 parent_id \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 (\u0441\u0442\u0440\u043e\u043a\u0430 7):<\/p>\n<pre><code class=\"sql\">create table node ( id        bigint primary key generated always as identity, parent_id bigint, tree_id   bigint not null,    constraint node_cycle_check check (parent_id != id),  unique (id, tree_id),  foreign key (parent_id, tree_id)     references node (id, tree_id)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u041d\u043e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>id<\/p>\n<\/th>\n<th data-colwidth=\"220\" width=\"220\">\n<p>parent_id<\/p>\n<\/th>\n<th>\n<p>tree_id<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<td data-colwidth=\"220\" width=\"220\">\n<p align=\"left\">2<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">2<\/p>\n<\/td>\n<td data-colwidth=\"220\" width=\"220\">\n<p align=\"left\">1<\/p>\n<\/td>\n<td>\n<p align=\"left\">1<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u041d\u0435\u0443\u0436\u0435\u043b\u0438 \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0432 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u0439 \u0446\u0435\u043f\u043e\u0447\u043a\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0432\u0448\u0435\u0433\u043e\u0441\u044f \u0446\u0438\u043a\u043b\u0430? \u041d\u0435\u0442, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0440\u0430\u0437 \u0441\u0445\u0438\u0442\u0440\u0438\u0442\u044c. \u041c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u044f\u043c\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438. \u041d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 id \u0432\u0441\u0435\u0445 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0434\u043e \u043a\u043e\u0440\u043d\u044f \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043b\u0438 \u0432 \u043d\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 id. \u041a\u0441\u0442\u0430\u0442\u0438, \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 parent_id \u2260 id \u0443\u0436\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e\u0439 (\u0441\u0442\u0440\u043e\u043a\u0438 6, 8):<\/p>\n<pre><code class=\"sql\">create table node ( id         bigint primary key generated always as identity, parent_id  bigint, tree_id    bigint not null, parent_ids bigint[],    constraint node_cycle_check check (not (parent_ids @> array[id])),  unique (id, tree_id),  foreign key (parent_id, tree_id)     references node (id, tree_id)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u041f\u0440\u0435\u0434\u0432\u043e\u0441\u0445\u0438\u0449\u0430\u044e \u043c\u043d\u043e\u0433\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0442\u0430\u043a\u043e\u043c\u0443 \u0448\u0430\u0433\u0443, \u043d\u043e \u043f\u0440\u043e\u0448\u0443 \u0437\u0430\u043f\u0430\u0441\u0442\u0438\u0441\u044c \u0442\u0435\u0440\u043f\u0435\u043d\u0438\u0435\u043c, \u0434\u0430\u043b\u044c\u0448\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u0441\u0435 \u043e\u0442\u0432\u0435\u0442\u044b. \u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 (@>) \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e parent_ids nullable. \u0410\u0442\u0440\u0438\u0431\u0443\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c null \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f. \u041c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0433\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c, \u0438 \u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 (\u0441\u0435\u0439\u0447\u0430\u0441 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c). \u041e\u0434\u043d\u0430\u043a\u043e, \u0437\u0430\u0431\u0435\u0433\u0430\u044f \u0432\u043f\u0435\u0440\u0435\u0434, \u0441\u043a\u0430\u0436\u0443, \u0447\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u043d\u0430 nullable.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21166: \u041a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c parent_ids<\/p>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u0432 parent_ids \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e. \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0440\u0430\u0436\u0430\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0431\u0430\u0437\u044b? \u0412\u0441\u0435-\u0442\u0430\u043a\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u043c \u043f\u0440\u043e\u0431\u0435\u0433\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f\u043c? \u041d\u0435\u0442. \u0417\u0434\u0435\u0441\u044c \u0441\u043d\u043e\u0432\u0430 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430. \u041d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0441\u044f \u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e, \u0430 \u0437\u0430\u0432\u0438\u0441\u0435\u043b \u043e\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438. \u041d\u043e \u043d\u0430 \u0447\u0442\u043e \u0435\u0433\u043e \u0441\u0441\u044b\u043b\u0430\u0442\u044c? \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c parent_ids \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u043e\u0442 parent_ids \u0434\u043e\u0447\u0435\u0440\u0438 \u043d\u0430 \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u0422\u043e\u0433\u0434\u0430 \u0432\u043d\u043e\u0432\u044c \u0441\u0445\u0438\u0442\u0440\u0438\u043c \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0430\u0442\u0440\u0438\u0431\u0443\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 parent_ids \u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e id. \u0410 parent_ids \u0431\u0443\u0434\u0435\u0442 \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043d\u0435\u0433\u043e. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0433\u043e \u0432 \u0438\u043d\u0434\u0435\u043a\u0441 \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447 (\u0441\u0442\u0440\u043e\u043a\u0438 7, 11, 13, 14):<\/p>\n<pre><code class=\"sql\">create table node ( id                 bigint primary key generated always as identity, parent_id          bigint, tree_id            bigint   not null, parent_ids         bigint[], parent_ids_with_it bigint[] not null,    constraint node_cycle_check check (not (parent_ids @> array [id])),  unique (id, tree_id, parent_ids_with_it),  foreign key (parent_id, tree_id, parent_ids)     references node (id, tree_id, parent_ids_with_it)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u0421\u0442\u043e\u0438\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u043d\u0435 nullable, \u0442.\u043a. \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u044d\u0442\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 == 1 (\u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f).<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21167: \u041a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u0442\u043d\u043e\u0441\u0442\u044c parent_ids_with_it<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c parent_ids_with_it \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e. \u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043d\u0438\u043c? \u041c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c, \u0442\u0443\u0442 \u043f\u0440\u043e\u0431\u0435\u0436\u0438\u043c\u0441\u044f \u043f\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f\u043c? \u041d\u0438 \u0432 \u043a\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 parent_ids. \u0421 \u201cgenerated always as\u201d \u043c\u044b \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u043f\u0440\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 id, \u043d\u043e \u0438 \u0437\u0434\u0435\u0441\u044c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043a\u0441\u0442\u0430\u0442\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 parent_ids \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 id (\u0441\u0442\u0440\u043e\u043a\u0430 7):<\/p>\n<pre><code class=\"sql\">create table node ( id                 bigint primary key generated always as identity, parent_id          bigint, tree_id            bigint   not null, parent_ids         bigint[], parent_ids_with_it bigint[] not null generated always as (parent_ids || id) stored,  constraint node_cycle_check check (not (parent_ids @> array [id])),  unique (id, tree_id, parent_ids_with_it),  foreign key (parent_id, tree_id, parent_ids)     references node (id, tree_id, parent_ids_with_it)     on delete cascade ); create unique index on node (tree_id) where parent_id is null;<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 (parent_ids || id) \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 id. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u044d\u0442\u043e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u043d\u0435\u043b\u044c\u0437\u044f \u0437\u0430\u0434\u0430\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u043e\u043d \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 parent_ids == null (\u0434\u043b\u044f \u043a\u043e\u0440\u043d\u044f) \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0432\u0435\u0440\u043d\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043e\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c (\u0442\u0435\u043a\u0443\u0449\u0438\u043c id). <\/p>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 stored. \u042d\u0442\u043e\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u043c \u0438\u043b\u0438 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c (\u0442.\u0435. \u0432\u044b\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438). \u0414\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u043f\u043e\u0434\u043e\u0448\u0435\u043b \u0431\u044b \u0432\u0442\u043e\u0440\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0435\u0435 \u0434\u0438\u0441\u043a\u043e\u0432\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e, \u043e\u0434\u043d\u0430\u043a\u043e PostgreSQL \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 (\u043f\u043e 16 \u0432\u0435\u0440\u0441\u0438\u044e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e) \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u044f\u0432\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u043c.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u21168: \u041a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c parent_ids \u0432 \u043a\u043e\u0440\u043d\u0435<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c parent_ids, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d parent_ids_with_it \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f. \u0412\u0441\u0435 \u044d\u0442\u043e \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u0434\u043e\u0447\u0435\u043a. \u0410 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043a\u043e\u0440\u043d\u0435\u043c? \u0412\u0435\u0434\u044c \u0435\u0433\u043e parent_id == null, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 <\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-375521","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/375521","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=375521"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/375521\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=375521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=375521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=375521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}