107#if defined(HAVE_SYNC_FILE_RANGE)
108#define PG_FLUSH_DATA_WORKS 1
109#elif !defined(WIN32) && defined(MS_ASYNC)
110#define PG_FLUSH_DATA_WORKS 1
111#elif defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
112#define PG_FLUSH_DATA_WORKS 1
130#define NUM_RESERVED_FDS 10
179 int _do_db_save_errno = errno; \
181 errno = _do_db_save_errno; \
188#define VFD_CLOSED (-1)
190#define FileIsValid(file) \
191 ((file) > 0 && (file) < (int) SizeVfdCache && VfdCache[file].fileName != NULL)
193#define FileIsNotOpen(file) (VfdCache[file].fd == VFD_CLOSED)
196#define FD_DELETE_AT_CLOSE (1 << 0)
197#define FD_CLOSE_AT_EOXACT (1 << 1)
198#define FD_TEMP_FILE_LIMIT (1 << 2)
243#ifdef USE_ASSERT_CHECKING
348static void walkdir(
const char *path,
349 void (*action) (
const char *fname,
bool isdir,
int elevel),
352#ifdef PG_FLUSH_DATA_WORKS
392#if !defined(WIN32) && defined(USE_ASSERT_CHECKING)
428#if defined(HAVE_FSYNC_WRITETHROUGH)
466#if defined(F_FULLFSYNC)
515 errmsg(
"could not access file \"%s\": %m",
name)));
541#if defined(HAVE_SYNC_FILE_RANGE)
584 errmsg(
"could not flush dirty data: %m")));
590#if !defined(WIN32) && defined(MS_ASYNC)
607 if (offset == 0 && nbytes == 0)
614 errmsg(
"could not determine dirty data size: %m")));
656 errmsg(
"could not flush dirty data: %m")));
660 rc =
munmap(p, (
size_t) nbytes);
666 errmsg(
"could not munmap() while flushing data: %m")));
673#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
692 errmsg(
"could not flush dirty data: %m")));
741 ret = truncate(path, length);
839 errmsg(
"could not rename file \"%s\" to \"%s\": %m",
879 errmsg(
"could not remove file \"%s\": %m",
913 errmsg(
"out of memory")));
945#ifdef USE_ASSERT_CHECKING
979 fd = (
int *)
palloc(size *
sizeof(
int));
1007 elog(
WARNING,
"duplicating stderr file descriptor failed after %d successes: %m", used);
1026 for (
j = 0;
j < used;
j++)
1075 errmsg(
"insufficient file descriptors available to start server process"),
1076 errdetail(
"System allows %d, server needs at least %d, %d files are already open.",
1081 elog(
DEBUG2,
"max_safe_fds = %d, usable_fds = %d, already_open = %d",
1117#ifdef PG_O_DIRECT_USE_F_NOCACHE
1120 fd =
open(fileName, fileFlags, fileMode);
1125#ifdef PG_O_DIRECT_USE_F_NOCACHE
1148 errmsg(
"out of file descriptors: %m; release and retry")));
1292 "could not close file \"%s\": %m",
vfdP->fileName);
1431 errmsg(
"out of memory")));
1465 file,
vfdP->fileName ?
vfdP->fileName :
""));
1472 vfdP->fdstate = 0x0;
1498 else if (
VfdCache[0].lruLessRecently != file)
1524 (
errmsg(
"temporary file: path \"%s\", size %lu",
1525 path, (
unsigned long) size)));
1583 fileName, fileFlags, fileMode));
1592 errmsg(
"out of memory")));
1626 vfdP->fileMode = fileMode;
1628 vfdP->fdstate = 0x0;
1663 errmsg(
"cannot create temporary directory \"%s\": %m",
1670 errmsg(
"cannot create temporary subdirectory \"%s\": %m",
1828 elog(
ERROR,
"could not create temporary file \"%s\": %m",
1867 errmsg(
"could not create temporary file \"%s\": %m",
1903 errmsg(
"could not open temporary file \"%s\": %m",
1944 errmsg(
"could not unlink temporary file \"%s\": %m",
1956 errmsg(
"could not stat file \"%s\": %m", path)));
1989 "could not close file \"%s\": %m",
vfdP->fileName);
2034 errmsg(
"could not delete file \"%s\": %m",
vfdP->fileName)));
2044 errmsg(
"could not stat file \"%s\": %m",
vfdP->fileName)));
2075#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
2094#elif defined(__darwin__)
2107 ra.ra_offset = offset;
2273 errmsg(
"temporary file size exceeds \"temp_file_limit\" (%dkB)",
2410#ifdef HAVE_POSIX_FALLOCATE
2576 errmsg(
"out of memory")));
2639 errmsg(
"exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2663 errmsg(
"out of file descriptors: %m; release and retry")));
2691 DO_DB(
elog(
LOG,
"OpenTransientFile: Allocated %d (%s)",
2698 errmsg(
"exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2743 errmsg(
"exceeded maxAllocatedDescs (%d) while trying to execute command \"%s\"",
2772 errmsg(
"out of file descriptors: %m; release and retry")));
2808 elog(
ERROR,
"AllocateDesc kind not recognized");
2843 elog(
WARNING,
"file passed to FreeFile was not obtained from AllocateFile");
2871 elog(
WARNING,
"fd passed to CloseTransientFile was not obtained from OpenTransientFile");
2902 errmsg(
"exceeded maxAllocatedDescs (%d) while trying to open directory \"%s\"",
2926 errmsg(
"out of file descriptors: %m; release and retry")));
2981 errmsg(
"could not open directory \"%s\": %m",
2993 errmsg(
"could not read directory \"%s\": %m",
3029 elog(
WARNING,
"dir passed to FreeDir was not obtained from AllocateDir");
3055 elog(
WARNING,
"file passed to ClosePipeStream was not obtained from OpenPipeStream");
3233#ifdef USE_ASSERT_CHECKING
3281 "temporary file %s not closed at end-of-transaction",
3293 elog(
WARNING,
"%d temporary files and directories not closed at end-of-transaction",
3420 errmsg(
"could not remove directory \"%s\": %m",
3428 errmsg(
"could not remove file \"%s\": %m",
3434 (
errmsg(
"unexpected file found in temporary-files directory: \"%s\"",
3490 errmsg(
"could not remove file \"%s\": %m",
3509 for (pos = 1;
isdigit((
unsigned char)
name[pos]); ++pos)
3511 if (pos == 1 ||
name[pos] !=
'_')
3521 if (
name[pos] ==
'_')
3529 if (
name[pos] ==
'.')
3541 if (
name[pos] !=
'\0')
3560 errmsg(
"could not open file \"%s\": %m", path)));
3566 errmsg(
"could not synchronize file system for file \"%s\": %m", path)));
3611 if (
lstat(
"pg_wal", &st) < 0)
3614 errmsg(
"could not stat file \"%s\": %m",
3659#ifdef PG_FLUSH_DATA_WORKS
3709 void (*action) (
const char *fname,
bool isdir,
int elevel),
3724 if (
strcmp(
de->d_name,
".") == 0 ||
3733 (*action) (
subpath,
false, elevel);
3758 (*action) (path,
true, elevel);
3768#ifdef PG_FLUSH_DATA_WORKS
3790 errmsg(
"could not open file \"%s\": %m", fname)));
3803 errmsg(
"could not close file \"%s\": %m", fname)));
3829 errmsg(
"could not remove directory \"%s\": %m", fname)));
3880 errmsg(
"could not open file \"%s\": %m", fname)));
3901 errmsg(
"could not fsync file \"%s\": %m", fname)));
3909 errmsg(
"could not close file \"%s\": %m", fname)));
4025 char *item = (
char *)
lfirst(l);
4045#if XLOG_BLCKSZ < PG_IO_ALIGN_SIZE
4049 "debug_io_direct",
"XLOG_BLCKSZ");
4053#if BLCKSZ < PG_IO_ALIGN_SIZE
4057 "debug_io_direct",
"BLCKSZ");
4073 *((
int *) *extra) = flags;
4081 int *flags = (
int *) extra;
void pgaio_closing_fd(int fd)
void pgaio_io_start_readv(PgAioHandle *ioh, int fd, int iovcnt, uint64 offset)
void begin_startup_progress_phase(void)
#define Assert(condition)
#define MemSet(start, val, len)
#define OidIsValid(objectId)
struct dirent * readdir(DIR *)
DIR * opendir(const char *)
int errcode_for_file_access(void)
int errcode(int sqlerrcode)
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ereport(elevel,...)
int pg_truncate(const char *path, pgoff_t length)
int max_files_per_process
int FileGetRawDesc(File file)
int MakePGDirectory(const char *directoryName)
int recovery_init_sync_method
static const ResourceOwnerDesc file_resowner_desc
int pg_fsync_no_writethrough(int fd)
FILE * OpenPipeStream(const char *command, const char *mode)
static int numTempTableSpaces
static bool ReleaseLruFile(void)
void FileWriteback(File file, pgoff_t offset, pgoff_t nbytes, uint32 wait_event_info)
#define FD_DELETE_AT_CLOSE
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
static int maxAllocatedDescs
static void Delete(File file)
static int FreeDesc(AllocateDesc *desc)
static long tempFileCounter
static char * ResOwnerPrintFile(Datum res)
int durable_rename(const char *oldfile, const char *newfile, int elevel)
char * FilePathName(File file)
static void ResourceOwnerForgetFile(ResourceOwner owner, File file)
static int pg_ftruncate(int fd, pgoff_t length)
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
static int numAllocatedDescs
File PathNameOpenTemporaryFile(const char *path, int mode)
static void LruDelete(File file)
#define FileIsValid(file)
void assign_debug_io_direct(const char *newval, void *extra)
int FileSync(File file, uint32 wait_event_info)
int FileStartReadV(PgAioHandle *ioh, File file, int iovcnt, pgoff_t offset, uint32 wait_event_info)
int CloseTransientFile(int fd)
int BasicOpenFile(const char *fileName, int fileFlags)
static File AllocateVfd(void)
File PathNameCreateTemporaryFile(const char *path, bool error_on_failure)
void PathNameDeleteTemporaryDir(const char *dirname)
int ClosePipeStream(FILE *file)
void AtEOXact_Files(bool isCommit)
int FileGetRawFlags(File file)
static int nextTempTableSpace
#define FD_CLOSE_AT_EOXACT
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
static void unlink_if_exists_fname(const char *fname, bool isdir, int elevel)
static void ResOwnerReleaseFile(Datum res)
static void RemovePgTempRelationFiles(const char *tsdirname)
ssize_t FileReadV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info)
mode_t FileGetRawMode(File file)
static AllocateDesc * allocatedDescs
struct dirent * ReadDirExtended(DIR *dir, const char *dirname, int elevel)
static void count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
int FileFallocate(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info)
static int FileAccess(File file)
pgoff_t FileSize(File file)
static void FreeVfd(File file)
int pg_fsync_writethrough(int fd)
void FileClose(File file)
void ReleaseExternalFD(void)
#define FD_TEMP_FILE_LIMIT
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
bool pg_file_exists(const char *name)
void RemovePgTempFiles(void)
#define FileIsNotOpen(file)
bool TempTablespacesAreSet(void)
void fsync_fname(const char *fname, bool isdir)
int data_sync_elevel(int elevel)
File PathNameOpenFile(const char *fileName, int fileFlags)
static void Insert(File file)
Oid GetNextTempTableSpace(void)
File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
static void datadir_fsync_fname(const char *fname, bool isdir, int elevel)
static void ReportTemporaryFileUsage(const char *path, pgoff_t size)
static File OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError)
void pg_flush_data(int fd, pgoff_t offset, pgoff_t nbytes)
bool AcquireExternalFD(void)
static void RegisterTemporaryFile(File file)
DIR * AllocateDir(const char *dirname)
static Oid * tempTableSpaces
int FileTruncate(File file, pgoff_t offset, uint32 wait_event_info)
static bool reserveAllocatedDesc(void)
void InitFileAccess(void)
static void RemovePgTempRelationFilesInDbspace(const char *dbspacedirname)
File OpenTemporaryFile(bool interXact)
int durable_unlink(const char *fname, int elevel)
static uint64 temporary_files_size
void ReserveExternalFD(void)
int FileZero(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info)
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 void CleanupTempFiles(bool isCommit, bool isProcExit)
static bool have_xact_temporary_files
static int LruInsert(File file)
static int numExternalFDs
static int fsync_parent_path(const char *fname, int elevel)
void PathNameCreateTemporaryDir(const char *basedir, const char *directory)
FILE * AllocateFile(const char *name, const char *mode)
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)
int FilePrefetch(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info)
ssize_t FileWriteV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info)
static void ReleaseLruFiles(void)
void SyncDataDirectory(void)
bool check_debug_io_direct(char **newval, void **extra, GucSource source)
static void ResourceOwnerRememberFile(ResourceOwner owner, File file)
static void BeforeShmemExit_Files(int code, Datum arg)
static void walkdir(const char *path, void(*action)(const char *fname, bool isdir, int elevel), bool process_symlinks, int elevel)
void SetTempTablespaces(Oid *tableSpaces, int numSpaces)
void TempTablespacePath(char *path, Oid tablespace)
#define DEFAULT_FILE_EXTEND_METHOD
#define IO_DIRECT_WAL_INIT
ssize_t pg_pwrite_zeros(int fd, size_t size, pgoff_t offset)
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
#define PG_TEMP_FILES_DIR
#define PG_TEMP_FILE_PREFIX
@ DATA_DIR_SYNC_METHOD_SYNCFS
@ DATA_DIR_SYNC_METHOD_FSYNC
void * guc_malloc(int elevel, size_t size)
#define GUC_check_errdetail
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
void list_free(List *list)
Datum subpath(PG_FUNCTION_ARGS)
char * pstrdup(const char *in)
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
#define CHECK_FOR_INTERRUPTS()
static PgChecksumMode mode
static ssize_t pg_preadv(int fd, const struct iovec *iov, int iovcnt, pgoff_t offset)
static ssize_t pg_pwritev(int fd, const struct iovec *iov, int iovcnt, pgoff_t offset)
uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax)
pg_prng_state pg_global_prng_state
static rewind_source * source
static char buf[DEFAULT_XLOG_SEG_SIZE]
void pgstat_report_tempfile(size_t filesize)
int pg_strcasecmp(const char *s1, const char *s2)
void get_parent_directory(char *path)
size_t strlcpy(char *dst, const char *src, size_t siz)
static Datum Int32GetDatum(int32 X)
static int32 DatumGetInt32(Datum X)
static int fd(const char *x, int i)
char * psprintf(const char *fmt,...)
int forkname_chars(const char *str, ForkNumber *fork)
#define TABLESPACE_VERSION_DIRECTORY
ResourceOwner CurrentResourceOwner
void ResourceOwnerForget(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
void ResourceOwnerRemember(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
void ResourceOwnerEnlarge(ResourceOwner owner)
@ RESOURCE_RELEASE_AFTER_LOCKS
#define RELEASE_PRIO_FILES
void pg_usleep(long microsec)
#define ereport_startup_progress(msg,...)
SubTransactionId create_subid
union AllocateDesc::@20 desc
bool SplitGUCList(char *rawstring, char separator, List **namelist)
static void pgstat_report_wait_start(uint32 wait_event_info)
static void pgstat_report_wait_end(void)
void _dosmaperr(unsigned long)
SubTransactionId GetCurrentSubTransactionId(void)
@ WAL_SYNC_METHOD_FSYNC_WRITETHROUGH
static const char * directory