{"id":307272,"date":"2020-07-21T15:00:57","date_gmt":"2020-07-21T15:00:57","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=307272"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=307272","title":{"rendered":"\u042d\u0442\u044e\u0434 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u0441 \u0435\u0434\u0438\u043d\u0438\u0447\u043d\u044b\u043c\u0438 \u0440\u0435\u0431\u0440\u0430\u043c\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f PL\/pgSQL"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/511830\/\">\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043e\u0431\u0449\u0438\u0435 \u0438\u0434\u0435\u0438 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u0432 PostgreSQL.<\/p>\n<p>  \u0413\u0440\u0430\u0444 \u0431\u044b\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434\u0447\u0438\u043d\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c\u0438, \u0432\u0437\u0430\u043c\u0435\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u043d\u0435\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 \u00ab\u043f\u0440\u0435\u0434\u043e\u043a-\u043f\u043e\u0442\u043e\u043c\u043e\u043a\u00bb \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043e\u0442\u0434\u0435\u043b\u043e\u0432.<\/p>\n<p>  \u041e\u043f\u044b\u0442 \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u043c, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0438 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u044f. \u042f \u0432 \u0441\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0438\u0441\u043a\u0430\u043b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 pqSQL, \u043d\u043e \u0432\u0438\u0434\u0438\u043c\u043e \u043f\u043b\u043e\u0445\u043e \u0438\u0441\u043a\u0430\u043b. \u041f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u043c\u0443. \u0427\u0442\u043e \u0432 \u043e\u0431\u0449\u0435\u043c-\u0442\u043e \u0434\u0430\u0436\u0435 \u043a \u043b\u0443\u0447\u0448\u0435\u043c\u0443, \u0437\u0430\u0434\u0430\u0447\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f, \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u0440\u0438\u044f\u0442\u043d\u043e \u0447\u0442\u043e-\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0432\u043e\u0438\u043c\u0438 \u0440\u0443\u043a\u0430\u043c\u0438, \u0442\u0430\u043a, \u0447\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0442\u0440\u0430\u0447\u0435\u043d\u043e \u043d\u0435 \u0437\u0440\u044f. <br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h2>\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/h2>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 <b>\u00ab\u0434\u043e\u043b\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u00bb<\/b> c \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u043c \u043a\u043b\u044e\u0447\u043e\u043c <b>id<\/b>. \u0414\u043e\u043b\u0436\u043d\u043e\u0441\u0442\u0438 \u0438\u043c\u0435\u044e\u0442 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u043f\u043e\u0434\u0447\u0438\u043d\u0435\u043d\u0438\u044f \u00ab\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0438\u043a-\u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u00bb. \u0418\u0434\u0435\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0434\u043e\u043b\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 <b>\u0433\u0440\u0430\u0444.<\/b><br \/>  \u0414\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0442\u0438 \u0432 \u0433\u0440\u0430\u0444\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b, \u043e\u0431\u0449\u0435\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/habr.com\/ru\/post\/111361\/\">\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u2014 \u0437\u0434\u0435\u0441\u044c<\/a><\/p>\n<h2>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/h2>\n<p>  <\/p>\n<ul>\n<li> \u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u0447\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e<\/li>\n<li> \u0421\u043f\u0438\u0441\u043e\u043a \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0438\u043a\u043e\u0432 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430<\/li>\n<li> \u042f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a \u043f\u043e\u0434\u0447\u0438\u043d\u0435\u043d\u043d\u044b\u043c \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e<\/li>\n<li> \u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u0447\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e(\u043f\u0443\u0442\u044c \u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0438\u043a\u0430 \u043a \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0443)<\/li>\n<\/ul>\n<p>  <\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f PL\/pgSQL<\/h2>\n<p>  <\/p>\n<h3>\u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430 \u0432 \u0432\u0438\u0434\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0440\u0435\u0431\u0435\u0440<\/h3>\n<p>  \u0412\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <b>id<\/b>, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b. <\/p>\n<pre><code class=\"pgsql\">CREATE TABLE graph (     vertex integer NOT NULL ,         --id \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435   edge integer NOT NULL ,           --id \u0440\u0435\u0431\u0440\u0430   vertex_order integer NOT NULL --\u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u044b ( 0 \u043f\u0440\u0435\u0434\u043e\u043a, 1 \u043f\u043e\u0442\u043e\u043c\u043e\u043a) );<\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 <b>id<\/b> \u0440\u0435\u0431\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/p>\n<pre><code class=\"pgsql\">CREATE SEQUENCE enge_seq OWNED BY graph.edge; <\/code><\/pre>\n<p>  <\/p>\n<h3>\u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0440\u0435\u0431\u0435\u0440<\/h3>\n<p>  \u0414\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u0440\u0435\u0431\u0440\u0430(\u0432\u0435\u0440\u0448\u0438\u043d\u044b <b>id0, id1<\/b>) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0432\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 INSERT <\/p>\n<pre><code class=\"pgsql\">--\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e id \u0440\u0435\u0431\u0440\u0430 new_id = nextval('enge_seq');<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"pgsql\">--\u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u043f\u0440\u0435\u0434\u043a\u0430 INSERT INTO  graph (vertex  , edge , vertex_order  ) VALUES ( id0 , new_id , 0  );<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"pgsql\">--\u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u043f\u043e\u0442\u043e\u043c\u043a\u0430 INSERT INTO  graph (vertex  , edge , vertex_order  ) VALUES ( id1 , new_id , 1  );<\/code><\/pre>\n<p>  <\/p>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0434\u0447\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e current_id<\/h3>\n<p>  <\/p>\n<pre><code class=\"pgsql\">SELECT    id  FROM    graph WHERE    vertex_order  = 1 AND    edge IN (SELECT edge FROM graph WHERE vertex  = current_id AND vertex_order = 0  );<\/code><\/pre>\n<p>  <\/p>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0438\u043a\u043e\u0432 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e current_id<\/h3>\n<p>  <\/p>\n<pre><code class=\"pgsql\">SELECT    id  FROM    graph WHERE    vertex_order  = 0 AND    edge IN (SELECT edge FROM graph WHERE vertex  = current_id AND vertex_order = 1  );<\/code><\/pre>\n<p>  <\/p>\n<h3> \u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438 (\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b) \u0438\u0437 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b start_id<\/h3>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b tmp_matrix<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"pgsql\">CREATE TEMPORARY TABLE tmp_matrix AS (   SELECT      DISTINCT vertex  ,      FALSE AS label ,     999999 AS distance ,     FALSE AS is_finish    FROM     graph );<\/code><\/pre>\n<\/div><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b tmp_matrix<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"pgsql\">UPDATE tmp_matrix  SET label = TRUE , distance = 0  WHERE vertex = current_id ;   current_id = start_id ;  SELECT    distance INTO    current_distance FROM    tmp_matrix WHERE    vertex = current_id;  SELECT    EXISTS (SELECT 1 FROM tmp_matrix WHERE label = FALSE ) INTO   not_visit;<\/code><\/pre>\n<\/div><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"pgsql\">WHILE not_visit  LOOP   FOR v_rec IN     SELECT        *    FROM       tmp_matrix   WHERE      NOT label AND      --\u0412\u0435\u0440\u0448\u0438\u043d\u0430 \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u0430 \u0437\u0430 \u043e\u0434\u0438\u043d \u0448\u0430\u0433      vertex IN ( SELECT                         id                       FROM                        graph                     WHERE                        vertex_order  = 1 AND                        edge IN (SELECT                                       edge                                      FROM                                        graph                                      WHERE                                        vertex  = current_id AND vertex_order = 0  ))   LOOP         --\u041d\u0430\u0439\u0434\u0435\u043d\u0430 \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430     IF v_rec.distance &gt; (current_distance +1)     THEN        UPDATE tmp_matrix SET distance = (current_distance +1) WHERE vertex = v_rec.vertex;     END IF ;         --\u0435\u0441\u043b\u0438 \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d \u043e\u0431\u0445\u043e\u0434    IF SELECT          NOT EXISTS          (           SELECT 1 FROM graph WHERE vertex = v_rec.vertex AND vertex_order = 0          )    THEN      UPDATE tmp_matrix SET label = TRUE , is_finish = TRUE WHERE vertex = v_rec.vertex;    END IF ;      END LOOP;      UPDATE tmp_matrix SET label = TRUE WHERE vertex = current_id ;    --\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f   SELECT      vertex   INTO     current_id    FROM      tmp_matrix    WHERE      NOT label AND     distance &lt; 999999 ;    SELECT      distance   INTO      current_distance   FROM      tmp_matrix    WHERE       vertex = current_id ;    SELECT      EXISTS (SELECT 1 FROM tmp_matrix  WHERE label = FALSE )   INTO    not_visit ;    IF current_id IS NULL    THEN      not_visit = FALSE ;    END IF; END LOOP;<\/code><\/pre>\n<\/div><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0412\u044b\u0434\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0443\u044e \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"pgsql\">SELECT    vertex ,    label ,     distance ,    is_finish FROM    tmp_matrix  WHERE    distance != 999999 ; <\/code><\/pre>\n<\/div><\/div>\n<h3> \u042f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a \u0441 check_id \u043f\u043e\u0434\u0447\u0438\u043d\u0435\u043d\u043d\u044b\u043c \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e start_id<\/h3>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f start_id (\u0441\u043c. \u0432\u044b\u0448\u0435).<\/p>\n<pre><code class=\"pgsql\">IF EXISTS    ( SELECT distance FROM tmp_matrix WHERE vertex = check_id  ) THEN     RETURN TRUE; ELSE    RETURN FALSE; END IF;<\/code><\/pre>\n<p>  <\/p>\n<h3> \u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u0447\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e(\u043f\u0443\u0442\u044c \u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0438\u043a\u0430 start_id \u043a \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0443 finish_id)<\/h3>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f start_id (\u0441\u043c. \u0432\u044b\u0448\u0435).<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043f\u0443\u0442\u0438 \u043c\u0435\u0436\u0434\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c\u0438 start_id \u0438 finish_id<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"pgsql\">current_id = finish_id ; result[1] = finish_id ;  WHILE current_id != start_id  LOOP   SELECT      par.id    FROM     ( SELECT         id       FROM        graph     WHERE        vertex_order  = 0 AND        edge IN (SELECT                       edge                      FROM                        graph                      WHERE                        vertex  = current_id AND vertex_order = 1  )) par   JOIN  tmp_matrix m ON ( par.id = m.vertex  )   INTO      parent_id   LIMIT 1 ;    SELECT      array_append( result , parent_id )   INTO      result ;   --\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f current_id = parent_id ;    END LOOP;<\/code><\/pre>\n<\/div><\/div>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 <b>result <\/b> \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0443\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u043d\u0430\u0431\u043e\u0440\u0430 <b>id<\/b> \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435. \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u043c\u0430\u0441\u0441\u0438\u0432 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0432\u0435\u0440\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c.<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/511830\/\"> https:\/\/habr.com\/ru\/post\/511830\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/511830\/\">\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043e\u0431\u0449\u0438\u0435 \u0438\u0434\u0435\u0438 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u0432 PostgreSQL.<\/p>\n<p>  \u0413\u0440\u0430\u0444 \u0431\u044b\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434\u0447\u0438\u043d\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c\u0438, \u0432\u0437\u0430\u043c\u0435\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u043d\u0435\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 \u00ab\u043f\u0440\u0435\u0434\u043e\u043a-\u043f\u043e\u0442\u043e\u043c\u043e\u043a\u00bb \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043e\u0442\u0434\u0435\u043b\u043e\u0432.<\/p>\n<p>  \u041e\u043f\u044b\u0442 \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u043c, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0438 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u044f. \u042f \u0432 \u0441\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0438\u0441\u043a\u0430\u043b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 pqSQL, \u043d\u043e \u0432\u0438\u0434\u0438\u043c\u043e \u043f\u043b\u043e\u0445\u043e \u0438\u0441\u043a\u0430\u043b. \u041f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u043c\u0443. \u0427\u0442\u043e \u0432 \u043e\u0431\u0449\u0435\u043c-\u0442\u043e \u0434\u0430\u0436\u0435 \u043a \u043b\u0443\u0447\u0448\u0435\u043c\u0443, \u0437\u0430\u0434\u0430\u0447\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f, \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u0440\u0438\u044f\u0442\u043d\u043e \u0447\u0442\u043e-\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0432\u043e\u0438\u043c\u0438 \u0440\u0443\u043a\u0430\u043c\u0438, \u0442\u0430\u043a, \u0447\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0442\u0440\u0430\u0447\u0435\u043d\u043e \u043d\u0435 \u0437\u0440\u044f.   <\/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-307272","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/307272","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=307272"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/307272\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=307272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=307272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=307272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}