Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cccc6cd

Browse files
Add support for syncfs() in frontend support functions.
This commit adds support for using syncfs() in fsync_pgdata() and fsync_dir_recurse() (which have been renamed to sync_pgdata() and sync_dir_recurse()). Like recovery_init_sync_method, sync_pgdata() calls syncfs() for the data directory, each tablespace, and pg_wal (if it is a symlink). For now, all of the frontend utilities that use these support functions are hard-coded to use fsync(), but a follow-up commit will allow specifying syncfs(). Co-authored-by: Justin Pryzby Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/20210930004340.GM831%40telsasoft.com
1 parent 3ed1956 commit cccc6cd

File tree

13 files changed

+190
-56
lines changed

13 files changed

+190
-56
lines changed

src/bin/initdb/initdb.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ static bool show_setting = false;
165165
static bool data_checksums = false;
166166
static char *xlog_dir = NULL;
167167
static int wal_segment_size_mb = (DEFAULT_XLOG_SEG_SIZE) / (1024 * 1024);
168+
static DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
168169

169170

170171
/* internal vars */
@@ -3322,7 +3323,7 @@ main(int argc, char *argv[])
33223323

33233324
atexit(cleanup_directories_atexit);
33243325

3325-
/* If we only need to fsync, just do it and exit */
3326+
/* If we only need to sync, just do it and exit */
33263327
if (sync_only)
33273328
{
33283329
setup_pgdata();
@@ -3333,7 +3334,7 @@ main(int argc, char *argv[])
33333334

33343335
fputs(_("syncing data to disk ... "), stdout);
33353336
fflush(stdout);
3336-
fsync_pgdata(pg_data, PG_VERSION_NUM);
3337+
sync_pgdata(pg_data, PG_VERSION_NUM, sync_method);
33373338
check_ok();
33383339
return 0;
33393340
}
@@ -3396,7 +3397,7 @@ main(int argc, char *argv[])
33963397
{
33973398
fputs(_("syncing data to disk ... "), stdout);
33983399
fflush(stdout);
3399-
fsync_pgdata(pg_data, PG_VERSION_NUM);
3400+
sync_pgdata(pg_data, PG_VERSION_NUM, sync_method);
34003401
check_ok();
34013402
}
34023403
else

src/bin/pg_basebackup/pg_basebackup.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ static bool verify_checksums = true;
148148
static bool manifest = true;
149149
static bool manifest_force_encode = false;
150150
static char *manifest_checksums = NULL;
151+
static DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
151152

152153
static bool success = false;
153154
static bool made_new_pgdata = false;
@@ -2199,11 +2200,11 @@ BaseBackup(char *compression_algorithm, char *compression_detail,
21992200
if (format == 't')
22002201
{
22012202
if (strcmp(basedir, "-") != 0)
2202-
(void) fsync_dir_recurse(basedir);
2203+
(void) sync_dir_recurse(basedir, sync_method);
22032204
}
22042205
else
22052206
{
2206-
(void) fsync_pgdata(basedir, serverVersion);
2207+
(void) sync_pgdata(basedir, serverVersion, sync_method);
22072208
}
22082209
}
22092210

src/bin/pg_checksums/pg_checksums.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static char *only_filenode = NULL;
4444
static bool do_sync = true;
4545
static bool verbose = false;
4646
static bool showprogress = false;
47+
static DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
4748

4849
typedef enum
4950
{
@@ -623,7 +624,7 @@ main(int argc, char *argv[])
623624
if (do_sync)
624625
{
625626
pg_log_info("syncing data directory");
626-
fsync_pgdata(DataDir, PG_VERSION_NUM);
627+
sync_pgdata(DataDir, PG_VERSION_NUM, sync_method);
627628
}
628629

629630
pg_log_info("updating control file");

src/bin/pg_dump/pg_backup.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define PG_BACKUP_H
2525

2626
#include "common/compression.h"
27+
#include "common/file_utils.h"
2728
#include "fe_utils/simple_list.h"
2829
#include "libpq-fe.h"
2930

@@ -307,7 +308,8 @@ extern Archive *OpenArchive(const char *FileSpec, const ArchiveFormat fmt);
307308
extern Archive *CreateArchive(const char *FileSpec, const ArchiveFormat fmt,
308309
const pg_compress_specification compression_spec,
309310
bool dosync, ArchiveMode mode,
310-
SetupWorkerPtrType setupDumpWorker);
311+
SetupWorkerPtrType setupDumpWorker,
312+
DataDirSyncMethod sync_method);
311313

312314
/* The --list option */
313315
extern void PrintTOCSummary(Archive *AHX);

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ typedef struct _parallelReadyList
6666
static ArchiveHandle *_allocAH(const char *FileSpec, const ArchiveFormat fmt,
6767
const pg_compress_specification compression_spec,
6868
bool dosync, ArchiveMode mode,
69-
SetupWorkerPtrType setupWorkerPtr);
69+
SetupWorkerPtrType setupWorkerPtr,
70+
DataDirSyncMethod sync_method);
7071
static void _getObjectDescription(PQExpBuffer buf, const TocEntry *te);
7172
static void _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData);
7273
static char *sanitize_line(const char *str, bool want_hyphen);
@@ -238,11 +239,12 @@ Archive *
238239
CreateArchive(const char *FileSpec, const ArchiveFormat fmt,
239240
const pg_compress_specification compression_spec,
240241
bool dosync, ArchiveMode mode,
241-
SetupWorkerPtrType setupDumpWorker)
242+
SetupWorkerPtrType setupDumpWorker,
243+
DataDirSyncMethod sync_method)
242244

