WAL与WAL写进程----数据架构师的PostgreSQL修炼

来源:互联网 发布:网络打印机新添加纸张 编辑:程序博客网 时间:2024/05/16 10:47

0. WAL 写进程

[plain] view plain copy
  1. bash-3.2# ps -fu postgres |grep wal  
  2. 1165666791 57442 57436   0 12:28PM ??         0:00.02 postgres: wal writer process  

1.WAL

当修改数据的时候,变更内容不会立即写入数据文件中。

更改的内容以block为单位放在缓冲区,这些变更记录被写入WAL缓冲区;当commit时,这些变更会更新到WAL区段。

WAL放在pg_xlog目录下,默认每个WAL区段16MB。

[plain] view plain copy
  1. sherrywangs-MacBook-Pro:~ postgres$ ls -l /PostgreSQL/9.6/data/pg_xlog/  
  2. total 819200  
  3. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000001  
  4. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000002  
  5. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000003  
  6. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000004  
  7. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000005  
  8. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000006  
  9. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000007  
  10. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000008  
  11. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000009  
  12. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 00000001000000000000000A  
  13. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 00000001000000000000000B  
  14. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 00000001000000000000000C  
  15. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 00000001000000000000000D  
  16. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 00000001000000000000000E  
  17. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 00000001000000000000000F  
  18. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000010  
  19. -rw-------  1 postgres  daemon  16777216 Sep 11 21:36 000000010000000000000011  
  20. -rw-------  1 postgres  daemon  16777216 Sep 11 21:37 000000010000000000000012  
  21. -rw-------  1 postgres  daemon  16777216 Sep 11 21:37 000000010000000000000013  
  22. -rw-------  1 postgres  daemon  16777216 Sep 11 21:37 000000010000000000000014  
  23. -rw-------  1 postgres  daemon  16777216 Sep 11 21:37 000000010000000000000015  
  24. -rw-------  1 postgres  daemon  16777216 Sep 11 21:37 000000010000000000000016  
  25. -rw-------  1 postgres  daemon  16777216 Sep 11 21:37 000000010000000000000017  
  26. -rw-------  1 postgres  daemon  16777216 Sep 11 21:37 000000010000000000000018  
  27. -rw-------  1 postgres  daemon  16777216 Sep 11 21:37 000000010000000000000019  
  28. drwx------  2 postgres  daemon        68 Sep 10 20:42 archive_status  

[plain] view plain copy
  1. test=# SELECT pg_xlogfile_name(pg_current_xlog_location());  
  2.      pg_xlogfile_name  
  3. --------------------------  
  4.  000000010000000000000019  
  5. (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的关键参数:

[plain] view plain copy
  1. sherrywangs-MacBook-Pro:~ postgres$ grep wal /PostgreSQL/9.6/data/postgresql.conf  
  2. #wal_level = minimal            # minimal, replica, or logical  
  3. #wal_sync_method = fsync        # the default is the first option  
  4. #wal_compression = off          # enable compression of full-page writes  
  5. #wal_log_hints = off            # also do full page writes of non-critical updates  
  6. #wal_buffers = -1           # min 32kB, -1 sets based on shared_buffers  
  7. #wal_writer_delay = 200ms       # 1-10000 milliseconds  
  8. #wal_writer_flush_after = 1MB       # measured in pages, 0 disables  
  9. #max_wal_size = 1GB  
  10. #min_wal_size = 80MB  
  11. #max_wal_senders = 0        # max number of walsender processes  
  12. #wal_keep_segments = 0      # in logfile segments, 16MB each; 0 disables  
  13. #wal_sender_timeout = 60s   # in milliseconds; 0 disables  
  14. #wal_receiver_status_interval = 10s # send replies at least this often  
  15. #wal_receiver_timeout = 60s     # time that receiver waits for  
  16. #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的时间频率。

原创粉丝点击