你需要 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 編輯過