aboutsummaryrefslogtreecommitdiff
path: root/src/backend/storage/file/fd.c
diff options
context:
space:
mode:
authorThomas Munro <tmunro@postgresql.org>2021-07-19 08:52:00 +1200
committerThomas Munro <tmunro@postgresql.org>2021-07-19 11:01:01 +1200
commit2dbe8905711ba09a2214b6e835f8f0c2c4981cb3 (patch)
tree775e44cb05c4dfa8f12e68eb388f04e9aa252c96 /src/backend/storage/file/fd.c
parentf157db862225a7bfe041ca3f7b73e913e2a8d8d6 (diff)
downloadpostgresql-2dbe8905711ba09a2214b6e835f8f0c2c4981cb3.tar.gz
postgresql-2dbe8905711ba09a2214b6e835f8f0c2c4981cb3.zip
Support direct I/O on macOS.
Macs don't understand O_DIRECT, but they can disable caching with a separate fcntl() call. Extend the file opening functions in fd.c to handle this for us if the caller passes in PG_O_DIRECT. For now, this affects only WAL data and even then only if you set: max_wal_senders=0 wal_level=minimal This is not expected to be very useful on its own, but later proposed patches will make greater use of direct I/O, and it'll be useful for testing if developers on Macs can see the effects. Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/CA%2BhUKG%2BADiyyHe0cun2wfT%2BSVnFVqNYPxoO6J9zcZkVO7%2BNGig%40mail.gmail.com
Diffstat (limited to 'src/backend/storage/file/fd.c')
-rw-r--r--src/backend/storage/file/fd.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index a340a5f6afe..0792c08ce1b 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -1057,10 +1057,46 @@ BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
int fd;
tryAgain:
+#ifdef PG_O_DIRECT_USE_F_NOCACHE
+
+ /*
+ * The value we defined to stand in for O_DIRECT when simulating it with
+ * F_NOCACHE had better not collide with any of the standard flags.
+ */
+ StaticAssertStmt((PG_O_DIRECT &
+ (O_APPEND |
+ O_CLOEXEC |
+ O_CREAT |
+ O_DSYNC |
+ O_RDWR |
+ O_RDONLY |
+ O_SYNC |
+ O_TRUNC |
+ O_WRONLY)) == 0,
+ "PG_O_DIRECT value collides with standard flag");
+ fd = open(fileName, fileFlags & ~PG_O_DIRECT, fileMode);
+#else
fd = open(fileName, fileFlags, fileMode);
+#endif
if (fd >= 0)
+ {
+#ifdef PG_O_DIRECT_USE_F_NOCACHE
+ if (fileFlags & PG_O_DIRECT)
+ {
+ if (fcntl(fd, F_NOCACHE, 1) < 0)
+ {
+ int save_errno = errno;
+
+ close(fd);
+ errno = save_errno;
+ return -1;
+ }
+ }
+#endif
+
return fd; /* success! */
+ }
if (errno == EMFILE || errno == ENFILE)
{