标签

PostgreSQL , redo 在线压缩 , wal 在线压缩 , 接收端压缩 , pg_receivexlog , pg_basebackup , 断点续传


背景

虽然现在磁盘已经很廉价,大多数时候不需要压缩了。

但是在一些嵌入式系统,或者一些未扩容的产品环境中,压缩还是非常有必要的。

特别是数据变化大、或者数据增量大的场景,例如物联网(IoT),每天单库可能新增TB级的增量。

比如 《PostgreSQL 如何潇洒的处理每天上百TB的数据增量》 指的就是IoT场景。

这样的场景,数据库的归档日志也会很大,这个时候就体现压缩的重要性了。

CPU和网络带宽的权衡选择

压缩势必会增加CPU的开销,我们可以选择是在数据库的服务端压缩,还是在客户端压缩。

在数据库的服务端压缩,增加的是数据库服务器的CPU运算开销,好处是网络传输的数据变少了。

而在客户端压缩的话,对数据库服务器没有额外的CPU需求,但是网络传输的数据量则是压缩前的量。

用户可以根据实际情况选择。

流式接收与在线压缩的结合

PostgreSQL很早以前的版本就提供了一种流式接收redo的方法,通过在客户端使用pg_receivexlog命令,与普通应用程序一样,连接PostgreSQL数据库,可以通过PG的流式协议实时接收来自数据库的REDO。

另一个命令是pg_basebackup,可以流式接收数据库的所有文件(包括数据文件,REDO等),常被用于备份。

10.0对pg_basebackup也有一个改进,请参考:

《元旦技术大礼包 - 2017金秋将要发布的PostgreSQL 10.0已装备了哪些核武器?》

流式接收与在线压缩结合,解决了IoT场景REDO归档日志体积庞大的问题。

pg_receivexlog支持通过开关控制是否需要开启压缩、以及选择压缩级别。

pg_receivexlog启动时,自动扫描存放归档文件的目标目录,选择断点续传的位置,然后向PostgreSQL数据库请求相应位置为起点的REDO。

pg_receivexlog开启压缩后,依旧支持同步反馈模式,所以它依旧可以作为一个同步副本。

关于PostgreSQL数据库的多副本同步,以及应用场景请参考

《PostgreSQL 9.6 同步多副本 与 remote_apply事务同步级别》

《PostgreSQL 金融行业高可用和容灾解决方案》

《元旦技术大礼包 - 2017金秋将要发布的PostgreSQL 10.0已装备了哪些核武器?》

正文

http://paquier.xyz/postgresql-2/postgres-10-pgreceivexlog-compression/

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

One of my personal areas of work lately is in finding ways to improve the user experience with WAL archiving and pg_receivexlog.

A couple of experiments have been done, and one of them has finished as a patch for upstream Postgres, in the shape of this commit:

commit: cada1af31d769a6b607018d68894f2c879ff275f
author: Magnus Hagander <magnus@hagander.net>
date: Tue, 17 Jan 2017 12:10:26 +0100
Add compression support to pg_receivexlog  Author: Michael Paquier, review and small changes by me

Combined with replication slots, pg_receivexlog is a nice way to ensure that there is no hole in WAL segments. Compared to the archive_command itself, any failure handling in case of successive failures in archiving a completed segment is easier as there is no need to tweak the parameter of the archive_command or the script used in the command itself to avoid a bloat in pg_xlog, resulting in a crash of Postgres if the partition holding this folder gets full. Any failure handling can happen from a remote position, and there is no need to have a superuser to do this work, only a user with replication rights is enough to drop a slot and unlock the situation. Note though that enforcing the recycling of past segments requires a checkpoint to happen.

The commit above has added a way to compression on-the-fly with zlib WAL records and to store them in .gz files, one for each segment. In those days where disk is cheaper than CPU, compression is not a big deal for many users and they are fine to afford more space to store the same amount of history. However, in cases where Postgres is embedded in a system and the amount of space allowed is controlled it may be a big deal to be able to retain more history using the same amount of space, particularly knowing that a WAL segment compressed with zlib is 3 to 4 times smaller.

The compression option can be activated with a new option switch called --compress, with which can be specified a number from 0 to 9, 0 meaning no compression and 9 the highest level of compression. Note that level 9 is a huge CPU eater and that in an INSERT-only load the compression of each segment may not be able to follow with the WAL generation, resulting in pg_receivexlog complaining that a segment it is requesting has already been removed by a backend checkpoint or, if a replication slot is used, resulting in a crash of the Postgres instance because of pg_xlog getting full.

$ pg_receivexlog --compress=1 -D /path/to/logs/ --verbose   pg_receivexlog: starting log streaming at 0/1000000 (timeline 1)
pg_receivexlog: finished segment at 0/2000000 (timeline 1)
pg_receivexlog: finished segment at 0/3000000 (timeline 1)
[...]

And this generates many gzip-ready files.

$ ls /path/to/logs/   000000010000000000000001.gz
000000010000000000000002.gz
[...]
000000010000000000000027.gz.partial

--synchronous works as well with the compression support and makes sure that the compressed files, even if not completed segments, are still available. Backup and history files are compressed as well.

Another thing to note is that at startup phase, pg_receivexlog scans the directory it writes the WAL data into for existing segments that are on it and decides based on that from which position it needs to continue working on. The committed patch is smart enough to make a difference between compressed, non-compressed, and even partial segments so it is perfectly fine to mix compression or not and keep the same range of segments saved.

参考

http://paquier.xyz/postgresql-2/postgres-10-pgreceivexlog-compression/

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

