PostgreSQL的磁盘空间占用问题

http://tech.ddvip.com   2007年10月26日    社区交流

本文详细介绍PostgreSQL的磁盘空间占用问题

  max_fsm_pages:

  PostgreSQLrecordsfreespaceineachofitsdatapages.Thisinformationisusefulforvacuumtofindouthowmanyandwhichpagestolookforwhenitfreesupthespace. 

  Ifyouhaveadatabasethatdoeslotsofupdatesanddeletes,thatisgoingtogeneratedeadtuples,duetoPostgreSQL'sMVCCsystem.Thespaceoccupiedbydeadtuplescanbefreedwithvacuum,unlessthereismorewastedspacethaniscoveredbytheFreeSpaceMap,inwhichcasethemuchlessconvenient"vacuumfull"isrequired.ByexpandingtheFSMtocoverallofthosedeadtuples,youmightneveragainneedtorunvacuumfullexceptonholidays. 

  Thebestwaytosetmax_fsm_pagesisinteractive;First,figureoutthevacuum(regular)frequencyofyourdatabasebasedonwriteactivity;next,runthedatabaseundernormalproductionload,andrun"vacuumverboseanalyze"insteadofvacuum,savingtheoutputtoafile;finally,calculatethemaximumtotalnumberofpagesreclaimedbetweenvacuumsbasedontheoutput,andusethat. 

  Remember,thisisadatabaseclusterwidesetting.Sobumpitupenoughtocoveralldatabasesinyourdatabasecluster.Also,eachFSMpageuses6bytesofRAMforadministrativeoverhead,soincreasingFSMsubstantiallyonsystemslowonRAMmaybecounter-productive. 

  大意是说,postgresql.conf中的这个参数(max_fsm_pages)用于告诉PostgreSQL申请多大的内存空间用于保存数据文件的freespace信息,按我的简单理解,如果在一个表中删除了一些记录,PostgreSQL会把这一改动记录在"FreeSpaceMap"中,下次如果再往表里插记录时,根据FreeSpaceMap中的信息,就能利用以前删记录而腾出来的磁盘空间。不过FreeSpaceMap是存在于内存中,大小毕竟是有限的,对于大量数据的删除+插入,要么指定一个较大的max_fsm_pages,要么及时进行vacuum以整理表中的碎片,否则,PostgreSQL只有把新插入的记录添加到文件的末尾,造成文件越来越大。我的一个程序就是意外地因为磁盘空间满了而中止的,它每次要往一个表里插500多万条记录,这之前先要delete同样条数的一批记录,可最后还是占满了整个硬盘。

责编:豆豆技术应用

正在加载评论...