Add initdb --sync-only option to sync the data directory to durable
storage.
Have pg_upgrade use it, and enable server options fsync=off and
full_page_writes=off.
Document that users turning fsync from off to on should run initdb
--sync-only.
initdb -S选项目前仅仅被pg_upgrade用于fsync数据库集群目录, 因为pg_upgrade使用了fsync=off来加速升级过程.
然后在数据库集群起来之前会修改为fsync=on, 这个过程之间必须要将kernel或文件系统cache中的脏块写入持久化存储中.
所以就有了initdb -S这个选项, 用来做fsync的事情.
initdb src:
+ /* If we only need to fsync, just to it and exit */
+ if (sync_only)
+ {
+ setup_pgdata();
+ perform_fsync();
+ return 0;
+ }
contrib/pg_upgrade/server.c
+ * Turn off durability requirements to improve object creation speed, and
+ * we only modify the new cluster, so only use it there. If there is a
+ * crash, the new cluster has to be recreated anyway. fsync=off is a big
+ * win on ext4.
+ (cluster == &new_cluster) ?
+ " -c synchronous_commit=off -c fsync=off -c full_page_writes=off" : "",
contrib/pg_upgrade/pg_upgrade.c
+ prep_status("Sync data directory to disk");
+ exec_prog(UTILITY_LOG_FILE, NULL, true,
+ "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
+ new_cluster.pgdata);
+ check_ok();
doc/src/sgml/config.sgml
+ For reliable recovery when changing <varname>fsync</varname>
+ 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 <command>initdb
+ --sync-only</command>, running <command>sync</>, unmounting the
+ file system, or rebooting the server.
doc/src/sgml/ref/initdb.sgml
+ Safely write all database files to disk and exit. This does not
+ perform any of the normal <application>initdb</> operations.
2. man initdb
-S, --sync-only
Safely write all database files to disk and exit. This does not perform any of the normal initdb
operations.
时间: 2024-10-31 18:17:55