{"id":398207,"date":"2024-06-29T13:56:10","date_gmt":"2024-06-29T13:56:10","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=398207"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T13:56:10","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=398207","title":{"rendered":"<span>PostgreSQL ltree: \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445<\/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>\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435. \u042d\u0442\u043e \u0432\u0435\u0440\u0441\u0438\u044f \u043c\u043e\u0435\u0439 <a href=\"https:\/\/medium.com\/@msmer\/ensuring-data-consistency-when-using-postgresql-ltree-extension-c05cfc4afada\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u0438<\/a> \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u043e\u043c \u044f\u0437\u044b\u043a\u0435. \u041e\u043d\u0430 \u0443 \u043c\u0435\u043d\u044f \u043f\u0435\u0440\u0432\u0430\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0448\u0443 \u043d\u0435 \u0441\u0443\u0434\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u0433\u043e. <\/p>\n<p>\u041e\u0434\u043d\u0438\u043c \u0438\u0437 <a href=\"https:\/\/habr.com\/ru\/articles\/537062\/\" rel=\"noopener noreferrer nofollow\">\u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0432 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0421\u0423\u0411\u0414<\/a> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u201c\u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u0443\u0442\u044c\u201d. \u0412 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0439 \u044d\u0442\u043e\u0433\u043e \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432, \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u0432\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.\u00a0<\/p>\n<p>\u0412 PostgreSQL \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 ltree, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u0435\u0442\u043a\u0430\u043c\u0438 \u0438 \u043f\u0443\u0442\u044f\u043c\u0438. \u041e \u043d\u0435\u043c \u0438 \u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c\u044e (\u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c\u044e) \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c.\u00a0<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0437\u0430\u0434\u0430\u0447\u0430: <strong>\u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 item \u0441 \u043f\u043e\u043b\u0435\u043c path \u0442\u0438\u043f\u0430 ltree \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0435\u0440\u0435\u0432\u043e\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0431\u0430\u0437\u044b.\u00a0<\/strong><\/p>\n<p>\u0422\u0435\u0440\u043c\u0438\u043d\u043e\u043b\u043e\u0433\u0438\u044f:<\/p>\n<ul>\n<li>\n<p><em>\u043c\u0435\u0442\u043a\u0430<\/em>\u00a0\u2014 \u0441\u0442\u0440\u043e\u043a\u0430, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0430\u044f \u0438\u0437\u00a0\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0445 \u0438 \u0437\u0430\u0433\u043b\u0430\u0432\u043d\u044b\u0445 \u0431\u0443\u043a\u0432, \u0446\u0438\u0444\u0440 \u0438 \u0437\u043d\u0430\u043a\u043e\u0432 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u043d\u0438\u044f. \u042f \u0432\u00a0\u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u0435\u0442\u043a\u0438 \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0446\u0438\u0444\u0440\u043e\u0432\u043e\u0439 id \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<\/li>\n<li>\n<p><em>\u043f\u0443\u0442\u044c<\/em>\u00a0\u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u0435\u0442\u043e\u043a, \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0442\u043e\u0447\u043a\u043e\u0439. \u041f\u0435\u0440\u0432\u043e\u0439 \u043c\u0435\u0442\u043a\u043e\u0439 \u0434\u043e\u043b\u0436\u0435\u043d\u00a0\u0431\u044b\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u0435\u0440\u0435\u0432\u0430, \u0430\u00a0\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439\u00a0\u2014 \u043c\u0435\u0442\u043a\u0430 \u0441\u0430\u043c\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/h2>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 item, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0443\u044e \u043f\u043e\u043b\u044f\u00a0 id(serial) \u0438 path(ltree). \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u043a\u043b\u044e\u0447\u0438\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443:<\/p>\n<pre><code class=\"sql\">CREATE EXTENSION IF NOT EXISTS ltree;  create table public.item (     id  serial  constraint item_pk primary key,     path ltree default ((lastval())::text)::ltree not null constraint item_pk2 unique );<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0435 path \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0443\u0441\u0442\u044b\u043c, \u0442.\u043a. \u0435\u0441\u043b\u0438 \u0434\u0430\u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439, \u0432 \u044d\u0442\u043e\u043c \u043f\u043e\u043b\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f id.<\/p>\n<p>\u0412 \u043c\u043e\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0438 \u043c\u043d\u043e\u0433\u043e item, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0447\u0430\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0442\u044c\u0441\u044f. \u042f \u0432\u044b\u0431\u0440\u0430\u043b \u0442\u0438\u043f serial \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430, \u0442.\u043a. \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0443\u043a\u0432\u0435\u043d\u043d\u043e-\u0446\u0438\u0444\u0440\u043e\u0432\u044b\u0445 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0431\u044a\u0435\u043c\u0435 \u0431\u0430\u0437\u044b \u043f\u0440\u0438 \u0440\u043e\u0441\u0442\u0435 \u043a\u043e\u043b-\u0432\u0430 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0438\u043b\u0438 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u043d\u043e-\u0431\u0443\u043a\u0432\u0435\u043d\u043d\u044b\u0445 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 \u0432\u0430\u043c \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435, \u0442.\u043a. \u0443 \u044d\u0442\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0435\u0441\u0442\u044c \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u043e\u0434\u0438\u043d \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u043b\u044e\u0441 &#8212; \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c id \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435. \u0412 path \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c UUID, \u0442.\u043a. \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 ltree \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0434\u0435\u0444\u0438\u0441.<\/p>\n<h2>\u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0435\u0439<\/h2>\n<p>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0430, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u044c. \u041f\u0435\u0440\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c item, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 path \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 id \u0437\u0430\u043f\u0438\u0441\u0438. \u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f id \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 insert \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 lastval(). \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0442\u0440\u0438\u0436\u0434\u044b:<\/p>\n<pre><code class=\"sql\">insert into item(path) values (lastval()::text::ltree);<\/code><\/pre>\n<p>\u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0442\u0440\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 (id = path).<\/p>\n<pre><code class=\"sql\">select * from item;  | id | path | | 1 | 1 | | 2 | 2 | | 3 | 3 |<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u043a\u043b\u044e\u0447\u043e\u043c, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u043c \u0438\u0437 \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u043a \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b\u0441\u044f \u0443\u0434\u0430\u0447\u043d\u043e:<\/p>\n<pre><code class=\"sql\">insert into item (path) values ('12.43.555.22.542');  select * from item;  | id | path | | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 12.43.555.22.542 | <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043d\u0435 \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u044b, \u0442.\u043a. \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0437\u0430\u043f\u0438\u0441\u044c \u0441 \u043f\u0443\u0442\u0435\u043c path, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u043c \u0438\u0437 \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u043a. \u0421 \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u0443\u0441\u043f\u0435\u0445\u043e\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u043f\u043e\u043b\u0435 path \u0438 \u201c\u0437\u0430\u0446\u0438\u043a\u043b\u0435\u043d\u043d\u043e\u0435\u201d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0443\u0442\u0438, \u0432\u0440\u043e\u0434\u0435 \u201c12.42.12\u201d \u0438 \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e \u0435\u0449\u0435. \u0422\u0430\u043a \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 PostgreSQL \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043b\u0430\u0441\u0442\u044c \u0432 \u043f\u043e\u043b\u0435 path \u043b\u044e\u0431\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0438\u0437 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. \u0420\u0435\u0448\u0438\u043c \u044d\u0442\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439. <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION item_before_update_insert_check_path() RETURNS TRIGGER AS $item_before_update_insert_check_path$ DECLARE     parentPath  ltree;     curLabel ltree;     parentId text; BEGIN     parentPath := subpath(NEW.path, 0, -1);     curLabel := subpath(NEW.path, -1);      -- \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043c\u0435\u0442\u043a\u0430 \u0432 \u043f\u0443\u0442\u0438 \u0434\u043e\u043b\u0436\u043d\u0430 \u0440\u0430\u0432\u043d\u044f\u0442\u044c\u0441\u044f id     if (curLabel::text != NEW.id::text) THEN         RAISE EXCEPTION 'The last path label % must be equal id %', curLabel::text, NEW.id::text;     end if;      -- \u0434\u043e\u043b\u0436\u043d\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u043c \u043f\u0443\u0442\u0435\u043c, \u0435\u0441\u043b\u0438 \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u043d\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u0430\u044f     if (parentPath != '') THEN         parentId := (select id from item where path = parentPath);         if (parentId IS NULL) THEN             RAISE EXCEPTION 'Parent item with path % not found', parentPath;         end if;     end if;      RETURN NEW; END; $item_before_update_insert_check_path$ LANGUAGE plpgsql; <\/code><\/pre>\n<p>\u0418 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0430\u0441\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u043b\u0438 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 path:<\/p>\n<pre><code class=\"sql\">CREATE TRIGGER tr_item_before_update_insert_check_path     BEFORE         INSERT OR         UPDATE OF path ON item     FOR EACH ROW EXECUTE FUNCTION item_before_update_insert_check_path();<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u043f\u0440\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0435 \u0438\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 path \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043d\u0430\u0440\u0443\u0448\u0430\u044e\u0449\u0435\u0435 \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0437\u0430\u043f\u0438\u0441\u044c \u0441 id=4 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439, \u043a\u0430\u043a \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435. \u0418\u0434\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435. \u0421\u0434\u0435\u043b\u0430\u0435\u043c item c id=1 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c id=2, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u043e\u0442\u043e\u043c \u043f\u043e\u043b\u043e\u0436\u0438\u043c 1 \u0432 3:<\/p>\n<pre><code class=\"sql\">update item set path = '1.2' where id = 2; update item set path = '3.1' where id = 1; select * from item;  | id | path | | 3 | 3 | | 2 | 1.2 | | 4 | 4 | | 1 | 3.1 |<\/code><\/pre>\n<p>\u0418 \u0441\u043d\u043e\u0432\u0430 \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0430 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c. \u0423 \u0437\u0430\u043f\u0438\u0441\u0438 \u0441 id 2 \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 path: \u0432\u043c\u0435\u0441\u0442\u043e \u20181.2\u2019 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u20183.1.2\u2019.\u00a0\u042d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430: \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043f\u0443\u0442\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430\u0434\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0443\u0442\u0438 \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u041e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u043c \u044d\u0442\u043e. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043d\u043e\u0432\u0430 \u0432\u0441\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c\u0438:<\/p>\n<pre><code class=\"sql\">update item set path = id::text::ltree;<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u0442\u0440\u0438\u0433\u0433\u0435\u0440:<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION item_after_update_set_children_path() RETURNS TRIGGER AS $item_after_update_set_children_path$ BEGIN      IF (NEW.path != OLD.path) THEN         UPDATE item         SET path = NEW.path || subpath(path, nlevel(OLD.path))         WHERE path &lt;@ OLD.path;     END IF;      RETURN NULL; END; $item_after_update_set_children_path$ LANGUAGE plpgsql;   CREATE TRIGGER tr_update_children_item_path     AFTER UPDATE OF path ON item     FOR EACH ROW EXECUTE FUNCTION item_after_update_set_children_path();<\/code><\/pre>\n<p>\u0430 \u0437\u0430\u0442\u0435\u043c \u0441\u043d\u043e\u0432\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u043f\u0443\u0442\u044f\u043c\u0438 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 2 \u0438 1:<\/p>\n<pre><code class=\"sql\">update item set path = '1.2' where id = 2; update item set path = '3.1' where id = 1; select * from item;  | id | path | | 3 | 3 | | 4 | 4 | | 1 | 3.1 | | 2 | 3.1.2 |<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c path \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441 path \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430. \u041f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0430 \u043a\u0430\u0441\u043a\u0430\u0434\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<h2>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0435\u0439<\/h2>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0438\u043c\u0435\u044e\u0449\u0438\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u043d\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u0442\u0440\u0438\u0433\u0433\u0435\u0440:<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION item_before_delete_check_children() RETURNS TRIGGER AS $item_check_children_before_delete$ DECLARE     childrenCount bigint; BEGIN     SELECT COUNT(*) into childrenCount     FROM item WHERE path &lt;@ OLD.path AND id != OLD.id;      IF (childrenCount > 0) THEN         RAISE EXCEPTION 'Deleting items containing child items by the path field is prohibited.';     end if; RETURN OLD; END; $item_check_children_before_delete$ LANGUAGE plpgsql;   CREATE TRIGGER tr_item_check_children_before_delete     BEFORE DELETE ON item     FOR EACH ROW EXECUTE FUNCTION item_before_delete_check_children();<\/code><\/pre>\n<p>\u0413\u043e\u0442\u043e\u0432\u043e.<\/p>\n<p><strong>\u0418\u0442\u043e\u0433\u043e<\/strong><\/p>\n<p>\u0414\u043b\u044f \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0439 \u043f\u043e\u043b\u0435 ltree, \u043d\u0443\u0436\u043d\u043e:<\/p>\n<ol>\n<li>\n<p>\u0421\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u043b\u0435 path not null;<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u0434\u043b\u044f\u00a0path;<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u044b:<\/p>\n<ul>\n<li>\n<p>BEFORE INSERT \u0438 BEFORE UPDATE, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0449\u0438\u0435, \u0447\u0442\u043e\u00a0\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043c\u0435\u0442\u043a\u0430 \u0432\u00a0\u043f\u043e\u043b\u0435 path \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f id \u0437\u0430\u043f\u0438\u0441\u0438 \u0438 \u0447\u0442\u043e\u00a0\u0432\u00a0\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u00a0path, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c path \u0437\u0430\u043f\u0438\u0441\u0438 \u0431\u0435\u0437\u00a0\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043c\u0435\u0442\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p>AFTER UPDATE, \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0449\u0438\u0439 path \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043f\u0440\u0438\u00a0\u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 path (\u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0438).<\/p>\n<\/li>\n<li>\n<p>BEFORE DELETE, \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u044e\u0449\u0438\u0439 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u0438, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c, \u0447\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043e\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439.<\/p>\n<\/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\/733814\/\"> https:\/\/habr.com\/ru\/articles\/733814\/<\/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>\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435. \u042d\u0442\u043e \u0432\u0435\u0440\u0441\u0438\u044f \u043c\u043e\u0435\u0439 <a href=\"https:\/\/medium.com\/@msmer\/ensuring-data-consistency-when-using-postgresql-ltree-extension-c05cfc4afada\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u0438<\/a> \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u043e\u043c \u044f\u0437\u044b\u043a\u0435. \u041e\u043d\u0430 \u0443 \u043c\u0435\u043d\u044f \u043f\u0435\u0440\u0432\u0430\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0448\u0443 \u043d\u0435 \u0441\u0443\u0434\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u0433\u043e. <\/p>\n<p>\u041e\u0434\u043d\u0438\u043c \u0438\u0437 <a href=\"https:\/\/habr.com\/ru\/articles\/537062\/\" rel=\"noopener noreferrer nofollow\">\u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0432 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0421\u0423\u0411\u0414<\/a> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u201c\u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u0443\u0442\u044c\u201d. \u0412 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0439 \u044d\u0442\u043e\u0433\u043e \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432, \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u0432\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.\u00a0<\/p>\n<p>\u0412 PostgreSQL \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 ltree, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u0435\u0442\u043a\u0430\u043c\u0438 \u0438 \u043f\u0443\u0442\u044f\u043c\u0438. \u041e \u043d\u0435\u043c \u0438 \u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c\u044e (\u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c\u044e) \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c.\u00a0<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0437\u0430\u0434\u0430\u0447\u0430: <strong>\u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 item \u0441 \u043f\u043e\u043b\u0435\u043c path \u0442\u0438\u043f\u0430 ltree \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0435\u0440\u0435\u0432\u043e\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0431\u0430\u0437\u044b.\u00a0<\/strong><\/p>\n<p>\u0422\u0435\u0440\u043c\u0438\u043d\u043e\u043b\u043e\u0433\u0438\u044f:<\/p>\n<ul>\n<li>\n<p><em>\u043c\u0435\u0442\u043a\u0430<\/em>\u00a0\u2014 \u0441\u0442\u0440\u043e\u043a\u0430, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0430\u044f \u0438\u0437\u00a0\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0445 \u0438 \u0437\u0430\u0433\u043b\u0430\u0432\u043d\u044b\u0445 \u0431\u0443\u043a\u0432, \u0446\u0438\u0444\u0440 \u0438 \u0437\u043d\u0430\u043a\u043e\u0432 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u043d\u0438\u044f. \u042f \u0432\u00a0\u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u0435\u0442\u043a\u0438 \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0446\u0438\u0444\u0440\u043e\u0432\u043e\u0439 id \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<\/li>\n<li>\n<p><em>\u043f\u0443\u0442\u044c<\/em>\u00a0\u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u0435\u0442\u043e\u043a, \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0442\u043e\u0447\u043a\u043e\u0439. \u041f\u0435\u0440\u0432\u043e\u0439 \u043c\u0435\u0442\u043a\u043e\u0439 \u0434\u043e\u043b\u0436\u0435\u043d\u00a0\u0431\u044b\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u0435\u0440\u0435\u0432\u0430, \u0430\u00a0\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439\u00a0\u2014 \u043c\u0435\u0442\u043a\u0430 \u0441\u0430\u043c\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/h2>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 item, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0443\u044e \u043f\u043e\u043b\u044f\u00a0 id(serial) \u0438 path(ltree). \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u043a\u043b\u044e\u0447\u0438\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443:<\/p>\n<pre><code class=\"sql\">CREATE EXTENSION IF NOT EXISTS ltree;  create table public.item (     id  serial  constraint item_pk primary key,     path ltree default ((lastval())::text)::ltree not null constraint item_pk2 unique );<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0435 path \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0443\u0441\u0442\u044b\u043c, \u0442.\u043a. \u0435\u0441\u043b\u0438 \u0434\u0430\u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439, \u0432 \u044d\u0442\u043e\u043c \u043f\u043e\u043b\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f id.<\/p>\n<p>\u0412 \u043c\u043e\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0438 \u043c\u043d\u043e\u0433\u043e item, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0447\u0430\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0442\u044c\u0441\u044f. \u042f \u0432\u044b\u0431\u0440\u0430\u043b \u0442\u0438\u043f serial \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430, \u0442.\u043a. \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0443\u043a\u0432\u0435\u043d\u043d\u043e-\u0446\u0438\u0444\u0440\u043e\u0432\u044b\u0445 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0431\u044a\u0435\u043c\u0435 \u0431\u0430\u0437\u044b \u043f\u0440\u0438 \u0440\u043e\u0441\u0442\u0435 \u043a\u043e\u043b-\u0432\u0430 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0438\u043b\u0438 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u043d\u043e-\u0431\u0443\u043a\u0432\u0435\u043d\u043d\u044b\u0445 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 \u0432\u0430\u043c \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435, \u0442.\u043a. \u0443 \u044d\u0442\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0435\u0441\u0442\u044c \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u043e\u0434\u0438\u043d \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u043b\u044e\u0441 &#8212; \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c id \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435. \u0412 path \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c UUID, \u0442.\u043a. \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 ltree \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0434\u0435\u0444\u0438\u0441.<\/p>\n<h2>\u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0435\u0439<\/h2>\n<p>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0430, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u044c. \u041f\u0435\u0440\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c item, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 path \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 id \u0437\u0430\u043f\u0438\u0441\u0438. \u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f id \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 insert \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 lastval(). \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0442\u0440\u0438\u0436\u0434\u044b:<\/p>\n<pre><code class=\"sql\">insert into item(path) values (lastval()::text::ltree);<\/code><\/pre>\n<p>\u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0442\u0440\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 (id = path).<\/p>\n<pre><code class=\"sql\">select * from item;  | id | path | | 1 | 1 | | 2 | 2 | | 3 | 3 |<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u043a\u043b\u044e\u0447\u043e\u043c, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u043c \u0438\u0437 \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u043a \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b\u0441\u044f \u0443\u0434\u0430\u0447\u043d\u043e:<\/p>\n<pre><code class=\"sql\">insert into item (path) values ('12.43.555.22.542');  select * from item;  | id | path | | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 12.43.555.22.542 | <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043d\u0435 \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u044b, \u0442.\u043a. \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0437\u0430\u043f\u0438\u0441\u044c \u0441 \u043f\u0443\u0442\u0435\u043c path, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u043c \u0438\u0437 \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u043a. \u0421 \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u0443\u0441\u043f\u0435\u0445\u043e\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u043f\u043e\u043b\u0435 path \u0438 \u201c\u0437\u0430\u0446\u0438\u043a\u043b\u0435\u043d\u043d\u043e\u0435\u201d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0443\u0442\u0438, \u0432\u0440\u043e\u0434\u0435 \u201c12.42.12\u201d \u0438 \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e \u0435\u0449\u0435. \u0422\u0430\u043a \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 PostgreSQL \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043b\u0430\u0441\u0442\u044c \u0432 \u043f\u043e\u043b\u0435 path \u043b\u044e\u0431\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0438\u0437 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. \u0420\u0435\u0448\u0438\u043c \u044d\u0442\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439. <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION item_before_update_insert_check_path() RETURNS TRIGGER AS $item_before_update_insert_check_path$ DECLARE     parentPath  ltree;     curLabel ltree;     parentId text; BEGIN     parentPath := subpath(NEW.path, 0, -1);     curLabel := subpath(NEW.path, -1);      -- \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043c\u0435\u0442\u043a\u0430 \u0432 \u043f\u0443\u0442\u0438 \u0434\u043e\u043b\u0436\u043d\u0430 \u0440\u0430\u0432\u043d\u044f\u0442\u044c\u0441\u044f id     if (curLabel::text != NEW.id::text) THEN         RAISE EXCEPTION 'The last path label % must be equal id %', curLabel::text, NEW.id::text;     end if;      -- \u0434\u043e\u043b\u0436\u043d\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u043c \u043f\u0443\u0442\u0435\u043c, \u0435\u0441\u043b\u0438 \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u043d\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u0430\u044f     if (parentPath != '') THEN         parentId := (select id from item where path = parentPath);         if (parentId IS NULL) THEN             RAISE EXCEPTION 'Parent item with path % not found', parentPath;         end if;     end if;      RETURN NEW; END; $item_before_update_insert_check_path$ LANGUAGE plpgsql; <\/code><\/pre>\n<p>\u0418 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0430\u0441\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u043b\u0438 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 path:<\/p>\n<pre><code class=\"sql\">CREATE TRIGGER tr_item_before_update_insert_check_path     BEFORE         INSERT OR         UPDATE OF path ON item     FOR EACH ROW EXECUTE FUNCTION item_before_update_insert_check_path();<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u043f\u0440\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0435 \u0438\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 path \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043d\u0430\u0440\u0443\u0448\u0430\u044e\u0449\u0435\u0435 \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0437\u0430\u043f\u0438\u0441\u044c \u0441 id=4 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439, \u043a\u0430\u043a \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435. \u0418\u0434\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435. \u0421\u0434\u0435\u043b\u0430\u0435\u043c item c id=1 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c id=2, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u043e\u0442\u043e\u043c \u043f\u043e\u043b\u043e\u0436\u0438\u043c 1 \u0432 3:<\/p>\n<pre><code class=\"sql\">update item set path = '1.2' where id = 2; update item set path = '3.1' where id = 1; select * from item;  | id | path | | 3 | 3 | | 2 | 1.2 | | 4 | 4 | | 1 | 3.1 |<\/code><\/pre>\n<p>\u0418 \u0441\u043d\u043e\u0432\u0430 \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0430 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c. \u0423 \u0437\u0430\u043f\u0438\u0441\u0438 \u0441 id 2 \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 path: \u0432\u043c\u0435\u0441\u0442\u043e \u20181.2\u2019 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u20183.1.2\u2019.\u00a0\u042d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430: \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043f\u0443\u0442\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430\u0434\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0443\u0442\u0438 \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u041e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u043c \u044d\u0442\u043e. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043d\u043e\u0432\u0430 \u0432\u0441\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c\u0438:<\/p>\n<pre><code class=\"sql\">update item set path = id::text::ltree;<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u0442\u0440\u0438\u0433\u0433\u0435\u0440:<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION item_after_update_set_children_path() RETURNS TRIGGER AS $item_after_update_set_children_path$ BEGIN      IF (NEW.path != OLD.path) THEN         UPDATE item         SET path = NEW.path || subpath(path, nlevel(OLD.path))         WHERE path &lt;@ OLD.path;     END IF;      RETURN NULL; END; $item_after_update_set_children_path$ LANGUAGE plpgsql;   CREATE TRIGGER tr_update_children_item_path     AFTER UPDATE OF path ON item     FOR EACH ROW EXECUTE FUNCTION item_after_update_set_children_path();<\/code><\/pre>\n<p>\u0430 \u0437\u0430\u0442\u0435\u043c \u0441\u043d\u043e\u0432\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u043f\u0443\u0442\u044f\u043c\u0438 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 2 \u0438 1:<\/p>\n<pre><code class=\"sql\">update item set path = '1.2' where id = 2; update item set path = '3.1' where id = 1; select * from item;  | id | path | | 3 | 3 | | 4 | 4 | | 1 | 3.1 | | 2 | 3.1.2 |<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c path \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441 path \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430. \u041f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0430 \u043a\u0430\u0441\u043a\u0430\u0434\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<h2>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0435\u0439<\/h2>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0438\u043c\u0435\u044e\u0449\u0438\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u043d\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u0442\u0440\u0438\u0433\u0433\u0435\u0440:<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION item_before_delete_check_children() RETURNS TRIGGER AS $item_check_children_before_delete$ DECLARE     childrenCount bigint; BEGIN     SELECT COUNT(*) into childrenCount     FROM item WHERE path &lt;@ OLD.path AND id != OLD.id;      IF (childrenCount > 0) THEN         RAISE EXCEPTION 'Deleting items containing child items by the path field is prohibited.';     end if; RETURN OLD; END; $item_check_children_before_delete$ LANGUAGE plpgsql;   CREATE TRIGGER tr_item_check_children_before_delete     BEFORE DELETE ON item     FOR EACH ROW EXECUTE FUNCTION item_before_delete_check_children();<\/code><\/pre>\n<p>\u0413\u043e\u0442\u043e\u0432\u043e.<\/p>\n<p><strong>\u0418\u0442\u043e\u0433\u043e<\/strong><\/p>\n<p>\u0414\u043b\u044f \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0439 \u043f\u043e\u043b\u0435 ltree, \u043d\u0443\u0436\u043d\u043e:<\/p>\n<ol>\n<li>\n<p>\u0421\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u043b\u0435 path not null;<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u0434\u043b\u044f\u00a0path;<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u044b:<\/p>\n<ul>\n<li>\n<p>BEFORE INSERT \u0438 BEFORE UPDATE, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0449\u0438\u0435, \u0447\u0442\u043e\u00a0\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043c\u0435\u0442\u043a\u0430 \u0432\u00a0\u043f\u043e\u043b\u0435 path \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f id \u0437\u0430\u043f\u0438\u0441\u0438 \u0438 \u0447\u0442\u043e\u00a0\u0432\u00a0\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u00a0path, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c path \u0437\u0430\u043f\u0438\u0441\u0438 \u0431\u0435\u0437\u00a0\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043c\u0435\u0442\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p>AFTER UPDATE, \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0449\u0438\u0439 path \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043f\u0440\u0438\u00a0\u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 path (\u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0438).<\/p>\n<\/li>\n<li>\n<p>BEFORE DELETE, \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u044e\u0449\u0438\u0439 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u0438, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c, \u0447\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043e\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439.<\/p>\n<\/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\/733814\/\"> https:\/\/habr.com\/ru\/articles\/733814\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-398207","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/398207","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=398207"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/398207\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}