Skip to content

Instantly share code, notes, and snippets.

@sebawebber
Created June 8, 2016 23:37
Show Gist options
  • Select an option

  • Save sebawebber/1a7dd556925787b2c660d7f034a501d0 to your computer and use it in GitHub Desktop.

Select an option

Save sebawebber/1a7dd556925787b2c660d7f034a501d0 to your computer and use it in GitHub Desktop.

CREATE multiple vacuums analyze for database

This is a simple hack using shellscript and psql. on 9.5, there's avaliable a parallel vacuumdb. ;)

psql -U postgres -d autosystem -f gera_vacuum.sql -At > vacuum_base.sh
split -l 200 vacuum_base.sh vacuum_job_
chmod +x vacuum_job_*
sed  -i '1i #!/bin/bash' vacuum_job_*
ls vacuum_job_a* | xargs -I job_name nohup job_name > job_name.log 2>&1 &

Change de 200 for the number of jobs per file.

SELECT
'vacuumdb --verbose --analyze --table=' ||
pg_stat_all_tables.schemaname || '.' || pg_stat_all_tables.relname
FROM pg_stat_all_tables
WHERE (
pg_stat_all_tables.schemaname <> ALL (ARRAY['pg_catalog'::name, 'information_schema'::name])
)
AND pg_stat_all_tables.schemaname !~ '^pg_toast'::text
AND pg_stat_all_tables.schemaname NOT ILIKE 'pg_temp%';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment