天天看点

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>