You are on page 1of 2

09/02/2015

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

RendyTan

Home
Archives
RSS
Search...

PostgresqlVacuumandAnalyze,MaintenanceandPerformance
IhavebeenusingPostgresqlforacoupleofyearsnowandIamveryhappywithitsperformanceandgrowthasaopensourcerelational
database.TodayIwillliketosharewithyouhowVacuumandAnalyzecanhelpyourdatabaseperformbetter.
VACUUMfreesthediskspaceoccupiedbyyourdatabasedeletesandupdates.Whenyoudeleteorupdatearecordinyourtable,itdoes
notgetremovedanditremainsuntilaVACUUMisdone.Forfrequentlyupdatedtables,VACUUMishighlyrecommended.
ANALYZEhelpsimproveyourqueriesbycollectingstatisticsaboutthecontentsoftablesinthedatabase.Theresultsoftheanalysisare
storedinthepg_statisticsystemcatalog.Withthesestatistics,thequeryplanneruseittodeterminethemostefficientexecutionplans,thus
improvetheperformanceofyourqueries.
Duringroutinemaintenance,VACUUMANALYZEareusedtogethertofreeupstorageandimprovetheperformanceofyourqueries.
VACUUMFULLANALYZEisrecommendedfortablesthathavejustwentthroughabulkdeleteorupdate.Donotethatwhenyourun
VACUUMFULL,theaffectedtableswillbelockeduntiltheprocessisdoneandextrastoragespaceisrequiredasatempstore.Normal
VACUUMdonotlockthetables,butdoeshasitsoverheadswhilerunningalongsidealivedatabase.
TheAutovacuumDaemonisturnedonbydefaultanditisoptimizedtosuitmostscenarios.Thedaemondeterminesifthetablerequires
VACUUMorANALYZEorBOTHandrunitinthebackground.Eventhoughitisturnedon,youwillnoticedthattableswithlesser
recordsarenotpartoftheAutovacuumandAutoanalyzeroutine.
Howdoweknow?
1SELECT relname, last_analyze, last_vacuum, last_autoanalyze, last_autovacuum FROM pg_stat_all_tables
2WHERE schemaname = 'public'
3ORDER BY relname;

HereisthepythonscriptthatVACUUMANALYZEthetablesnotcoveredbythedaemon.Youcansetthenumberofdaysyouwantto
checkifPostgresqlhasalreadydoneaAutovacuum.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#!/usr/bin/python
importpsycopg2
importsys

con=None
query=''

try:
iflen(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('SELECTrelname'+
'FROMpg_stat_all_tables'+
"WHEREschemaname='public'"+
'AND((last_analyzeisNULL'+
'ANDlast_autoanalyzeisNULL)'+
'OR((last_analyze<last_autoanalyzeORlast_analyzeisnull)'+
"ANDlast_autoanalyze<now()interval%s)"+
'OR((last_autoanalyze<last_analyzeORlast_autoanalyzeisnull)'+
"ANDlast_analyze<now()interval%s))",[days+'day',days+'day'])
rows=cur.fetchall()
con.set_isolation_level(0)

http://rendykstan.github.io/blog/2013/04/04/postgresql-vacuum-and-analyze-maintenance-and-performance/

1/2

09/02/2015
30
31
32
33
34
35
36
37
38
39
40
41
42

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

forrowinrows:
query='VACUUMANALYZE%s'%(row[0])
cur.execute(query)
else:
print'Thisscriptneeds5arguments[host][database][user][password][days]'

exceptpsycopg2.DatabaseError,e:
print'Error:%s'%e
sys.exit(1)

finally:
ifcon:
con.close()

vacuum_analyze.pyhostedwithbyGitHub

viewraw

Schedulethepythonscripttorunat4am(lowpeak)usingCrontab.
1$crontab -e
200 04 * * *

Tweet

/python/path/python /script/path/vacuum_analyze.py [host] [database] [user] [password] [days]

Copyright2013RendyTan.PoweredbyOctopress|ThemefabricbyPankajKumar
ToTop

http://rendykstan.github.io/blog/2013/04/04/postgresql-vacuum-and-analyze-maintenance-and-performance/

2/2

You might also like