PolarDB开源数据库进阶课7 实时流式归档

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
简介: 本文介绍了如何在PolarDB RAC一写多读集群中实现实时归档,确保WAL日志的及时备份。实验依赖于Docker容器和loop设备模拟的共享存储环境。通过配置主节点的`pg_hba.conf`、创建复制槽以及使用`pg_receivewal`工具,实现实时接收并归档WAL文件。此外,还提供了详细的命令行帮助和相关文档链接,方便读者参考和操作。注意:如果已搭建容灾节点,则无需重复进行实时归档。

背景

穷鬼玩PolarDB RAC一写多读集群系列已经写了几篇:

本篇文章介绍一下如何进行实时归档? 实验环境依赖 《在Docker容器中用loop设备模拟共享存储》 , 如果没有环境, 请自行参考以上文章搭建环境.

还需要参考如下文档:

在线归档需要等wal文件切换时才会进行copy, PolarDB默认的wal文件大小是1GB, 这样可能会有一些弊端:

  • 在主节点进行归档时, copy可能会带来较大的突发IO.
  • 如果存储故障, 可能丢失未归档的wal日志, 最多1GB.

所以这篇文档想介绍一下实时归档. 注意: 如果你已经建立了PolarDB容灾(standby)节点, 可以忽略这篇文档, 因为容灾节点本身就有实时接收WAL, 在容灾节点归档即可, 没有必要再接收一份. 当然了, 如果你就是喜欢多弄几份WAL归档, 请继续.

DEMO

b站视频链接

Youtube视频链接

1、新建docker容器 pb4, 作为实时归档机.

cd ~/data_volumn    
PWD=`pwd`    
    
docker run -d -it -v $PWD:/data -P --shm-size=1g --cap-add=SYS_PTRACE --cap-add SYS_ADMIN --privileged=true --name pb4 registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu22.04 bash

在宿主机映射到docker容器的目录内创建实时归档目录.

# 进入容器pb4  
docker exec -ti pb4 bash  
  
# 创建实时归档目录  
mkdir /data/polardb_wal_archive/

将编译好的二进制拷贝到pb4的HOME目录, 便于调用:

$ cp -r /data/polardb/tmp_polardb_pg_15_base ~/
# 或者使用软链也可以, 软链还有个好处, 编译插件时只需要编译一次即可:   
$ ln -s /data/polardb/tmp_polardb_pg_15_base ~/   
    
    
$ which psql    
/home/postgres/tmp_polardb_pg_15_base/bin/psql

2、pb1 primary节点, 配置pg_hba.conf, 允许备份机使用流复制链接

# 进入容器pb1  
docker exec -ti pb1 bash  
  
# 由于这里pb4网段已经配置了, 所以不需要再配置, 如下:   
cat ~/primary/pg_hba.conf  
  
host  replication postgres  172.17.0.0/16 trust   
  
# 如果有修改, 需要reload  
# pg_ctl reload -D ~/primary

3、pb1 primary节点, 创建 replication slot, 防止实时归档机未接收的WAL被主节点删除.

psql -p 5432 -d postgres -c "SELECT pg_create_physical_replication_slot('online_archwal_1');"    
    
 pg_create_physical_replication_slot   
-------------------------------------  
 (online_archwal_1,)  
(1 row)

4、pb4 实时归档机, 使用pg_receivewal工具开始实时归档

编辑接收wal的脚本, 如果pg_receivewal进程退出了可以自动重启.

vi /home/postgres/wal.sh

脚本如下:

#!/bin/bash  
  
while true   
do  
  # 假设实时归档机上只有一个pg_receivewal命令, 没有其他pg_receivewal命令.  
  if pgrep -x "pg_receivewal" >/dev/null  
  then  
    echo "pg_receivewal 进程正在运行"   
  else  
    # 如果想降低IO频率, 可以去掉 --synchronous  
    nohup pg_receivewal --synchronous -D /data/polardb_wal_archive -S online_archwal_1 -d "host=172.17.0.2 port=5432 user=postgres dbname=replication" >>/data/polardb_wal_archive/arch.log 2>&1 &     
  fi  
  
  sleep 10  
done

修改脚本权限

chmod 555 /home/postgres/wal.sh

开启归档

nohup /home/postgres/wal.sh >/dev/null 2>&1 &

实际的生产环境当中, 可以把wal.sh配置为服务器启动/容器启动时自动执行.

5、检查归档是否正常

pb4 实时归档机. 接收已经开始了.

