PostgreSQL 11 新特性解读 : Initdb/Pg_resetwal支持修改WAL文件大小

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: PostgreSQL 11 版本的一个重要调整是支持 initdb 和 pg_resetwal 修改 WAL 文件大小,而 11 版本之前只能在编译安装 PostgreSQL 时设置 WAL 文件大小。

PostgreSQL 11 版本的一个重要调整是支持 initdbpg_resetwal 修改 WAL 文件大小,而 11 版本之前只能在编译安装 PostgreSQL 时设置 WAL 文件大小。这一特性能够方便 WAL 文件的管理。

Release 的说明

Allow the WAL file size to be set via initdb (Beena Emerson)
Previously the 16MB default could only be changed at compile time.

下面分别演示通过 initdbpg_resetwal 修改 WAL 文件大小。

使用 initdb 调整WAL文件大小

initdb 命令关于修改 WAL 文件大小选项,如下:

--wal-segsize=size

Set the WAL segment size, in megabytes. This is the size of each individual file in the WAL log. The default size is 16 megabytes. The value must be a power of 2 between 1 and 1024 (megabytes). This option can only be set during initialization, and cannot be changed later.
It may be useful to adjust this size to control the granularity of WAL log shipping or archiving. Also, in databases with a high volume of WAL, the sheer number of WAL files per directory can become a performance and management problem. Increasing the WAL file size will reduce the number of WAL files.

WAL 日志文件大小默认为16MB,该值必须是1到1024之间的2的次方,增大WAL文件大小能够减少WAL日志文件的产生。

初始化一个新的 PostgreSQL 数据库实例,指定WAL文件大小64MB,如下:

[pg11@pghost2 ~]$ initdb -E UTF8 --locale=C --wal-segsize=64 -D /home/pg11/data01 -U postgres -W
The files belonging to this database system will be owned by user "pg11".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

Enter new superuser password: 
Enter it again: 

creating directory /home/pg11/data01 ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /home/pg11/data01 -l logfile start

修改 postgresql.conf 相关配置,之后启动数据库。

[pg11@pghost2 data01]$ pg_ctl start -D /home/pg11/data01
waiting for server to start....2018-10-16 15:58:16.714 CST [10583] LOG:  listening on IPv6 address "::1", port 1950
2018-10-16 15:58:16.714 CST [10583] LOG:  listening on IPv4 address "127.0.0.1", port 1950
2018-10-16 15:58:16.731 CST [10583] LOG:  listening on Unix socket "/tmp/.s.PGSQL.1950"
2018-10-16 15:58:16.762 CST [10584] LOG:  database system was shut down at 2018-10-16 15:56:46 CST
2018-10-16 15:58:16.782 CST [10583] LOG:  database system is ready to accept connections
 done
server started

验证WAL文件大小,如下:

[pg11@pghost2 ~]$ ll /home/pg11/data01/pg_wal
total 65M
-rw------- 1 pg11 pg11  64M Oct 16 16:03 000000010000000000000001
drwx------ 2 pg11 pg11 4.0K Oct 16 15:56 archive_status

可见WAL文件大小为64MB。

使用 pg_resetwal 调整WAL文件大小

pg_resetwal 用来重置WAL日志和一些控制信息,常用于数据库恢复场景,不到万不得已不轻易使用,以下演示使用pg_resetwal命令调整WAL日志文件大小,仅供测试参考,生产环境慎用。

pg_resetwal 命令关于调整WAL文件大小的选项,如下:

--wal-segsize=wal_segment_size

Set the new WAL segment size, in megabytes. The value must be set to a power of 2 between 1 and 1024 (megabytes). See the same option of initdb for more information.

以下演示在已有PostgreSQL实例基础上调整WAL日志文件大小。

查看当前数据库的 pg_wal 目录,如下:

[pg11@pghost2 pg_wal]$ ll /database/pg11/pg_root/pg_wal/
total 2.3G
-rw------- 1 pg11 pg11 16M Sep 30 14:45 000000010000001700000013
-rw------- 1 pg11 pg11 16M Sep 30 14:45 000000010000001700000014
-rw------- 1 pg11 pg11 16M Sep 30 14:45 000000010000001700000015
-rw------- 1 pg11 pg11 16M Sep 30 14:45 000000010000001700000016
-rw------- 1 pg11 pg11 16M Sep 30 14:45 000000010000001700000017
-rw------- 1 pg11 pg11 16M Sep 30 14:45 000000010000001700000018
-rw------- 1 pg11 pg11 16M Sep 30 14:45 000000010000001700000019
-rw------- 1 pg11 pg11 16M Sep 30 14:45 00000001000000170000001A
-rw------- 1 pg11 pg11 16M Sep 30 14:45 00000001000000170000001B
...
省略
drwx------ 2 pg11 pg11 16K Oct 16 08:38 archive_status

pg_wal 目录中已有大量WAL日志文件,WAL文件大小为16MB,计划将WAL日志文件调整成64MB。