《元旦技术大礼包 - 2017金秋将要发布的PostgreSQL 10.0已装备了哪些核武器?》

《PostgreSQL 9.6 同步多副本 与 remote_apply事务同步级别》

《PostgreSQL 金融行业高可用和容灾解决方案》

PostgreSQL 10 新特性, 流式接收端在线压缩redo相关推荐

  1. JDK8 新特性流式数据处理

    在学习JDK8新特性Optional类的时候,提到对于Optional的两个操作映射和过滤设计到JDK提供的流式出来.这篇文章便详细的介绍流式处理: 一. 流式处理简介 流式处理给开发者的第一感觉就是 ...

  2. JDK 5 ~ 10 新特性倾情整理

    转载自 JDK 5 ~ 10 新特性倾情整理 最近连 JDK11都在准备发布的路上了,大家都整明白了吗?也许现在大部分人还在用6-8,8的新特性都没用熟,9刚出不久,10-11就不用说了. 为了大家对 ...

  3. C# 10 新特性 —— Lambda 优化

    C# 10 新特性 -- Lambda 优化 Intro C# 10 对于 Lambda 做了很多的优化,我们可以在 C# 中更加方便地使用委托和 Lambda 了,下面就来看一些示例 Lambda ...

  4. C# 10 新特性 —— CallerArgumentExpression

    C# 10 新特性 -- CallerArgumentExpression Intro C# 10 支持使用 CallerArgumentExpression 来自动地获取调用方的信息,这可以简化我们 ...

  5. C# 10 新特性 —— 命名空间的变化

    C# 10 新特性 -- 命名空间的变化 Intro C# 10 针对命名空间做了一些改变,主要是 Global Usings 和 File-scoped Namespace,我们前面分享的示例其实也 ...

  6. JavaScript高级之ECMASript 7、8 、9 、10 新特性

    第3章 ECMASript 7 新特性 3.1. Array.prototype.includes Includes 方法用来检测数组中是否包含某个元素,返回布尔类型值 3.2. 指数操作符 在ES7 ...

  7. Java 10新特性

    Java 10新特性 Java 10是其23年历史中最快的java版本.Java因其缓慢的增长和发展而受到批评,但Java 10刚刚破坏了这一概念.Java 10是一个具有许多未来变化的版本,其范围和 ...

  8. PostgreSQL 11 新特性之 PL/pgSQL 增强

    文章目录 PostgreSQL 11 增加了一个新的编程对象,存储过程(PROCEDURE).它与存储函数类似,但是没有返回值.存储过程还支持事务,参考文章"PostgreSQL 11 新特 ...

  9. 探索PostgreSQL 14新特性--SEARCH和CYCLE

    探索PostgreSQL 14新特性--SEARCH和CYCLE PG14的SEARCH和CYCLE新功能大大简化了递归查询的方式,本文给出一些基于旅行计划的示例. 创建数据库 本文示例基于任何PG1 ...

  10. Java 10 新特性概述

    Java 10是其23年历史中最快发布的java版本.Java因其缓慢的增长和发展而受到批评,但Java 10刚刚破坏了这个概念.Java 10是一个具有许多未来变化的版本,其范围和影响可能并不明显, ...

最新文章

  1. 再谈工作的主动性和有效提问
  2. AI之父图灵登上50英镑钞票,荣耀比肩牛顿达尔文;吴恩达:将激励更多人
  3. 寒假每日一题(入门组)【week1 完结】
  4. 更换ip地址_手机怎么改ip地址
  5. 四、Git多人开发:不同人修改了同文件的相同区域如何处理?
  6. Pytorch 自定义激活函数前向与反向传播 Tanh
  7. python判断字符串里的字符_python 判断检测字符串中是否包含指定字符或字符串(比如:?)...
  8. php正则匹配域名不包含端口_3分钟短文 | PHP极速匹配子字符串,你是怎么做的?...
  9. 钩子怎么画_画男生校服有什么技巧?该注意什么?
  10. 在计算机操作系统中操作系统是处于应用软件,计算机操作系统应用试题与答案.pdf...
  11. java去0,Java如何处理除零?
  12. 向上传递 java_Java向上转型向下转型
  13. android虚拟机改变sim,逍遥安卓模拟器修改手机型号的方法
  14. erp系统软件php,SMALL-ERP 一个用PHP写的小型ERP系统,麻雀虽小,五脏俱全。 ERP-EIP-OA-Portal 企业管理 271万源代码下载- www.pudn.com...
  15. php调用微信公众号支付接口,Thinkphp实现微信公众号支付接口
  16. NDB Cluster基本操作
  17. HDU 6438 Buy and Resell
  18. 语音合成(TTS)论文优选:Forward Attention in Sequence- To-Sequence Acoustic Modeling for Speech Synthesis
  19. 解决SQL Server占用服务器内存过高问题
  20. bilibili缓存文件在哪里_别再盲目清理了,手机删除这3个文件夹,能瞬间清出十几G垃圾...

热门文章

  1. 谈谈学完Asp.net 中的自定义控件后的感受
  2. [转]c# winform tcp connect timeout 连接超时设置
  3. 双11背后的黑科技:大数据实时计算如何为你量身定制?
  4. spring-boot-starter-thymeleaf对没有结束符的HTML5标签解析出错
  5. TCL语言笔记:TCL过程控制练习
  6. 什么是商业智能BI和实施BI的解决方案【转】
  7. 为什么「margin:auto」可以让块级元素水平居中?
  8. XCL-Charts图表库中柱形图的同源风格切换介绍
  9. 搭建 SQL Server 复制 (一)
  10. 【炼数成金 RapidMiner 三 】关联分析、关联规则