Skip to content

Commit 0ba06e0

Browse files
committed
Allow concurrent-safe open() and fopen() in frontend code for Windows
PostgreSQL uses a custom wrapper for open() and fopen() which is concurrent-safe, allowing multiple processes to open and work on the same file. This has a couple of advantages: - pg_test_fsync does not handle O_DSYNC correctly otherwise, leading to false claims that disks are unsafe. - TAP tests can run into race conditions when a postmaster and pg_ctl open postmaster.pid, fixing some random failures in the buildfam. pg_upgrade is one frontend tool using workarounds to bypass file locking issues with the log files it generates, however the interactions with pg_ctl are proving to be tedious to get rid of, so this is left for later. Author: Laurenz Albe Reviewed-by: Michael Paquier, Kuntal Ghosh Discussion: https://postgr.es/m/1527846213.2475.31.camel@cybertec.at Discussion: https://postgr.es/m/16922.1520722108@sss.pgh.pa.us
1 parent 28a8fa9 commit 0ba06e0

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

src/bin/initdb/initdb.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,15 @@ readfile(const char *path)
491491
char *buffer;
492492
int c;
493493

494+
#ifdef WIN32
495+
/*
496+
* On Windows, we have to open the file in text mode so that carriage
497+
* returns are stripped.
498+
*/
499+
if ((infile = fopen(path, "rt")) == NULL)
500+
#else
494501
if ((infile = fopen(path, "r")) == NULL)
502+
#endif
495503
{
496504
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
497505
progname, path, strerror(errno));

src/bin/pg_basebackup/pg_receivewal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ FindStreamingStart(uint32 *tli)
288288

289289
snprintf(fullpath, sizeof(fullpath), "%s/%s", basedir, dirent->d_name);
290290

291-
fd = open(fullpath, O_RDONLY | PG_BINARY);
291+
fd = open(fullpath, O_RDONLY | PG_BINARY, 0);
292292
if (fd < 0)
293293
{
294294
fprintf(stderr, _("%s: could not open compressed file \"%s\": %s\n"),

src/bin/pg_verify_checksums/pg_verify_checksums.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ scan_file(const char *fn, BlockNumber segmentno)
8080
int f;
8181
BlockNumber blockno;
8282

83-
f = open(fn, O_RDONLY | PG_BINARY);
83+
f = open(fn, O_RDONLY | PG_BINARY, 0);
8484
if (f < 0)
8585
{
8686
fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),

src/common/file_utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ pre_sync_fname(const char *fname, bool isdir, const char *progname)
222222
{
223223
int fd;
224224

225-
fd = open(fname, O_RDONLY | PG_BINARY);
225+
fd = open(fname, O_RDONLY | PG_BINARY, 0);
226226

227227
if (fd < 0)
228228
{
@@ -283,7 +283,7 @@ fsync_fname(const char *fname, bool isdir, const char *progname)
283283
* unsupported operations, e.g. opening a directory under Windows), and
284284
* logging others.
285285
*/
286-
fd = open(fname, flags);
286+
fd = open(fname, flags, 0);
287287
if (fd < 0)
288288
{
289289
if (errno == EACCES || (isdir && errno == EISDIR))

src/include/port.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,8 @@ extern bool rmtree(const char *path, bool rmtopdir);
249249
#define O_DIRECT 0x80000000
250250
extern int pgwin32_open(const char *, int,...);
251251
extern FILE *pgwin32_fopen(const char *, const char *);
252-
253-
#ifndef FRONTEND
254252
#define open(a,b,c) pgwin32_open(a,b,c)
255253
#define fopen(a,b) pgwin32_fopen(a,b)
256-
#endif
257254

258255
/*
259256
* Mingw-w64 headers #define popen and pclose to _popen and _pclose. We want

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy