{"id":319366,"date":"2021-03-11T09:00:20","date_gmt":"2021-03-11T09:00:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=319366"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=319366","title":{"rendered":"\u041e\u0431\u0440\u0435\u0437\u0430\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 PostgreSQL \u0432 production"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0440\u0435\u0446\u0435\u043f\u0442\u043e\u043c \u043f\u043e \u043e\u0431\u0440\u0435\u0437\u0430\u043d\u0438\u044e \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b PostgreSQL \u0432 production. \u041f\u0440\u0438\u043c\u0435\u0440: \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u0432 \u043f\u0440\u043e\u0434\u043e\u0432\u043e\u0439 \u0411\u0414 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c task (\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0442\u0435\u043d \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u043e\u0432 \u0441\u0442\u0440\u043e\u043a) \u0441 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043c \u0443\u0436\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b. \u0422\u043e\u0447\u043d\u0435\u0435, \u043e\u043d\u0438 \u043c\u0435\u0448\u0430\u044e\u0442 \u2014 \u0411\u0414 \u0434\u043e\u043b\u0433\u043e \u0434\u0430\u043c\u043f\u0438\u0442\u0441\u044f, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c\u0438.&nbsp;\u041c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0431\u0440\u0435\u0437\u0430\u0442\u044c \u044d\u0442\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 (\u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0440\u0430\u043d\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0434\u0430\u0442\u044b). \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0432 \u0431\u0430\u0437\u0435 \u043d\u0435\u0442 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 foreign key \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 task (\u043f\u0440\u0438 \u0438\u0445 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442\u0441\u044f). <\/p>\n<p>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0442\u0430\u043a\u0430\u044f (\u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440):<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5c7\/af6\/4e5\/5c7af64e58a39f6dea7f6049c6c3584c.png\" width=\"706\" height=\"572\"><figcaption><\/figcaption><\/figure>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u043b\u043e\u0431 (<code>delete from task where id &lt; 1234567<\/code>) \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0434\u043e\u043b\u0433\u043e \u0438\u0437-\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0438 \u043d\u0430\u0441 \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442.<\/p>\n<p>\u0411\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c:<\/p>\n<ol>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 task_new, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u043c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<br \/><code>CREATE TABLE task_new LIKE tasks;<br \/>ALTER TABLE task_new ADD PRIMARY KEY (id);<\/code><\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0440\u0438\u0433\u0433\u0435\u0440 \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0430 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u0442\u0440\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.<\/p>\n<\/li>\n<li>\n<p>\u041c\u0435\u043d\u044f\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u0435\u0441\u0442\u0430\u043c\u0438.<\/p>\n<\/li>\n<\/ol>\n<hr>\n<h3>\u0428\u0430\u0433 1. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/h3>\n<p>\u041d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 (\u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0430 NULL \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e). \u0418\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0432\u0435\u0441\u0438\u043c \u043f\u043e\u0437\u0434\u043d\u0435\u0435, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0442\u044c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code>CREATE TABLE task_new (LIKE task INCLUDING DEFAULTS); ALTER TABLE task_new ADD PRIMARY KEY (id);<\/code><\/pre>\n<h3>\u0428\u0430\u0433 2. \u0417\u0430\u043f\u0443\u0441\u043a \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430<\/h3>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u0442\u0440\u0438\u0433\u0433\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 (\u043a\u043e\u043c\u0430\u043d\u0434\u044b <em>insert\/update\/delete<\/em>).<\/p>\n<pre><code>create or replace function task_replication_trg_func() returns trigger  AS $func$ begin     if TG_OP = 'INSERT' then         insert into task_new(             id,             created,             updated,             status,             json_data,             project_id,             service_id,             error         )          values (             NEW.id,             NEW.created,             NEW.updated,             NEW.status,             NEW.json_data,             NEW.project_id,             NEW.service_id,             NEW.error         );     elsif TG_OP = 'UPDATE' then         update task_new set             created = NEW.created,             updated = NEW.updated,             status = NEW.status,             json_data = NEW.json_data,             project_id = NEW.project_id,             service_id = NEW.service_id,             error = NEW.error                       where id = NEW.id;     elsif TG_OP = 'DELETE' then         delete from task_new              where id = OLD.id;     end if;     return NULL; end; $func$ LANGUAGE plpgsql;   create trigger task_replication_trg  after insert or update or delete on task  for each row EXECUTE PROCEDURE task_replication_trg_func();<\/code><\/pre>\n<h3>\u0428\u0430\u0433 3. \u0417\u0430\u043f\u0443\u0441\u043a \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0430 \u0441\u0442\u0440\u043e\u043a<\/h3>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u0442 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e.<\/p>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 (\u043d\u0430\u043f\u0438\u0441\u0430\u043d \u043d\u0430 Python 3) \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 &#8212; <a href=\"https:\/\/gist.github.com\/olegborzov\/1d056104875a5a8eb769c3c441ab6068\"><u>https:\/\/gist.github.com\/olegborzov\/1d056104875a5a8eb769c3c441ab6068<\/u><\/a>&nbsp;<\/p>\n<p>\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u043a\u0440\u0438\u043f\u0442\u0430:&nbsp;<\/p>\n<ul>\n<li>\n<p>conn_str \u2014 \u0441\u0442\u0440\u043e\u043a\u0430 \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0411\u0414;<\/p>\n<\/li>\n<li>\n<p>first_row_id \u2014 \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u0442\u043e\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u043d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 (\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c select id from task where created::date = &#8216;2020-01-01&#8217;::date &#8212; interval &#8216;1 day&#8217; limit 1;);<\/p>\n<\/li>\n<li>\n<p>first_trigger_id \u2014 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u0432 \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u043c (\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c select id from task_new order by id limit 1);<\/p>\n<\/li>\n<li>\n<p>sleep_ms \u2014 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043f\u0430\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442 \u043c\u0435\u0436\u0434\u0443 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438;<\/p>\n<\/li>\n<li>\n<p>chunk_size \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a, \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c\u044b\u0445 \u0437\u0430 \u043e\u0434\u0438\u043d \u0440\u0430\u0437.<\/p>\n<\/li>\n<\/ul>\n<p>\u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430:<\/p>\n<pre><code>pip3 install psycopg2-binary~=2.8 python3 mastersber_transfer.py task --conn_str=postgresql:\/\/{username}:{password}@{host}:{port}\/{db_name} --first_row_id={first_row_id} --first_trigger_id={first_trigger_id}&nbsp;<\/code><\/pre>\n<h3>\u0428\u0430\u0433 4. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0432\u0435\u0441\u0438\u0442\u044c \u043d\u0430 \u043d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441\u043e \u0441\u0442\u0430\u0440\u043e\u0439.<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0432 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 (\u043a\u0440\u043e\u043c\u0435 PK):<\/p>\n<pre><code>SELECT pg_get_indexdef(indexrelid) || ';' AS idx FROM pg_index WHERE indrelid = 'task'::regclass and not indisprimary;<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043d\u0430\u0431\u043e\u0440 \u043a\u043e\u043c\u0430\u043d\u0434:<\/p>\n<pre><code>CREATE INDEX ix_task_status ON public.task USING btree (status); CREATE INDEX task_project_id_index ON public.task USING btree (project_id);<\/code><\/pre>\n<p>\u041a\u043e\u043c\u0430\u043d\u0434\u044b \u043d\u0443\u0436\u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c: \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e CONCURRENTLY, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430 \u043d\u043e\u0432\u0443\u044e \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u0438\u043b\u0438 \u0441\u0443\u0444\u0444\u0438\u043a\u0441 \u201c_new\u201d \u043a \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432:<\/p>\n<pre><code>CREATE INDEX CONCURRENTLY new_ix_task_status ON public.task_new USING btree (status); CREATE INDEX CONCURRENTLY new_task_project_id_index ON public.task_new USING btree (project_id);<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<\/p>\n<pre><code>SELECT 'ALTER TABLE task_new ADD CONSTRAINT ' || conname || ' ' || pg_get_constraintdef(oid) || ';' FROM pg_constraint WHERE contype = 'f' AND&nbsp;conrelid::regclass::text = 'task' ORDER BY conrelid::regclass::text, conname;<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043d\u0430\u0431\u043e\u0440 \u043a\u043e\u043c\u0430\u043d\u0434 (\u0432 \u043d\u0438\u0445 \u043d\u0438\u0447\u0435\u0433\u043e \u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e):<\/p>\n<pre><code>ALTER TABLE task_new ADD CONSTRAINT task_project_id_fkey FOREIGN KEY (project_id) REFERENCES project(id); ALTER TABLE task_new ADD CONSTRAINT task_service_id_fkey FOREIGN KEY (service_id) REFERENCES service(id);<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u044d\u0442\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b:<\/p>\n<pre><code>BEGIN;  CREATE INDEX CONCURRENTLY new_ix_task_status ON public.task_new USING btree (status); CREATE INDEX CONCURRENTLY new_task_project_id_index ON public.task_new USING btree (project_id);  ALTER TABLE task_new ADD CONSTRAINT task_project_id_fkey FOREIGN KEY (project_id) REFERENCES project(id); ALTER TABLE task_new ADD CONSTRAINT task_service_id_fkey FOREIGN KEY (service_id) REFERENCES service(id);  COMMIT;<\/code><\/pre>\n<h3>\u0428\u0430\u0433 5. \u041c\u0435\u043d\u044f\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u0435\u0441\u0442\u0430\u043c\u0438<\/h3>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u0442\u0430\u043f: \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u0435\u0441\u0442\u0430\u043c\u0438, \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u0442\u044c sequence \u0441\u043e \u0441\u0442\u0430\u0440\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430 \u043d\u043e\u0432\u0443\u044e \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0442\u0440\u0438\u0433\u0433\u0435\u0440.<\/p>\n<pre><code>BEGIN;  set statement_timeout = 3000; set deadlock_timeout = '3s';  ALTER TABLE task_new ALTER COLUMN id SET DEFAULT nextval('task_id_seq'); ALTER SEQUENCE task_id_seq OWNED BY task_new.id; ALTER TABLE task RENAME TO task_old; ALTER TABLE task_new RENAME TO task; DROP TRIGGER IF EXISTS task_replication_trg ON task_old CASCADE;  COMMIT;  -- \u0415\u0441\u043b\u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430 \u0441\u0442\u0430\u0440\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 - \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0435\u0435: DROP TABLE task_old CASCADE;<\/code><\/pre>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043e\u0442 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0436\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b, \u043d\u043e \u0443\u0445\u0443\u0434\u0448\u0430\u044e\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0435\u0441\u0442\u044c \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 foreign keys, \u043f\u0435\u0440\u0435\u0434 \u0447\u0438\u0441\u0442\u043a\u043e\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0447\u0438\u0441\u0442\u0438\u043c \u0441\u0441\u044b\u043b\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u043d\u0430 \u043d\u0435\u0435 (\u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u043d\u0430 \u043a\u043e\u043b\u043e\u043d\u043a\u0443 c foreign key \u043d\u0430 \u043d\u0443\u0436\u043d\u0443\u044e).<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441\u044b &#8212; \u043f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n<\/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\/company\/domclick\/blog\/546008\/\"> https:\/\/habr.com\/ru\/company\/domclick\/blog\/546008\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0440\u0435\u0446\u0435\u043f\u0442\u043e\u043c \u043f\u043e \u043e\u0431\u0440\u0435\u0437\u0430\u043d\u0438\u044e \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b PostgreSQL \u0432 production. \u041f\u0440\u0438\u043c\u0435\u0440: \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u0432 \u043f\u0440\u043e\u0434\u043e\u0432\u043e\u0439 \u0411\u0414 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c task (\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0442\u0435\u043d \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u043e\u0432 \u0441\u0442\u0440\u043e\u043a) \u0441 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043c \u0443\u0436\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b. \u0422\u043e\u0447\u043d\u0435\u0435, \u043e\u043d\u0438 \u043c\u0435\u0448\u0430\u044e\u0442 \u2014 \u0411\u0414 \u0434\u043e\u043b\u0433\u043e \u0434\u0430\u043c\u043f\u0438\u0442\u0441\u044f, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c\u0438.&nbsp;\u041c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0431\u0440\u0435\u0437\u0430\u0442\u044c \u044d\u0442\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 (\u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0440\u0430\u043d\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0434\u0430\u0442\u044b). \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0432 \u0431\u0430\u0437\u0435 \u043d\u0435\u0442 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 foreign key \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 task (\u043f\u0440\u0438 \u0438\u0445 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442\u0441\u044f). <\/p>\n<p>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0442\u0430\u043a\u0430\u044f (\u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440):<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u043b\u043e\u0431 (<code>delete from task where id &lt; 1234567<\/code>) \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0434\u043e\u043b\u0433\u043e \u0438\u0437-\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0438 \u043d\u0430\u0441 \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442.<\/p>\n<p>\u0411\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c:<\/p>\n<ol>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 task_new, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u043c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<br \/><code>CREATE TABLE task_new LIKE tasks;<br \/>ALTER TABLE task_new ADD PRIMARY KEY (id);<\/code><\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0440\u0438\u0433\u0433\u0435\u0440 \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0430 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u0442\u0440\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.<\/p>\n<\/li>\n<li>\n<p>\u041c\u0435\u043d\u044f\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u0435\u0441\u0442\u0430\u043c\u0438.<\/p>\n<\/li>\n<\/ol>\n<hr>\n<h3>\u0428\u0430\u0433 1. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/h3>\n<p>\u041d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 (\u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0430 NULL \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e). \u0418\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0432\u0435\u0441\u0438\u043c \u043f\u043e\u0437\u0434\u043d\u0435\u0435, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0442\u044c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code>CREATE TABLE task_new (LIKE task INCLUDING DEFAULTS); ALTER TABLE task_new ADD PRIMARY KEY (id);<\/code><\/pre>\n<h3>\u0428\u0430\u0433 2. \u0417\u0430\u043f\u0443\u0441\u043a \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430<\/h3>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u0442\u0440\u0438\u0433\u0433\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 (\u043a\u043e\u043c\u0430\u043d\u0434\u044b <em>insert\/update\/delete<\/em>).<\/p>\n<pre><code>create or replace function task_replication_trg_func() returns trigger  AS $func$ begin     if TG_OP = 'INSERT' then         insert into task_new(             id,             created,             updated,             status,             json_data,             project_id,             service_id,             error         )          values (             NEW.id,             NEW.created,             NEW.updated,             NEW.status,             NEW.json_data,             NEW.project_id,             NEW.service_id,             NEW.error         );     elsif TG_OP = 'UPDATE' then         update task_new set             created = NEW.created,             updated = NEW.updated,             status = NEW.status,             json_data = NEW.json_data,             project_id = NEW.project_id,             service_id = NEW.service_id,             error = NEW.error                       where id = NEW.id;     elsif TG_OP = 'DELETE' then         delete from task_new              where id = OLD.id;     end if;     return NULL; end; $func$ LANGUAGE plpgsql;   create trigger task_replication_trg  after insert or update or delete on task  for each row EXECUTE PROCEDURE task_replication_trg_func();<\/code><\/pre>\n<h3>\u0428\u0430\u0433 3. \u0417\u0430\u043f\u0443\u0441\u043a \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0430 \u0441\u0442\u0440\u043e\u043a<\/h3>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u0442 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e.<\/p>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 (\u043d\u0430\u043f\u0438\u0441\u0430\u043d \u043d\u0430 Python 3) \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 &#8212; <a href=\"https:\/\/gist.github.com\/olegborzov\/1d056104875a5a8eb769c3c441ab6068\"><u>https:\/\/gist.github.com\/olegborzov\/1d056104875a5a8eb769c3c441ab6068<\/u><\/a>&nbsp;<\/p>\n<p>\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u043a\u0440\u0438\u043f\u0442\u0430:&nbsp;<\/p>\n<ul>\n<li>\n<p>conn_str \u2014 \u0441\u0442\u0440\u043e\u043a\u0430 \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0411\u0414;<\/p>\n<\/li>\n<li>\n<p>first_row_id \u2014 \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u0442\u043e\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u043d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 (\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c select id from task where created::date = &#8216;2020-01-01&#8217;::date &#8212; interval &#8216;1 day&#8217; limit 1;);<\/p>\n<\/li>\n<li>\n<p>first_trigger_id \u2014 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u0432 \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u043c (\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c select id from task_new order by id limit 1);<\/p>\n<\/li>\n<li>\n<p>sleep_ms \u2014 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043f\u0430\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442 \u043c\u0435\u0436\u0434\u0443 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438;<\/p>\n<\/li>\n<li>\n<p>chunk_size \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a, \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c\u044b\u0445 \u0437\u0430 \u043e\u0434\u0438\u043d \u0440\u0430\u0437.<\/p>\n<\/li>\n<\/ul>\n<p>\u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430:<\/p>\n<pre><code>pip3 install psycopg2-binary~=2.8 python3 mastersber_transfer.py task --conn_str=postgresql:\/\/{username}:{password}@{host}:{port}\/{db_name} --first_row_id={first_row_id} --first_trigger_id={first_trigger_id}&nbsp;<\/code><\/pre>\n<h3>\u0428\u0430\u0433 4. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0432\u0435\u0441\u0438\u0442\u044c \u043d\u0430 \u043d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441\u043e \u0441\u0442\u0430\u0440\u043e\u0439.<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0432 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 (\u043a\u0440\u043e\u043c\u0435 PK):<\/p>\n<pre><code>SELECT pg_get_indexdef(indexrelid) || ';' AS idx FROM pg_index WHERE indrelid = 'task'::regclass and not indisprimary;<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043d\u0430\u0431\u043e\u0440 \u043a\u043e\u043c\u0430\u043d\u0434:<\/p>\n<pre><code>CREATE INDEX ix_task_status ON public.task USING btree (status); CREATE INDEX task_project_id_index ON public.task USING btree (project_id);<\/code><\/pre>\n<p>\u041a\u043e\u043c\u0430\u043d\u0434\u044b \u043d\u0443\u0436\u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c: \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e CONCURRENTLY, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430 \u043d\u043e\u0432\u0443\u044e \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u0438\u043b\u0438 \u0441\u0443\u0444\u0444\u0438\u043a\u0441 \u201c_new\u201d \u043a \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432:<\/p>\n<pre><code>CREATE INDEX CONCURRENTLY new_ix_task_status ON public.task_new USING btree (status); CREATE INDEX CONCURRENTLY new_task_project_id_index ON public.task_new USING btree (project_id);<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<\/p>\n<pre><code>SELECT 'ALTER TABLE task_new ADD CONSTRAINT ' || conname || ' ' || pg_get_constraintdef(oid) || ';' FROM pg_constraint WHERE contype = 'f' AND&nbsp;conrelid::regclass::text = 'task' ORDER BY conrelid::regclass::text, conname;<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043d\u0430\u0431\u043e\u0440 \u043a\u043e\u043c\u0430\u043d\u0434 (\u0432 \u043d\u0438\u0445 \u043d\u0438\u0447\u0435\u0433\u043e \u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e):<\/p>\n<pre><code>ALTER TABLE task_new ADD CONSTRAINT task_project_id_fkey FOREIGN KEY (project_id) REFERENCES project(id); ALTER TABLE task_new ADD CONSTRAINT task_service_id_fkey FOREIGN KEY (service_id) REFERENCES service(id);<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u044d\u0442\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b:<\/p>\n<pre><code>BEGIN;  CREATE INDEX CONCURRENTLY new_ix_task_status ON public.task_new USING btree (status); CREATE INDEX CONCURRENTLY new_task_project_id_index ON public.task_new USING btree (project_id);  ALTER TABLE task_new ADD CONSTRAINT task_project_id_fkey FOREIGN KEY (project_id) REFERENCES project(id); ALTER TABLE task_new ADD CONSTRAINT task_service_id_fkey FOREIGN KEY (service_id) REFERENCES service(id);  COMMIT;<\/code><\/pre>\n<h3>\u0428\u0430\u0433 5. \u041c\u0435\u043d\u044f\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u0435\u0441\u0442\u0430\u043c\u0438<\/h3>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u0442\u0430\u043f: \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u0435\u0441\u0442\u0430\u043c\u0438, \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u0442\u044c sequence \u0441\u043e \u0441\u0442\u0430\u0440\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430 \u043d\u043e\u0432\u0443\u044e \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0442\u0440\u0438\u0433\u0433\u0435\u0440.<\/p>\n<pre><code>BEGIN;  set statement_timeout = 3000; set deadlock_timeout = '3s';  ALTER TABLE task_new ALTER COLUMN id SET DEFAULT nextval('task_id_seq'); ALTER SEQUENCE task_id_seq OWNED BY task_new.id; ALTER TABLE task RENAME TO task_old; ALTER TABLE task_new RENAME TO task; DROP TRIGGER IF EXISTS task_replication_trg ON task_old CASCADE;  COMMIT;  -- \u0415\u0441\u043b\u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430 \u0441\u0442\u0430\u0440\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 - \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0435\u0435: DROP TABLE task_old CASCADE;<\/code><\/pre>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043e\u0442 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0436\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b, \u043d\u043e \u0443\u0445\u0443\u0434\u0448\u0430\u044e\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0435\u0441\u0442\u044c \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 foreign keys, \u043f\u0435\u0440\u0435\u0434 \u0447\u0438\u0441\u0442\u043a\u043e\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0447\u0438\u0441\u0442\u0438\u043c \u0441\u0441\u044b\u043b\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u043d\u0430 \u043d\u0435\u0435 (\u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u043d\u0430 \u043a\u043e\u043b\u043e\u043d\u043a\u0443 c foreign key \u043d\u0430 \u043d\u0443\u0436\u043d\u0443\u044e).<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441\u044b &#8212; \u043f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n<\/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\/company\/domclick\/blog\/546008\/\"> https:\/\/habr.com\/ru\/company\/domclick\/blog\/546008\/<\/a><br \/><\/br><\/br><\/li>\n<\/ol>\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-319366","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319366","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=319366"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319366\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}