合 PostgreSQL11修改--wal-segsize大小
Tags: PGPostgreSQL整理自网络体系结构wal日志wal-segsize
简介
编译安装PG的一些参数可以参考:https://www.dbaup.com/dbbao68shiyongyuanmabianyilaianzhuangpostgresql13-3shujuku.html
PostgreSQL11以前改变wal-segsize(WAL段大小)必须在编译时通过–with-wal-segsize参数确定,编译后不可更改。
PostgreSQL11之前的版本
- 数据块按8K划分(--with-blocksize,单位为K,默认为8K)
- WAL块尺寸按8k划分(--with-wal-blocksize,单位为K,默认为8K)
- 数据文件按1G一个分解(--with-segsize,单位为G,默认为1G)
- WAL段尺寸按16MB划分(--with-wal-segsize,单位为K,默认为16MB)
1 ./configure --with-blocksize=8 --with-wal-blocksize=8 --with-segsize=1 --with-wal-segsize=16
PostgreSQL 11 版本的一个重要调整是支持 initdb 和 pg_resetwal 修改 WAL 文件大小,而 11 版本之前只能在编译安装 PostgreSQL 时设置 WAL 文件大小。这一特性能够方便 WAL 文件的管理。PostgreSQL11取消了编译参数–with-wal-segsize。
使用pg_resetwal(--wal-segsize)更改WAL段大小
PostgreSQL11先编译程序
- 数据块按8K划分(--with-blocksize,单位为K,默认为8K)
- WAL块尺寸按8k划分(--with-wal-blocksize,单位为K,默认为8K)
- 数据文件按1G一个分解(--with-segsize,单位为G,默认为1G)
1 ./configure --with-blocksize=8 --with-wal-blocksize=8 --with-segsize=1
编译完成并初始化后使用pg_resetwal修改wal-segsize
1 2 3 4 5 6 | #首先停止PostgreSQL服务 pg_ctl -D /data/pgdata stop -m fast #修改wal-segsize段大小,默认单位是MB,值必须是2的幂并且在1到1024MB之间 pg_resetwal --wal-segsize=32 /data/pgdata |
使用initdb时更改WAL段大小
1 | initdb --wal-segsize =< MB中的大小> [其他选项] |
initdb的参数--wal-segsize参数的取值范围是1MB到1024MB
查看
1 2 3 4 5 6 7 | show block_size; show wal_block_size; show segment_size; show wal_segment_size; pg_controldata |grep -i wal ls /data/pgdata/pg_wal -lh |
1 2 3 4 5 6 7 | [pg1110@lhr ~]$ pg_controldata |grep -i wal Latest checkpoint's REDO WAL file: 000000010000000000000038 wal_level setting: replica wal_log_hints setting: off WAL block size: 8192 Bytes per WAL segment: 16777216 ----->>>>此处说明wal segment的大小是16MB |
示例
使用 initdb 调整WAL文件大小
1 2 3 4 5 6 7 8 9 10 11 12 | [pg1110@lhr ~]$ initdb --help|grep -C 4 wal-seg default text search configuration -U, --username=NAME database superuser name -W, --pwprompt prompt for a password for the new superuser -X, --waldir=WALDIR location for the write-ahead log directory --wal-segsize=SIZE size of WAL segments, in megabytes ----->>>此处!!! Less commonly used options: -d, --debug generate lots of debugging output -k, --data-checksums use data page checksums [pg1110@lhr ~]$ |
initdb 命令关于修改 WAL 文件大小选项,如下:
1 | --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,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | [pg11@lhrpg ~]$ 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 相关配置,之后启动数据库。
1 2 3 4 5 6 7 8 | [pg11@lhrpg 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文件大小,如下:
1 2 3 4 | [pg11@lhrpg ~]$ 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文件大小的选项,如下:
1 | --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 目录,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [pg11@lhrpg 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 操作时需要关闭数据库,如下。
1 2 3 | [pg11@lhrpg ~]$ pg_ctl stop -m fast waiting for server to shut down.... done server stopped |
pg_resetwal
命令调整WAL日志文件大小为 64MB:
1 2 3 | [pg11@lhrpg ~]$ pg_resetwal --wal-segsize=64 -D /database/pg11/pg_root Write-ahead log reset |
验证WAL文件大小,如下:
1 2 3 4 | [pg11@lhrpg ~]$ 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文件。