{"Entry":{"collection":"storage","key":"wal-internals","name":"WAL Internals","aliases":[],"metadata":{"aliases":[],"category":"Physical structures","content_hash":"b4c1bf3f3df56edb5a2887edb2951954e9a7a700ca9e455fa49af5c29b889e22","imported_at":"2026-09-30T00:40:47.121215+08:00","name":"WAL Internals","name_zh":"","slug":"wal-internals","summary":"WAL is automatically enabled; no action is required from the administrator except ensuring that the disk-space requirements for the WAL files are met, and that any necessary tuning is done (see Section 28.5 )."}},"Definition":{"Collection":"storage","Key":"wal-internals","SourceDatabase":"center","Version":"18","SourceTable":"storage_structure","SourceKey":"wal-internals","SourceRevision":"555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f","Facts":{"comparison_data":{"layouts":[]},"comparison_hash":"27341929154bfb55531f03501322b65af719c5561f3c4ddc9528a14aa4df69b5","description":["WAL is automatically enabled; no action is required from the administrator except ensuring that the disk-space requirements for the WAL files are met, and that any necessary tuning is done (see Section 28.5 )."],"facts":[{"label":"Definition scope","value":"Same-version core physical storage documentation"}],"manual_html":"\u003cdiv class=\"sect1\"\u003e\n\u003cdiv class=\"titlepage\"\u003e\n\u003cdiv\u003e\n\u003cdiv\u003e\n\u003ch2 class=\"title\"\u003e28.6. WAL Internals \u003c/h2\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cp\u003eWAL is automatically enabled; no action is required from the administrator except ensuring that the disk-space requirements for the WAL files are met, and that any necessary tuning is done (see \u003ca class=\"xref\" href=\"/docs/18/wal-configuration.html\" title=\"28.5. WAL Configuration\"\u003eSection 28.5\u003c/a\u003e).\u003c/p\u003e\n\u003cp\u003eWAL records are appended to the WAL files as each new record is written. The insert position is described by a Log Sequence Number (LSN) that is a byte offset into the WAL, increasing monotonically with each new record. LSN values are returned as the datatype \u003ca class=\"link\" href=\"/docs/18/datatype-pg-lsn.html\" title=\"8.20. pg_lsn Type\"\u003e\u003ccode class=\"type\"\u003epg_lsn\u003c/code\u003e\u003c/a\u003e. Values can be compared to calculate the volume of WAL data that separates them, so they are used to measure the progress of replication and recovery.\u003c/p\u003e\n\u003cp\u003eWAL files are stored in the directory \u003ccode class=\"filename\"\u003epg_wal\u003c/code\u003e under the data directory, as a set of segment files, normally each 16 MB in size (but the size can be changed by altering the \u003ccode class=\"option\"\u003e--wal-segsize\u003c/code\u003e \u003cspan class=\"application\"\u003einitdb\u003c/span\u003e option). Each segment is divided into pages, normally 8 kB each (this size can be changed via the \u003ccode class=\"option\"\u003e--with-wal-blocksize\u003c/code\u003e configure option). The WAL record headers are described in \u003ccode class=\"filename\"\u003eaccess/xlogrecord.h\u003c/code\u003e; the record content is dependent on the type of event that is being logged. Segment files are given ever-increasing numbers as names, starting at \u003ccode class=\"filename\"\u003e000000010000000000000001\u003c/code\u003e. The numbers do not wrap, but it will take a very, very long time to exhaust the available stock of numbers.\u003c/p\u003e\n\u003cp\u003eIt is advantageous if the WAL is located on a different disk from the main database files. This can be achieved by moving the \u003ccode class=\"filename\"\u003epg_wal\u003c/code\u003e directory to another location (while the server is shut down, of course) and creating a symbolic link from the original location in the main data directory to the new location.\u003c/p\u003e\n\u003cp\u003eThe aim of WAL is to ensure that the log is written before database records are altered, but this can be subverted by disk drives that falsely report a successful write to the kernel, when in fact they have only cached the data and not yet stored it on the disk. A power failure in such a situation might lead to irrecoverable data corruption. Administrators should try to ensure that disks holding \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e's WAL files do not make such false reports. (See \u003ca class=\"xref\" href=\"/docs/18/wal-reliability.html\" title=\"28.1. Reliability\"\u003eSection 28.1\u003c/a\u003e.)\u003c/p\u003e\n\u003cp\u003eAfter a checkpoint has been made and the WAL flushed, the checkpoint's position is saved in the file \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e. Therefore, at the start of recovery, the server first reads \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e and then the checkpoint record; then it performs the REDO operation by scanning forward from the WAL location indicated in the checkpoint record. Because the entire content of data pages is saved in the WAL on the first page modification after a checkpoint (assuming \u003ca class=\"xref\" href=\"/docs/18/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\"\u003efull_page_writes\u003c/a\u003e is not disabled), all pages changed since the checkpoint will be restored to a consistent state.\u003c/p\u003e\n\u003cp\u003eTo deal with the case where \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e is corrupt, we should support the possibility of scanning existing WAL segments in reverse order — newest to oldest — in order to find the latest checkpoint. This has not been implemented yet. \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e is small enough (less than one disk page) that it is not subject to partial-write problems, and as of this writing there have been no reports of database failures due solely to the inability to read \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e itself. So while it is theoretically a weak spot, \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e does not seem to be a problem in practice.\u003c/p\u003e\n\u003c/div\u003e","manual_path":"/docs/18/wal-internals.html","related":[{"label":"Table AM","url":"/wiki/tableam/?v=18"},{"label":"Storage Parameters","url":"/wiki/relopts/?v=18"}],"release":{"channel":"stable","label":"18.6","major":"18","ref":"https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2","revision":"555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f"},"sections":[],"signature":"","sources":[{"label":"PostgreSQL 18 English manual","path":"wal-internals.html","sha256":"81c125b1657049ba0b146e15b1decb82aa524ac54df1add0b07e154d95f3f300","url":"/docs/18/wal-internals.html"}],"tables":[]},"ManualEvidence":{"manual_path":"/docs/18/wal-internals.html","release":{"channel":"stable","label":"18.6","major":"18","ref":"https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2","revision":"555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f"},"sources":[{"label":"PostgreSQL 18 English manual","path":"wal-internals.html","sha256":"81c125b1657049ba0b146e15b1decb82aa524ac54df1add0b07e154d95f3f300","url":"/docs/18/wal-internals.html"}]},"MeasuredEvidence":{}},"Text":{"Collection":"storage","Key":"wal-internals","SourceDatabase":"center","Version":"18","Locale":"en","Title":"WAL Internals","Summary":"WAL is automatically enabled; no action is required from the administrator except ensuring that the disk-space requirements for the WAL files are met, and that any necessary tuning is done (see Section 28.5 ).","BodyHTML":"\u003cdiv\u003e\n\u003cdiv\u003e\n\u003cdiv\u003e\n\u003cdiv\u003e\n\u003ch2\u003e28.6. WAL Internals \u003c/h2\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cp\u003eWAL is automatically enabled; no action is required from the administrator except ensuring that the disk-space requirements for the WAL files are met, and that any necessary tuning is done (see \u003ca href=\"/docs/18/wal-configuration.html\" rel=\"nofollow\"\u003eSection 28.5\u003c/a\u003e).\u003c/p\u003e\n\u003cp\u003eWAL records are appended to the WAL files as each new record is written. The insert position is described by a Log Sequence Number (LSN) that is a byte offset into the WAL, increasing monotonically with each new record. LSN values are returned as the datatype \u003ca href=\"/docs/18/datatype-pg-lsn.html\" rel=\"nofollow\"\u003e\u003ccode\u003epg_lsn\u003c/code\u003e\u003c/a\u003e. Values can be compared to calculate the volume of WAL data that separates them, so they are used to measure the progress of replication and recovery.\u003c/p\u003e\n\u003cp\u003eWAL files are stored in the directory \u003ccode\u003epg_wal\u003c/code\u003e under the data directory, as a set of segment files, normally each 16 MB in size (but the size can be changed by altering the \u003ccode\u003e--wal-segsize\u003c/code\u003e \u003cspan\u003einitdb\u003c/span\u003e option). Each segment is divided into pages, normally 8 kB each (this size can be changed via the \u003ccode\u003e--with-wal-blocksize\u003c/code\u003e configure option). The WAL record headers are described in \u003ccode\u003eaccess/xlogrecord.h\u003c/code\u003e; the record content is dependent on the type of event that is being logged. Segment files are given ever-increasing numbers as names, starting at \u003ccode\u003e000000010000000000000001\u003c/code\u003e. The numbers do not wrap, but it will take a very, very long time to exhaust the available stock of numbers.\u003c/p\u003e\n\u003cp\u003eIt is advantageous if the WAL is located on a different disk from the main database files. This can be achieved by moving the \u003ccode\u003epg_wal\u003c/code\u003e directory to another location (while the server is shut down, of course) and creating a symbolic link from the original location in the main data directory to the new location.\u003c/p\u003e\n\u003cp\u003eThe aim of WAL is to ensure that the log is written before database records are altered, but this can be subverted by disk drives that falsely report a successful write to the kernel, when in fact they have only cached the data and not yet stored it on the disk. A power failure in such a situation might lead to irrecoverable data corruption. Administrators should try to ensure that disks holding \u003cspan\u003ePostgreSQL\u003c/span\u003e\u0026#39;s WAL files do not make such false reports. (See \u003ca href=\"/docs/18/wal-reliability.html\" rel=\"nofollow\"\u003eSection 28.1\u003c/a\u003e.)\u003c/p\u003e\n\u003cp\u003eAfter a checkpoint has been made and the WAL flushed, the checkpoint\u0026#39;s position is saved in the file \u003ccode\u003epg_control\u003c/code\u003e. Therefore, at the start of recovery, the server first reads \u003ccode\u003epg_control\u003c/code\u003e and then the checkpoint record; then it performs the REDO operation by scanning forward from the WAL location indicated in the checkpoint record. Because the entire content of data pages is saved in the WAL on the first page modification after a checkpoint (assuming \u003ca href=\"/docs/18/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\" rel=\"nofollow\"\u003efull_page_writes\u003c/a\u003e is not disabled), all pages changed since the checkpoint will be restored to a consistent state.\u003c/p\u003e\n\u003cp\u003eTo deal with the case where \u003ccode\u003epg_control\u003c/code\u003e is corrupt, we should support the possibility of scanning existing WAL segments in reverse order — newest to oldest — in order to find the latest checkpoint. This has not been implemented yet. \u003ccode\u003epg_control\u003c/code\u003e is small enough (less than one disk page) that it is not subject to partial-write problems, and as of this writing there have been no reports of database failures due solely to the inability to read \u003ccode\u003epg_control\u003c/code\u003e itself. So while it is theoretically a weak spot, \u003ccode\u003epg_control\u003c/code\u003e does not seem to be a problem in practice.\u003c/p\u003e\n\u003c/div\u003e","SourceRevision":"555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f","ContentHash":"298696d6cee55d34b97c3f49f8693731a605ca843a13691a3c8a1c30dc961f81","Payload":{"description":["WAL is automatically enabled; no action is required from the administrator except ensuring that the disk-space requirements for the WAL files are met, and that any necessary tuning is done (see Section 28.5 )."],"manual_html":"\u003cdiv class=\"sect1\"\u003e\n\u003cdiv class=\"titlepage\"\u003e\n\u003cdiv\u003e\n\u003cdiv\u003e\n\u003ch2 class=\"title\"\u003e28.6. WAL Internals \u003c/h2\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cp\u003eWAL is automatically enabled; no action is required from the administrator except ensuring that the disk-space requirements for the WAL files are met, and that any necessary tuning is done (see \u003ca class=\"xref\" href=\"/docs/18/wal-configuration.html\" title=\"28.5. WAL Configuration\"\u003eSection 28.5\u003c/a\u003e).\u003c/p\u003e\n\u003cp\u003eWAL records are appended to the WAL files as each new record is written. The insert position is described by a Log Sequence Number (LSN) that is a byte offset into the WAL, increasing monotonically with each new record. LSN values are returned as the datatype \u003ca class=\"link\" href=\"/docs/18/datatype-pg-lsn.html\" title=\"8.20. pg_lsn Type\"\u003e\u003ccode class=\"type\"\u003epg_lsn\u003c/code\u003e\u003c/a\u003e. Values can be compared to calculate the volume of WAL data that separates them, so they are used to measure the progress of replication and recovery.\u003c/p\u003e\n\u003cp\u003eWAL files are stored in the directory \u003ccode class=\"filename\"\u003epg_wal\u003c/code\u003e under the data directory, as a set of segment files, normally each 16 MB in size (but the size can be changed by altering the \u003ccode class=\"option\"\u003e--wal-segsize\u003c/code\u003e \u003cspan class=\"application\"\u003einitdb\u003c/span\u003e option). Each segment is divided into pages, normally 8 kB each (this size can be changed via the \u003ccode class=\"option\"\u003e--with-wal-blocksize\u003c/code\u003e configure option). The WAL record headers are described in \u003ccode class=\"filename\"\u003eaccess/xlogrecord.h\u003c/code\u003e; the record content is dependent on the type of event that is being logged. Segment files are given ever-increasing numbers as names, starting at \u003ccode class=\"filename\"\u003e000000010000000000000001\u003c/code\u003e. The numbers do not wrap, but it will take a very, very long time to exhaust the available stock of numbers.\u003c/p\u003e\n\u003cp\u003eIt is advantageous if the WAL is located on a different disk from the main database files. This can be achieved by moving the \u003ccode class=\"filename\"\u003epg_wal\u003c/code\u003e directory to another location (while the server is shut down, of course) and creating a symbolic link from the original location in the main data directory to the new location.\u003c/p\u003e\n\u003cp\u003eThe aim of WAL is to ensure that the log is written before database records are altered, but this can be subverted by disk drives that falsely report a successful write to the kernel, when in fact they have only cached the data and not yet stored it on the disk. A power failure in such a situation might lead to irrecoverable data corruption. Administrators should try to ensure that disks holding \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e's WAL files do not make such false reports. (See \u003ca class=\"xref\" href=\"/docs/18/wal-reliability.html\" title=\"28.1. Reliability\"\u003eSection 28.1\u003c/a\u003e.)\u003c/p\u003e\n\u003cp\u003eAfter a checkpoint has been made and the WAL flushed, the checkpoint's position is saved in the file \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e. Therefore, at the start of recovery, the server first reads \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e and then the checkpoint record; then it performs the REDO operation by scanning forward from the WAL location indicated in the checkpoint record. Because the entire content of data pages is saved in the WAL on the first page modification after a checkpoint (assuming \u003ca class=\"xref\" href=\"/docs/18/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\"\u003efull_page_writes\u003c/a\u003e is not disabled), all pages changed since the checkpoint will be restored to a consistent state.\u003c/p\u003e\n\u003cp\u003eTo deal with the case where \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e is corrupt, we should support the possibility of scanning existing WAL segments in reverse order — newest to oldest — in order to find the latest checkpoint. This has not been implemented yet. \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e is small enough (less than one disk page) that it is not subject to partial-write problems, and as of this writing there have been no reports of database failures due solely to the inability to read \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e itself. So while it is theoretically a weak spot, \u003ccode class=\"filename\"\u003epg_control\u003c/code\u003e does not seem to be a problem in practice.\u003c/p\u003e\n\u003c/div\u003e","related":[{"label":"Table AM","url":"/wiki/tableam/?v=18"},{"label":"Storage Parameters","url":"/wiki/relopts/?v=18"}],"sections":[],"tables":[]}},"RequestedLocale":"zh-Hans","Fallback":true,"Versions":["10","11","12","13","14","15","16","17","18","19","20"],"Locales":["en"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
