{"id":363755,"date":"2024-05-21T02:02:48","date_gmt":"2024-05-21T02:02:48","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=363755"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=363755","title":{"rendered":"<span>PostgreSQL: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0435\u0441\u0442\u043e \u043f\u043e\u0441\u043b\u0435 delete<\/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<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/87b\/7c1\/a38\/87b7c1a38fabec978377a375ad529ddb.png\" width=\"1920\" height=\"1080\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/87b\/7c1\/a38\/87b7c1a38fabec978377a375ad529ddb.png\"\/><\/figure>\n<h2>\u0421\u0443\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b<\/h2>\n<p>\u0423 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043b\u0438\u0431\u043e \u0440\u044f\u0434 \u0442\u0430\u0431\u043b\u0438\u0446, \u0441\u0442\u0440\u043e\u043a\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0443\u0436\u043d\u043e \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0438 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c &#8212; \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u00a0<code>DELETE<\/code>.<\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u0432 \u0431\u041e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043d\u043e\u0432\u0438\u0447\u043a\u0430\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u0431\u0434 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u0438 \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u043e\u043f\u044b\u0442\u0430 \u043f\u0440\u043e\u043c\u044b\u0448\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449.<\/p>\n<p>\u0426\u0435\u043b\u0435\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f &#171;\u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0445&#187; \u0437\u0430\u043f\u0438\u0441\u0435\u0439 &#8212; <code>pg_repack<\/code>.<\/p>\n<pre><code class=\"sql\">delete from schema.table where condition;<\/code><\/pre>\n<p>\u041f\u043e\u043c\u0438\u043c\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0439 \u0446\u0435\u043b\u0438 &#8212; \u043e\u0447\u0438\u0441\u0442\u043a\u0438 \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0442\u0430\u043a\u0436\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0434\u0438\u0441\u043a\u0430, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0433\u043e \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u044b\u0445 postgresql. \u041d\u043e \u043f\u0440\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 &#8212; \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f <code>DELETE<\/code> \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0435\u0441\u0442\u043e, \u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f <code>UPDATE<\/code>  \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0435\u0433\u043e \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442.<\/p>\n<p>\u0427\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0440\u0435\u0448\u0430\u044e\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u044b\u00a0<code>vacuum<\/code>\u00a0\u0438\u00a0<code>vacuum full<\/code>, \u043d\u043e \u0443 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0432\u0443\u0445 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0435\u0441\u0442\u044c \u0432\u00a0<a href=\"https:\/\/www.postgresql.org\/docs\/10\/routine-vacuuming.html#vacuum-for-space-recovery\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u043e &#8212;\u00a0<code>vacuum<\/code>\u00a0\u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442 \u043c\u0435\u0441\u0442\u043e \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e \u0443\u0434\u0430\u043b\u044f\u043b\u0438\u0441\u044c \u0441\u0432\u0435\u0436\u0438\u0435 (\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435) \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 &#171;\u0432\u0435\u0440\u0445\u043d\u044e\u044e&#187; \u0447\u0430\u0441\u0442\u044c \u0434\u0430\u0442\u0430-\u0444\u0430\u0439\u043b\u0430.<br \/><code>vacuum full<\/code>\u00a0&#8212; \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442 \u043c\u0435\u0441\u0442\u043e, \u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u043c\u0443 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0436\u0435 \u043c\u0435\u0441\u0442\u0430 \u043d\u0430 \u0434\u0438\u0441\u043a\u0435, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0436\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u044d\u0442\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u043f\u0435\u0440\u0435\u043b\u0438\u0432\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443.<\/p>\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u043c\u043d\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430 \u043d\u0430 \u0434\u0438\u0441\u043a\u0435 &#8212; \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0439\u0442\u0435\u00a0<code>vacuum full schema.table<\/code>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435\u0442 &#8212; \u0442\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0431\u0443\u0434\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0443\u0442\u0438\u043b\u0438\u0442\u044b\u00a0<code>pgcompacttable<\/code>, \u0441\u0441\u044b\u043b\u043a\u0430\u00a0<a href=\"https:\/\/github.com\/dataegret\/pgcompacttable\" rel=\"noopener noreferrer nofollow\">github<\/a>.<\/p>\n<p>\u0421\u0442\u0430\u0432\u0438\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<ul>\n<li>\n<p>Debian-based Linux OS <code>apt-get install libdbi-perl libdbd-pg-perl<\/code><\/p>\n<\/li>\n<li>\n<p>RedHat\/Centos <code>yum install perl-Time-HiRes perl-DBI perl-DBD-Pg<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0440\u0435\u0436\u0438\u043c\u00a0<code>psql<\/code>:<\/p>\n<pre><code class=\"bash\">sudo -u postgres psql<\/code><\/pre>\n<pre><code class=\"sql\">\\c your_database create extension if not exists pgstattuple;  grant execute on function pgstattuple(text) to &lt;db user>; grant execute on function pgstattuple(regclass) to &lt;db user>; grant execute on function pgstattuple_approx to &lt;db user>;  -- \u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u0431\u044b\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u043e\u0442 \u0430\u0434\u043c\u0438\u043d\u0441\u043a\u043e\u0439 \u0443\u0437 postgres alter user postgres with password 'your new admin password'; \\q<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0435\u0431\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u00a0<code>perl<\/code>\u00a0\u0441\u043a\u0440\u0438\u043f\u0442.<\/p>\n<pre><code class=\"bash\">git clone https:\/\/github.com\/dataegret\/pgcompacttable.git<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438, \u043f\u043e\u043b\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0447\u0435\u0440\u0435\u0437:<\/p>\n<pre><code class=\"bash\">perl pgcompacttable --man<\/code><\/pre>\n<pre><code class=\"bash\">perl pgcompacttable \\ --dbname &lt;database_name> \\ -n &lt;schema_name> \\ -h localhost \\ -p 5432 \\ -U &lt;user> \\ -W &lt;password> \\ -t &lt;table_name> \\ -f<\/code><\/pre>\n<h3>\u041a\u0430\u043a\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043d\u044b\u0435?<\/h3>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c SQL \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445 &#8212; \u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 5 \u0441\u0442\u043e\u043b\u0431\u0446\u0435 (<code>wastedbytes<\/code>)<\/p>\n<pre><code class=\"sql\">SELECT   current_database(), schemaname, tablename, \/*reltuples::bigint, relpages::bigint, otta,*\/   ROUND((CASE WHEN otta=0 THEN 0.0 ELSE sml.relpages::float\/otta END)::numeric,1) AS tbloat,   pg_size_pretty(CASE WHEN relpages &lt; otta THEN 0 ELSE bs*(sml.relpages-otta)::BIGINT end) AS wastedbytes,   iname, \/*ituples::bigint, ipages::bigint, iotta,*\/   ROUND((CASE WHEN iotta=0 OR ipages=0 THEN 0.0 ELSE ipages::float\/iotta END)::numeric,1) AS ibloat,   CASE WHEN ipages &lt; iotta THEN 0 ELSE bs*(ipages-iotta) END AS wastedibytes FROM (   SELECT     schemaname, tablename, cc.reltuples, cc.relpages, bs,     CEIL((cc.reltuples*((datahdr+ma-       (CASE WHEN datahdr%ma=0 THEN ma ELSE datahdr%ma END))+nullhdr2+4))\/(bs-20::float)) AS otta,     COALESCE(c2.relname,'?') AS iname, COALESCE(c2.reltuples,0) AS ituples, COALESCE(c2.relpages,0) AS ipages,     COALESCE(CEIL((c2.reltuples*(datahdr-12))\/(bs-20::float)),0) AS iotta \/* very rough approximation, assumes all cols *\/   FROM (     SELECT       ma,bs,schemaname,tablename,       (datawidth+(hdr+ma-(case when hdr%ma=0 THEN ma ELSE hdr%ma END)))::numeric AS datahdr,       (maxfracsum*(nullhdr+ma-(case when nullhdr%ma=0 THEN ma ELSE nullhdr%ma END))) AS nullhdr2     FROM (       SELECT         schemaname, tablename, hdr, ma, bs,         SUM((1-null_frac)*avg_width) AS datawidth,         MAX(null_frac) AS maxfracsum,         hdr+(           SELECT 1+count(*)\/8           FROM pg_stats s2           WHERE null_frac&lt;>0 AND s2.schemaname = s.schemaname AND s2.tablename = s.tablename         ) AS nullhdr       FROM pg_stats s, (         SELECT           (SELECT current_setting('block_size')::numeric) AS bs,           CASE WHEN substring(v,12,3) IN ('8.0','8.1','8.2') THEN 27 ELSE 23 END AS hdr,           CASE WHEN v ~ 'mingw32' THEN 8 ELSE 4 END AS ma         FROM (SELECT version() AS v) AS foo       ) AS constants       GROUP BY 1,2,3,4,5     ) AS foo   ) AS rs   JOIN pg_class cc ON cc.relname = rs.tablename   JOIN pg_namespace nn ON cc.relnamespace = nn.oid AND nn.nspname = rs.schemaname AND nn.nspname &lt;> 'information_schema'   LEFT JOIN pg_index i ON indrelid = cc.oid   LEFT JOIN pg_class c2 ON c2.oid = i.indexrelid ) AS sml --where iname like '%ccnew%' ORDER BY (CASE WHEN relpages &lt; otta THEN 0 ELSE bs*(sml.relpages-otta)::BIGINT end) DESC;<\/code><\/pre>\n<p>\u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c &#171;\u0441\u0436\u0430\u0442\u0438\u0435&#187; \u0442\u0430\u0431\u043b\u0438\u0446\u044b &#8212; \u044d\u0442\u043e \u043f\u0430\u0434\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043a\u043e\u0434\u0430. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432 \u0431\u0434 \u043c\u043e\u0433\u0443\u0442 \u043e\u0441\u0442\u0430\u0442\u044c\u0441\u044f &#171;\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435&#187; \u0438\u043d\u0434\u0435\u043a\u0441\u044b <code>%ccnew%<\/code>, \u0438\u0445 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c <code>drop index index_name<\/code>.<\/p>\n<p>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 <a href=\"https:\/\/www.youtube.com\/watch?v=i1HYbATmWaw\" rel=\"noopener noreferrer nofollow\">\u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043d\u0430 youtube<\/a> \u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 PostgreSQL<\/p>\n<hr\/>\n<p>\u0411\u043e\u043b\u044c\u0448\u043e\u0435 \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0432\u0441\u0435\u043c \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435! \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f &#8212; \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u043d\u0430 \u043c\u043e\u0439 \u043a\u0430\u043d\u0430\u043b\u00a0<a href=\"https:\/\/t.me\/+mfY_q3ZYoEZlNzli\" rel=\"noopener noreferrer nofollow\">artydev &amp; Co<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/782560\/\"> https:\/\/habr.com\/ru\/articles\/782560\/<\/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<figure class=\"full-width\"><\/figure>\n<h2>\u0421\u0443\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b<\/h2>\n<p>\u0423 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043b\u0438\u0431\u043e \u0440\u044f\u0434 \u0442\u0430\u0431\u043b\u0438\u0446, \u0441\u0442\u0440\u043e\u043a\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0443\u0436\u043d\u043e \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0438 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c &#8212; \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u00a0<code>DELETE<\/code>.<\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u0432 \u0431\u041e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043d\u043e\u0432\u0438\u0447\u043a\u0430\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u0431\u0434 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u0438 \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u043e\u043f\u044b\u0442\u0430 \u043f\u0440\u043e\u043c\u044b\u0448\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449.<\/p>\n<p>\u0426\u0435\u043b\u0435\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f &#171;\u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0445&#187; \u0437\u0430\u043f\u0438\u0441\u0435\u0439 &#8212; <code>pg_repack<\/code>.<\/p>\n<pre><code class=\"sql\">delete from schema.table where condition;<\/code><\/pre>\n<p>\u041f\u043e\u043c\u0438\u043c\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0439 \u0446\u0435\u043b\u0438 &#8212; \u043e\u0447\u0438\u0441\u0442\u043a\u0438 \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0442\u0430\u043a\u0436\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0434\u0438\u0441\u043a\u0430, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0433\u043e \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u044b\u0445 postgresql. \u041d\u043e \u043f\u0440\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 &#8212; \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f <code>DELETE<\/code> \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0435\u0441\u0442\u043e, \u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f <code>UPDATE<\/code>  \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0435\u0433\u043e \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442.<\/p>\n<p>\u0427\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0440\u0435\u0448\u0430\u044e\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u044b\u00a0<code>vacuum<\/code>\u00a0\u0438\u00a0<code>vacuum full<\/code>, \u043d\u043e \u0443 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0432\u0443\u0445 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0435\u0441\u0442\u044c \u0432\u00a0<a href=\"https:\/\/www.postgresql.org\/docs\/10\/routine-vacuuming.html#vacuum-for-space-recovery\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u043e &#8212;\u00a0<code>vacuum<\/code>\u00a0\u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442 \u043c\u0435\u0441\u0442\u043e \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e \u0443\u0434\u0430\u043b\u044f\u043b\u0438\u0441\u044c \u0441\u0432\u0435\u0436\u0438\u0435 (\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435) \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 &#171;\u0432\u0435\u0440\u0445\u043d\u044e\u044e&#187; \u0447\u0430\u0441\u0442\u044c \u0434\u0430\u0442\u0430-\u0444\u0430\u0439\u043b\u0430.<br \/><code>vacuum full<\/code>\u00a0&#8212; \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442 \u043c\u0435\u0441\u0442\u043e, \u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u043c\u0443 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0436\u0435 \u043c\u0435\u0441\u0442\u0430 \u043d\u0430 \u0434\u0438\u0441\u043a\u0435, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0436\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u044d\u0442\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u043f\u0435\u0440\u0435\u043b\u0438\u0432\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443.<\/p>\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u043c\u043d\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430 \u043d\u0430 \u0434\u0438\u0441\u043a\u0435 &#8212; \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0439\u0442\u0435\u00a0<code>vacuum full schema.table<\/code>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435\u0442 &#8212; \u0442\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0431\u0443\u0434\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0443\u0442\u0438\u043b\u0438\u0442\u044b\u00a0<code>pgcompacttable<\/code>, \u0441\u0441\u044b\u043b\u043a\u0430\u00a0<a href=\"https:\/\/github.com\/dataegret\/pgcompacttable\" rel=\"noopener noreferrer nofollow\">github<\/a>.<\/p>\n<p>\u0421\u0442\u0430\u0432\u0438\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<ul>\n<li>\n<p>Debian-based Linux OS <code>apt-get install libdbi-perl libdbd-pg-perl<\/code><\/p>\n<\/li>\n<li>\n<p>RedHat\/Centos <code>yum install perl-Time-HiRes perl-DBI perl-DBD-Pg<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0440\u0435\u0436\u0438\u043c\u00a0<code>psql<\/code>:<\/p>\n<pre><code class=\"bash\">sudo -u postgres psql<\/code><\/pre>\n<pre><code class=\"sql\">\\c your_database create extension if not exists pgstattuple;  grant execute on function pgstattuple(text) to &lt;db user>; grant execute on function pgstattuple(regclass) to &lt;db user>; grant execute on function pgstattuple_approx to &lt;db user>;  -- \u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u0431\u044b\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u043e\u0442 \u0430\u0434\u043c\u0438\u043d\u0441\u043a\u043e\u0439 \u0443\u0437 postgres alter user postgres with password 'your new admin password'; \\q<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0435\u0431\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u00a0<code>perl<\/code>\u00a0\u0441\u043a\u0440\u0438\u043f\u0442.<\/p>\n<pre><code class=\"bash\">git clone https:\/\/github.com\/dataegret\/pgcompacttable.git<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438, \u043f\u043e\u043b\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0447\u0435\u0440\u0435\u0437:<\/p>\n<pre><code class=\"bash\">perl pgcompacttable --man<\/code><\/pre>\n<pre><code class=\"bash\">perl pgcompacttable \\ --dbname &lt;database_name> \\ -n &lt;schema_name> \\ -h localhost \\ -p 5432 \\ -U &lt;user> \\ -W &lt;password> \\ -t &lt;table_name> \\ -f<\/code><\/pre>\n<h3>\u041a\u0430\u043a\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043d\u044b\u0435?<\/h3>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c SQL \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445 &#8212; \u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 5 \u0441\u0442\u043e\u043b\u0431\u0446\u0435 (<code>wastedbytes<\/code>)<\/p>\n<pre><code class=\"sql\">SELECT   current_database(), schemaname, tablename, \/*reltuples::bigint, relpages::bigint, otta,*\/   ROUND((CASE WHEN otta=0 THEN 0.0 ELSE sml.relpages::float\/otta END)::numeric,1) AS tbloat,   pg_size_pretty(CASE WHEN relpages &lt; otta THEN 0 ELSE bs*(sml.relpages-otta)::BIGINT end) AS wastedbytes,   iname, \/*ituples::bigint, ipages::bigint, iotta,*\/   ROUND((CASE WHEN iotta=0 OR ipages=0 THEN 0.0 ELSE ipages::float\/iotta END)::numeric,1) AS ibloat,   CASE WHEN ipages &lt; iotta THEN 0 ELSE bs*(ipages-iotta) END AS wastedibytes FROM (   SELECT     schemaname, tablename, cc.reltuples, cc.relpages, bs,     CEIL((cc.reltuples*((datahdr+ma-       (CASE WHEN datahdr%ma=0 THEN ma ELSE datahdr%ma END))+nullhdr2+4))\/(bs-20::float)) AS otta,     COALESCE(c2.relname,'?') AS iname, COALESCE(c2.reltuples,0) AS ituples, COALESCE(c2.relpages,0) AS ipages,     COALESCE(CEIL((c2.reltuples*(datahdr-12))\/(bs-20::float)),0) AS iotta \/* very rough approximation, assumes all cols *\/   FROM (     SELECT       ma,bs,schemaname,tablename,       (datawidth+(hdr+ma-(case when hdr%ma=0 THEN ma ELSE hdr%ma END)))::numeric AS datahdr,       (maxfracsum*(nullhdr+ma-(case when nullhdr%ma=0 THEN ma ELSE nullhdr%ma END))) AS nullhdr2     FROM (       SELECT         schemaname, tablename, hdr, ma, bs,         SUM((1-null_frac)*avg_width) AS datawidth,         MAX(null_frac) AS maxfracsum,         hdr+(           SELECT 1+count(*)\/8           FROM pg_stats s2           WHERE null_frac&lt;>0 AND s2.schemaname = s.schemaname AND s2.tablename = s.tablename         ) AS nullhdr       FROM pg_stats s, (         SELECT           (SELECT current_setting('block_size')::numeric) AS bs,           CASE WHEN substring(v,12,3) IN ('8.0','8.1','8.2') THEN 27 ELSE 23 END AS hdr,           CASE WHEN v ~ 'mingw32' THEN 8 ELSE 4 END AS ma         FROM (SELECT version() AS v) AS foo       ) AS constants       GROUP BY 1,2,3,4,5     ) AS foo   ) AS rs   JOIN pg_class cc ON cc.relname = rs.tablename   JOIN pg_namespace nn ON cc.relnamespace = nn.oid AND nn.nspname = rs.schemaname AND nn.nspname &lt;> 'information_schema'   LEFT JOIN pg_index i ON indrelid = cc.oid   LEFT JOIN pg_class c2 ON c2.oid = i.indexrelid ) AS sml --where iname like '%ccnew%' ORDER BY (CASE WHEN relpages &lt; otta THEN 0 ELSE bs*(sml.relpages-otta)::BIGINT end) DESC;<\/code><\/pre>\n<p>\u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c &#171;\u0441\u0436\u0430\u0442\u0438\u0435&#187; \u0442\u0430\u0431\u043b\u0438\u0446\u044b &#8212; \u044d\u0442\u043e \u043f\u0430\u0434\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043a\u043e\u0434\u0430. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432 \u0431\u0434 \u043c\u043e\u0433\u0443\u0442 \u043e\u0441\u0442\u0430\u0442\u044c\u0441\u044f &#171;\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435&#187; \u0438\u043d\u0434\u0435\u043a\u0441\u044b <code>%ccnew%<\/code>, \u0438\u0445 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c <code>drop index index_name<\/code>.<\/p>\n<p>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 <a href=\"https:\/\/www.youtube.com\/watch?v=i1HYbATmWaw\" rel=\"noopener noreferrer nofollow\">\u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043d\u0430 youtube<\/a> \u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 PostgreSQL<\/p>\n<hr\/>\n<p>\u0411\u043e\u043b\u044c\u0448\u043e\u0435 \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0432\u0441\u0435\u043c \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435! \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f &#8212; \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u043d\u0430 \u043c\u043e\u0439 \u043a\u0430\u043d\u0430\u043b\u00a0<a href=\"https:\/\/t.me\/+mfY_q3ZYoEZlNzli\" rel=\"noopener noreferrer nofollow\">artydev &amp; Co<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/782560\/\"> https:\/\/habr.com\/ru\/articles\/782560\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-363755","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/363755","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=363755"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/363755\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=363755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=363755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=363755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}