1/*-------------------------------------------------------------------------
4 * Virtual file descriptor definitions.
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/storage/fd.h
12 *-------------------------------------------------------------------------
18 * File {Close, Read, ReadV, Write, WriteV, Size, Sync}
19 * {Path Name Open, Allocate, Free} File
21 * These are NOT JUST RENAMINGS OF THE UNIX ROUTINES.
22 * Use them for all file activity...
25 * fd = PathNameOpenFile("foo", O_RDONLY);
30 * Use AllocateFile, not fopen, if you need a stdio file (FILE*); then
31 * use FreeFile, not fclose, to close it. AVOID using stdio for files
32 * that you intend to hold open for any length of time, since there is
33 * no way for them to share kernel file descriptors with other files.
35 * Likewise, use AllocateDir/FreeDir, not opendir/closedir, to allocate
36 * open directories (DIR*), and OpenTransientFile/CloseTransientFile for an
37 * unbuffered file descriptor.
39 * If you really can't use any of the above, at least call AcquireExternalFD
40 * or ReserveExternalFD to report any file descriptors that are held for any
41 * length of time. Failure to do so risks unnecessary EMFILE errors.
54 #define IO_DIRECT_DATA 0x01
55 #define IO_DIRECT_WAL 0x02
56 #define IO_DIRECT_WAL_INIT 0x04
66 * This is private to fd.c, but exported for save/restore_backend_variables()
71 * On Windows, we have to interpret EACCES as possibly meaning the same as
72 * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
73 * that's what you get. Ugh. This code is designed so that we don't
74 * actually believe these cases are okay without further evidence (namely,
75 * a pending fsync request getting canceled ... see ProcessSyncRequests).
78 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT)
80#define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES)
84 * O_DIRECT is not standard, but almost every Unix has it. We translate it
85 * to the appropriate Windows flag in src/port/open.c. We simulate it with
86 * fcntl(F_NOCACHE) on macOS inside fd.c's open() wrapper. We use the name
87 * PG_O_DIRECT rather than defining O_DIRECT in that case (probably not a good
88 * idea on a Unix). We can only use it if the compiler will correctly align
89 * PGIOAlignedBlock for us, though.
91#if defined(O_DIRECT) && defined(pg_attribute_aligned)
92#define PG_O_DIRECT O_DIRECT
93#elif defined(F_NOCACHE)
94#define PG_O_DIRECT 0x80000000
95#define PG_O_DIRECT_USE_F_NOCACHE
101 * prototypes for functions in fd.c
106/* Operations on virtual Files --- equivalent to Unix kernel file ops */
112extern ssize_t
FileReadV(
File file,
const struct iovec *iov,
int iovcnt, off_t offset,
uint32 wait_event_info);
113extern ssize_t
FileWriteV(
File file,
const struct iovec *iov,
int iovcnt, off_t offset,
uint32 wait_event_info);
127/* Operations used for sharing named temporary files */
135/* Operations that allow use of regular stdio --- USE WITH CAUTION */
139/* Operations that allow use of pipe streams (popen/pclose) */
143/* Operations to allow use of the <dirent.h> library routines */
150/* Operations to allow use of a plain kernel FD, with automatic cleanup */
155/* If you've really really gotta have a plain kernel FD, use this */
156extern int BasicOpenFile(
const char *fileName,
int fileFlags);
157extern int BasicOpenFilePerm(
const char *fileName,
int fileFlags, mode_t fileMode);
159/* Use these for other cases, and also for long-lived BasicOpenFile FDs */
164/* Make a directory with default permissions */
167/* Miscellaneous support routines */
190extern int pg_truncate(
const char *path, off_t length);
191extern void fsync_fname(
const char *fname,
bool isdir);
192extern int fsync_fname_ext(
const char *fname,
bool isdir,
bool ignore_perm,
int elevel);
193extern int durable_rename(
const char *oldfile,
const char *newfile,
int elevel);
207 return FileReadV(file, &iov, 1, offset, wait_event_info);
219 return FileWriteV(file, &iov, 1, offset, wait_event_info);
#define unconstify(underlying_type, expr)
PGDLLIMPORT int max_safe_fds
void pg_flush_data(int fd, off_t offset, off_t nbytes)
int FileGetRawDesc(File file)
int MakePGDirectory(const char *directoryName)
void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info)
int pg_fsync_no_writethrough(int fd)
FILE * OpenPipeStream(const char *command, const char *mode)
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
PGDLLIMPORT int io_direct_flags
int durable_rename(const char *oldfile, const char *newfile, int elevel)
char * FilePathName(File file)
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
File PathNameOpenTemporaryFile(const char *path, int mode)
int FileSync(File file, uint32 wait_event_info)
int CloseTransientFile(int fd)
int BasicOpenFile(const char *fileName, int fileFlags)
File PathNameCreateTemporaryFile(const char *path, bool error_on_failure)
void PathNameDeleteTemporaryDir(const char *dirname)
int ClosePipeStream(FILE *file)
PGDLLIMPORT bool data_sync_retry
void AtEOXact_Files(bool isCommit)
int FileGetRawFlags(File file)
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
PGDLLIMPORT int recovery_init_sync_method
mode_t FileGetRawMode(File file)
struct dirent * ReadDirExtended(DIR *dir, const char *dirname, int elevel)
int pg_fsync_writethrough(int fd)
void FileClose(File file)
void ReleaseExternalFD(void)
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
bool pg_file_exists(const char *name)
void RemovePgTempFiles(void)
bool TempTablespacesAreSet(void)
void fsync_fname(const char *fname, bool isdir)
int FileFallocate(File file, off_t offset, off_t amount, uint32 wait_event_info)
int FilePrefetch(File file, off_t offset, off_t amount, uint32 wait_event_info)
int data_sync_elevel(int elevel)
File PathNameOpenFile(const char *fileName, int fileFlags)
Oid GetNextTempTableSpace(void)
File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
int FileStartReadV(struct PgAioHandle *ioh, File file, int iovcnt, off_t offset, uint32 wait_event_info)
bool AcquireExternalFD(void)
DIR * AllocateDir(const char *dirname)
void InitFileAccess(void)
File OpenTemporaryFile(bool interXact)
int durable_unlink(const char *fname, int elevel)
PGDLLIMPORT int max_files_per_process
void ReserveExternalFD(void)
struct dirent * ReadDir(DIR *dir, const char *dirname)
bool looks_like_temp_rel_name(const char *name)
bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure)
void set_max_safe_fds(void)
static ssize_t FileRead(File file, void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
void PathNameCreateTemporaryDir(const char *basedir, const char *directory)
FILE * AllocateFile(const char *name, const char *mode)
static ssize_t FileWrite(File file, const void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
int OpenTransientFile(const char *fileName, int fileFlags)
void InitTemporaryFileAccess(void)
int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
ssize_t FileWriteV(File file, const struct iovec *iov, int iovcnt, off_t offset, uint32 wait_event_info)
void SyncDataDirectory(void)
int FileZero(File file, off_t offset, off_t amount, uint32 wait_event_info)
off_t FileSize(File file)
ssize_t FileReadV(File file, const struct iovec *iov, int iovcnt, off_t offset, uint32 wait_event_info)
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
int pg_truncate(const char *path, off_t length)
void SetTempTablespaces(Oid *tableSpaces, int numSpaces)
void TempTablespacePath(char *path, Oid tablespace)
static PgChecksumMode mode
static int fd(const char *x, int i)
static const char * directory