天天看點

PostgreSQL 11 新特性解讀 : Initdb/Pg_resetwal支援修改WAL檔案大小Release 的說明使用 initdb 調整WAL檔案大小使用 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檔案,本部落格的測試樣例僅供參考,生産環境使用需慎重。

參考

新書推薦

最後推薦和張文升共同編寫的《PostgreSQL實戰》,本書基于PostgreSQL 10 編寫,共18章,重點介紹SQL進階特性、并行查詢、分區表、實體複制、邏輯複制、備份恢複、高可用、性能優化、PostGIS等,涵蓋大量實戰用例!

連結:

https://item.jd.com/12405774.html
PostgreSQL 11 新特性解讀 : Initdb/Pg_resetwal支援修改WAL檔案大小Release 的說明使用 initdb 調整WAL檔案大小使用 pg_resetwal 調整WAL檔案大小總結參考新書推薦