修改有风险,谨慎行事。
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) ./configure --with-blocksize=8--with-wal-blocksize=8--with-segsize=1 --with-wal-segsize=16
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) ./configure --with-blocksize=8--with-wal-blocksize=8--with-segsize=1
编译完成并初始化后使用pg_resetwal修改wal-segsize
#首先停止postgresql服务 pg_ctl -d /data/pgdata stop -m fast #修改wal-segsize段大小,默认单位是mb,值必须是2的幂并且在1到1024mb之间 pg_resetwal --wal-segsize=32 /data/pgdata;
查看
show block_size; show wal_block_size; show segment_size; show wal_segment_size; ls /data/pgdata/pg_wal -lh
补充:postgresql 11 新特性解读 : initdb/pg_resetwal支持修改wal文件大小
postgresql 11 版本的一个重要调整是支持 initdb 和 pg_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.
下面分别演示通过 initdb 和 pg_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 版本通过 initdb 和 pg_resetwal 调整wal文件大小。
pg_resetwal 会清除pg_wal目录的wal文件,本博客的测试样例仅供参考,生产环境使用需慎重。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。如有错误或未考虑完全的地方,望不吝赐教。