postgresql 定时vacuum脚本

# -*- coding: utf-8 -*-

import pg,multiprocessing

lst=[
{"dbname":"postgres","host":"127.0.01","port":5432,"user":"postgres","passwd":"123456"},
]

def exe_job(item):
    try:
        db = pg.DB(dbname=item['dbname'], host=item['host'], port=item['port'], user=item['user'], passwd=item['passwd'])
        for table in db.get_tables():
            db.query("VACUUM %s ;" %(table))
            db.query("analyze %s ;" %(table))
            print("%s %s" % (item['dbname'],table))
    except Exception as e:
        print(e)
    finally:
        db.close()

if __name__ == '__main__':
    pool_size=16
    pool = multiprocessing.Pool(processes=pool_size)
    pool.map(exe_job,lst)
    pool.close() # no more tasks
    pool.join()  # wrap up current tasks

你可能感兴趣的:(PostgreSQL)