Last active
December 15, 2015 20:19
-
-
Save rendykstan/5318027 to your computer and use it in GitHub Desktop.
VACUUM ANALYZE Postresql tables not covered by the daemon on the daily basis. More info: http://rendykstan.github.com/blog/2013/04/04/postgresql-vacuum-and-analyze-maintenance-and-performance/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import psycopg2 | |
import sys | |
con = None | |
query = '' | |
try: | |
if len(sys.argv) == 6: | |
host = sys.argv[1] | |
database = sys.argv[2] | |
user = sys.argv[3] | |
password = sys.argv[4] | |
days = sys.argv[5] | |
con = psycopg2.connect(host=host, database=database, user=user, password=password) | |
cur = con.cursor() | |
cur.execute('SELECT relname ' + | |
'FROM pg_stat_all_tables ' + | |
"WHERE schemaname = 'public' " + | |
'AND ((last_analyze is NULL ' + | |
'AND last_autoanalyze is NULL)' + | |
'OR ((last_analyze < last_autoanalyze OR last_analyze is null) ' + | |
"AND last_autoanalyze < now() - interval %s) " + | |
'OR ((last_autoanalyze < last_analyze OR last_autoanalyze is null) ' + | |
"AND last_analyze < now() - interval %s));", [days + ' day', days + ' day']) | |
rows = cur.fetchall() | |
con.set_isolation_level(0) | |
for row in rows: | |
query = 'VACUUM ANALYZE %s;' % (row[0]) | |
cur.execute(query) | |
else: | |
print 'This script needs 5 arguments [host] [database] [user] [password] [days]' | |
except psycopg2.DatabaseError, e: | |
print 'Error: %s' % e | |
sys.exit(1) | |
finally: | |
if con: | |
con.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment