{"id":470296,"date":"2025-08-11T21:05:07","date_gmt":"2025-08-11T21:05:07","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=470296"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=470296","title":{"rendered":"<span>COPY \u0432 PostgreSQL: \u0433\u0440\u0443\u0437\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0431\u044b\u0441\u0442\u0440\u043e, \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0438 \u0431\u0435\u0437 \u0441\u044e\u0440\u043f\u0440\u0438\u0437\u043e\u0432<\/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>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440. <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c COPY \u0432 PostgreSQL. \u042d\u0442\u043e \u0440\u0430\u0431\u043e\u0447\u0430\u044f \u043b\u043e\u0448\u0430\u0434\u043a\u0430 \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u043e\u0432\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0438 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<h3>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 COPY \u0438 \u0447\u0435\u043c \u043e\u043d \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 INSERT<\/h3>\n<p><code>COPY<\/code> \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u0435\u0436\u0434\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 \u0438 \u0444\u0430\u0439\u043b\u043e\u043c \u0438\u043b\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 STDIN\/STDOUT. \u0412\u0430\u0440\u0438\u0430\u043d\u0442 <code>COPY FROM<\/code> \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442, <code>COPY TO<\/code> \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0435\u0442. \u0423\u043c\u0435\u0435\u0442 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u044b <code>text<\/code>, <code>csv<\/code>, <code>binary<\/code>.<\/p>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b <code>ON_ERROR<\/code>, <code>FREEZE<\/code>, <code>HEADER<\/code> \u0438 <code>HEADER MATCH<\/code>, <code>FORCE_*<\/code>, <code>ENCODING<\/code>, <code>WHERE<\/code>, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u043f\u0443\u0441\u043a \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c \u0447\u0435\u0440\u0435\u0437 <code>PROGRAM<\/code>. \u042d\u0442\u043e \u0440\u0430\u0437\u0430 \u0432 \u0434\u0432\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043b\u044e\u0431\u043e\u0433\u043e \u0431\u0430\u0442\u0447\u0435\u0432\u043e\u0433\u043e <code>INSERT<\/code> \u043f\u0440\u0438 \u0440\u0430\u0432\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u0438 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u043f\u0440\u043e\u0449\u0435 \u0432 \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f:<\/p>\n<pre><code class=\"sql\">-- \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 CSV \u0432 \u043f\u043e\u0442\u043e\u043a COPY public.events TO STDOUT WITH (FORMAT csv, HEADER true);  -- \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437 CSV \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u0430 COPY public.events FROM STDIN WITH (FORMAT csv, HEADER true);  -- \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 (\u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0430\u0432\u0430) COPY public.events FROM '\/var\/lib\/postgresql\/import.csv' WITH (FORMAT csv, HEADER true);  -- \u0447\u0435\u0440\u0435\u0437 \u0432\u043d\u0435\u0448\u043d\u044e\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 (\u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u043a\u0430 \u043d\u0430 \u043b\u0435\u0442\u0443) COPY public.events FROM PROGRAM 'gzip -dc \/data\/import.csv.gz' WITH (FORMAT csv, HEADER true);  -- \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043d\u0435 \u0432\u0441\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b COPY (   SELECT id, payload, created_at   FROM public.events   WHERE created_at &gt;= DATE '2025-01-01' ) TO STDOUT WITH (FORMAT csv, HEADER true);<\/code><\/pre>\n<h3>COPY \u043f\u0440\u043e\u0442\u0438\u0432 \\copy \u0432 psql<\/h3>\n<p>\u0412\u0430\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0442\u044c \u0434\u0432\u0430 \u043c\u0438\u0440\u0430. <code>COPY ... FROM\/TO 'filename'<\/code> \u0438 <code>COPY ... PROGRAM<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u0440\u043e\u043b\u0435\u0439. \u0424\u0430\u0439\u043b\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0438\u043c\u0435\u043d\u043d\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0443. \u041c\u0435\u0442\u0430\u043a\u043e\u043c\u0430\u043d\u0434\u0430 <code>\\copy<\/code> \u0438\u0437 <code>psql<\/code> \u044d\u0442\u043e \u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u0432\u043e\u043a\u0440\u0443\u0433 <code>COPY ... FROM STDIN\/TO STDOUT<\/code>, \u0438 \u0444\u0430\u0439\u043b\u044b \u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f\/\u043f\u0438\u0448\u0443\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0430. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0445 \u043f\u0440\u0430\u0432 \u0438\u043b\u0438 \u0444\u0430\u0439\u043b \u043b\u0435\u0436\u0438\u0442 \u0443 \u0432\u0430\u0441 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u0436\u0435 <code>\\copy<\/code>. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0438 \u0444\u0430\u0439\u043b\u044b \u0443\u0436\u0435 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435,\u0442\u043e \u0442\u0443\u0442 \u0443\u0436\u0435 \u043d\u0443\u0436\u0435\u043d \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 <code>COPY<\/code>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f <code>psql<\/code>:<\/p>\n<pre><code class=\"sql\">-- \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0432 \u0441\u0436\u0430\u0442\u044b\u0439 \u0444\u0430\u0439\u043b \\copy (SELECT * FROM public.events) TO PROGRAM 'gzip &gt; \/tmp\/events.csv.gz' WITH (FORMAT csv, HEADER true)  -- \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \\copy public.events FROM '\/home\/user\/import.csv' WITH (FORMAT csv, HEADER true)<\/code><\/pre>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435<\/h3>\n<p>\u041f\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u0443 <code>COPY FROM<\/code> \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435. \u0412 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u0445 \u00ab\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0432\u0441\u0451, \u0447\u0442\u043e \u0432\u0430\u043b\u0438\u0434\u043d\u043e, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0432 \u043a\u0430\u0440\u0430\u043d\u0442\u0438\u043d\u00bb \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 <code>ON_ERROR ignore<\/code>:<\/p>\n<pre><code class=\"sql\">COPY public.events FROM STDIN   WITH (     FORMAT csv,     HEADER true,     ON_ERROR ignore,     LOG_VERBOSITY verbose   );<\/code><\/pre>\n<p>\u041f\u0440\u0438 <code>ignore<\/code> \u0432\u0441\u0435 \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u044b, \u0432 \u043a\u043e\u043d\u0446\u0435 \u043f\u0440\u0438\u0434\u0451\u0442 <code>NOTICE<\/code> \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043e\u0432. \u0421 <code>LOG_VERBOSITY verbose<\/code> \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0438\u0448\u0435\u0442, \u043a\u0430\u043a\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043a\u0430\u043a\u0430\u044f \u043a\u043e\u043b\u043e\u043d\u043a\u0430 \u0443\u043f\u0430\u043b\u0438 \u043d\u0430 \u043a\u043e\u043d\u0432\u0435\u0440\u0441\u0438\u0438. \u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e, \u043d\u043e \u043d\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u044d\u0442\u043e \u0432 \u043d\u043e\u0440\u043c\u0443 \u043d\u0430 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0439 \u043e\u0441\u043d\u043e\u0432\u0435: \u0434\u043b\u044f \u0447\u0438\u0441\u0442\u044b\u0445 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u043e\u0432 \u043b\u0443\u0447\u0448\u0435 staging-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u043c\u0438 \u043a\u043e\u043b\u043e\u043d\u043a\u0430\u043c\u0438, \u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0442\u0443\u0434\u0430, \u0430 \u0437\u0430\u0442\u0435\u043c \u044f\u0432\u043d\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432 \u0432\u043d\u043e\u0441\u0438\u0442\u044c \u0432 \u0446\u0435\u043b\u0435\u0432\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 staging-\u043f\u043e\u0442\u043e\u043a\u0430:<\/p>\n<pre><code class=\"sql\">-- \u0441\u044b\u0440\u043e\u0439 \u0441\u043b\u043e\u0439 CREATE UNLOGGED TABLE staging.events_raw (   id_text text,   payload_text text,   created_at_text text );  -- \u0431\u0435\u0437 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e COPY staging.events_raw FROM STDIN WITH (FORMAT csv, HEADER true);  -- \u0447\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u043b\u043e\u0439 INSERT INTO public.events (id, payload, created_at) SELECT   id_text::bigint,   payload_text::jsonb,   created_at_text::timestamptz FROM staging.events_raw WHERE id_text ~ '^\\d+$'   AND created_at_text IS NOT NULL ON CONFLICT (id) DO UPDATE SET payload = EXCLUDED.payload,     created_at = EXCLUDED.created_at;<\/code><\/pre>\n<h3>\u0413\u0434\u0435 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441<\/h3>\n<p>\u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b <code>COPY<\/code> \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432 <code>pg_stat_progress_copy<\/code>. <\/p>\n<pre><code class=\"sql\">SELECT pid, datname, relid::regclass AS relation, command, type, bytes_processed, bytes_total,        tuples_processed, tuples_excluded, error_count FROM pg_stat_progress_copy;<\/code><\/pre>\n<p><code>tuples_excluded<\/code> \u0440\u0430\u0441\u0442\u0451\u0442, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>WHERE<\/code> \u0438 \u0441\u0442\u0440\u043e\u043a\u0438 \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f. <code>error_count<\/code> \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u043f\u0440\u0438 <code>ON_ERROR ignore<\/code>.<\/p>\n<h3>\u0424\u043e\u0440\u043c\u0430\u0442\u044b: \u043a\u043e\u0433\u0434\u0430 \u043a\u0430\u043a\u043e\u0439<\/h3>\n<p>\u0422\u0440\u0438 \u0440\u0435\u0436\u0438\u043c\u0430 \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u044e\u0442 \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435 \u0441\u043b\u0443\u0447\u0430\u0438.<\/p>\n<p>\u0422\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u0434\u0430\u0451\u0442 \u0442\u0430\u0431\u0443\u043b\u044f\u0446\u0438\u044e \u043a\u0430\u043a \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044c \u0438 <code>\\N<\/code> \u043a\u0430\u043a NULL. \u041e\u043d \u0435\u0441\u0442\u044c, \u043d\u043e \u0440\u0435\u0434\u043a\u043e \u043d\u0443\u0436\u0435\u043d: CSV \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u0435\u0435, \u0430 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u0431\u044b\u0441\u0442\u0440\u0435\u0435.<\/p>\n<p>CSV \u2014 \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0439. \u041f\u0430\u0440\u0430 \u043d\u044e\u0430\u043d\u0441\u043e\u0432:<\/p>\n<ul>\n<li>\n<p><code>HEADER true<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043d\u0430 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0435 \u0438 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043f\u0435\u0440\u0432\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u043d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435. \u0412\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u0438\u043b\u0438\u0442\u044c <code>HEADER MATCH<\/code>, \u0442\u043e\u0433\u0434\u0430 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043e\u0431\u044f\u0437\u0430\u043d \u0441\u043e\u0432\u043f\u0430\u0441\u0442\u044c \u0441 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 \u043a\u043e\u043b\u043e\u043d\u043e\u043a \u0438 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0443\u0441\u0442\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 NULL \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f. \u041f\u0443\u0441\u0442\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u044d\u0442\u043e <code>\"\"<\/code>, NULL \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u043e \u0431\u0435\u0437 \u043a\u0430\u0432\u044b\u0447\u0435\u043a, \u043b\u0438\u0431\u043e \u0441\u0442\u0440\u043e\u043a\u0430 \u0438\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 <code>NULL '...'<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u044f\u0432\u043d\u043e\u0433\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u0430\u0432\u044b\u0447\u043a\u0430\u043c\u0438 \u0438 \u044d\u043a\u0440\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>QUOTE<\/code>, <code>ESCAPE<\/code>, \u0430 \u0434\u043b\u044f \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043a\u043e\u043b\u043e\u043d\u043e\u043a <code>FORCE_QUOTE (col1, col2)<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u0432\u044b\u0433\u043e\u0434\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u0430 Postgres \u2192 Postgres \u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0451\u043c\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0430\u043c\u0438 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435. \u0412\u0435\u0440\u0441\u0438\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0442\u044c, \u0442\u0438\u043f\u044b \u043a\u043e\u043b\u043e\u043d\u043e\u043a \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b, \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c \u043c\u0435\u0436\u0434\u0443 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430\u043c\u0438, \u0433\u0434\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0431\u0430\u0439\u0442\u043e\u0432.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b CSV-\u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0441 \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u044f\u043c\u0438:<\/p>\n<pre><code class=\"sql\">-- \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0441 \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u043a\u0430\u0432\u044b\u0447\u043a\u0430\u043c\u0438 \u0434\u043b\u044f \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u043a\u043e\u043b\u043e\u043d\u043e\u043a COPY public.users (id, email, country) TO STDOUT WITH (FORMAT csv, HEADER true, FORCE_QUOTE (email, country));  -- \u0433\u0440\u0443\u0437\u0438\u043c, \u0442\u0440\u0435\u0431\u0443\u044f \u0441\u0442\u0440\u043e\u0433\u043e\u0433\u043e \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 COPY public.users (id, email, country) FROM STDIN WITH (FORMAT csv, HEADER match);<\/code><\/pre>\n<h3>FREEZE: \u043a\u043e\u0433\u0434\u0430 \u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u0442<\/h3>\n<p><code>COPY FROM ... WITH (FREEZE true)<\/code> \u0437\u0430\u043c\u043e\u0440\u0430\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441\u0440\u0430\u0437\u0443, \u043a\u0430\u043a \u043f\u043e\u0441\u043b\u0435 <code>VACUUM FREEZE<\/code>. \u042d\u0442\u043e \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u0443\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u043e\u0432\u043e\u0439 \u0438\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u043e\u0447\u0438\u0449\u0435\u043d\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442 \u0434\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u0430\u0432\u0442ovacuum. \u0423\u0441\u043b\u043e\u0432\u0438\u044f \u0442\u0430\u043c \u0441\u0442\u0440\u043e\u0433\u0438\u0435: \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0430 \u0438\u043b\u0438 \u043e\u043f\u0443\u0441\u0442\u043e\u0448\u0435\u043d\u0430 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u0431\u0435\u0437 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u043a\u0443\u0440\u0441\u043e\u0440\u043e\u0432 \u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u043d\u0438\u043c\u043a\u043e\u0432. \u041d\u0430 \u0441\u0435\u043a\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445 <code>FREEZE<\/code> \u0441\u0435\u0439\u0447\u0430\u0441 \u043d\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439:<\/p>\n<pre><code class=\"sql\">BEGIN; CREATE TABLE public.import_users (LIKE public.users INCLUDING ALL); COPY public.import_users FROM PROGRAM 'gzip -dc \/data\/users.csv.gz'   WITH (FORMAT csv, HEADER true, FREEZE true); ANALYZE public.import_users; COMMIT;<\/code><\/pre>\n<h3>WHERE \u043f\u0440\u044f\u043c\u043e \u0432 COPY<\/h3>\n<p>\u041d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e. <\/p>\n<pre><code class=\"sql\">COPY public.events (id, payload, created_at) FROM STDIN   WITH (FORMAT csv, HEADER true)   WHERE id IS NOT NULL AND created_at &gt;= DATE '2025-01-01';<\/code><\/pre>\n<p>\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0451\u043d\u043d\u044b\u0445 \u0441\u0442\u0440\u043e\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u0438\u0434\u043d\u043e \u0432 <code>pg_stat_progress_copy<\/code>.<\/p>\n<h3>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 <code>COPY ... FROM\/TO 'filename'<\/code> \u0438\u043b\u0438 <code>PROGRAM<\/code>, \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0438\u0432\u0438\u043b\u0435\u0433\u0438\u0438 \u0441\u0443\u043f\u0435\u0440\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0447\u043b\u0435\u043d\u0441\u0442\u0432\u043e \u0432 \u0440\u043e\u043b\u044f\u0445 <code>pg_read_server_files<\/code>, <code>pg_write_server_files<\/code>, <code>pg_execute_server_program<\/code>. <\/p>\n<p>\u042d\u0442\u043e \u043e\u0441\u043e\u0437\u043d\u0430\u043d\u043d\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435: \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0438 shell. \u0412 <code>PROGRAM<\/code> \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u0434\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0439 \u0432\u0432\u043e\u0434 \u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f. \u0414\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u0434\u0435\u0440\u0436\u0438\u0442\u0435 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0431\u0435\u043b\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u0438 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u044d\u043a\u0440\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/p>\n<p>\u0415\u0449\u0451 \u0434\u0432\u0430 \u043c\u043e\u043c\u0435\u043d\u0442\u0430. \u0414\u043b\u044f <code>COPY TO<\/code> \u043f\u0443\u0442\u044c \u043e\u0431\u044f\u0437\u0430\u043d \u0431\u044b\u0442\u044c \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u043c. \u0414\u043b\u044f <code>COPY FROM<\/code> \u044d\u0442\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u044f, \u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u043d\u0435 \u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0440\u0430\u0431\u043e\u0447\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0430. \u0418 \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e \u043f\u0440\u0430\u0432\u0430 \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u044b: \u0434\u043b\u044f <code>COPY TO<\/code> \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0430\u0432\u043e <code>SELECT<\/code> \u043d\u0430 \u043a\u043e\u043b\u043e\u043d\u043a\u0438, \u0434\u043b\u044f <code>COPY FROM<\/code> \u043f\u0440\u0430\u0432\u043e <code>INSERT<\/code>.<\/p>\n<h3>\u041a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438 \u0438 \u0444\u043e\u0440\u043c\u0430\u0442\u044b \u0434\u0430\u0442<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0444\u0430\u0439\u043b \u043d\u0435 \u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0437\u0430\u0434\u0430\u0435\u043c <code>ENCODING '...'<\/code> \u0432 <code>COPY<\/code>. \u0414\u043b\u044f \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c\u043e\u0439 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u044f \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u0441\u0442\u0430\u0432\u0438\u043c ISO-\u0444\u043e\u0440\u043c\u0430\u0442 \u0434\u0430\u0442:<\/p>\n<pre><code class=\"sql\">SET DateStyle TO ISO, YMD; COPY (SELECT * FROM public.events WHERE created_at &gt;= DATE '2025-01-01') TO STDOUT WITH (FORMAT csv, HEADER true, ENCODING 'UTF8');<\/code><\/pre>\n<p>\u041d\u0430 CSV \u0432\u0441\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0437\u043d\u0430\u0447\u0438\u043c\u044b, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043f\u0440\u043e\u0431\u0435\u043b\u044b. \u0415\u0441\u043b\u0438 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0434\u043e\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u0440\u043e\u0431\u0435\u043b\u0430\u043c\u0438 \u0434\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0448\u0438\u0440\u0438\u043d\u044b, \u0447\u0438\u0441\u0442\u0438\u043c \u0444\u0430\u0439\u043b \u0434\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438. \u0412 CSV \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u044b \u0441\u0442\u0440\u043e\u043a, \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u0432 \u043a\u0430\u0432\u044b\u0447\u043a\u0430\u0445.<\/p>\n<h3>COPY \u0438\u0437 \u043a\u043e\u0434\u0430: Python, Go, Rust<\/h3>\n<h4>Python, psycopg3<\/h4>\n<p>\u041d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>cursor.copy(...)<\/code>. COPY \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 SQL-\u0441\u0442\u0440\u043e\u043a\u0443 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u0430\u043c\u0438 \u0438\u0437 \u0431\u0435\u043b\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432.<\/p>\n<pre><code class=\"python\">import psycopg from psycopg.rows import dict_row  def copy_csv_to_table(dsn: str, table: str, csv_path: str):     if table not in {\"public_events\", \"public_users\"}:         raise ValueError(\"table not allowed\")     copy_sql = f\"COPY {table} FROM STDIN WITH (FORMAT csv, HEADER true)\"     with psycopg.connect(dsn, autocommit=False) as conn:         with conn.cursor(row_factory=dict_row) as cur, open(csv_path, \"rb\") as f:             with cur.copy(copy_sql) as cp:                 while chunk := f.read(256 * 1024):                     cp.write(chunk)         conn.commit()  def copy_table_to_csv(dsn: str, table: str, out_path: str):     if table not in {\"public_events\", \"public_users\"}:         raise ValueError(\"table not allowed\")     copy_sql = f\"COPY {table} TO STDOUT WITH (FORMAT csv, HEADER true)\"     with psycopg.connect(dsn, autocommit=True) as conn:         with conn.cursor() as cur, open(out_path, \"wb\") as f:             with cur.copy(copy_sql) as cp:                 for data in cp:                     f.write(data)  def copy_between(dsn_src: str, dsn_dst: str, src_query: str, dst_table: str):     # \u043f\u0435\u0440\u0435\u043d\u043e\u0441 Postgres \u2192 Postgres \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u043f\u0440\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445     sql_out = f\"COPY ({src_query}) TO STDOUT WITH (FORMAT binary)\"     sql_in  = f\"COPY {dst_table} FROM STDIN WITH (FORMAT binary)\"     with psycopg.connect(dsn_src) as s, psycopg.connect(dsn_dst) as d:         with s.cursor() as cs, d.cursor() as cd:             with cs.copy(sql_out) as cp_out, cd.copy(sql_in) as cp_in:                 for data in cp_out:                     cp_in.write(data)         d.commit()<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u043f\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438: \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 <code>COPY<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e whitelisting \u0442\u0430\u0431\u043b\u0438\u0446 \u0438 \u043a\u043e\u043b\u043e\u043d\u043e\u043a. \u041f\u0430\u043a\u0443\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u043c\u0438 \u0431\u043b\u043e\u043a\u0430\u043c\u0438, \u043d\u0430 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0445 \u043a\u0443\u0441\u043a\u0430\u0445 \u0431\u0443\u0434\u0435\u0442 \u043b\u0438\u0448\u043d\u044f\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u0430\u044f \u0432\u043e\u0437\u043d\u044f.<\/p>\n<h4>Go, pgx<\/h4>\n<p>\u0412 <code>pgx<\/code> \u0435\u0441\u0442\u044c <code>CopyFrom<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0441\u0442\u0440\u043e\u043a \u0438 \u0448\u043b\u0451\u0442 \u0438\u0445 \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 COPY. \u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0435, \u0447\u0435\u043c \u0441\u0430\u043c\u043e\u043c\u0443 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c CSV, \u0438 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c <code>INSERT<\/code>.<\/p>\n<pre><code class=\"go\">package bulk  import (     \"context\"     \"encoding\/csv\"     \"io\"     \"strconv\"      \"github.com\/jackc\/pgx\/v5\" )  func CopyRows(ctx context.Context, conn *pgx.Conn, table string, columns []string, rows [][]any) (int64, error) {     \/\/ columns \u0432\u0440\u043e\u0434\u0435 []string{\"id\", \"payload\", \"created_at\"}     return conn.CopyFrom(ctx, pgx.Identifier{table}, columns, pgx.CopyFromRows(rows)) }  func CopyCSV(ctx context.Context, conn *pgx.Conn, table string, columns []string, r io.Reader) (int64, error) {     dec := csv.NewReader(r)     \/\/ \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a     if _, err := dec.Read(); err != nil {         return 0, err     }     const batch = 5000     buf := make([][]any, 0, batch)     var total int64      flush := func() error {         if len(buf) == 0 {             return nil         }         n, err := conn.CopyFrom(ctx, pgx.Identifier{table}, columns, pgx.CopyFromRows(buf))         total += n         buf = buf[:0]         return err     }      for {         rec, err := dec.Read()         if err == io.EOF {             break         }         if err != nil {             return total, err         }         id, _ := strconv.ParseInt(rec[0], 10, 64)         row := []any{id, rec[1], rec[2]} \/\/ \u043f\u0440\u0438\u043c\u0435\u0440         buf = append(buf, row)         if len(buf) &gt;= batch {             if err := flush(); err != nil {                 return total, err             }         }     }     if err := flush(); err != nil {         return total, err     }     return total, nil }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c CSV \u0441\u0430\u043c\u0438. <code>pgx<\/code> \u0443\u043f\u0430\u043a\u0443\u0435\u0442 \u0432\u0441\u0451 \u0432 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b <code>COPY<\/code>.<\/p>\n<h4>Rust, tokio-postgres<\/h4>\n<p>\u0423 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0435\u0441\u0442\u044c <code>copy_out<\/code> \u0438 <code>copy_in<\/code>. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438, \u0431\u0435\u0437 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432.<\/p>\n<pre><code class=\"rust\">use tokio_postgres::{NoTls, Error}; use futures_util::{StreamExt, SinkExt}; use bytes::Bytes;  pub async fn copy_out_csv(conn_str: &amp;str, table: &amp;str) -&gt; Result&lt;Vec&lt;u8&gt;, Error&gt; {     let (client, connection) = tokio_postgres::connect(conn_str, NoTls).await?;     tokio::spawn(async move { let _ = connection.await; });      let sql = format!(\"COPY {} TO STDOUT WITH (FORMAT csv, HEADER true)\", table);     let mut stream = client.copy_out(&amp;*sql).await?;     let mut out = Vec::new();      while let Some(chunk) = stream.next().await {         out.extend_from_slice(&amp;chunk?);     }     Ok(out) }  pub async fn copy_in_csv(conn_str: &amp;str, table: &amp;str, data: &amp;[u8]) -&gt; Result&lt;(), Error&gt; {     let (client, connection) = tokio_postgres::connect(conn_str, NoTls).await?;     tokio::spawn(async move { let _ = connection.await; });      let sql = format!(\"COPY {} FROM STDIN WITH (FORMAT csv, HEADER true)\", table);     let mut sink = client.copy_in(&amp;*sql).await?;     sink.send(Bytes::from(data.to_vec())).await?;     sink.close().await?;     Ok(()) }<\/code><\/pre>\n<hr\/>\n<p><code>COPY<\/code> \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u043c\u0430\u0441\u0441\u043e\u0432\u044b\u0445 \u0437\u0430\u0433\u0440\u0443\u0437\u043e\u043a \u0438 \u0432\u044b\u0433\u0440\u0443\u0437\u043e\u043a \u0432 Postgres. \u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0441 \u043e\u0433\u043b\u044f\u0434\u043a\u043e\u0439 \u043d\u0430 \u043f\u0440\u0430\u0432\u0430, \u0444\u043e\u0440\u043c\u0430\u0442\u044b, \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438 \u0438 \u043f\u043b\u0430\u043d \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0435 \u0438 \u0431\u044b\u0441\u0442\u0440\u044b\u0435 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u044b. \u0412 \u0441\u043f\u043e\u0440\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445 \u043f\u0440\u043e\u0449\u0435 \u0432\u044b\u0434\u0435\u0440\u0436\u0430\u0442\u044c staging, \u0432 \u0433\u043e\u0440\u044f\u0447\u0438\u0445 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0431\u0435\u0437 \u0434\u0438\u0441\u043a\u0430, \u0432 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0430\u0445 \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u043f\u0440\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445. <\/p>\n<blockquote>\n<p>\u041f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043f\u0440\u043e\u0439\u0442\u0438 <a href=\"https:\/\/otus.pw\/fbXp\/\" rel=\"noopener noreferrer nofollow\"><strong>\u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/a> \u043f\u043e\u00a0\u0442\u0435\u043c\u0435 \u00ab\u0411\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445\u00bb. \u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0437\u043d\u0430\u043d\u0438\u0439 \u0438 \u043d\u0430\u0432\u044b\u043a\u043e\u0432, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u0434\u043b\u044f\u00a0\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0443\u0441\u0432\u043e\u0435\u043d\u0438\u044f \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430 \u043a\u0443\u0440\u0441\u0430. <\/p>\n<p><a href=\"https:\/\/otus.pw\/gdcT\/\" rel=\"noopener noreferrer nofollow\"><strong>\u041a\u0443\u0440\u0441 \u00ab\u0411\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445\u00bb<\/strong><\/a> \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u043a\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u00a0\u0441\u0438\u0441\u0442\u0435\u043c\u0430\u043c\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0438 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c\u0438 \u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438, \u0430\u00a0\u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 SQL. \u041f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 <a href=\"https:\/\/otus.pw\/gdcT\/\" rel=\"noopener noreferrer nofollow\"><strong>\u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c\u0441\u044f<\/strong><\/a> \u043a\u00a0\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0435 \u0437\u043d\u0430\u043d\u0438\u044f \u0438 \u043d\u0430\u0432\u044b\u043a\u0438 \u0432\u00a0\u044d\u0442\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438. <\/p>\n<\/blockquote>\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\/935454\/\"> https:\/\/habr.com\/ru\/articles\/935454\/<\/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>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440. <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c COPY \u0432 PostgreSQL. \u042d\u0442\u043e \u0440\u0430\u0431\u043e\u0447\u0430\u044f \u043b\u043e\u0448\u0430\u0434\u043a\u0430 \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u043e\u0432\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0438 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<h3>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 COPY \u0438 \u0447\u0435\u043c \u043e\u043d \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 INSERT<\/h3>\n<p><code>COPY<\/code> \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u0435\u0436\u0434\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 \u0438 \u0444\u0430\u0439\u043b\u043e\u043c \u0438\u043b\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 STDIN\/STDOUT. \u0412\u0430\u0440\u0438\u0430\u043d\u0442 <code>COPY FROM<\/code> \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442, <code>COPY TO<\/code> \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0435\u0442. \u0423\u043c\u0435\u0435\u0442 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u044b <code>text<\/code>, <code>csv<\/code>, <code>binary<\/code>.<\/p>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b <code>ON_ERROR<\/code>, <code>FREEZE<\/code>, <code>HEADER<\/code> \u0438 <code>HEADER MATCH<\/code>, <code>FORCE_*<\/code>, <code>ENCODING<\/code>, <code>WHERE<\/code>, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u043f\u0443\u0441\u043a \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c \u0447\u0435\u0440\u0435\u0437 <code>PROGRAM<\/code>. \u042d\u0442\u043e \u0440\u0430\u0437\u0430 \u0432 \u0434\u0432\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043b\u044e\u0431\u043e\u0433\u043e \u0431\u0430\u0442\u0447\u0435\u0432\u043e\u0433\u043e <code>INSERT<\/code> \u043f\u0440\u0438 \u0440\u0430\u0432\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u0438 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u043f\u0440\u043e\u0449\u0435 \u0432 \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f:<\/p>\n<pre><code class=\"sql\">-- \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 CSV \u0432 \u043f\u043e\u0442\u043e\u043a COPY public.events TO STDOUT WITH (FORMAT csv, HEADER true);  -- \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437 CSV \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u0430 COPY public.events FROM STDIN WITH (FORMAT csv, HEADER true);  -- \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 (\u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0430\u0432\u0430) COPY public.events FROM '\/var\/lib\/postgresql\/import.csv' WITH (FORMAT csv, HEADER true);  -- \u0447\u0435\u0440\u0435\u0437 \u0432\u043d\u0435\u0448\u043d\u044e\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 (\u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u043a\u0430 \u043d\u0430 \u043b\u0435\u0442\u0443) COPY public.events FROM PROGRAM 'gzip -dc \/data\/import.csv.gz' WITH (FORMAT csv, HEADER true);  -- \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043d\u0435 \u0432\u0441\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b COPY (   SELECT id, payload, created_at   FROM public.events   WHERE created_at &gt;= DATE '2025-01-01' ) TO STDOUT WITH (FORMAT csv, HEADER true);<\/code><\/pre>\n<h3>COPY \u043f\u0440\u043e\u0442\u0438\u0432 \\copy \u0432 psql<\/h3>\n<p>\u0412\u0430\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0442\u044c \u0434\u0432\u0430 \u043c\u0438\u0440\u0430. <code>COPY ... FROM\/TO 'filename'<\/code> \u0438 <code>COPY ... PROGRAM<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u0440\u043e\u043b\u0435\u0439. \u0424\u0430\u0439\u043b\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0438\u043c\u0435\u043d\u043d\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0443. \u041c\u0435\u0442\u0430\u043a\u043e\u043c\u0430\u043d\u0434\u0430 <code>\\copy<\/code> \u0438\u0437 <code>psql<\/code> \u044d\u0442\u043e \u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u0432\u043e\u043a\u0440\u0443\u0433 <code>COPY ... FROM STDIN\/TO STDOUT<\/code>, \u0438 \u0444\u0430\u0439\u043b\u044b \u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f\/\u043f\u0438\u0448\u0443\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0430. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0445 \u043f\u0440\u0430\u0432 \u0438\u043b\u0438 \u0444\u0430\u0439\u043b \u043b\u0435\u0436\u0438\u0442 \u0443 \u0432\u0430\u0441 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u0436\u0435 <code>\\copy<\/code>. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0438 \u0444\u0430\u0439\u043b\u044b \u0443\u0436\u0435 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435,\u0442\u043e \u0442\u0443\u0442 \u0443\u0436\u0435 \u043d\u0443\u0436\u0435\u043d \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 <code>COPY<\/code>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f <code>psql<\/code>:<\/p>\n<pre><code class=\"sql\">-- \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0432 \u0441\u0436\u0430\u0442\u044b\u0439 \u0444\u0430\u0439\u043b \\copy (SELECT * FROM public.events) TO PROGRAM 'gzip &gt; \/tmp\/events.csv.gz' WITH (FORMAT csv, HEADER true)  -- \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \\copy public.events FROM '\/home\/user\/import.csv' WITH (FORMAT csv, HEADER true)<\/code><\/pre>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435<\/h3>\n<p>\u041f\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u0443 <code>COPY FROM<\/code> \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435. \u0412 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u0445 \u00ab\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0432\u0441\u0451, \u0447\u0442\u043e \u0432\u0430\u043b\u0438\u0434\u043d\u043e, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0432 \u043a\u0430\u0440\u0430\u043d\u0442\u0438\u043d\u00bb \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 <code>ON_ERROR ignore<\/code>:<\/p>\n<pre><code class=\"sql\">COPY public.events FROM STDIN   WITH (     FORMAT csv,     HEADER true,     ON_ERROR ignore,     LOG_VERBOSITY verbose   );<\/code><\/pre>\n<p>\u041f\u0440\u0438 <code>ignore<\/code> \u0432\u0441\u0435 \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u044b, \u0432 \u043a\u043e\u043d\u0446\u0435 \u043f\u0440\u0438\u0434\u0451\u0442 <code>NOTICE<\/code> \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043e\u0432. \u0421 <code>LOG_VERBOSITY verbose<\/code> \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0438\u0448\u0435\u0442, \u043a\u0430\u043a\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043a\u0430\u043a\u0430\u044f \u043a\u043e\u043b\u043e\u043d\u043a\u0430 \u0443\u043f\u0430\u043b\u0438 \u043d\u0430 \u043a\u043e\u043d\u0432\u0435\u0440\u0441\u0438\u0438. \u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e, \u043d\u043e \u043d\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u044d\u0442\u043e \u0432 \u043d\u043e\u0440\u043c\u0443 \u043d\u0430 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0439 \u043e\u0441\u043d\u043e\u0432\u0435: \u0434\u043b\u044f \u0447\u0438\u0441\u0442\u044b\u0445 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u043e\u0432 \u043b\u0443\u0447\u0448\u0435 staging-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u043c\u0438 \u043a\u043e\u043b\u043e\u043d\u043a\u0430\u043c\u0438, \u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0442\u0443\u0434\u0430, \u0430 \u0437\u0430\u0442\u0435\u043c \u044f\u0432\u043d\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432 \u0432\u043d\u043e\u0441\u0438\u0442\u044c \u0432 \u0446\u0435\u043b\u0435\u0432\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 staging-\u043f\u043e\u0442\u043e\u043a\u0430:<\/p>\n<pre><code class=\"sql\">-- \u0441\u044b\u0440\u043e\u0439 \u0441\u043b\u043e\u0439 CREATE UNLOGGED TABLE staging.events_raw (   id_text text,   payload_text text,   created_at_text text );  -- \u0431\u0435\u0437 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e COPY staging.events_raw FROM STDIN WITH (FORMAT csv, HEADER true);  -- \u0447\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u043b\u043e\u0439 INSERT INTO public.events (id, payload, created_at) SELECT   id_text::bigint,   payload_text::jsonb,   created_at_text::timestamptz FROM staging.events_raw WHERE id_text ~ '^\\d+$'   AND created_at_text IS NOT NULL ON CONFLICT (id) DO UPDATE SET payload = EXCLUDED.payload,     created_at = EXCLUDED.created_at;<\/code><\/pre>\n<h3>\u0413\u0434\u0435 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441<\/h3>\n<p>\u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b <code>COPY<\/code> \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432 <code>pg_stat_progress_copy<\/code>. <\/p>\n<pre><code class=\"sql\">SELECT pid, datname, relid::regclass AS relation, command, type, bytes_processed, bytes_total,        tuples_processed, tuples_excluded, error_count FROM pg_stat_progress_copy;<\/code><\/pre>\n<p><code>tuples_excluded<\/code> \u0440\u0430\u0441\u0442\u0451\u0442, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>WHERE<\/code> \u0438 \u0441\u0442\u0440\u043e\u043a\u0438 \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f. <code>error_count<\/code> \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u043f\u0440\u0438 <code>ON_ERROR ignore<\/code>.<\/p>\n<h3>\u0424\u043e\u0440\u043c\u0430\u0442\u044b: \u043a\u043e\u0433\u0434\u0430 \u043a\u0430\u043a\u043e\u0439<\/h3>\n<p>\u0422\u0440\u0438 \u0440\u0435\u0436\u0438\u043c\u0430 \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u044e\u0442 \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435 \u0441\u043b\u0443\u0447\u0430\u0438.<\/p>\n<p>\u0422\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u0434\u0430\u0451\u0442 \u0442\u0430\u0431\u0443\u043b\u044f\u0446\u0438\u044e \u043a\u0430\u043a \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044c \u0438 <code>\\N<\/code> \u043a\u0430\u043a NULL. \u041e\u043d \u0435\u0441\u0442\u044c, \u043d\u043e \u0440\u0435\u0434\u043a\u043e \u043d\u0443\u0436\u0435\u043d: CSV \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u0435\u0435, \u0430 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u0431\u044b\u0441\u0442\u0440\u0435\u0435.<\/p>\n<p>CSV \u2014 \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0439. \u041f\u0430\u0440\u0430 \u043d\u044e\u0430\u043d\u0441\u043e\u0432:<\/p>\n<ul>\n<li>\n<p><code>HEADER true<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043d\u0430 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0435 \u0438 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043f\u0435\u0440\u0432\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u043d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435. \u0412\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u0438\u043b\u0438\u0442\u044c <code>HEADER MATCH<\/code>, \u0442\u043e\u0433\u0434\u0430 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043e\u0431\u044f\u0437\u0430\u043d \u0441\u043e\u0432\u043f\u0430\u0441\u0442\u044c \u0441 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 \u043a\u043e\u043b\u043e\u043d\u043e\u043a \u0438 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0443\u0441\u0442\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 NULL \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f. \u041f\u0443\u0441\u0442\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u044d\u0442\u043e <code>\"\"<\/code>, NULL \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u043e \u0431\u0435\u0437 \u043a\u0430\u0432\u044b\u0447\u0435\u043a, \u043b\u0438\u0431\u043e \u0441\u0442\u0440\u043e\u043a\u0430 \u0438\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 <code>NULL '...'<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u044f\u0432\u043d\u043e\u0433\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u0430\u0432\u044b\u0447\u043a\u0430\u043c\u0438 \u0438 \u044d\u043a\u0440\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>QUOTE<\/code>, <code>ESCAPE<\/code>, \u0430 \u0434\u043b\u044f \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043a\u043e\u043b\u043e\u043d\u043e\u043a <code>FORCE_QUOTE (col1, col2)<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u0432\u044b\u0433\u043e\u0434\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u0430 Postgres \u2192 Postgres \u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0451\u043c\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0430\u043c\u0438 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435. \u0412\u0435\u0440\u0441\u0438\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0442\u044c, \u0442\u0438\u043f\u044b \u043a\u043e\u043b\u043e\u043d\u043e\u043a \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b, \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c \u043c\u0435\u0436\u0434\u0443 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430\u043c\u0438, \u0433\u0434\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0431\u0430\u0439\u0442\u043e\u0432.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b CSV-\u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0441 \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u044f\u043c\u0438:<\/p>\n<pre><code class=\"sql\">-- \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0441 \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u043a\u0430\u0432\u044b\u0447\u043a\u0430\u043c\u0438 \u0434\u043b\u044f \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u043a\u043e\u043b\u043e\u043d\u043e\u043a COPY public.users (id, email, country) TO STDOUT WITH (FORMAT csv, HEADER true, FORCE_QUOTE (email, country));  -- \u0433\u0440\u0443\u0437\u0438\u043c, \u0442\u0440\u0435\u0431\u0443\u044f \u0441\u0442\u0440\u043e\u0433\u043e\u0433\u043e \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 COPY public.users (id, email, country) FROM STDIN WITH (FORMAT csv, HEADER match);<\/code><\/pre>\n<h3>FREEZE: \u043a\u043e\u0433\u0434\u0430 \u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u0442<\/h3>\n<p><code>COPY FROM ... WITH (FREEZE true)<\/code> \u0437\u0430\u043c\u043e\u0440\u0430\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441\u0440\u0430\u0437\u0443, \u043a\u0430\u043a \u043f\u043e\u0441\u043b\u0435 <code>VACUUM FREEZE<\/code>. \u042d\u0442\u043e \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u0443\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u043e\u0432\u043e\u0439 \u0438\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u043e\u0447\u0438\u0449\u0435\u043d\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442 \u0434\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u0430\u0432\u0442ovacuum. \u0423\u0441\u043b\u043e\u0432\u0438\u044f \u0442\u0430\u043c \u0441\u0442\u0440\u043e\u0433\u0438\u0435: \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0430 \u0438\u043b\u0438 \u043e\u043f\u0443\u0441\u0442\u043e\u0448\u0435\u043d\u0430 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u0431\u0435\u0437 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u043a\u0443\u0440\u0441\u043e\u0440\u043e\u0432 \u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u043d\u0438\u043c\u043a\u043e\u0432. \u041d\u0430 \u0441\u0435\u043a\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445 <code>FREEZE<\/code> \u0441\u0435\u0439\u0447\u0430\u0441 \u043d\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439:<\/p>\n<pre><code class=\"sql\">BEGIN; CREATE TABLE public.import_users (LIKE public.users INCLUDING ALL); COPY public.import_users FROM PROGRAM 'gzip -dc \/data\/users.csv.gz'   WITH (FORMAT csv, HEADER true, FREEZE true); ANALYZE public.import_users; COMMIT;<\/code><\/pre>\n<h3>WHERE \u043f\u0440\u044f\u043c\u043e \u0432 COPY<\/h3>\n<p>\u041d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e. <\/p>\n<pre><code class=\"sql\">COPY public.events (id, payload, created_at) FROM STDIN   WITH (FORMAT csv, HEADER true)   WHERE id IS NOT NULL AND created_at &gt;= DATE '2025-01-01';<\/code><\/pre>\n<p>\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0451\u043d\u043d\u044b\u0445 \u0441\u0442\u0440\u043e\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u0438\u0434\u043d\u043e \u0432 <code>pg_stat_progress_copy<\/code>.<\/p>\n<h3>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 <code>COPY ... FROM\/TO 'filename'<\/code> \u0438\u043b\u0438 <code>PROGRAM<\/code>, \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0438\u0432\u0438\u043b\u0435\u0433\u0438\u0438 \u0441\u0443\u043f\u0435\u0440\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0447\u043b\u0435\u043d\u0441\u0442\u0432\u043e \u0432 \u0440\u043e\u043b\u044f\u0445 <code>pg_read_server_files<\/code>, <code>pg_write_server_files<\/code>, <code>pg_execute_server_program<\/code>. <\/p>\n<p>\u042d\u0442\u043e \u043e\u0441\u043e\u0437\u043d\u0430\u043d\u043d\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435: \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0438 shell. \u0412 <code>PROGRAM<\/code> \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u0434\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0439 \u0432\u0432\u043e\u0434 \u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f. \u0414\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u0434\u0435\u0440\u0436\u0438\u0442\u0435 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0431\u0435\u043b\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u0438 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u044d\u043a\u0440\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/p>\n<p>\u0415\u0449\u0451 \u0434\u0432\u0430 \u043c\u043e\u043c\u0435\u043d\u0442\u0430. \u0414\u043b\u044f <code>COPY TO<\/code> \u043f\u0443\u0442\u044c \u043e\u0431\u044f\u0437\u0430\u043d \u0431\u044b\u0442\u044c \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u043c. \u0414\u043b\u044f <code>COPY FROM<\/code> \u044d\u0442\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u044f, \u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u043d\u0435 \u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0440\u0430\u0431\u043e\u0447\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0430. \u0418 \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e \u043f\u0440\u0430\u0432\u0430 \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u044b: \u0434\u043b\u044f <code>COPY TO<\/code> \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0430\u0432\u043e <code>SELECT<\/code> \u043d\u0430 \u043a\u043e\u043b\u043e\u043d\u043a\u0438, \u0434\u043b\u044f <code>COPY FROM<\/code> \u043f\u0440\u0430\u0432\u043e <code>INSERT<\/code>.<\/p>\n<h3>\u041a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438 \u0438 \u0444\u043e\u0440\u043c\u0430\u0442\u044b \u0434\u0430\u0442<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0444\u0430\u0439\u043b \u043d\u0435 \u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0437\u0430\u0434\u0430\u0435\u043c <code>ENCODING '...'<\/code> \u0432 <code>COPY<\/code>. \u0414\u043b\u044f \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c\u043e\u0439 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u044f \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u0441\u0442\u0430\u0432\u0438\u043c ISO-\u0444\u043e\u0440\u043c\u0430\u0442 \u0434\u0430\u0442:<\/p>\n<pre><code class=\"sql\">SET DateStyle TO ISO, YMD; COPY (SELECT * FROM public.events WHERE created_at &gt;= DATE '2025-01-01') TO STDOUT WITH (FORMAT csv, HEADER true, ENCODING 'UTF8');<\/code><\/pre>\n<p>\u041d\u0430 CSV \u0432\u0441\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0437\u043d\u0430\u0447\u0438\u043c\u044b, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043f\u0440\u043e\u0431\u0435\u043b\u044b. \u0415\u0441\u043b\u0438 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0434\u043e\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u0440\u043e\u0431\u0435\u043b\u0430\u043c\u0438 \u0434\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0448\u0438\u0440\u0438\u043d\u044b, \u0447\u0438\u0441\u0442\u0438\u043c \u0444\u0430\u0439\u043b \u0434\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438. \u0412 CSV \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u044b \u0441\u0442\u0440\u043e\u043a, \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u0432 \u043a\u0430\u0432\u044b\u0447\u043a\u0430\u0445.<\/p>\n<h3>COPY \u0438\u0437 \u043a\u043e\u0434\u0430: Python, Go, Rust<\/h3>\n<h4>Python, psycopg3<\/h4>\n<p>\u041d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>cursor.copy(...)<\/code>. COPY \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 SQL-\u0441\u0442\u0440\u043e\u043a\u0443 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u0430\u043c\u0438 \u0438\u0437 \u0431\u0435\u043b\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432.<\/p>\n<pre><code class=\"python\">import psycopg from psycopg.rows import dict_row  def copy_csv_to_table(dsn: str, table: str, csv_path: str):     if table not in {\"public_events\", \"public_users\"}:         raise ValueError(\"table not allowed\")     copy_sql = f\"COPY {table} FROM STDIN WITH (FORMAT csv, HEADER true)\"     with psycopg.connect(dsn, autocommit=False) as conn:         with conn.cursor(row_factory=dict_row) as cur, open(csv_path, \"rb\") as f:             with cur.copy(copy_sql) as cp:                 while chunk := f.read(256 * 1024):                     cp.write(chunk)         conn.commit()  def copy_table_to_csv(dsn: str, table: str, out_path: str):     if table not in {\"public_events\", \"public_users\"}:         raise ValueError(\"table not allowed\")     copy_sql = f\"COPY {table} TO STDOUT WITH (FORMAT csv, HEADER true)\"     with psycopg.connect(dsn, autocommit=True) as conn:         with conn.cursor() as cur, open(out_path, \"wb\") as f:             with cur.copy(copy_sql) as cp:                 for data in cp:                     f.write(data)  def copy_between(dsn_src: str, dsn_dst: str, src_query: str, dst_table: str):     # \u043f\u0435\u0440\u0435\u043d\u043e\u0441 Postgres \u2192 Postgres \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u043f\u0440\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445     sql_out = f\"COPY ({src_query}) TO STDOUT WITH (FORMAT binary)\"     sql_in  = f\"COPY {dst_table} FROM STDIN WITH (FORMAT binary)\"     with psycopg.connect(dsn_src) as s, psycopg.connect(dsn_dst) as d:         with s.cursor() as cs, d.cursor() as cd:             with cs.copy(sql_out) as cp_out, cd.copy(sql_in) as cp_in:                 for data in cp_out:                     cp_in.write(data)         d.commit()<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u043f\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438: \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 <code>COPY<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e whitelisting \u0442\u0430\u0431\u043b\u0438\u0446 \u0438 \u043a\u043e\u043b\u043e\u043d\u043e\u043a. \u041f\u0430\u043a\u0443\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u043c\u0438 \u0431\u043b\u043e\u043a\u0430\u043c\u0438, \u043d\u0430 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0445 \u043a\u0443\u0441\u043a\u0430\u0445 \u0431\u0443\u0434\u0435\u0442 \u043b\u0438\u0448\u043d\u044f\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u0430\u044f \u0432\u043e\u0437\u043d\u044f.<\/p>\n<h4>Go, pgx<\/h4>\n<p>\u0412 <code>pgx<\/code> \u0435\u0441\u0442\u044c <code>CopyFrom<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0441\u0442\u0440\u043e\u043a \u0438 \u0448\u043b\u0451\u0442 \u0438\u0445 \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 COPY. \u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0435, \u0447\u0435\u043c \u0441\u0430\u043c\u043e\u043c\u0443 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c CSV, \u0438 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c <code>INSERT<\/code>.<\/p>\n<pre><code class=\"go\">package bulk  import (     \"context\"     \"encoding\/csv\"     \"io\"     \"strconv\"      \"github.com\/jackc\/pgx\/v5\" )  func CopyRows(ctx context.Context, conn *pgx.Conn, table string, columns []string, rows [][]any) (int64, error) {     \/\/ columns \u0432\u0440\u043e\u0434\u0435 []string{\"id\", \"payload\", \"created_at\"}     return conn.CopyFrom(ctx, pgx.Identifier{table}, columns, pgx.CopyFromRows(rows)) }  func CopyCSV(ctx context.Context, conn *pgx.Conn, table string, columns []string, r io.Reader) (int64, error) {     dec := csv.NewReader(r)     \/\/ \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a     if _, err := dec.Read(); err != nil {         return 0, err     }     const <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-470296","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/470296","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=470296"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/470296\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=470296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=470296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=470296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}