0. WAL 写进程
- bash-3.2# ps -fu postgres |grep wal
- 1165666791 57442 57436 0 12:28PM ?? 0:00.02 postgres: wal writer process
1.WAL
当修改数据的时候,变更内容不会立即写入数据文件中。
更改的内容以block为单位放在缓冲区,这些变更记录被写入WAL缓冲区;当commit时,这些变更会更新到WAL区段。
WAL放在pg_xlog目录下,默认每个WAL区段16MB。
- sherrywangs-MacBook-Pro:~ postgres$ ls -l /PostgreSQL/9.6/data/pg_xlog/
- total 819200
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000001
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000002
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000003
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000004
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000005
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000006
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000007
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000008
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000009
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 00000001000000000000000A
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 00000001000000000000000B
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 00000001000000000000000C
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 00000001000000000000000D
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 00000001000000000000000E
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 00000001000000000000000F
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000010
- -rw------- 1 postgres daemon 16777216 Sep 11 21:36 000000010000000000000011
- -rw------- 1 postgres daemon 16777216 Sep 11 21:37 000000010000000000000012
- -rw------- 1 postgres daemon 16777216 Sep 11 21:37 000000010000000000000013
- -rw------- 1 postgres daemon 16777216 Sep 11 21:37 000000010000000000000014
- -rw------- 1 postgres daemon 16777216 Sep 11 21:37 000000010000000000000015
- -rw------- 1 postgres daemon 16777216 Sep 11 21:37 000000010000000000000016
- -rw------- 1 postgres daemon 16777216 Sep 11 21:37 000000010000000000000017
- -rw------- 1 postgres daemon 16777216 Sep 11 21:37 000000010000000000000018
- -rw------- 1 postgres daemon 16777216 Sep 11 21:37 000000010000000000000019
- drwx------ 2 postgres daemon 68 Sep 10 20:42 archive_status
- test=# SELECT pg_xlogfile_name(pg_current_xlog_location());
- pg_xlogfile_name
- --------------------------
- 000000010000000000000019
- (1 row)
第一个8位标识时间表;第二个8位标识逻辑xlog文件;
剩下的位标识物理xlog文件(区段)。
每个WAL包含若干8KB大小的block,默认WAL区段大小16MB。
WAL主要是被写入,很少被读取。WAL被读取存在以下几种,展开来说说。
2. WAL用于崩溃后的恢复
这是WAL的主要用途。
已经被commit,但是还未写入数据文件的数据在recovery时,需要依赖WAL。
变更在写入数据文件之前,必须先写入WAL文件。
3. WAL用于增量备份和时间点恢复
可以使用文件系统的snapshot功能,加上archive产生的WAL文件,达到基于时间点的恢复的目的。
具体会在后面的章节介绍。
4.复制
所有发生在服务器上的变更都被记录在WAL文件中,可以利用WAL复制到另外一台服务器,来做failover。
如果没有WAL,commit一个事务,会立即导致所有受事务影响的数据文件的写入操作,这样的操作在磁盘上可能是非常分散的,影响性能;
而使用了WAL,因为WAL是连续写入,保证在commit能够迅速返回,提高了性能,事后的checkpoint将真正的脏数据刷回磁盘,相当于一个batch操作,性能也会比较可观。
WAL的关键参数:
- sherrywangs-MacBook-Pro:~ postgres$ grep wal /PostgreSQL/9.6/data/postgresql.conf
- #wal_level = minimal # minimal, replica, or logical
- #wal_sync_method = fsync # the default is the first option
- #wal_compression = off # enable compression of full-page writes
- #wal_log_hints = off # also do full page writes of non-critical updates
- #wal_buffers = -1 # min 32kB, -1 sets based on shared_buffers
- #wal_writer_delay = 200ms # 1-10000 milliseconds
- #wal_writer_flush_after = 1MB # measured in pages, 0 disables
- #max_wal_size = 1GB
- #min_wal_size = 80MB
- #max_wal_senders = 0 # max number of walsender processes
- #wal_keep_segments = 0 # in logfile segments, 16MB each; 0 disables
- #wal_sender_timeout = 60s # in milliseconds; 0 disables
- #wal_receiver_status_interval = 10s # send replies at least this often
- #wal_receiver_timeout = 60s # time that receiver waits for
- #wal_retrieve_retry_interval = 5s # time to wait before retrying to
我做实验时用的是Mac OS下的PostgreSQL 9.5版本。所以配置项跟书中的有差异。
4.1 wal_level
minimal:仅仅写最少的信息,用于崩溃时恢复
replica:书中较老版本没提到,后续学习
logical:书中较老版本没提到,后续学习
4.2 fsync = on 和 wal_sync_method
该机制提供一个刷磁盘的选项,为了安全建议fsync = on一直开启,否则遇到异常断电/关机肯能导致数据丢失。
当fsync = on,wal_sync_method生效;当fsync = off, wal_sync_method完全不生效。
wal_sync_method可以从open_datasync,datasync (default on Linux),fsync,fsync_writethrough,open_sync选择,但是和OS平台相关。
4.3 wal_buffers
wal_buffers = -1,意味着该值从shared_buffers计算而来,具体需要后续学习。
4.3 wal_writer_delay
写WAL的时间频率。