PostgreSQL 10.0 preview 功能增强 - 逻辑复制支持并行COPY初始化数据

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

标签

PostgreSQL , 10.0 , 逻辑复制 , 初始数据COPY


背景

PostgreSQL 已支持逻辑复制,同时对逻辑复制增加了一个初始同步的增强功能,支持通过wal receiver协议跑COPY命令(已封装在逻辑复制的内核代码中),支持多表并行。

也就是说,你可以使用PostgreSQL的逻辑复制,快速的(流式、并行)将一个实例迁移到另一个实例。

Logical replication support for initial data copy  
  
Add functionality for a new subscription to copy the initial data in the  
tables and then sync with the ongoing apply process.  
  
For the copying, add a new internal COPY option to have the COPY source  
data provided by a callback function.  The initial data copy works on  
the subscriber by receiving COPY data from the publisher and then  
providing it locally into a COPY that writes to the destination table.  
  
A WAL receiver can now execute full SQL commands.  This is used here to  
obtain information about tables and publications.  
  
Several new options were added to CREATE and ALTER SUBSCRIPTION to  
control whether and when initial table syncing happens.  
  
Change pg_dump option --no-create-subscription-slots to  
--no-subscription-connect and use the new CREATE SUBSCRIPTION  
... NOCONNECT option for that.  
  
Author: Petr Jelinek <petr.jelinek@2ndquadrant.com>  
Tested-by: Erik Rijkers <er@xs4all.nl>  

逻辑复制包含的初始化COPY的流程如下

主库开启事务快照(快照支持在多个会话间共享, 这也是PostgreSQL的独门秘籍之一), COPY数据, COPY结束后释放快照, 从快照对应的WAL LSN开始接收增量.

/*-------------------------------------------------------------------------  
   2  * tablesync.c  
   3  *    PostgreSQL logical replication  
   4  *  
   5  * Copyright (c) 2012-2016, PostgreSQL Global Development Group  
   6  *  
   7  * IDENTIFICATION  
   8  *    src/backend/replication/logical/tablesync.c  
   9  *  
  10  * NOTES  
  11  *    This file contains code for initial table data synchronization for  
  12  *    logical replication.  
  13  *  
  14  *    The initial data synchronization is done separately for each table,  
  15  *    in separate apply worker that only fetches the initial snapshot data  
  16  *    from the publisher and then synchronizes the position in stream with  
  17  *    the main apply worker.  
  18  *  
  19  *    The are several reasons for doing the synchronization this way:  
  20  *     - It allows us to parallelize the initial data synchronization  
  21  *       which lowers the time needed for it to happen.  
  22  *     - The initial synchronization does not have to hold the xid and LSN  
  23  *       for the time it takes to copy data of all tables, causing less  
  24  *       bloat and lower disk consumption compared to doing the  
  25  *       synchronization in single process for whole database.  
  26  *     - It allows us to synchronize the tables added after the initial  
  27  *       synchronization has finished.  
  28  *  
  29  *    The stream position synchronization works in multiple steps.  
  30  *     - Sync finishes copy and sets table state as SYNCWAIT and waits  
  31  *       for state to change in a loop.  
  32  *     - Apply periodically checks tables that are synchronizing for SYNCWAIT.  
  33  *       When the desired state appears it will compare its position in the  
  34  *       stream with the SYNCWAIT position and based on that changes the  
  35  *       state to based on following rules:  
  36  *        - if the apply is in front of the sync in the wal stream the new  
  37  *          state is set to CATCHUP and apply loops until the sync process  
  38  *          catches up to the same LSN as apply  
  39  *        - if the sync is in front of the apply in the wal stream the new  
  40  *          state is set to SYNCDONE  
  41  *        - if both apply and sync are at the same position in the wal stream  
  42  *          the state of the table is set to READY  
  43  *     - If the state was set to CATCHUP sync will read the stream and  
  44  *       apply changes until it catches up to the specified stream  
  45  *       position and then sets state to READY and signals apply that it  
  46  *       can stop waiting and exits, if the state was set to something  
  47  *       else than CATCHUP the sync process will simply end.  
  48  *     - If the state was set to SYNCDONE by apply, the apply will  
  49  *       continue tracking the table until it reaches the SYNCDONE stream  
  50  *       position at which point it sets state to READY and stops tracking.  
  51  *  
  52  *    The catalog pg_subscription_rel is used to keep information about  
  53  *    subscribed tables and their state and some transient state during  
  54  *    data synchronization is kept in shared memory.  
  55  *  
  56  *    Example flows look like this:  
  57  *     - Apply is in front:  
  58  *        sync:8  
  59  *          -> set SYNCWAIT  
  60  *        apply:10  
  61  *          -> set CATCHUP  
  62  *          -> enter wait-loop  
  63  *        sync:10  
  64  *          -> set READY  
  65  *          -> exit  
  66  *        apply:10  
  67  *          -> exit wait-loop  
  68  *          -> continue rep  
  69  *     - Sync in front:  
  70  *        sync:10  
  71  *          -> set SYNCWAIT  
  72  *        apply:8  
  73  *          -> set SYNCDONE  
  74  *          -> continue per-table filtering  
  75  *        sync:10  
  76  *          -> exit  
  77  *        apply:10  
  78  *          -> set READY  
  79  *          -> stop per-table filtering  
  80  *          -> continue rep  
  81  *-------------------------------------------------------------------------  
  82  */  
  83   

