Skip to content

Commit 932f9fb

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 ed2c7f6 commit 932f9fb

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ test_open(void)
224224
/*
225225
* test if we can open the target file
226226
*/
227-
if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
227+
if ((tmpfile = open(filename, O_RDWR | O_CREAT | PG_BINARY, S_IRUSR | S_IWUSR)) == -1)
228228
die("could not open output file");
229229
needs_unlink = 1;
230230
if (write(tmpfile, full_buf, DEFAULT_XLOG_SEG_SIZE) !=
@@ -259,7 +259,7 @@ test_sync(int writes_per_op)
259259
fflush(stdout);
260260

261261
#ifdef OPEN_DATASYNC_FLAG
262-
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1)
262+
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT | PG_BINARY, 0)) == -1)
263263
{
264264
printf(NA_FORMAT, _("n/a*"));
265265
fs_warning = true;
@@ -289,7 +289,7 @@ test_sync(int writes_per_op)
289289
fflush(stdout);
290290

291291
#ifdef HAVE_FDATASYNC
292-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
292+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
293293
die("could not open output file");
294294
START_TIMER;
295295
for (ops = 0; alarm_triggered == false; ops++)
@@ -313,7 +313,7 @@ test_sync(int writes_per_op)
313313
printf(LABEL_FORMAT, "fsync");
314314
fflush(stdout);
315315

316-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
316+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
317317
die("could not open output file");
318318
START_TIMER;
319319
for (ops = 0; alarm_triggered == false; ops++)
@@ -336,7 +336,7 @@ test_sync(int writes_per_op)
336336
fflush(stdout);
337337

338338
#ifdef HAVE_FSYNC_WRITETHROUGH
339-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
339+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
340340
die("could not open output file");
341341
START_TIMER;
342342
for (ops = 0; alarm_triggered == false; ops++)
@@ -362,7 +362,7 @@ test_sync(int writes_per_op)
362362
fflush(stdout);
363363

364364
#ifdef OPEN_SYNC_FLAG
365-
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
365+
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT | PG_BINARY, 0)) == -1)
366366
{
367367
printf(NA_FORMAT, _("n/a*"));
368368
fs_warning = true;
@@ -429,7 +429,7 @@ test_open_sync(const char *msg, int writes_size)
429429
fflush(stdout);
430430

431431
#ifdef OPEN_SYNC_FLAG
432-
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
432+
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT | PG_BINARY, 0)) == -1)
433433
printf(NA_FORMAT, _("n/a*"));
434434
else
435435
{
@@ -477,7 +477,7 @@ test_file_descriptor_sync(void)
477477
START_TIMER;
478478
for (ops = 0; alarm_triggered == false; ops++)
479479
{
480-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
480+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
481481
die("could not open output file");
482482
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
483483
die("write failed");
@@ -489,7 +489,7 @@ test_file_descriptor_sync(void)
489489
* open and close the file again to be consistent with the following
490490
* test
491491
*/
492-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
492+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
493493
die("could not open output file");
494494
close(tmpfile);
495495
}
@@ -505,13 +505,13 @@ test_file_descriptor_sync(void)
505505
START_TIMER;
506506
for (ops = 0; alarm_triggered == false; ops++)
507507
{
508-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
508+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
509509
die("could not open output file");
510510
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
511511
die("write failed");
512512
close(tmpfile);
513513
/* reopen file */
514-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
514+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
515515
die("could not open output file");
516516
if (fsync(tmpfile) != 0)
517517
die("fsync failed");
@@ -536,7 +536,7 @@ test_non_sync(void)
536536
START_TIMER;
537537
for (ops = 0; alarm_triggered == false; ops++)
538538
{
539-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
539+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
540540
die("could not open output file");
541541
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
542542
die("write failed");

0 commit comments

Comments
 (0)