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

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

背景

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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
4月前
|
关系型数据库 数据库 PostgreSQL
postgresql|数据库|启动数据库时报错:FATAL: could not map anonymous shared memory的解决
postgresql|数据库|启动数据库时报错:FATAL: could not map anonymous shared memory的解决
46 1
|
4月前
|
关系型数据库 数据库
Harbor断电重启postgres报错 could not locate a valid checkpoint record
Harbor断电重启postgres报错 could not locate a valid checkpoint record
197 0
|
9月前
|
关系型数据库
PG修改数据页页头等信息时是否会产生WAL?
PG修改数据页页头等信息时是否会产生WAL?
39 0
|
11月前
|
存储 关系型数据库 MySQL
mysql 数据库无法启动(Ignoring the redo log due to missing MLOG_CHECKPOINT between the checkpoint .... and)
数据库机器的CPU和主板都换了,重新开机,发现mysql数据库无法启动!
156 0
|
11月前
|
关系型数据库 MySQL 数据库
修改innodb_buffer_pool_instances解决mysqlbinlog恢复慢的问题
一个客户的mysql数据库恢复在最后一步是滚binlog,结果恢复特别慢,CPU占用率100%,磁盘IO几乎是零,show processlist发现线程在sleep。从general log里面看不到任何动静,似乎找不到解决的办法。
334 0
|
11月前
|
Oracle 前端开发 关系型数据库
使用隐含参数_disable_logging分析oracle写redo logfile的性能
oracle有一个隐含参数_disable_logging可以禁止日志的生成,这个参数当然不能在生产库使用,但我们可以将其因为与测试,例如,如果我们怀疑数据库写redo logfile存在性能问题,我们可以将这个参数设置为true,禁止写日志,看看oracle的性能提高了多少。
|
SQL 存储 缓存
分析MySQL执行的流程(连接、缓存、分析、优化、执行、Undo Log、Binlog、Redo Log)
熟悉MySQL的都知道MySQL服务端实现主要分为Server层和存储引擎层。Server层负责接收和管理客户端连接、管理缓存、解析SQL、优化SQL、调用存储引擎执行SQL;存储引擎层主要负责存储、查询数据。
分析MySQL执行的流程(连接、缓存、分析、优化、执行、Undo Log、Binlog、Redo Log)
|
关系型数据库 数据库 PostgreSQL
PG异常无法启动的问题:could not read file "pg_logical/replorigin_checkpoint": Success
问题描述 新安装不久的PostgreSQL数据库,断电后重启,查看日志如下 2019-01-08 08:44:19.989 UTC [7493] LOG: database system was interrupted; last known up at 2018-12-24 10:56:28 UTC 2019-01-08 08:44:19.
3229 0
|
SQL
MySQL:简单记录删除binary log的接口和O_DIRECT不会用到REDO
一、栈帧 #0 my_delete (name=0x7ffff0fa0490 "./binlog.000005", MyFlags=0) at /root/softm/percona-server-5.
869 0