PG空间膨胀方案(二)

来源:这里教程网 时间:2026-03-14 21:21:27 作者:

方案二:Vacuum(业务低峰期执行) 1.查询是否有活跃会话

SELECT 
    s.procpid, 
    s.start, 
    now() - s.start AS elapsed_time,
    a.state,
    a.wait_event,
    s.current_query 
FROM 
    (SELECT 
        backendid, 
        pg_stat_get_backend_pid(S.backendid) AS procpid, 
        pg_stat_get_backend_activity_start(S.backendid) AS start, 
       pg_stat_get_backend_activity(S.backendid) AS current_query 
    FROM 
        (SELECT pg_stat_get_backend_idset() AS backendid) AS S 
    ) AS s,
    pg_stat_activity  a
WHERE 
   s.current_query <> '' 
AND
   s.procpid=a.pid
AND
--exclude current pid
   s.procpid <> pg_backend_pid()                                                     
ORDER BY 
   now() - s.start DESC;
   
//参考示例
 procpid | start | elapsed_time | state |     wait_event      |        current_query         
---------+-------+--------------+-------+---------------------+------------------------------
    3620 |       |              |       | AutoVacuumMain      | <command string not enabled>
    3623 |       |              |       | LogicalLauncherMain | <command string not enabled>
    3618 |       |              |       | BgWriterHibernate   | <command string not enabled>
    3617 |       |              |       | CheckpointerMain    | <command string not enabled>
    3619 |       |              |       | WalWriterMain       | <command string not enabled>
(5 rows)

2.确认无活跃会话(业务低峰期)执行vacumm --postgrs库

\c postges
vacuum full verbose analyze;
--fi库
\c xx_fi;
vacuum full verbose analyze ;

3.示例:

pgdb=# vacuum full verbose analyze;
INFO:  vacuuming "pg_catalog.pg_statistic"
INFO:  "pg_statistic": found 88 removable, 424 nonremovable row versions in 28 pages
DETAIL:  0 dead row versions cannot be removed yet.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
INFO:  vacuuming "pg_catalog.pg_type"
INFO:  "pg_type": found 3 removable, 417 nonremovable row versions in 10 pages
DETAIL:  0 dead row versions cannot be removed yet.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
INFO:  analyzing "pg_catalog.pg_type"
INFO:  "pg_type": scanned 10 of 10 pages, containing 414 live rows and 3 dead rows; 414 rows in sample, 414 estimated total rows
INFO:  vacuuming "pg_catalog.pg_foreign_server"
INFO:  "pg_foreign_server": found 0 removable, 0 nonremovable row versions in 0 pages
......
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
INFO:  analyzing "information_schema.sql_sizing_profiles"
INFO:  "sql_sizing_profiles": scanned 0 of 0 pages, containing 0 live rows and 0 dead rows; 0 rows in sample, 0 estimated total rows
INFO:  vacuuming "public.t2"
INFO:  "t2": found 0 removable, 1 nonremovable row versions in 1 pages
DETAIL:  0 dead row versions cannot be removed yet.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
INFO:  analyzing "public.t2"
INFO:  "t2": scanned 1 of 1 pages, containing 1 live rows and 0 dead rows; 1 rows in sample, 1 estimated total rows
INFO:  vacuuming "pg_catalog.pg_class"
INFO:  "pg_class": found 19 removable, 401 nonremovable row versions in 12 pages
DETAIL:  0 dead row versions cannot be removed yet.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
INFO:  analyzing "pg_catalog.pg_class"
INFO:  "pg_class": scanned 11 of 11 pages, containing 400 live rows and 2 dead rows; 400 rows in sample, 400 estimated total rows
VACUUM

相关推荐