Skip to content

Commit ab7ce97

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 d61dccc commit ab7ce97

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
@@ -218,7 +218,7 @@ test_open(void)
218218
/*
219219
* test if we can open the target file
220220
*/
221-
if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
221+
if ((tmpfile = open(filename, O_RDWR | O_CREAT | PG_BINARY, S_IRUSR | S_IWUSR)) == -1)
222222
die("could not open output file");
223223
needs_unlink = 1;
224224
if (write(tmpfile, full_buf, XLOG_SEG_SIZE) != XLOG_SEG_SIZE)
@@ -252,7 +252,7 @@ test_sync(int writes_per_op)
252252
fflush(stdout);
253253

254254
#ifdef OPEN_DATASYNC_FLAG
255-
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1)
255+
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT | PG_BINARY, 0)) == -1)
256256
{
257257
printf(NA_FORMAT, "n/a*\n");
258258
fs_warning = true;
@@ -282,7 +282,7 @@ test_sync(int writes_per_op)
282282
fflush(stdout);
283283

284284
#ifdef HAVE_FDATASYNC
285-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
285+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
286286
die("could not open output file");
287287
START_TIMER;
288288
for (ops = 0; alarm_triggered == false; ops++)
@@ -306,7 +306,7 @@ test_sync(int writes_per_op)
306306
printf(LABEL_FORMAT, "fsync");
307307
fflush(stdout);
308308

309-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
309+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
310310
die("could not open output file");
311311
START_TIMER;
312312
for (ops = 0; alarm_triggered == false; ops++)
@@ -329,7 +329,7 @@ test_sync(int writes_per_op)
329329
fflush(stdout);
330330

331331
#ifdef HAVE_FSYNC_WRITETHROUGH
332-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
332+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
333333
die("could not open output file");
334334
START_TIMER;
335335
for (ops = 0; alarm_triggered == false; ops++)
@@ -355,7 +355,7 @@ test_sync(int writes_per_op)
355355
fflush(stdout);
356356

357357
#ifdef OPEN_SYNC_FLAG
358-
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
358+
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT | PG_BINARY, 0)) == -1)
359359
{
360360
printf(NA_FORMAT, "n/a*\n");
361361
fs_warning = true;
@@ -422,7 +422,7 @@ test_open_sync(const char *msg, int writes_size)
422422
fflush(stdout);
423423

424424
#ifdef OPEN_SYNC_FLAG
425-
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
425+
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT | PG_BINARY, 0)) == -1)
426426
printf(NA_FORMAT, "n/a*\n");
427427
else
428428
{
@@ -470,7 +470,7 @@ test_file_descriptor_sync(void)
470470
START_TIMER;
471471
for (ops = 0; alarm_triggered == false; ops++)
472472
{
473-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
473+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
474474
die("could not open output file");
475475
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
476476
die("write failed");
@@ -482,7 +482,7 @@ test_file_descriptor_sync(void)
482482
* open and close the file again to be consistent with the following
483483
* test
484484
*/
485-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
485+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
486486
die("could not open output file");
487487
close(tmpfile);
488488
}
@@ -498,13 +498,13 @@ test_file_descriptor_sync(void)
498498
START_TIMER;
499499
for (ops = 0; alarm_triggered == false; ops++)
500500
{
501-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
501+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
502502
die("could not open output file");
503503
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
504504
die("write failed");
505505
close(tmpfile);
506506
/* reopen file */
507-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
507+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
508508
die("could not open output file");
509509
if (fsync(tmpfile) != 0)
510510
die("fsync failed");
@@ -529,7 +529,7 @@ test_non_sync(void)
529529
START_TIMER;
530530
for (ops = 0; alarm_triggered == false; ops++)
531531
{
532-
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
532+
if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
533533
die("could not open output file");
534534
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
535535
die("write failed");

0 commit comments

Comments
 (0)