Commit 932f9fb5 authored by Michael Paquier's avatar Michael Paquier

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 0ba06e0b.  And
40cfe860, 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 0ba06e0b and 40cfe860, 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
parent ed2c7f65
...@@ -224,7 +224,7 @@ test_open(void) ...@@ -224,7 +224,7 @@ test_open(void)
/* /*
* test if we can open the target file * test if we can open the target file
*/ */
if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1) if ((tmpfile = open(filename, O_RDWR | O_CREAT | PG_BINARY, S_IRUSR | S_IWUSR)) == -1)
die("could not open output file"); die("could not open output file");
needs_unlink = 1; needs_unlink = 1;
if (write(tmpfile, full_buf, DEFAULT_XLOG_SEG_SIZE) != if (write(tmpfile, full_buf, DEFAULT_XLOG_SEG_SIZE) !=
...@@ -259,7 +259,7 @@ test_sync(int writes_per_op) ...@@ -259,7 +259,7 @@ test_sync(int writes_per_op)
fflush(stdout); fflush(stdout);
#ifdef OPEN_DATASYNC_FLAG #ifdef OPEN_DATASYNC_FLAG
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT | PG_BINARY, 0)) == -1)
{ {
printf(NA_FORMAT, _("n/a*")); printf(NA_FORMAT, _("n/a*"));
fs_warning = true; fs_warning = true;
...@@ -289,7 +289,7 @@ test_sync(int writes_per_op) ...@@ -289,7 +289,7 @@ test_sync(int writes_per_op)
fflush(stdout); fflush(stdout);
#ifdef HAVE_FDATASYNC #ifdef HAVE_FDATASYNC
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
die("could not open output file"); die("could not open output file");
START_TIMER; START_TIMER;
for (ops = 0; alarm_triggered == false; ops++) for (ops = 0; alarm_triggered == false; ops++)
...@@ -313,7 +313,7 @@ test_sync(int writes_per_op) ...@@ -313,7 +313,7 @@ test_sync(int writes_per_op)
printf(LABEL_FORMAT, "fsync"); printf(LABEL_FORMAT, "fsync");
fflush(stdout); fflush(stdout);
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
die("could not open output file"); die("could not open output file");
START_TIMER; START_TIMER;
for (ops = 0; alarm_triggered == false; ops++) for (ops = 0; alarm_triggered == false; ops++)
...@@ -336,7 +336,7 @@ test_sync(int writes_per_op) ...@@ -336,7 +336,7 @@ test_sync(int writes_per_op)
fflush(stdout); fflush(stdout);
#ifdef HAVE_FSYNC_WRITETHROUGH #ifdef HAVE_FSYNC_WRITETHROUGH
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
die("could not open output file"); die("could not open output file");
START_TIMER; START_TIMER;
for (ops = 0; alarm_triggered == false; ops++) for (ops = 0; alarm_triggered == false; ops++)
...@@ -362,7 +362,7 @@ test_sync(int writes_per_op) ...@@ -362,7 +362,7 @@ test_sync(int writes_per_op)
fflush(stdout); fflush(stdout);
#ifdef OPEN_SYNC_FLAG #ifdef OPEN_SYNC_FLAG
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT | PG_BINARY, 0)) == -1)
{ {
printf(NA_FORMAT, _("n/a*")); printf(NA_FORMAT, _("n/a*"));
fs_warning = true; fs_warning = true;
...@@ -429,7 +429,7 @@ test_open_sync(const char *msg, int writes_size) ...@@ -429,7 +429,7 @@ test_open_sync(const char *msg, int writes_size)
fflush(stdout); fflush(stdout);
#ifdef OPEN_SYNC_FLAG #ifdef OPEN_SYNC_FLAG
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT | PG_BINARY, 0)) == -1)
printf(NA_FORMAT, _("n/a*")); printf(NA_FORMAT, _("n/a*"));
else else
{ {
...@@ -477,7 +477,7 @@ test_file_descriptor_sync(void) ...@@ -477,7 +477,7 @@ test_file_descriptor_sync(void)
START_TIMER; START_TIMER;
for (ops = 0; alarm_triggered == false; ops++) for (ops = 0; alarm_triggered == false; ops++)
{ {
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
die("could not open output file"); die("could not open output file");
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ) if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
die("write failed"); die("write failed");
...@@ -489,7 +489,7 @@ test_file_descriptor_sync(void) ...@@ -489,7 +489,7 @@ test_file_descriptor_sync(void)
* open and close the file again to be consistent with the following * open and close the file again to be consistent with the following
* test * test
*/ */
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
die("could not open output file"); die("could not open output file");
close(tmpfile); close(tmpfile);
} }
...@@ -505,13 +505,13 @@ test_file_descriptor_sync(void) ...@@ -505,13 +505,13 @@ test_file_descriptor_sync(void)
START_TIMER; START_TIMER;
for (ops = 0; alarm_triggered == false; ops++) for (ops = 0; alarm_triggered == false; ops++)
{ {
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
die("could not open output file"); die("could not open output file");
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ) if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
die("write failed"); die("write failed");
close(tmpfile); close(tmpfile);
/* reopen file */ /* reopen file */
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
die("could not open output file"); die("could not open output file");
if (fsync(tmpfile) != 0) if (fsync(tmpfile) != 0)
die("fsync failed"); die("fsync failed");
...@@ -536,7 +536,7 @@ test_non_sync(void) ...@@ -536,7 +536,7 @@ test_non_sync(void)
START_TIMER; START_TIMER;
for (ops = 0; alarm_triggered == false; ops++) for (ops = 0; alarm_triggered == false; ops++)
{ {
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
die("could not open output file"); die("could not open output file");
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ) if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
die("write failed"); die("write failed");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment