aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2019-12-10 13:17:08 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2019-12-10 13:17:08 -0500
commitbe9d4b9280606a0b0984ba46c452a48961cf92d4 (patch)
tree4e69348ac25ac168d802e4cd65e518fc2a0aa817 /src
parent547e454cbc8f8896b535cf2debd61b2c5701d8c0 (diff)
downloadpostgresql-be9d4b9280606a0b0984ba46c452a48961cf92d4.tar.gz
postgresql-be9d4b9280606a0b0984ba46c452a48961cf92d4.zip
In pg_ctl, work around ERROR_SHARING_VIOLATION on the postmaster log file.
On Windows, we use CMD.EXE to redirect the postmaster's stdout/stderr into a log file. CMD.EXE will open that file with non-sharing-friendly parameters, and the file will remain open for a short time after the postmaster has removed postmaster.pid. This can result in an ERROR_SHARING_VIOLATION failure if we attempt to start a new postmaster immediately with the same log file (e.g. during "pg_ctl restart"). This seems to explain intermittent buildfarm failures we've been seeing on Windows machines. To fix, just open and close the log file using our own pgwin32_open(), which will wait if necessary to avoid the failure. (Perhaps someday we should stop using CMD.EXE, but that would be a far more complex patch, and it doesn't seem worth the trouble ... yet.) Back-patch to v12. This only solves the problem when frontend fopen() is redirected to pgwin32_fopen(), which has only been true since commit 0ba06e0bf. Hence, no point in back-patching further, unless we care to back-patch that change too. Diagnosis and patch by Alexander Lakhin (bug #16154). Discussion: https://postgr.es/m/16154-1ccf0b537b24d5e0@postgresql.org
Diffstat (limited to 'src')
-rw-r--r--src/bin/pg_ctl/pg_ctl.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index dfb6c19f5a4..bcb1ddb1351 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -519,8 +519,29 @@ start_postmaster(void)
PROCESS_INFORMATION pi;
if (log_file != NULL)
+ {
+ /*
+ * First, touch the log file. The main value of this is that if the
+ * file is still locked by a previous postmaster run, we'll wait until
+ * it comes free, instead of failing with ERROR_SHARING_VIOLATION.
+ * (It'd be better to open the file in a sharing-friendly mode, but we
+ * can't use CMD.EXE to do that, so work around it. Note that the
+ * previous postmaster will still have the file open for a short time
+ * after removing postmaster.pid.)
+ */
+ FILE *fd = fopen(log_file, "a");
+
+ if (fd == NULL)
+ {
+ write_stderr(_("%s: could not create log file \"%s\": %s\n"),
+ progname, log_file, strerror(errno));
+ exit(1);
+ }
+ fclose(fd);
+
snprintf(cmd, MAXPGPATH, "CMD /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
exec_path, pgdata_opt, post_opts, DEVNULL, log_file);
+ }
else
snprintf(cmd, MAXPGPATH, "CMD /C \"\"%s\" %s%s < \"%s\" 2>&1\"",
exec_path, pgdata_opt, post_opts, DEVNULL);