106#if defined(HAVE_SYNC_FILE_RANGE)
107#define PG_FLUSH_DATA_WORKS 1
108#elif !defined(WIN32) && defined(MS_ASYNC)
109#define PG_FLUSH_DATA_WORKS 1
110#elif defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
111#define PG_FLUSH_DATA_WORKS 1
129#define NUM_RESERVED_FDS 10
175 int _do_db_save_errno = errno; \
177 errno = _do_db_save_errno; \
184#define VFD_CLOSED (-1)
186#define FileIsValid(file) \
187 ((file) > 0 && (file) < (int) SizeVfdCache && VfdCache[file].fileName != NULL)
189#define FileIsNotOpen(file) (VfdCache[file].fd == VFD_CLOSED)
192#define FD_DELETE_AT_CLOSE (1 << 0)
193#define FD_CLOSE_AT_EOXACT (1 << 1)
194#define FD_TEMP_FILE_LIMIT (1 << 2)
239#ifdef USE_ASSERT_CHECKING
344static void walkdir(
const char *path,
345 void (*action) (
const char *fname,
bool isdir,
int elevel),
348#ifdef PG_FLUSH_DATA_WORKS
388#if !defined(WIN32) && defined(USE_ASSERT_CHECKING)
424#if defined(HAVE_FSYNC_WRITETHROUGH)
462#if defined(F_FULLFSYNC)
511 errmsg(
"could not access file \"%s\": %m",
name)));
537#if defined(HAVE_SYNC_FILE_RANGE)
580 errmsg(
"could not flush dirty data: %m")));
586#if !defined(WIN32) && defined(MS_ASYNC)
603 if (offset == 0 && nbytes == 0)
610 errmsg(
"could not determine dirty data size: %m")));
652 errmsg(
"could not flush dirty data: %m")));
656 rc =
munmap(p, (
size_t) nbytes);
662 errmsg(
"could not munmap() while flushing data: %m")));
669#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
688 errmsg(
"could not flush dirty data: %m")));
737 ret = truncate(path, length);
835 errmsg(
"could not rename file \"%s\" to \"%s\": %m",
875 errmsg(
"could not remove file \"%s\": %m",
909 errmsg(
"out of memory")));
941#ifdef USE_ASSERT_CHECKING
975 fd = (
int *)
palloc(size *
sizeof(
int));
1003 elog(
WARNING,
"duplicating stderr file descriptor failed after %d successes: %m", used);
1022 for (
j = 0;
j < used;
j++)
1071 errmsg(
"insufficient file descriptors available to start server process"),
1072 errdetail(
"System allows %d, server needs at least %d, %d files are already open.",
1077 elog(
DEBUG2,
"max_safe_fds = %d, usable_fds = %d, already_open = %d",
1113#ifdef PG_O_DIRECT_USE_F_NOCACHE
1116 fd =
open(fileName, fileFlags, fileMode);
1121#ifdef PG_O_DIRECT_USE_F_NOCACHE
1144 errmsg(
"out of file descriptors: %m; release and retry")));
1288 "could not close file \"%s\": %m",
vfdP->fileName);
1427 errmsg(
"out of memory")));
1461 file,
vfdP->fileName ?
vfdP->fileName :
""));
1468 vfdP->fdstate = 0x0;
1494 else if (
VfdCache[0].lruLessRecently != file)
1520 (
errmsg(
"temporary file: path \"%s\", size %lu",
1521 path, (
unsigned long) size)));
1579 fileName, fileFlags, fileMode));
1588 errmsg(
"out of memory")));
1622 vfdP->fileMode = fileMode;
1624 vfdP->fdstate = 0x0;
1659 errmsg(
"cannot create temporary directory \"%s\": %m",
1666 errmsg(
"cannot create temporary subdirectory \"%s\": %m",
1824 elog(
ERROR,
"could not create temporary file \"%s\": %m",
1863 errmsg(
"could not create temporary file \"%s\": %m",
1899 errmsg(
"could not open temporary file \"%s\": %m",
1940 errmsg(
"could not unlink temporary file \"%s\": %m",
1952 errmsg(
"could not stat file \"%s\": %m", path)));
1985 "could not close file \"%s\": %m",
vfdP->fileName);
2030 errmsg(
"could not delete file \"%s\": %m",
vfdP->fileName)));
2040 errmsg(
"could not stat file \"%s\": %m",
vfdP->fileName)));
2071#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
2090#elif defined(__darwin__)
2103 ra.ra_offset = offset;
2269 errmsg(
"temporary file size exceeds \"temp_file_limit\" (%dkB)",
2406#ifdef HAVE_POSIX_FALLOCATE
2572 errmsg(
"out of memory")));
2635 errmsg(
"exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2659 errmsg(
"out of file descriptors: %m; release and retry")));
2687 DO_DB(
elog(
LOG,
"OpenTransientFile: Allocated %d (%s)",
2694 errmsg(
"exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2739 errmsg(
"exceeded maxAllocatedDescs (%d) while trying to execute command \"%s\"",
2768 errmsg(
"out of file descriptors: %m; release and retry")));
2804 elog(
ERROR,
"AllocateDesc kind not recognized");
2839 elog(
WARNING,
"file passed to FreeFile was not obtained from AllocateFile");
2867 elog(
WARNING,
"fd passed to CloseTransientFile was not obtained from OpenTransientFile");
2898 errmsg(
"exceeded maxAllocatedDescs (%d) while trying to open directory \"%s\"",
2922 errmsg(
"out of file descriptors: %m; release and retry")));
2977 errmsg(
"could not open directory \"%s\": %m",
2989 errmsg(
"could not read directory \"%s\": %m",
3025 elog(
WARNING,
"dir passed to FreeDir was not obtained from AllocateDir");
3051 elog(
WARNING,
"file passed to ClosePipeStream was not obtained from OpenPipeStream");
3229#ifdef USE_ASSERT_CHECKING
3277 "temporary file %s not closed at end-of-transaction",
3289 elog(
WARNING,
"%d temporary files and directories not closed at end-of-transaction",
3416 errmsg(
"could not remove directory \"%s\": %m",
3424 errmsg(
"could not remove file \"%s\": %m",
3430 (
errmsg(
"unexpected file found in temporary-files directory: \"%s\"",
3486 errmsg(
"could not remove file \"%s\": %m",
3505 for (pos = 1;
isdigit((
unsigned char)
name[pos]); ++pos)
3507 if (pos == 1 ||
name[pos] !=
'_')
3517 if (
name[pos] ==
'_')
3525 if (
name[pos] ==
'.')
3537 if (
name[pos] !=
'\0')
3556 errmsg(
"could not open file \"%s\": %m", path)));
3562 errmsg(
"could not synchronize file system for file \"%s\": %m", path)));
3607 if (
lstat(
"pg_wal", &st) < 0)
3610 errmsg(
"could not stat file \"%s\": %m",
3655#ifdef PG_FLUSH_DATA_WORKS
3705 void (*action) (
const char *fname,
bool isdir,
int elevel),
3720 if (
strcmp(
de->d_name,
".") == 0 ||
3729 (*action) (
subpath,
false, elevel);
3754 (*action) (path,
true, elevel);
3764#ifdef PG_FLUSH_DATA_WORKS
3786 errmsg(
"could not open file \"%s\": %m", fname)));
3799 errmsg(
"could not close file \"%s\": %m", fname)));
3825 errmsg(
"could not remove directory \"%s\": %m", fname)));
3876 errmsg(
"could not open file \"%s\": %m", fname)));
3897 errmsg(
"could not fsync file \"%s\": %m", fname)));
3905 errmsg(
"could not close file \"%s\": %m", fname)));
4021 char *item = (
char *)
lfirst(l);
4041#if XLOG_BLCKSZ < PG_IO_ALIGN_SIZE
4045 "debug_io_direct",
"XLOG_BLCKSZ");
4049#if BLCKSZ < PG_IO_ALIGN_SIZE
4053 "debug_io_direct",
"BLCKSZ");
4069 *((
int *) *extra) = flags;
4077 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 errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#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 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