pg_resetwal 操作时需要关闭数据库,如下。

[pg11@pghost2 ~]$ pg_ctl stop -m fast
waiting for server to shut down.... done
server stopped

pg_resetwal 命令调整WAL日志文件大小为 64MB:

[pg11@pghost2 ~]$ pg_resetwal --wal-segsize=64 -D /database/pg11/pg_root
Write-ahead log reset

验证WAL文件大小,如下:

[pg11@pghost2 ~]$ ll /database/pg11/pg_root/pg_wal/
total 65M
-rw------- 1 pg11 pg11 64M Oct 16 08:55 000000010000001700000029
drwx------ 2 pg11 pg11 16K Oct 16 08:55 archive_status

发现 pg_wal 目录中原有的WAL日志被清理,同时生成了大小为64MB新的WAL文件。

启动数据库提示 min_wal_size 参数至少需设置成 wal_segment_size 大小为 2 倍。

[pg11@pghost2 ~]$ pg_ctl start
waiting for server to start....2018-10-16 09:01:26.096 CST [24318] FATAL:  "min_wal_size" must be at least twice "wal_segment_size".
2018-10-16 09:01:26.096 CST [24318] LOG:  database system is shut down
 stopped waiting
pg_ctl: could not start server
Examine the log output.

根据提示调整 postgresql.conf,设置如下:

min_wal_size = 128MB

启动数据库正常,如下:

[pg11@pghost2 ~]$ pg_ctl start
waiting for server to start....2018-10-16 09:02:45.680 CST [24614] LOG:  listening on IPv4 address "0.0.0.0", port 1930
2018-10-16 09:02:45.680 CST [24614] LOG:  listening on IPv6 address "::", port 1930
2018-10-16 09:02:45.687 CST [24614] LOG:  listening on Unix socket "/tmp/.s.PGSQL.1930"
2018-10-16 09:02:45.715 CST [24614] LOG:  redirecting log output to logging collector process
2018-10-16 09:02:45.715 CST [24614] HINT:  Future log output will appear in directory "log".

总结

  • 以上演示了 11 版本通过 initdbpg_resetwal 调整WAL文件大小。
  • pg_resetwal 会清除pg_wal目录的WAL文件,本博客的测试样例仅供参考,生产环境使用需慎重。

参考

新书推荐

最后推荐和张文升共同编写的《PostgreSQL实战》,本书基于PostgreSQL 10 编写,共18章,重点介绍SQL高级特性、并行查询、分区表、物理复制、逻辑复制、备份恢复、高可用、性能优化、PostGIS等,涵盖大量实战用例!

链接:https://item.jd.com/12405774.html

_5_PostgreSQL_

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
4月前
|
存储 Oracle 关系型数据库
postgresql数据库|wal日志的开启以及如何管理
postgresql数据库|wal日志的开启以及如何管理
247 0
|
9月前
|
存储 关系型数据库 数据库
探索PostgreSQL 14新特性--SEARCH和CYCLE
探索PostgreSQL 14新特性--SEARCH和CYCLE
50 0
|
10月前
|
Oracle 安全 关系型数据库
如何在openGauss/PostgreSQL手动清理XLOG/WAL 文件?
openGauss/PostgreSQL中的预写式日志WAL(Write Ahead Log),又名Xlog或redo log,相当于oracle的online redo log, 不同的是oracle online redo log是提前创建几组滚动使用,但在opengauss中只需要本配置参数控制WAL日志的周期,数据库会一直的创建并自动清理,但存在一些情况WAL日志未清理导致目录空间耗尽,或目录空间紧张时手动删除wal日志时,比如如何确认在非归档模式下哪些WAL日志文件可以安全删除?
560 0
|
7月前
|
关系型数据库 分布式数据库 数据库
polardb里面的wal流水线技术的优势是什么
polardb里面的wal流水线技术的优势是什么
54 1
|
9月前
|
存储 算法 安全
[翻译]PostgreSQL中的WAL压缩以及版本15中的改进
[翻译]PostgreSQL中的WAL压缩以及版本15中的改进
123 0
|
9月前
|
存储 缓存 关系型数据库
PostgreSQL 14新特性--减少索引膨胀
PostgreSQL 14新特性--减少索引膨胀
389 0
|
9月前
|
关系型数据库 PostgreSQL
PostgreSQL pg_resetwal处理机制
PostgreSQL pg_resetwal处理机制
82 0
|
9月前
|
关系型数据库 PostgreSQL
PostgreSQL崩溃恢复读取WAL
PostgreSQL崩溃恢复读取WAL
163 0
|
9月前
|
存储 固态存储 Ubuntu
postgresql email列表对NVM WAL BUFFER的讨论
postgresql email列表对NVM WAL BUFFER的讨论
44 0
|
9月前
|
存储 关系型数据库 PostgreSQL
PostgreSQL WAL解析:构建WAL记录准备
PostgreSQL WAL解析:构建WAL记录准备
95 0