$ ll -h /data/polardb_wal_archive  
total 1.1G  
drwxr-xr-x 4 postgres postgres  128 Dec 19 11:17 ./  
drwxr-xr-x 9 postgres postgres  288 Dec 18 17:53 ../  
-rw------- 1 postgres postgres 1.0G Dec 19 11:17 000000010000000200000000.partial  
-rw-r--r-- 1 postgres postgres  334 Dec 19 11:16 arch.log

pb1, PolarDB primary节点. 查看复制槽, 可以看到连接已建立.

postgres=# select * from pg_stat_replication where application_name='pg_receivewal';  
-[ RECORD 1 ]----+------------------------------  
pid              | 4204  
usesysid         | 10  
usename          | postgres  
application_name | pg_receivewal  
client_addr      | 172.17.0.5  
client_hostname  |   
client_port      | 52744  
backend_start    | 2024-12-19 11:17:10.982052+08  
backend_xmin     |   
state            | streaming  
sent_lsn         | 2/658  
write_lsn        | 2/658  
flush_lsn        | 2/658  
replay_lsn       |   
write_lag        | 00:00:12.95224  
flush_lag        | 00:01:14.971488  
replay_lag       | 00:01:14.971488  
sync_priority    | 0  
sync_state       | async  
reply_time       | 2024-12-19 11:18:25.957865+08

6、在pb1, PolarDB primary节点写入一些数据, 可以看到pg_stat_replication.sent_lsn的变化.

postgres=# create table test (id int, info text, ts timestamp);  
CREATE TABLE  
postgres=# insert into test select generate_series(1,100),md5(random()::text),clock_timestamp();  
INSERT 0 100

接收wal位置已推进

postgres=# select * from pg_stat_replication where application_name='pg_receivewal';  
-[ RECORD 1 ]----+------------------------------  
pid              | 4285  
usesysid         | 10  
usename          | postgres  
application_name | pg_receivewal  
client_addr      | 172.17.0.5  
client_hostname  |   
client_port      | 43402  
backend_start    | 2024-12-19 11:32:27.928248+08  
backend_xmin     |   
state            | streaming  
sent_lsn         | 2/40021960  
write_lsn        | 2/40021960  
flush_lsn        | 2/40021960 
replay_lsn       |   
write_lag        | 00:00:05.118327  
flush_lag        | 00:00:10.002961  
replay_lag       | 00:00:10.002961  
sync_priority    | 0  
sync_state       | async  
reply_time       | 2024-12-19 11:32:37.937766+08

slot状态正常

postgres=# select * from pg_replication_slots where slot_name='online_archwal_1';  
-[ RECORD 1 ]-------+-----------------  
slot_name           | online_archwal_1  
plugin              |   
slot_type           | physical  
datoid              |   
database            |   
temporary           | f  
active              | t  
active_pid          | 4285  
xmin                |   
catalog_xmin        |   
restart_lsn         | 2/40021960 
confirmed_flush_lsn |   
wal_status          | reserved  
safe_wal_size       |   
two_phase           | f

附, pg_receivewal 命令行帮助:

$ pg_receivewal --help  
pg_receivewal receives PostgreSQL streaming write-ahead logs.  
  
Usage:  
  pg_receivewal [OPTION]...  
  
Options:  
  -D, --directory=DIR    receive write-ahead log files into this directory  
  -E, --endpos=LSN       exit after receiving the specified LSN  
      --if-not-exists    do not error if slot already exists when creating a slot  
  -n, --no-loop          do not loop on connection lost  
      --no-sync          do not wait for changes to be written safely to disk  
  -s, --status-interval=SECS  
                         time between status packets sent to server (default: 10)  
  -S, --slot=SLOTNAME    replication slot to use  
      --synchronous      flush write-ahead log immediately after writing  
  -v, --verbose          output verbose messages  
  -V, --version          output version information, then exit  
  -Z, --compress=METHOD[:DETAIL]  
                         compress as specified  
  -?, --help             show this help, then exit  
  
Connection options:  
  -d, --dbname=CONNSTR   connection string  
  -h, --host=HOSTNAME    database server host or socket directory  
  -p, --port=PORT        database server port number  
  -U, --username=NAME    connect as specified database user  
  -w, --no-password      never prompt for password  
  -W, --password         force password prompt (should happen automatically)  
  