243245
{
244246
ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression_spec,
245-
dosync, mode, setupDumpWorker);
247+
dosync, mode, setupDumpWorker, sync_method);
246248

247249
return (Archive *) AH;
248250
}
@@ -257,7 +259,8 @@ OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
257259

258260
compression_spec.algorithm = PG_COMPRESSION_NONE;
259261
AH = _allocAH(FileSpec, fmt, compression_spec, true,
260-
archModeRead, setupRestoreWorker);
262+
archModeRead, setupRestoreWorker,
263+
DATA_DIR_SYNC_METHOD_FSYNC);
261264

262265
return (Archive *) AH;
263266
}
@@ -2233,7 +2236,7 @@ static ArchiveHandle *
22332236
_allocAH(const char *FileSpec, const ArchiveFormat fmt,
22342237
const pg_compress_specification compression_spec,
22352238
bool dosync, ArchiveMode mode,
2236-
SetupWorkerPtrType setupWorkerPtr)
2239+
SetupWorkerPtrType setupWorkerPtr, DataDirSyncMethod sync_method)
22372240
{
22382241
ArchiveHandle *AH;
22392242
CompressFileHandle *CFH;
@@ -2287,6 +2290,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
22872290
AH->mode = mode;
22882291
AH->compression_spec = compression_spec;
22892292
AH->dosync = dosync;
2293+
AH->sync_method = sync_method;
22902294

22912295
memset(&(AH->sqlparse), 0, sizeof(AH->sqlparse));
22922296

src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ struct _archiveHandle
312312
pg_compress_specification compression_spec; /* Requested specification for
313313
* compression */
314314
bool dosync; /* data requested to be synced on sight */
315+
DataDirSyncMethod sync_method;
315316
ArchiveMode mode; /* File mode - r or w */
316317
void *formatData; /* Header data specific to file format */
317318

src/bin/pg_dump/pg_backup_directory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ _CloseArchive(ArchiveHandle *AH)
613613
* individually. Just recurse once through all the files generated.
614614
*/
615615
if (AH->dosync)
616-
fsync_dir_recurse(ctx->directory);
616+
sync_dir_recurse(ctx->directory, AH->sync_method);
617617
}
618618
AH->FH = NULL;
619619
}

src/bin/pg_dump/pg_dump.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ main(int argc, char **argv)
357357
char *compression_algorithm_str = "none";
358358
char *error_detail = NULL;
359359
bool user_compression_defined = false;
360+
DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
360361

361362
static DumpOptions dopt;
362363

@@ -777,7 +778,7 @@ main(int argc, char **argv)
777778

778779
/* Open the output file */
779780
fout = CreateArchive(filename, archiveFormat, compression_spec,
780-
dosync, archiveMode, setupDumpWorker);
781+
dosync, archiveMode, setupDumpWorker, sync_method);
781782

782783
/* Make dump options accessible right away */
783784
SetArchiveOptions(fout, &dopt, NULL);

src/bin/pg_rewind/file_ops.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,17 +286,17 @@ remove_target_symlink(const char *path)
286286
*
287287
* We do this once, for the whole data directory, for performance reasons. At
288288
* the end of pg_rewind's run, the kernel is likely to already have flushed
289-
* most dirty buffers to disk. Additionally fsync_pgdata uses a two-pass
290-
* approach (only initiating writeback in the first pass), which often reduces
291-
* the overall amount of IO noticeably.
289+
* most dirty buffers to disk. Additionally sync_pgdata uses a two-pass
290+
* approach when fsync is specified (only initiating writeback in the first
291+
* pass), which often reduces the overall amount of IO noticeably.
292292
*/
293293
void
294294
sync_target_dir(void)
295295
{
296296
if (!do_sync || dry_run)
297297
return;
298298

299-
fsync_pgdata(datadir_target, PG_VERSION_NUM);
299+
sync_pgdata(datadir_target, PG_VERSION_NUM, sync_method);
300300
}
301301

302302

src/bin/pg_rewind/pg_rewind.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ bool showprogress = false;
7474
bool dry_run = false;
7575
bool do_sync = true;
7676
bool restore_wal = false;
77+
DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
7778

7879
/* Target history */
7980
TimeLineHistoryEntry *targetHistory;

src/bin/pg_rewind/pg_rewind.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "access/timeline.h"
1515
#include "common/logging.h"
16+
#include "common/file_utils.h"
1617
#include "datapagemap.h"
1718
#include "libpq-fe.h"
1819
#include "storage/block.h"
@@ -24,6 +25,7 @@ extern bool showprogress;
2425
extern bool dry_run;
2526
extern bool do_sync;
2627
extern int WalSegSz;
28+
extern DataDirSyncMethod sync_method;
2729

2830
/* Target history */
2931
extern TimeLineHistoryEntry *targetHistory;

0 commit comments

Comments
 (0)