Before VACUUM FULL I want to know, which table really needs this operation. To check this I found this query:
SELECT tuple_percent, dead_tuple_count, dead_tuple_percent, free_space, free_percent FROM pgstattuple('public.book_formula_operation_log');
Based on dead_tuple_percent
I can decide – run VACUUM FULL or it’s not necessary.
But this script shows only one table. How can I give this information from all tables from database sorted by dead_tuple_percent
?
2
Answers
You can use
pg_stat_user_tables
instead:You can use a lateral join to the function call, from a list of tables.
pg_stat_user_tables provides a handy list of tables, but you might want something else.
But, it is doubtful this is useful. For one thing, dead_tuple_percent is adequately dealt with by a regular (non-FULL) vacuum, so sorting on it with the intention of driving VACUUM FULL doesn’t make sense. It might more sense to use free_space or free_percent. But even that is questionable. Why squeeze out the freespace, if you are just going to create it again soon anyway? There is no point in doing that unless the bloat is actually a problem, and until you think you fixed the problem which created the bloat in the first place, which there is no automated tool for.