PostgreSQL에는 Vacuum이라는 개념이 존재한다. PostgreSQL에선 Vacuum을 잘 이해하고 적절히 관리하는 게 중요하고 회사에서도 주기적으로 버큠작업을 해주고 있다.
SELECT
n.nspname AS schenma_name
, c.relname AS table_name
, pg_stat_get_live_tuples(c.oid) + pg_stat_get_dead_tuples(c.oid) AS total_tuple
, pg_stat_get_live_tuples(c.oid) AS live_tuple
, pg_stat_get_dead_tuples(c.oid) AS dead_tupple
, round(100*pg_stat_get_live_tuples(c.oid) / (pg_stat_get_live_tuples(c.oid) + pg_stat_get_dead_tuples(c.oid)),2) AS live_tuple_rate
, round(100*pg_stat_get_dead_tuples(c.oid) / (pg_stat_get_live_tuples(c.oid) + pg_stat_get_dead_tuples(c.oid)),2) AS dead_tuple_rate
, pg_size_pretty(pg_total_relation_size(c.oid)) AS total_relation_size
, pg_size_pretty(pg_relation_size(c.oid)) AS relation_size
FROM
pg_class AS c
JOIN pg_catalog.pg_namespace AS n
ON n.oid = c.relnamespace
WHERE
pg_stat_get_live_tuples(c.oid) > 0
AND c.relname NOT LIKE 'pg_%'
ORDER BY dead_tupple DESC
;
SELECT * FROM pg_stat_all_tables ORDER BY schemaname, relname
;
Vacuum FULL 실행시 pg_class의 relfilenode 값이 변경되므로, 아래 쿼리로 relfilenode의 물리적인 파일 위치를 확인.
SELECT
oid
, pg_relation_filepath(oid)
, relname
, relfilenode
FROM pg_class LIMIT 10
;
SELECT
datname
, usename
, pid
, CURRENT_TIMESTAMP - xact_start AS xact_runtime
, query
FROM
pg_stat_activity
WHERE
upper(query) LIKE '%VACUUM%'
ORDER BY xact_start
;