Skip to content

Commit 800ec48

Browse files
committed
Switch pg_test_fsync to use binary mode on Windows
pg_test_fsync has always opened files using the text mode on Windows, as this is the default mode used if not enforced by _setmode(). This fixes a failure when running pg_test_fsync down to 12 because O_DSYNC and the text mode are not able to work together nicely. We fixed the handling of O_DSYNC in 12~ for the tool by switching to the concurrent-safe version of fopen() in src/port/ with 0ba06e0. And 40cfe86, by enforcing the text mode for compatibility reasons if O_TEXT or O_BINARY are not specified by the caller, broke pg_test_fsync. For all versions, this avoids any translation overhead, and pg_test_fsync should test binary writes, so it is a gain in all cases. Note that O_DSYNC is still not handled correctly in ~11, leading to pg_test_fsync to show insanely high numbers for open_datasync() (using this property it is easy to notice that the binary mode is much faster). This would require a backpatch of 0ba06e0 and 40cfe86, which could potentially break existing applications, so this is left out. There are no TAP tests for this tool yet, so I have checked all builds manually using MSVC. We could invent a new option to run a single transaction instead of using a duration of 1s to make the tests a maximum short, but this is left as future work. Thanks to Bruce Momjian for the discussion. Reported-by: Jeff Janes Author: Michael Paquier Discussion: https://postgr.es/m/16526-279ded30a230d275@postgresql.org Backpatch-through: 9.5
1 parent 0140dec commit 800ec48

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/bin/pg_test_fsync/pg_test_fsync.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ test_open(void)
223223
/*
224224
* test if we can open the target file
225225
*/
226-
if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
226+
if ((tmpfile = open(filename, O_RDWR | O_CREAT | PG_BINARY, S_IRUSR | S_IWUSR)) == -1)
227227
die("could not open output file");
228228
needs_unlink = 1;
229229
if (write(tmpfile, full_buf, XLOG_SEG_SIZE) != XLOG_SEG_SIZE)
@@ -257,7 +257,7 @@ test_sync(int writes_per_op)
257257
fflush(stdout);
258258

259259
#ifdef OPEN_DATASYNC_FLAG
260-
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1)
260+
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT | PG_BINARY, 0)) == -1)
261261
{
262262
printf(NA_FORMAT, _("n/a*"));
263263
fs_warning = true;
@@ -287,7 +287,7 @@ test_sync(int writes_per_op)
287287
fflush(stdout);
288288

289289
#ifdef HAVE_FDATASYNC
290-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
290+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
291291
die("could not open output file");
292292
START_TIMER;
293293
for (ops = 0; alarm_triggered == false; ops++)
@@ -311,7 +311,7 @@ test_sync(int writes_per_op)
311311
printf(LABEL_FORMAT, "fsync");
312312
fflush(stdout);
313313

314-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
314+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
315315
die("could not open output file");
316316
START_TIMER;
317317
for (ops = 0; alarm_triggered == false; ops++)
@@ -334,7 +334,7 @@ test_sync(int writes_per_op)
334334
fflush(stdout);
335335

336336
#ifdef HAVE_FSYNC_WRITETHROUGH
337-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
337+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
338338
die("could not open output file");
339339
START_TIMER;
340340
for (ops = 0; alarm_triggered == false; ops++)
@@ -360,7 +360,7 @@ test_sync(int writes_per_op)
360360
fflush(stdout);
361361

362362
#ifdef OPEN_SYNC_FLAG
363-
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
363+
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT | PG_BINARY, 0)) == -1)
364364
{
365365
printf(NA_FORMAT, _("n/a*"));
366366
fs_warning = true;
@@ -427,7 +427,7 @@ test_open_sync(const char *msg, int writes_size)
427427
fflush(stdout);
428428

429429
#ifdef OPEN_SYNC_FLAG
430-
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
430+
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT | PG_BINARY, 0)) == -1)
431431
printf(NA_FORMAT, _("n/a*"));
432432
else
433433
{
@@ -475,7 +475,7 @@ test_file_descriptor_sync(void)
475475
START_TIMER;
476476
for (ops = 0; alarm_triggered == false; ops++)
477477
{
478-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
478+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
479479
die("could not open output file");
480480
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
481481
die("write failed");
@@ -487,7 +487,7 @@ test_file_descriptor_sync(void)
487487
* open and close the file again to be consistent with the following
488488
* test
489489
*/
490-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
490+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
491491
die("could not open output file");
492492
close(tmpfile);
493493
}
@@ -503,13 +503,13 @@ test_file_descriptor_sync(void)
503503
START_TIMER;
504504
for (ops = 0; alarm_triggered == false; ops++)
505505
{
506-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
506+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
507507
die("could not open output file");
508508
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
509509
die("write failed");
510510
close(tmpfile);
511511
/* reopen file */
512-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
512+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
513513
die("could not open output file");
514514
if (fsync(tmpfile) != 0)
515515
die("fsync failed");
@@ -534,7 +534,7 @@ test_non_sync(void)
534534
START_TIMER;
535535
for (ops = 0; alarm_triggered == false; ops++)
536536
{
537-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
537+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
538538
die("could not open output file");
539539
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
540540
die("write failed");

0 commit comments

Comments
 (0)