作者:瀚高PG实验室 (Highgo PG Lab)-天蝎座
先看一下PG官网关于这两个参数的介绍:
fsync (boolean)
If this parameter is on, the PostgreSQL server will try to make sure that updates are physically written to disk, by issuing fsync() system calls or various equivalent methods (see wal_sync_method). This ensures that the database cluster can recover to a consistent state after an operating system or hardware crash.
While turning off fsync is often a performance benefit, this can result in unrecoverable data corruption in the event of a power failure or system crash. Thus it is only advisable to turn off fsync if you can easily recreate your entire database from external data.
Examples of safe circumstances for turning off fsync include the initial loading of a new database cluster from a backup file, using a database cluster for processing a batch of data after which the database will be thrown away and recreated, or for a read-only database clone which gets recreated frequently and is not used for failover. High quality hardware alone is not a sufficient justification for turning off fsync. For reliable recovery when changing fsync off to on, it is necessary to force all modified buffers in the kernel to durable storage. This can be done while the cluster is shutdown or while fsync is on by running initdb --sync-only, running sync, unmounting the file system, or rebooting the server.
In many situations, turning off synchronous_commit for noncritical transactions can provide much of the potential performancebenefit of turning off fsync, without the attendant risks of data corruption. fsync can only be set in the postgresql.conf file or on the server command line.
If you turn this parameter off, also consider turning off full_page_writes.
如果这个参数是打开的,PostgreSQL服务器将尝试通过发出fsync()系统调用或者各种等价的方法(参见wal_sync_method)来确保更新物理写入磁盘。这确保了在操作系统或硬件崩溃之后,数据库集群可以恢复到一致的状态。
关闭fsync通常会带来性能方面的好处,但如果发生电源故障或系统崩溃,则可能会导致无法恢复的数据损坏。因此,如果您可以轻松地从外部数据重新创建整个数据库,则仅建议关闭fsync。
关闭fsync的安全环境示例包括:从备份文件初始加载新的数据库集群,使用数据库集群处理一批数据,之后数据库将被丢弃并重新创建,或用于只读数据库克隆经常重新创建,不用于故障转移。高质量的硬件本身并不足以成为关闭fsync的理由。
为了在将fsync更改为on时进行可靠的恢复,必须强制内核中的所有已修改的缓冲区进行持久存储。这可以在群集关闭或fsync打开时通过运行initdb --sync-only,运行同步,卸载文件系统或重新启动服务器来完成。
在许多情况下,关闭用于非关键事务的synchronous_commit可以提供关闭fsync的潜在性能优势,而不会带来数据损坏的风险。
只能在postgresql.conf文件或服务器命令行中设置fsync。如果关闭此参数,也可以考虑关闭full_page_writes。
full_page_writes (boolean)
When this parameter is on, the PostgreSQL server writes the entire content of each disk page to WAL during the first modification of that page after a checkpoint. This is needed because a page write that is in process during an operating system crash might be only partially completed, leading to an on-disk page that contains a mix of old and new data. The row-level change data normally stored in WAL will not be enough to completely restore such a page during post-crash recovery.
Storing the full page image guarantees that the page can be correctly restored, but at the price of increasing the amount of data that must be written to WAL. (Because WAL replay always starts from a checkpoint, it is sufficient to do this during the first change of each page after a checkpoint. Therefore, one way to reduce the cost of full-page writes is to increase the checkpoint interval parameters.)Turning this parameter off speeds normal operation, but might lead to either unrecoverable data corruption, or silent data corruption, after a system failure. The risks are similar to turning off fsync, though smaller, and it should be turned off only based on the same circumstances recommended for that parameter.
Turning off this parameter does not affect use of WAL archiving for point-in-time recovery (PITR).
This parameter can only be set in the postgresql.conf file or on the server command line. The default is on.
当此参数打开时,PostgreSQL服务器在检查点之后的第一次修改页面期间将每个磁盘页面的全部内容写入WAL。这是需要的,因为在操作系统崩溃期间进行的页面写入可能只是部分完成,导致包含新旧数据混合的磁盘页面。通常存储在WAL中的行级更改数据将不足以在故障恢复期间完全恢复这样的页面。存储整页图像可以保证页面可以正确地恢复,但是以增加必须写入WAL的数据量为代价。 (因为WAL重放总是从一个检查点开始的,所以在检查点之后的每个页面的第一次改变期间这样做是足够的,因此减少全页面写入的一个方法是增加检查点间隔参数。
关闭此参数可加速正常操作,但系统故障后可能导致无法恢复的数据损坏或无声的数据损坏。风险类似于关闭fsync,虽然较小,只能根据推荐的参数相同的情况下关闭它。
关闭此参数不会影响WAL归档在时间点恢复(PITR)中的使用(请参阅第25.3节)。
此参数只能在postgresql.conf文件或服务器命令行中设置。默认打开。
看上面的解释其实已经十分清楚了:
1、fsync 比 full_page_writes 更重要。
2、两者默认都为on
3、关闭full_page_writes不影响PITR
4、检查点之后的第一个修改页面会把页面全部内容写入WAL,再下一个检查点到来之前,无论怎样修改,将不会再记录整个页面。
5、关闭full_page_writes,或者增加检查点间隔都会减少WAL的日志量。
6、关闭fsync后,可以考虑关闭full_page_writes,因为 fsync为off,及时 full_page_writes为on,也无法保证数据安全性。