Optional actions:  
      --create-slot      create a new replication slot (for the slot's name see --slot)  
      --drop-slot        drop the replication slot (for the slot's name see --slot)  
  
Report bugs to <pgsql-bugs@lists.postgresql.org>.  
PostgreSQL home page: <https://www.postgresql.org/>

附, libpq connstr配置.

参考

《穷鬼玩PolarDB RAC一写多读集群系列 | 在Docker容器中用loop设备模拟共享存储》

《穷鬼玩PolarDB RAC一写多读集群系列 | 如何搭建PolarDB容灾(standby)节点》

《穷鬼玩PolarDB RAC一写多读集群系列 | 共享存储在线扩容》

《穷鬼玩PolarDB RAC一写多读集群系列 | 计算节点 Switchover》

《穷鬼玩PolarDB RAC一写多读集群系列 | 在线备份》

《穷鬼玩PolarDB RAC一写多读集群系列 | 在线归档》

https://www.postgresql.org/docs/current/app-pgreceivewal.html

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
1月前
|
SQL 关系型数据库 MySQL
开源新发布|PolarDB-X v2.4.2开源生态适配升级
PolarDB-X v2.4.2开源发布,重点完善生态能力:新增客户端驱动、开源polardbx-proxy组件,支持读写分离与高可用;强化DDL变更、扩缩容等运维能力,并兼容MySQL主备复制及MCP AI生态。
开源新发布|PolarDB-X v2.4.2开源生态适配升级
|
30天前
|
SQL 关系型数据库 MySQL
开源新发布|PolarDB-X v2.4.2开源生态适配升级
PolarDB-X v2.4.2发布,新增开源Proxy组件与客户端驱动,支持读写分离、无感高可用切换及DDL在线变更,兼容MySQL生态,提升千亿级大表运维稳定性。
482 24
开源新发布|PolarDB-X v2.4.2开源生态适配升级
|
4月前
|
存储 关系型数据库 分布式数据库
喜报|阿里云PolarDB数据库(分布式版)荣获国内首台(套)产品奖项
阿里云PolarDB数据库管理软件(分布式版)荣获「2024年度国内首版次软件」称号,并跻身《2024年度浙江省首台(套)推广应用典型案例》。
|
2月前
|
Cloud Native 关系型数据库 MySQL
免费体验!高效实现自建 MySQL 数据库平滑迁移至 PolarDB-X
PolarDB-X 是阿里云推出的云原生分布式数据库,支持PB级存储扩展、高并发访问与数据强一致,助力企业实现MySQL平滑迁移。现已开放免费体验,点击即享高效、稳定的数据库升级方案。
免费体验!高效实现自建 MySQL 数据库平滑迁移至 PolarDB-X
|
5月前
|
关系型数据库 分布式数据库 数据库
再获殊荣,阿里云PolarDB数据库蝉联SIGMOD最佳论文奖
内存池化技术新突破,阿里云PolarDB蝉联SIGMOD最佳论文奖
|
2月前
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。
|
3月前
|
人工智能 关系型数据库 MySQL
开源PolarDB-X:单节点误删除binlog恢复
本文由邵亚鹏撰写,分享了在使用开源PolarDB-X过程中,因误删binlog导致数据库服务无法启动的问题及恢复过程。作者结合实践经验,详细介绍了在无备份情况下如何通过单节点恢复机制重启数据库,并提出了避免类似问题的几点建议,包括采用高可用部署、定期备份及升级至最新版本等。
|
4月前
|
关系型数据库 分布式数据库 数据库
阿里云PolarDB数据库蝉联SIGMOD最佳论文奖
阿里云PolarDB凭借全球首创基于CXL Switch的分布式内存池技术,在SIGMOD 2025上荣获工业赛道“最佳论文奖”,连续两年蝉联该顶会最高奖项。其创新架构PolarCXLMem打破传统RDMA技术瓶颈,性能提升2.1倍,并已落地应用于内存池化场景,推动大模型推理与多模态存储发展,展现CXL Switch在高速互联中的巨大潜力。
阿里云PolarDB数据库蝉联SIGMOD最佳论文奖
|
5月前
|
Cloud Native 关系型数据库 分布式数据库
客户说|知乎基于阿里云PolarDB,实现最大数据库集群云原生升级
近日,知乎最大的风控业务数据库集群,基于阿里云瑶池数据库完成了云原生技术架构的升级。此次升级不仅显著提升了系统的高可用性和性能上限,还大幅降低了底层资源成本。
|
存储 关系型数据库 MySQL
开源PolarDB- X|替换Opengemini时序数据场景下产品力校验
本文作者:黄周霖,数据库技术专家,就职于南京北路智控股份有限公司,负责数据库运维及大数据开发。

相关产品

  • 云原生数据库 PolarDB