]> begriffs open source - ai-pg/blob - full-docs/txt/wal-reliability.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / wal-reliability.txt
1
2 28.1. Reliability #
3
4    Reliability is an important property of any serious database system,
5    and PostgreSQL does everything possible to guarantee reliable
6    operation. One aspect of reliable operation is that all data recorded
7    by a committed transaction should be stored in a nonvolatile area that
8    is safe from power loss, operating system failure, and hardware failure
9    (except failure of the nonvolatile area itself, of course).
10    Successfully writing the data to the computer's permanent storage (disk
11    drive or equivalent) ordinarily meets this requirement. In fact, even
12    if a computer is fatally damaged, if the disk drives survive they can
13    be moved to another computer with similar hardware and all committed
14    transactions will remain intact.
15
16    While forcing data to the disk platters periodically might seem like a
17    simple operation, it is not. Because disk drives are dramatically
18    slower than main memory and CPUs, several layers of caching exist
19    between the computer's main memory and the disk platters. First, there
20    is the operating system's buffer cache, which caches frequently
21    requested disk blocks and combines disk writes. Fortunately, all
22    operating systems give applications a way to force writes from the
23    buffer cache to disk, and PostgreSQL uses those features. (See the
24    wal_sync_method parameter to adjust how this is done.)
25
26    Next, there might be a cache in the disk drive controller; this is
27    particularly common on RAID controller cards. Some of these caches are
28    write-through, meaning writes are sent to the drive as soon as they
29    arrive. Others are write-back, meaning data is sent to the drive at
30    some later time. Such caches can be a reliability hazard because the
31    memory in the disk controller cache is volatile, and will lose its
32    contents in a power failure. Better controller cards have
33    battery-backup units (BBUs), meaning the card has a battery that
34    maintains power to the cache in case of system power loss. After power
35    is restored the data will be written to the disk drives.
36
37    And finally, most disk drives have caches. Some are write-through while
38    some are write-back, and the same concerns about data loss exist for
39    write-back drive caches as for disk controller caches. Consumer-grade
40    IDE and SATA drives are particularly likely to have write-back caches
41    that will not survive a power failure. Many solid-state drives (SSD)
42    also have volatile write-back caches.
43
44    These caches can typically be disabled; however, the method for doing
45    this varies by operating system and drive type:
46      * On Linux, IDE and SATA drives can be queried using hdparm -I; write
47        caching is enabled if there is a * next to Write cache. hdparm -W 0
48        can be used to turn off write caching. SCSI drives can be queried
49        using sdparm. Use sdparm --get=WCE to check whether the write cache
50        is enabled and sdparm --clear=WCE to disable it.
51      * On FreeBSD, IDE drives can be queried using camcontrol identify and
52        write caching turned off using hw.ata.wc=0 in /boot/loader.conf;
53        SCSI drives can be queried using camcontrol identify, and the write
54        cache both queried and changed using sdparm when available.
55      * On Solaris, the disk write cache is controlled by format -e. (The
56        Solaris ZFS file system is safe with disk write-cache enabled
57        because it issues its own disk cache flush commands.)
58      * On Windows, if wal_sync_method is open_datasync (the default),
59        write caching can be disabled by unchecking My Computer\Open\disk
60        drive\Properties\Hardware\Properties\Policies\Enable write caching
61        on the disk. Alternatively, set wal_sync_method to fdatasync (NTFS
62        only) or fsync, which prevent write caching.
63      * On macOS, write caching can be prevented by setting wal_sync_method
64        to fsync_writethrough.
65
66    Recent SATA drives (those following ATAPI-6 or later) offer a drive
67    cache flush command (FLUSH CACHE EXT), while SCSI drives have long
68    supported a similar command SYNCHRONIZE CACHE. These commands are not
69    directly accessible to PostgreSQL, but some file systems (e.g., ZFS,
70    ext4) can use them to flush data to the platters on write-back-enabled
71    drives. Unfortunately, such file systems behave suboptimally when
72    combined with battery-backup unit (BBU) disk controllers. In such
73    setups, the synchronize command forces all data from the controller
74    cache to the disks, eliminating much of the benefit of the BBU. You can
75    run the pg_test_fsync program to see if you are affected. If you are
76    affected, the performance benefits of the BBU can be regained by
77    turning off write barriers in the file system or reconfiguring the disk
78    controller, if that is an option. If write barriers are turned off,
79    make sure the battery remains functional; a faulty battery can
80    potentially lead to data loss. Hopefully file system and disk
81    controller designers will eventually address this suboptimal behavior.
82
83    When the operating system sends a write request to the storage
84    hardware, there is little it can do to make sure the data has arrived
85    at a truly non-volatile storage area. Rather, it is the administrator's
86    responsibility to make certain that all storage components ensure
87    integrity for both data and file-system metadata. Avoid disk
88    controllers that have non-battery-backed write caches. At the drive
89    level, disable write-back caching if the drive cannot guarantee the
90    data will be written before shutdown. If you use SSDs, be aware that
91    many of these do not honor cache flush commands by default. You can
92    test for reliable I/O subsystem behavior using diskchecker.pl.
93
94    Another risk of data loss is posed by the disk platter write operations
95    themselves. Disk platters are divided into sectors, commonly 512 bytes
96    each. Every physical read or write operation processes a whole sector.
97    When a write request arrives at the drive, it might be for some
98    multiple of 512 bytes (PostgreSQL typically writes 8192 bytes, or 16
99    sectors, at a time), and the process of writing could fail due to power
100    loss at any time, meaning some of the 512-byte sectors were written
101    while others were not. To guard against such failures, PostgreSQL
102    periodically writes full page images to permanent WAL storage before
103    modifying the actual page on disk. By doing this, during crash recovery
104    PostgreSQL can restore partially-written pages from WAL. If you have
105    file-system software that prevents partial page writes (e.g., ZFS), you
106    can turn off this page imaging by turning off the full_page_writes
107    parameter. Battery-Backed Unit (BBU) disk controllers do not prevent
108    partial page writes unless they guarantee that data is written to the
109    BBU as full (8kB) pages.
110
111    PostgreSQL also protects against some kinds of data corruption on
112    storage devices that may occur because of hardware errors or media
113    failure over time, such as reading/writing garbage data.
114      * Each individual record in a WAL file is protected by a CRC-32C
115        (32-bit) check that allows us to tell if record contents are
116        correct. The CRC value is set when we write each WAL record and
117        checked during crash recovery, archive recovery and replication.
118      * Data pages are checksummed by default, and full page images
119        recorded in WAL records are always checksum protected.
120      * Internal data structures such as pg_xact, pg_subtrans,
121        pg_multixact, pg_serial, pg_notify, pg_stat, pg_snapshots are not
122        directly checksummed, nor are pages protected by full page writes.
123        However, where such data structures are persistent, WAL records are
124        written that allow recent changes to be accurately rebuilt at crash
125        recovery and those WAL records are protected as discussed above.
126      * Individual state files in pg_twophase are protected by CRC-32C.
127      * Temporary data files used in larger SQL queries for sorts,
128        materializations and intermediate results are not currently
129        checksummed, nor will WAL records be written for changes to those
130        files.
131
132    PostgreSQL does not protect against correctable memory errors and it is
133    assumed you will operate using RAM that uses industry standard Error
134    Correcting Codes (ECC) or better protection.