天天看點

pg_dump一緻性備份以及cache lookup failed錯誤的原因分析

postgresql邏輯備份,如何保證備份資料的一緻性呢,例如備份的同時,資料被纂改或者有新增的資料,如何保證在全庫視角,備份出來的資料是在備份開始時看到的一緻資料。

可以追溯到1999年的代碼,早期postgresql通過serializable隔離級别來保證備份的一緻性。

<a href="https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=bcb5aac81dec14d892fae18b446315367563be4c">https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=bcb5aac81dec14d892fae18b446315367563be4c</a>

<a href="https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=df9e539ea2c434c73d724234e792536789fd97b8">https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=df9e539ea2c434c73d724234e792536789fd97b8</a>

9.1 開始,postgresql改進并支援了ssi隔離級别,比sql92标準更加苛刻。

同時支援transaction snapshot,一緻性備份不再需要serializable,使用repeatable read即可。

是以pg_dump.c改成如下

<a href="https://git.postgresql.org/gitweb/?p=postgresql.git;a=blobdiff;f=src/bin/pg_dump/pg_dump.c;h=d3eb7662880b3dda57f052d7fbbd22392a8cb562;hp=e844b5b062440ecf7233576d396ca36dafc5e8b9;hb=dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21;hpb=c18f51da17d8cf01d62218e0404e18ba246bde54">https://git.postgresql.org/gitweb/?p=postgresql.git;a=blobdiff;f=src/bin/pg_dump/pg_dump.c;h=d3eb7662880b3dda57f052d7fbbd22392a8cb562;hp=e844b5b062440ecf7233576d396ca36dafc5e8b9;hb=dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21;hpb=c18f51da17d8cf01d62218e0404e18ba246bde54</a>

除了考慮資料的一緻性備份,還需要考慮結構的一緻性。

某些擷取資料結構的調用是snapshot now的,是以可能有很小的視窗期可能被執行ddl,進而導緻relcache變化。

如果pg_dump發現relcache變化,則會爆出cache lookup failed的錯誤,導緻備份失敗。

<a href="https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21">https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21</a>

是以應該避免在使用邏輯備份期間執行ddl。

如果系統無法避免,建議使用實體備份。

<a href="http://info.flagcounter.com/h9v1">count</a>