PSQL Vacuum
- A record is "deleted" from a
DELETE
- Creates a "dead tuple"
- doesn't create empty space yet
VACUUM
removes dead tuples from table and indexes- will not return disk space back to OS
- unless you run
VACUUM FULL
- unless you run
- will create space for new rows
- will not return disk space back to OS
- need to update planning statistics with
ANALYZE
VACUUM FULL
¶
- gives disk space back to OS
- EXCLUSIVE LOCK on table
- can't even
SELECT
- can't even
- creates a copy of the table
- doubles the storage needed
Autovacuum¶
Check if autovacuum is on¶
SELECT name, setting
FROM pg_settings
WHERE name IN (
'autovacuum',
'log_autovacuum_min_duration',
'autovacuum_max_workers',
'autovacuum_naptime')
+-----------------------------+---------+
| name | setting |
|-----------------------------+---------|
| autovacuum | on |
| autovacuum_max_workers | 3 |
| autovacuum_naptime | 60 |
| log_autovacuum_min_duration | -1 |
+-----------------------------+---------+
Why autovacuum should always be on¶
- tons of dead
- will run out of transaction IDs
- DB will shut down
View the last time (auto)vacuum was run for a table¶
SELECT
schemaname,
relname AS table_name,
last_vacuum,
last_autovacuum,
vacuum_count,
autovacuum_count
FROM pg_stat_user_tables;
Last update:
2023-04-24