pg_dump一致性备份以及cache lookup failed错误的原因分析

本文涉及的产品
云数据库 PolarDB MySQL 版,列存表分析加速 4核8GB
PolarDB Agent Express,2核4GB
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介:

背景

PostgreSQL逻辑备份,如何保证备份数据的一致性呢,例如备份的同时,数据被纂改或者有新增的数据,如何保证在全库视角,备份出来的数据是在备份开始时看到的一致数据。

一致性逻辑备份分析

可以追溯到1999年的代码,早期PostgreSQL通过serializable隔离级别来保证备份的一致性。
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=bcb5aac81dec14d892fae18b446315367563be4c

Add keywords to implement Vadim's transaction isolation  
 and lock syntax as fully parsed tokens.  
Two keywords for isolation are non-reserved SQL92  
 (COMMITTED, SERIALIZABLE).  
All other new keywords are non-reserved Postgres (not SQL92)  
 (ACCESS, EXCLUSIVE, MODE, SHARE).  
Add syntax to allow CREATE [GLOBAL|LOCAL] TEMPORARY TABLE, throwing an  
 error if GLOBAL is specified.    

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

+   /*  
+    * Start serializable transaction to dump consistent data  
+    */  
+   {  
+       PGresult   *res;  
+  
+       res = PQexec(g_conn, "begin");  
+       if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)  
+       {  
+           fprintf(stderr, "BEGIN command failed.  Explanation from backend: '%s'.\n", PQerrorMessage(g_conn));  
+           exit_nicely(g_conn);  
+       }  
+       PQclear(res);  
+       res = PQexec(g_conn, "set transaction isolation level serializable");  
+       if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)  
+       {  
+           fprintf(stderr, "SET TRANSACTION command failed.  Explanation from backend: '%s'.\n", PQerrorMessage(g_conn));  
+           exit_nicely(g_conn);  
+       }  
+       PQclear(res);  
+   }  
+  

9.1 开始,PostgreSQL改进并支持了SSI隔离级别,比SQL92标准更加苛刻。

同时支持transaction snapshot,一致性备份不再需要serializable,使用repeatable read即可。

所以pg_dump.c改成如下

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

    /*  
         * Start transaction-snapshot mode transaction to dump consistent data.  
         */  
        ExecuteSqlStatement(AH, "BEGIN");  
        if (AH->remoteVersion >= 90100)  
        {  
                /*  
                 * To support the combination of serializable_deferrable with the jobs  
                 * option we use REPEATABLE READ for the worker connections that are  
                 * passed a snapshot.  As long as the snapshot is acquired in a  
                 * SERIALIZABLE, READ ONLY, DEFERRABLE transaction, its use within a  
                 * REPEATABLE READ transaction provides the appropriate integrity  
                 * guarantees.  This is a kluge, but safe for back-patching.  
                 */  
                if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL)  
                        ExecuteSqlStatement(AH,  
                                                                "SET TRANSACTION ISOLATION LEVEL "  
                                                                "SERIALIZABLE, READ ONLY, DEFERRABLE");  
                else  
                        ExecuteSqlStatement(AH,  
                                                                "SET TRANSACTION ISOLATION LEVEL "  
                                                                "REPEATABLE READ, READ ONLY");  
        }  
        else if (AH->remoteVersion >= 70400)  
        {  
                /* note: comma was not accepted in SET TRANSACTION before 8.0 */  
                ExecuteSqlStatement(AH,  
                                                        "SET TRANSACTION ISOLATION LEVEL "  
                                                        "SERIALIZABLE READ ONLY");  
        }  
        else  
                ExecuteSqlStatement(AH,  
                                                        "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");  

逻辑备份cache lookup failed错误分析

除了考虑数据的一致性备份,还需要考虑结构的一致性。

某些获取数据结构的调用是snapshot now的,所以可能有很小的窗口期可能被执行DDL,从而导致relcache变化。

如果pg_dump发现relcache变化,则会爆出cache lookup failed的错误,导致备份失败。

  14  *  Note that pg_dump runs in a transaction-snapshot mode transaction,  
  15  *  so it sees a consistent snapshot of the database including system  
  16  *  catalogs. However, it relies in part on various specialized backend  
  17  *  functions like pg_get_indexdef(), and those things tend to run on  
  18  *  SnapshotNow time, ie they look at the currently committed state.  So  
  19  *  it is possible to get 'cache lookup failed' error if someone  
  20  *  performs DDL changes while a dump is happening. The window for this  
  21  *  sort of thing is from the acquisition of the transaction snapshot to  
  22  *  getSchemaData() (when pg_dump acquires AccessShareLock on every  
  23  *  table it intends to dump). It isn't very large, but it can happen.  

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

所以应该避免在使用逻辑备份期间执行DDL。

如果系统无法避免,建议使用物理备份。

Count

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
Java 数据库连接 数据库
springboot 集成kingbase的步骤
要在Spring Boot中集成Kingbase数据库,你可以按照以下步骤进行操作: 步骤1:添加Kingbase JDBC驱动 首先,将Kingbase JDBC驱动程序添加到Spring Boot项目的依赖项中。你可以在Maven或Gradle构建文件中添加以下依赖项(根据你使用的构建管理工具进行选择): 对于Maven: ```xml <dependency> <groupId>com.kingbase</groupId> <artifactId>kingbase-driver</artifactId> <version>{驱动版本}</version> </
2467 0
|
SQL Oracle 关系型数据库
Oracle系列十七:Sqluldr2与Sqlldr
Oracle系列十七:Sqluldr2与Sqlldr
|
IDE Android开发 iOS开发
探索安卓与iOS系统的技术差异:开发者的视角
本文深入分析了安卓(Android)与苹果iOS两大移动操作系统在技术架构、开发环境、用户体验和市场策略方面的主要差异。通过对比这两种系统的不同特点,旨在为移动应用开发者提供有价值的见解,帮助他们在不同平台上做出更明智的开发决策。
|
消息中间件 Kafka
Kafka【问题记录 01】kill -9 导致 Kakfa 重启失败问题处理(doesn‘t match stored clusterId xxx in meta.properties)
【2月更文挑战第20天】Kafka【问题记录 01】kill -9 导致 Kakfa 重启失败问题处理(doesn‘t match stored clusterId xxx in meta.properties)
549 0
|
Oracle Java 关系型数据库
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合Mybatis连接Oracle数据库
|
存储 固态存储 关系型数据库
PostgreSQL核心操作之数据备份恢复
PostgreSQL核心操作之数据备份恢复
1987 0
|
Java 测试技术 数据库
SpringBoot启动时设置不加载数据库
SpringBoot启动时设置不加载数据库
1246 0
|
Oracle 关系型数据库
Oracle中filesystemio_options 和 disk_asynch_io 参数的设置
参考文档Doc ID 1987437.1 filesystemio_options参数,中间是这个参数的配置值。
876 0
|
机器学习/深度学习 人工智能 vr&ar
【机器学习】线性回归——最小二乘法(理论+图解+公式推导)
【机器学习】线性回归——最小二乘法(理论+图解+公式推导)
1819 0
【机器学习】线性回归——最小二乘法(理论+图解+公式推导)