这个patch的讨论,详见邮件组,本文末尾URL。

PostgreSQL社区的作风非常严谨,一个patch可能在邮件组中讨论几个月甚至几年,根据大家的意见反复的修正,patch合并到master已经非常成熟,所以PostgreSQL的稳定性也是远近闻名的。

参考

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

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
1月前
|
关系型数据库 Serverless 分布式数据库
【公测】PolarDB PostgreSQL版Serverless功能免费使用​!
【公测】PolarDB PostgreSQL版Serverless功能免费使用​,公测于2024年3月28日开始,持续三个月,公测期间可以免费使用!
|
2月前
|
存储 关系型数据库 分布式数据库
PolarDB常见问题之PolarDB冷存数据到OSS之后恢复失败如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
2月前
|
SQL 关系型数据库 分布式数据库
在PolarDB中,行数评估是通过对表的统计数据、基数估计以及算子代价模型来进行估算的。
【2月更文挑战第14天】在PolarDB中,行数评估是通过对表的统计数据、基数估计以及算子代价模型来进行估算的。
91 1
|
2月前
|
关系型数据库 Serverless 分布式数据库
PolarDB PostgreSQL版Serverless功能上线公测啦,公测期间免费使用!
Serverless数据库能够使得数据库集群资源随客户业务负载动态弹性扩缩,将客户从复杂的业务资源评估和运维工作中解放出来。PolarDB PostgreSQL版 Serverless提供了CPU、内存、存储、网络资源的实时弹性能力,构建计算与存储分离架构下的 PolarDB PostgreSQL版产品新形态。
|
3天前
|
关系型数据库 分布式数据库 数据库
「活动预告」PolarDB走进青岛,邀请您一起畅游琴岛山海春韵,共话数据生态创新
由PolarDB开源社区、海信聚好看、PostgreSQL中文社区共同发起的《开源数据库沙龙》,将于青岛举办。
|
12天前
|
分布式计算 DataWorks 关系型数据库
DataWorks产品使用合集之在使用 DataWorks 数据集成同步 PostgreSQL 数据库中的 Geometry 类型数据如何解决
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
24 0
|
13天前
|
SQL 运维 关系型数据库
PolarDB产品使用合集之PolarDB 2.3.0 版本的 CDC 功能支持 Polardb-X 到 Polardb-X 的数据同步吗
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
13天前
|
关系型数据库 MySQL 分布式数据库
PolarDB产品使用合集之PolarDB MySQL标准版中带有分区功能吗
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
18天前
|
SQL 关系型数据库 API
从API获取数据并将其插入到PostgreSQL数据库:步骤解析
使用Python处理从API获取的数据并插入到PostgreSQL数据库:安装`psycopg2`,建立数据库连接,确保DataFrame与表结构匹配,然后使用`to_sql`方法将数据插入到已存在的表中。注意数据准备、权限设置、性能优化和安全处理。
|
25天前
|
SQL 关系型数据库 MySQL
关系型数据库插入数据的语句
使用SQL的`INSERT INTO`语句向关系型数据库的`students`表插入数据。例如,插入一个`id`为1,`name`为&#39;张三&#39;,`age`为20的记录:`INSERT INTO students (id, name, age) VALUES (1, &#39;张三&#39;, 20)。如果`id`自增,则可简化为`INSERT INTO students (name, age) VALUES (&#39;张三&#39;, 20)`。
26 2

相关产品

  • 云原生数据库 PolarDB