Help Center/ GaussDB/ Developer Guide(Distributed_V2.0-8.x)/ Best Practices/ Best Practices for VACUUM/ FAQ/ How Do I Evaluate Whether the Current Table or Database Requires VACUUM?
Updated on 2025-05-29 GMT+08:00

How Do I Evaluate Whether the Current Table or Database Requires VACUUM?

GaussDB provides the system function pg_autovac_status to query whether a table has reached the VACUUM threshold. Combined with the pg_class system catalog, you can query the top N tables with the highest dead row rates. For example, the following SQL statement queries the top 10 tables with the highest dead row rates:

SELECT
C.oid AS relid,
N.nspname AS schemaname,
C.relname AS relname,
C.reltuples AS n_rel_tup,
pg_catalog.pg_stat_get_dead_tuples(C.oid) AS n_dead_tup,
pg_catalog.pg_autovac_status(C.oid).doVacuum as need_Vacuum,
CAST(n_dead_tup / C.reltuples AS decimal(10, 2)) AS dead_radio
FROM pg_class C LEFT JOIN
pg_index I ON C.oid = I.indrelid
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE C.relkind IN ('r') and C.reltuples > 0
GROUP BY C.oid, N.nspname, C.relname, C.reltuples, need_Vacuum
order by dead_radio desc limit 10;

The command output is as follows:

 relid | schemaname |         relname         | n_rel_tup | n_dead_tup | need_vacuum | dead_radio
-------+------------+-------------------------+-----------+------------+-------------+------------
  2604 | pg_catalog | pg_attrdef              |         1 |         14 |             |      14.00
  2606 | pg_catalog | pg_constraint           |        12 |         15 |             |       1.25
  3118 | pg_catalog | pg_foreign_table        |         2 |          2 |             |       1.00
  3079 | pg_catalog | pg_extension            |        24 |         15 |             |        .63
  2601 | pg_catalog | pg_am                   |        11 |          4 |             |        .36
  1417 | pg_catalog | pg_foreign_server       |         4 |          1 |             |        .25
  2610 | pg_catalog | pg_index                |       421 |         82 |             |        .19
  2328 | pg_catalog | pg_foreign_data_wrapper |         6 |          1 |             |        .17
  2619 | pg_catalog | pg_statistic            |      1231 |        187 |             |        .15
  2609 | pg_catalog | pg_description          |      4748 |         25 |             |        .01
(10 rows)