Sunteți pe pagina 1din 2

09/02/2015

Postgresql Vacuum and Analyze, Maintenance and Performance - Coding, Design and Agile Processes

Rendy Tan

Home Archives RSS Search
Home Archives RSS Search
Home Archives RSS Search
Home
Archives
RSS

RSS

Search

Search

Postgresql Vacuum and Analyze, Maintenance and Performance

I have been using Postgresql for a couple of years now and I am very happy with its performance and growth as a open source relational database. Today I will like to share with you how Vacuum and Analyze can help your database perform better.

VACUUM frees the disk space occupied by your database deletes and updates. When you delete or update a record in your table, it does not get removed and it remains until a VACUUM is done. For frequently­updated tables, VACUUM is highly recommended.

ANALYZE helps improve your queries by collecting statistics about the contents of tables in the database. The results of the analysis are stored in the pg_statistic system catalog. With these statistics, the query planner use it to determine the most efficient execution plans, thus improve the performance of your queries.

During routine maintenance, VACUUM ANALYZE are used together to free up storage and improve the performance of your queries.

VACUUM FULL ANALYZE is recommended for tables that have just went through a bulk delete or update. Do note that when you run VACUUM FULL, the affected tables will be locked until the process is done and extra storage space is required as a temp store. Normal VACUUM do not lock the tables, but does has its overheads while running along side a live database.

The Autovacuum Daemon is turned on by default and it is optimized to suit most scenarios. The daemon determines if the table requires VACUUM or ANALYZE or BOTH and run it in the background. Even though it is turned on, you will noticed that tables with lesser records are not part of the Autovacuum and Autoanalyze routine.

How do we know?

1 SELECTrelname,last_analyze,last_vacuum,last_autoanalyze,last_autovacuumFROMpg_stat_all_tables

2 WHEREschemaname='public'

3 ORDERBYrelname;

Here is the python script that VACUUM ANALYZE the tables not covered by the daemon. You can set the number of days you want to check if Postgresql has already done a Autovacuum.

1

#!/usr/bin/python

2

importpsycopg2

3

importsys

4

5

con=None

6

query=''

7

8

try:

9

iflen(sys.argv)==6:

10

11

host=sys.argv[1]

12

database=sys.argv[2]

13

user=sys.argv[3]

14

password=sys.argv[4]

15

days=sys.argv[5]

16

17

con=psycopg2.connect(host=host,database=database,user=user,password=password)

18

cur=con.cursor()

19

cur.execute('SELECTrelname'+

20

'FROMpg_stat_all_tables'+

21

"WHEREschemaname='public'"+

22

'AND((last_analyzeisNULL'+

23

'ANDlast_autoanalyzeisNULL)'+

24

'OR((last_analyze<last_autoanalyzeORlast_analyzeisnull)'+

25

"ANDlast_autoanalyze<now()­interval%s)"+

26

'OR((last_autoanalyze<last_analyzeORlast_autoanalyzeisnull)'+

27

"ANDlast_analyze<now()­interval%s));",[days+'day',days+'day'])

28

rows=cur.fetchall()

29

con.set_isolation_level(0)

09/02/2015

Postgresql Vacuum and Analyze, Maintenance and Performance - Coding, Design and Agile Processes

30

forrowinrows:

31

query='VACUUMANALYZE%s;'%(row[0])

32

cur.execute(query)

33

else:

34

print'Thisscriptneeds5arguments[host][database][user][password][days]'

35

36

exceptpsycopg2.DatabaseError,e:

37

print'Error:%s'%e

38

sys.exit(1)

39

40

finally:

41

ifcon:

42

con.close()

vacuum_analyze.py hosted with by GitHub

Schedule the python script to run at 4am (low peak) using Crontab.

1 $crontab-e 2 0004 *** /python/path/python/script/path/vacuum_analyze.py[host][database][user][password][days]

Tweet 3
Tweet
3

2

Copyright © 2013 Rendy Tan . Powered by Octopress | Theme fabric by Pankaj Kumar To Top