LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

PostgreSQL 导出数据支持 LZ4 压缩,速率与空间灵活掌控

admin
2025年6月14日 18:41 本文热度 34

你需要 pg_dump 导出的数据文件更小吗?或者,你需要 pg_dump 更快速地导出数据吗?

沙滩上漫步的大象

特性提交日志

为 pg_dump 添加 LZ4 压缩功能。

扩展 pg_dump 的压缩流和文件 API 以支持 LZ4 算法。新添加的compress_lz4.{c,h}文件涵盖了上述 API 的所有功能。需要在多个pg_backup_*文件中进行细微修改,添加针对 'lz4' 文件后缀的代码,同时修改 pg_dump 压缩选项的处理逻辑。

讨论:https://postgr.es/m/faUNEOpts9vunEaLnmxmG-DldLSg_ql137OC3JYDmgrOMHm1RvvWY2IdBkv_CRxm5spCCb_OmKNk2T03TMm0fBEWveFF9wA1WizPuAgB7Ss%3D%40protonmail.com

示例

PostgreSQL 的最新版本已经引入了新的压缩选项,并为 pg_dump 提供了 LZ4 压缩。下面,让我们来进行一次简单的演示。

为了能导出一些内容,我们将使用 pgbench 创建一个小型数据库:

psql -c "create database dump"

pgbench -i -s 100 dump
dropping old tables...
creating tables...
generating data (client-side)...
10000000 of 10000000 tuples (100%) done (elapsed 28.91 s, remaining 0.00 s)
vacuuming...
creating primary keys...
done in 36.32 s (drop tables 0.00 s, create tables 0.02 s, client-side generate 29.17 s, vacuum 0.57 s, primary keys 6.56 s).

让我们使用传统方式,使用 gzip 最高压缩级别来转储它:

mkdir /var/tmp/dump
time pg_dump --format=d --compress=gzip:9 --file=/var/tmp/dump/gzip dump
real    0m12.332s
user    0m7.687s
sys     0m0.271s

ls -lha /var/tmp/dump/gzip/
total 27M
drwx------ 2 postgres postgres 4.0K May 24 09:02 .
drwxr-xr-x 3 postgres postgres 4.0K May 24 09:02 ..
-rw-r--r-- 1 postgres postgres   25 May 24 09:02 3347.dat.gz
-rw-r--r-- 1 postgres postgres 1.8K May 24 09:02 3348.dat.gz
-rw-r--r-- 1 postgres postgres  27M May 24 09:02 3349.dat.gz
-rw-r--r-- 1 postgres postgres  208 May 24 09:02 3350.dat.gz
-rw-r--r-- 1 postgres postgres 4.0K May 24 09:02 toc.dat

这大约花费了 12 秒,结果大约为 27MB。让我们使用 LZ4 执行相同的操作:

time pg_dump --format=d --compress=lz4:9 --file=/var/tmp/dump/lz4 dump
real    0m10.803s
user    0m6.246s
sys     0m0.271s

ls -lha /var/tmp/dump/lz4
total 48M
drwx------ 2 postgres postgres 4.0K May 24 09:06 .
drwxr-xr-x 4 postgres postgres 4.0K May 24 09:06 ..
-rw-r--r-- 1 postgres postgres   20 May 24 09:06 3347.dat.lz4
-rw-r--r-- 1 postgres postgres 4.4K May 24 09:06 3348.dat.lz4
-rw-r--r-- 1 postgres postgres  48M May 24 09:06 3349.dat.lz4
-rw-r--r-- 1 postgres postgres  415 May 24 09:06 3350.dat.lz4
-rw-r--r-- 1 postgres postgres 4.0K May 24 09:06 toc.dat

这样速度会快一点,但也占用了更多空间。使用较低级别会怎么样呢?

rm -rf /var/tmp/dump/lz4
rm -rf /var/tmp/dump/gzip/

time pg_dump --format=d --compress=gzip:2 --file=/var/tmp/dump/gzip dump
real    0m7.819s
user    0m3.191s
sys     0m0.203s

time pg_dump --format=d --compress=lz4:2 --file=/var/tmp/dump/lz4 dump
real    0m5.426s
user    0m0.906s
sys     0m0.236s

ls -lha /var/tmp/dump/gzip
total 28M
drwx------ 2 postgres postgres 4.0K May 24 09:10 .
drwxr-xr-x 4 postgres postgres 4.0K May 24 09:10 ..
-rw-r--r-- 1 postgres postgres   25 May 24 09:10 3347.dat.gz
-rw-r--r-- 1 postgres postgres 2.3K May 24 09:10 3348.dat.gz
-rw-r--r-- 1 postgres postgres  28M May 24 09:10 3349.dat.gz
-rw-r--r-- 1 postgres postgres  206 May 24 09:10 3350.dat.gz
-rw-r--r-- 1 postgres postgres 4.0K May 24 09:10 toc.dat

ls -lha /var/tmp/dump/lz4/
total 50M
drwx------ 2 postgres postgres 4.0K May 24 09:10 .
drwxr-xr-x 4 postgres postgres 4.0K May 24 09:10 ..
-rw-r--r-- 1 postgres postgres   20 May 24 09:10 3347.dat.lz4
-rw-r--r-- 1 postgres postgres 4.5K May 24 09:10 3348.dat.lz4
-rw-r--r-- 1 postgres postgres  50M May 24 09:10 3349.dat.lz4
-rw-r--r-- 1 postgres postgres  426 May 24 09:10 3350.dat.lz4
-rw-r--r-- 1 postgres postgres 4.0K May 24 09:10 toc.dat

同样的情况:gzip 压缩效果更好,但 LZ4 压缩速度更快。现在你可以自由选择了。

非常不错的特性。感谢社区的所有相关人员。

参考

提交日志:https://git.postgresql.org/pg/commitdiff/0da243fed0875932f781aff08df782b56af58d02


阅读原文:https://mp.weixin.qq.com/s/KggAR26SU_z1NujRX9Wqmw


该文章在 2025/6/16 9:03:33 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved