PostgreSQL Source Code git master
Loading...
Searching...
No Matches
fd.h File Reference
#include "port/pg_iovec.h"
#include <dirent.h>
#include <fcntl.h>
Include dependency graph for fd.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define IO_DIRECT_DATA   0x01
 
#define IO_DIRECT_WAL   0x02
 
#define IO_DIRECT_WAL_INIT   0x04
 
#define DEFAULT_FILE_EXTEND_METHOD   0
 
#define FILE_POSSIBLY_DELETED(err)   ((err) == ENOENT)
 
#define PG_O_DIRECT   0
 

Typedefs

typedef int File
 

Enumerations

enum  FileExtendMethod { FILE_EXTEND_METHOD_WRITE_ZEROS }
 

Functions

File PathNameOpenFile (const char *fileName, int fileFlags)
 
File PathNameOpenFilePerm (const char *fileName, int fileFlags, mode_t fileMode)
 
File OpenTemporaryFile (bool interXact)
 
void FileClose (File file)
 
int FilePrefetch (File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info)
 
ssize_t FileReadV (File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info)
 
ssize_t FileWriteV (File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info)
 
int FileStartReadV (struct PgAioHandle *ioh, File file, int iovcnt, pgoff_t offset, uint32 wait_event_info)
 
int FileSync (File file, uint32 wait_event_info)
 
int FileZero (File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info)
 
int FileFallocate (File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info)
 
pgoff_t FileSize (File file)
 
int FileTruncate (File file, pgoff_t offset, uint32 wait_event_info)
 
void FileWriteback (File file, pgoff_t offset, pgoff_t nbytes, uint32 wait_event_info)
 
charFilePathName (File file)
 
int FileGetRawDesc (File file)
 
int FileGetRawFlags (File file)
 
mode_t FileGetRawMode (File file)
 
File PathNameCreateTemporaryFile (const char *path, bool error_on_failure)
 
File PathNameOpenTemporaryFile (const char *path, int mode)
 
bool PathNameDeleteTemporaryFile (const char *path, bool error_on_failure)
 
void PathNameCreateTemporaryDir (const char *basedir, const char *directory)
 
void PathNameDeleteTemporaryDir (const char *dirname)
 
void TempTablespacePath (char *path, Oid tablespace)
 
FILEAllocateFile (const char *name, const char *mode)
 
int FreeFile (FILE *file)
 
FILEOpenPipeStream (const char *command, const char *mode)
 
int ClosePipeStream (FILE *file)
 
DIRAllocateDir (const char *dirname)
 
struct direntReadDir (DIR *dir, const char *dirname)
 
struct direntReadDirExtended (DIR *dir, const char *dirname, int elevel)
 
int FreeDir (DIR *dir)
 
int OpenTransientFile (const char *fileName, int fileFlags)
 
int OpenTransientFilePerm (const char *fileName, int fileFlags, mode_t fileMode)
 
int CloseTransientFile (int fd)
 
int BasicOpenFile (const char *fileName, int fileFlags)
 
int BasicOpenFilePerm (const char *fileName, int fileFlags, mode_t fileMode)
 
bool AcquireExternalFD (void)
 
void ReserveExternalFD (void)
 
void ReleaseExternalFD (void)
 
int MakePGDirectory (const char *directoryName)
 
void InitFileAccess (void)
 
void InitTemporaryFileAccess (void)
 
void set_max_safe_fds (void)
 
void closeAllVfds (void)
 
void SetTempTablespaces (Oid *tableSpaces, int numSpaces)
 
bool TempTablespacesAreSet (void)
 
int GetTempTablespaces (Oid *tableSpaces, int numSpaces)
 
Oid GetNextTempTableSpace (void)
 
void AtEOXact_Files (bool isCommit)
 
void AtEOSubXact_Files (bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
 
void RemovePgTempFiles (void)
 
void RemovePgTempFilesInDir (const char *tmpdirname, bool missing_ok, bool unlink_all)
 
bool looks_like_temp_rel_name (const char *name)
 
int pg_fsync (int fd)
 
int pg_fsync_no_writethrough (int fd)
 
int pg_fsync_writethrough (int fd)
 
int pg_fdatasync (int fd)
 
bool pg_file_exists (const char *name)
 
void pg_flush_data (int fd, pgoff_t offset, pgoff_t nbytes)
 
int pg_truncate (const char *path, pgoff_t length)
 
void fsync_fname (const char *fname, bool isdir)
 
int fsync_fname_ext (const char *fname, bool isdir, bool ignore_perm, int elevel)
 
int durable_rename (const char *oldfile, const char *newfile, int elevel)
 
int durable_unlink (const char *fname, int elevel)
 
void SyncDataDirectory (void)
 
int data_sync_elevel (int elevel)
 
static ssize_t FileRead (File file, void *buffer, size_t amount, pgoff_t offset, uint32 wait_event_info)
 
static ssize_t FileWrite (File file, const void *buffer, size_t amount, pgoff_t offset, uint32 wait_event_info)
 

Variables

PGDLLIMPORT int max_files_per_process
 
PGDLLIMPORT bool data_sync_retry
 
PGDLLIMPORT int recovery_init_sync_method
 
PGDLLIMPORT int io_direct_flags
 
PGDLLIMPORT int file_extend_method
 
PGDLLIMPORT int max_safe_fds
 

Macro Definition Documentation

◆ DEFAULT_FILE_EXTEND_METHOD

#define DEFAULT_FILE_EXTEND_METHOD   0

Definition at line 67 of file fd.h.

◆ FILE_POSSIBLY_DELETED

#define FILE_POSSIBLY_DELETED (   err)    ((err) == ENOENT)

Definition at line 89 of file fd.h.

◆ IO_DIRECT_DATA

#define IO_DIRECT_DATA   0x01

Definition at line 54 of file fd.h.

◆ IO_DIRECT_WAL

#define IO_DIRECT_WAL   0x02

Definition at line 55 of file fd.h.

◆ IO_DIRECT_WAL_INIT

#define IO_DIRECT_WAL_INIT   0x04

Definition at line 56 of file fd.h.

◆ PG_O_DIRECT

#define PG_O_DIRECT   0

Definition at line 123 of file fd.h.

Typedef Documentation

◆ File

Definition at line 51 of file fd.h.

Enumeration Type Documentation

◆ FileExtendMethod

Enumerator
FILE_EXTEND_METHOD_WRITE_ZEROS 

Definition at line 58 of file fd.h.

59{
60#ifdef HAVE_POSIX_FALLOCATE
62#endif
64};
@ FILE_EXTEND_METHOD_WRITE_ZEROS
Definition fd.h:63
static int fb(int x)

Function Documentation

◆ AcquireExternalFD()

bool AcquireExternalFD ( void  )
extern

Definition at line 1172 of file fd.c.

1173{
1174 /*
1175 * We don't want more than max_safe_fds / 3 FDs to be consumed for
1176 * "external" FDs.
1177 */
1178 if (numExternalFDs < max_safe_fds / 3)
1179 {
1181 return true;
1182 }
1183 errno = EMFILE;
1184 return false;
1185}
int max_safe_fds
Definition fd.c:160
void ReserveExternalFD(void)
Definition fd.c:1207
static int numExternalFDs
Definition fd.c:278

References fb(), max_safe_fds, numExternalFDs, and ReserveExternalFD().

Referenced by CreateWaitEventSet(), and libpqsrv_connect_prepare().

◆ AllocateDir()

DIR * AllocateDir ( const char dirname)
extern

Definition at line 2891 of file fd.c.

2892{
2893 DIR *dir;
2894
2895 DO_DB(elog(LOG, "AllocateDir: Allocated %d (%s)",
2896 numAllocatedDescs, dirname));
2897
2898 /* Can we allocate another non-virtual FD? */
2899 if (!reserveAllocatedDesc())
2900 ereport(ERROR,
2902 errmsg("exceeded maxAllocatedDescs (%d) while trying to open directory \"%s\"",
2903 maxAllocatedDescs, dirname)));
2904
2905 /* Close excess kernel FDs. */
2907
2908TryAgain:
2909 if ((dir = opendir(dirname)) != NULL)
2910 {
2912
2913 desc->kind = AllocateDescDir;
2914 desc->desc.dir = dir;
2917 return desc->desc.dir;
2918 }
2919
2920 if (errno == EMFILE || errno == ENFILE)
2921 {
2922 int save_errno = errno;
2923
2924 ereport(LOG,
2926 errmsg("out of file descriptors: %m; release and retry")));
2927 errno = 0;
2928 if (ReleaseLruFile())
2929 goto TryAgain;
2930 errno = save_errno;
2931 }
2932
2933 return NULL;
2934}
DIR * opendir(const char *)
Definition dirent.c:33
int errcode(int sqlerrcode)
Definition elog.c:874
#define LOG
Definition elog.h:31
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
static bool ReleaseLruFile(void)
Definition fd.c:1370
static int maxAllocatedDescs
Definition fd.c:272
static int numAllocatedDescs
Definition fd.c:271
#define DO_DB(A)
Definition fd.c:184
static AllocateDesc * allocatedDescs
Definition fd.c:273
@ AllocateDescDir
Definition fd.c:255
static bool reserveAllocatedDesc(void)
Definition fd.c:2553
static void ReleaseLruFiles(void)
Definition fd.c:1392
static char * errmsg
SubTransactionId create_subid
Definition fd.c:262
DIR * dir
Definition fd.c:266
union AllocateDesc::@20 desc
AllocateDescKind kind
Definition fd.c:261
Definition dirent.c:26
SubTransactionId GetCurrentSubTransactionId(void)
Definition xact.c:793

References allocatedDescs, AllocateDescDir, AllocateDesc::create_subid, AllocateDesc::desc, AllocateDesc::dir, DO_DB, elog, ereport, errcode(), errmsg, ERROR, fb(), GetCurrentSubTransactionId(), AllocateDesc::kind, LOG, maxAllocatedDescs, numAllocatedDescs, opendir(), ReleaseLruFile(), ReleaseLruFiles(), and reserveAllocatedDesc().

Referenced by calculate_database_size(), calculate_tablespace_size(), CheckPointLogicalRewriteHeap(), CheckPointSnapBuild(), CheckTablespaceDirectory(), CleanupBackupHistory(), copydir(), db_dir_size(), DeleteAllExportedSnapshotFiles(), destroy_tablespace_directories(), directory_is_empty(), do_pg_backup_start(), dsm_cleanup_for_mmap(), extension_file_exists(), get_ext_ver_list(), GetConfFilesInDir(), getInstallationPaths(), GetWalSummaries(), movedb(), ParseTzFile(), perform_base_backup(), pg_available_extension_versions(), pg_available_extensions(), pg_ls_dir(), pg_ls_dir_files(), pg_tablespace_databases(), pg_tzenumerate_next(), pg_tzenumerate_start(), pgarch_readyXlog(), RelationCacheInitFileRemove(), RelationCacheInitFileRemoveInDir(), RemoveNonParentXlogFiles(), RemoveOldXlogFiles(), RemovePgTempFiles(), RemovePgTempFilesInDir(), RemovePgTempRelationFiles(), RemovePgTempRelationFilesInDbspace(), RemoveTempXlogFiles(), ReorderBufferCleanupSerializedTXNs(), ResetUnloggedRelations(), ResetUnloggedRelationsInDbspaceDir(), ResetUnloggedRelationsInTablespaceDir(), restoreTwoPhaseData(), scan_directory_ci(), sendDir(), SlruScanDirectory(), StartupReorderBuffer(), StartupReplicationSlots(), SyncDataDirectory(), UpdateLogicalMappings(), walkdir(), and XLogGetOldestSegno().

◆ AllocateFile()

FILE * AllocateFile ( const char name,
const char mode 
)
extern

Definition at line 2628 of file fd.c.

2629{
2630 FILE *file;
2631
2632 DO_DB(elog(LOG, "AllocateFile: Allocated %d (%s)",
2634
2635 /* Can we allocate another non-virtual FD? */
2636 if (!reserveAllocatedDesc())
2637 ereport(ERROR,
2639 errmsg("exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2641
2642 /* Close excess kernel FDs. */
2644
2645TryAgain:
2646 if ((file = fopen(name, mode)) != NULL)
2647 {
2649
2650 desc->kind = AllocateDescFile;
2651 desc->desc.file = file;
2654 return desc->desc.file;
2655 }
2656
2657 if (errno == EMFILE || errno == ENFILE)
2658 {
2659 int save_errno = errno;
2660
2661 ereport(LOG,
2663 errmsg("out of file descriptors: %m; release and retry")));
2664 errno = 0;
2665 if (ReleaseLruFile())
2666 goto TryAgain;
2667 errno = save_errno;
2668 }
2669
2670 return NULL;
2671}
@ AllocateDescFile
Definition fd.c:253
static PgChecksumMode mode
FILE * file
Definition fd.c:265
const char * name

References allocatedDescs, AllocateDescFile, AllocateDesc::create_subid, AllocateDesc::desc, DO_DB, elog, ereport, errcode(), errmsg, ERROR, fb(), AllocateDesc::file, GetCurrentSubTransactionId(), AllocateDesc::kind, LOG, maxAllocatedDescs, mode, name, numAllocatedDescs, ReleaseLruFile(), ReleaseLruFiles(), and reserveAllocatedDesc().

Referenced by AlterSystemSetConfigFile(), apw_dump_now(), apw_load_buffers(), BeginCopyFrom(), BeginCopyTo(), checkControlFile(), do_pg_backup_stop(), entry_reset(), existsTimeLineHistory(), ExportSnapshot(), gc_qtexts(), GetHugePageSize(), ImportSnapshot(), load_dh_file(), load_relcache_init_file(), open_auth_file(), parse_extension_control_file(), ParseConfigFile(), ParseTzFile(), pg_current_logfile(), pg_promote(), pgss_shmem_shutdown(), pgss_shmem_startup(), pgstat_read_statsfile(), pgstat_write_statsfile(), read_backup_label(), read_binary_file(), read_tablespace_map(), read_whole_file(), readTimeLineHistory(), test_custom_stats_var_from_serialized_data(), test_custom_stats_var_to_serialized_data(), tsearch_readline_begin(), ValidatePgVersion(), write_relcache_init_file(), XLogArchiveForceDone(), and XLogArchiveNotify().

◆ AtEOSubXact_Files()

void AtEOSubXact_Files ( bool  isCommit,
SubTransactionId  mySubid,
SubTransactionId  parentSubid 
)
extern

Definition at line 3181 of file fd.c.

3183{
3184 Index i;
3185
3186 for (i = 0; i < numAllocatedDescs; i++)
3187 {
3188 if (allocatedDescs[i].create_subid == mySubid)
3189 {
3190 if (isCommit)
3192 else
3193 {
3194 /* have to recheck the item after FreeDesc (ugly) */
3196 }
3197 }
3198 }
3199}
unsigned int Index
Definition c.h:700
static int FreeDesc(AllocateDesc *desc)
Definition fd.c:2787
int i
Definition isn.c:77

References allocatedDescs, AllocateDesc::create_subid, fb(), FreeDesc(), i, and numAllocatedDescs.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

◆ AtEOXact_Files()

void AtEOXact_Files ( bool  isCommit)
extern

Definition at line 3214 of file fd.c.

3215{
3216 CleanupTempFiles(isCommit, false);
3218 numTempTableSpaces = -1;
3219}
static int numTempTableSpaces
Definition fd.c:293
static Oid * tempTableSpaces
Definition fd.c:292
static void CleanupTempFiles(bool isCommit, bool isProcExit)
Definition fd.c:3251

References CleanupTempFiles(), fb(), numTempTableSpaces, and tempTableSpaces.

Referenced by AbortTransaction(), AutoVacLauncherMain(), BackgroundWriterMain(), CheckpointerMain(), CommitTransaction(), pgarch_archiveXlog(), PrepareTransaction(), WalSummarizerMain(), and WalWriterMain().

◆ BasicOpenFile()

int BasicOpenFile ( const char fileName,
int  fileFlags 
)
extern

Definition at line 1090 of file fd.c.

1091{
1092 return BasicOpenFilePerm(fileName, fileFlags, pg_file_create_mode);
1093}
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition fd.c:1112
int pg_file_create_mode
Definition file_perm.c:19

References BasicOpenFilePerm(), and pg_file_create_mode.

Referenced by AlterSystemSetConfigFile(), ReadControlFile(), update_controlfile(), wal_segment_open(), WalSndSegmentOpen(), WriteControlFile(), XLogFileInit(), XLogFileInitInternal(), XLogFileOpen(), and XLogFileRead().

◆ BasicOpenFilePerm()

int BasicOpenFilePerm ( const char fileName,
int  fileFlags,
mode_t  fileMode 
)
extern

Definition at line 1112 of file fd.c.

1113{
1114 int fd;
1115
1116tryAgain:
1117#ifdef PG_O_DIRECT_USE_F_NOCACHE
1118 fd = open(fileName, fileFlags & ~PG_O_DIRECT, fileMode);
1119#else
1120 fd = open(fileName, fileFlags, fileMode);
1121#endif
1122
1123 if (fd >= 0)
1124 {
1125#ifdef PG_O_DIRECT_USE_F_NOCACHE
1126 if (fileFlags & PG_O_DIRECT)
1127 {
1128 if (fcntl(fd, F_NOCACHE, 1) < 0)
1129 {
1130 int save_errno = errno;
1131
1132 close(fd);
1133 errno = save_errno;
1134 return -1;
1135 }
1136 }
1137#endif
1138
1139 return fd; /* success! */
1140 }
1141
1142 if (errno == EMFILE || errno == ENFILE)
1143 {
1144 int save_errno = errno;
1145
1146 ereport(LOG,
1148 errmsg("out of file descriptors: %m; release and retry")));
1149 errno = 0;
1150 if (ReleaseLruFile())
1151 goto tryAgain;
1152 errno = save_errno;
1153 }
1154
1155 return -1; /* failure */
1156}
#define PG_O_DIRECT
Definition fd.h:123
#define close(a)
Definition win32.h:12
static int fd(const char *x, int i)

References close, ereport, errcode(), errmsg, fb(), fd(), LOG, PG_O_DIRECT, and ReleaseLruFile().

Referenced by BasicOpenFile(), LruInsert(), OpenTransientFilePerm(), PathNameOpenFilePerm(), and readRecoverySignalFile().

◆ closeAllVfds()

void closeAllVfds ( void  )
extern

Definition at line 3068 of file fd.c.

3069{
3070 Index i;
3071
3072 if (SizeVfdCache > 0)
3073 {
3074 Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
3075 for (i = 1; i < SizeVfdCache; i++)
3076 {
3077 if (!FileIsNotOpen(i))
3078 LruDelete(i);
3079 }
3080 }
3081}
#define Assert(condition)
Definition c.h:945
static void LruDelete(File file)
Definition fd.c:1273
static Size SizeVfdCache
Definition fd.c:221
#define FileIsNotOpen(file)
Definition fd.c:193

References Assert, FileIsNotOpen, i, LruDelete(), and SizeVfdCache.

Referenced by standard_ProcessUtility().

◆ ClosePipeStream()

int ClosePipeStream ( FILE file)
extern

Definition at line 3039 of file fd.c.

3040{
3041 int i;
3042
3043 DO_DB(elog(LOG, "ClosePipeStream: Allocated %d", numAllocatedDescs));
3044
3045 /* Remove file from list of allocated files, if it's present */
3046 for (i = numAllocatedDescs; --i >= 0;)
3047 {
3048 AllocateDesc *desc = &allocatedDescs[i];
3049
3050 if (desc->kind == AllocateDescPipe && desc->desc.file == file)
3051 return FreeDesc(desc);
3052 }
3053
3054 /* Only get here if someone passes us a file not in allocatedDescs */
3055 elog(WARNING, "file passed to ClosePipeStream was not obtained from OpenPipeStream");
3056
3057 return pclose(file);
3058}
#define WARNING
Definition elog.h:36
@ AllocateDescPipe
Definition fd.c:254

References allocatedDescs, AllocateDescPipe, AllocateDesc::desc, DO_DB, elog, fb(), AllocateDesc::file, FreeDesc(), i, AllocateDesc::kind, LOG, numAllocatedDescs, and WARNING.

Referenced by ClosePipeFromProgram(), ClosePipeToProgram(), pg_import_system_collations(), run_ssl_passphrase_command(), and shell_finish_command().

◆ CloseTransientFile()

int CloseTransientFile ( int  fd)
extern

Definition at line 2855 of file fd.c.

2856{
2857 int i;
2858
2859 DO_DB(elog(LOG, "CloseTransientFile: Allocated %d", numAllocatedDescs));
2860
2861 /* Remove fd from list of allocated files, if it's present */
2862 for (i = numAllocatedDescs; --i >= 0;)
2863 {
2864 AllocateDesc *desc = &allocatedDescs[i];
2865
2866 if (desc->kind == AllocateDescRawFD && desc->desc.fd == fd)
2867 return FreeDesc(desc);
2868 }
2869
2870 /* Only get here if someone passes us a file not in allocatedDescs */
2871 elog(WARNING, "fd passed to CloseTransientFile was not obtained from OpenTransientFile");
2872
2874
2875 return close(fd);
2876}
void pgaio_closing_fd(int fd)
Definition aio.c:1220
@ AllocateDescRawFD
Definition fd.c:256
int fd
Definition fd.c:267

References allocatedDescs, AllocateDescRawFD, close, AllocateDesc::desc, DO_DB, elog, AllocateDesc::fd, fd(), FreeDesc(), i, AllocateDesc::kind, LOG, numAllocatedDescs, pgaio_closing_fd(), and WARNING.

Referenced by ApplyLogicalMappingFile(), be_lo_export(), CheckPointLogicalRewriteHeap(), CheckPointReplicationOrigin(), clone_file(), compare_files(), copy_file(), CreateDirAndVersionFile(), dsm_impl_mmap(), durable_rename(), fsync_fname_ext(), get_controlfile_by_exact_path(), heap_xlog_logical_rewrite(), lo_import_internal(), perform_base_backup(), pg_truncate(), qtext_load_file(), qtext_store(), read_relmap_file(), ReadTwoPhaseFile(), RecreateTwoPhaseFile(), ReorderBufferSerializeChange(), ReorderBufferSerializeTXN(), RestoreSlotFromDisk(), SaveSlotToPath(), sendFile(), SendTimeLineHistory(), SimpleLruDoesPhysicalPageExist(), SimpleLruWriteAll(), SlruInternalWritePage(), SlruPhysicalReadPage(), SlruPhysicalWritePage(), SlruSyncFileTag(), SnapBuildRestoreContents(), SnapBuildRestoreSnapshot(), SnapBuildSerialize(), StartupReplicationOrigin(), write_relmap_file(), writeTimeLineHistory(), writeTimeLineHistoryFile(), and XLogFileCopy().

◆ data_sync_elevel()

◆ durable_rename()

int durable_rename ( const char oldfile,
const char newfile,
int  elevel 
)
extern

Definition at line 783 of file fd.c.

784{
785 int fd;
786
787 /*
788 * First fsync the old and target path (if it exists), to ensure that they
789 * are properly persistent on disk. Syncing the target file is not
790 * strictly necessary, but it makes it easier to reason about crashes;
791 * because it's then guaranteed that either source or target file exists
792 * after a crash.
793 */
794 if (fsync_fname_ext(oldfile, false, false, elevel) != 0)
795 return -1;
796
798 if (fd < 0)
799 {
800 if (errno != ENOENT)
801 {
802 ereport(elevel,
804 errmsg("could not open file \"%s\": %m", newfile)));
805 return -1;
806 }
807 }
808 else
809 {
810 if (pg_fsync(fd) != 0)
811 {
812 int save_errno;
813
814 /* close file upon error, might not be in transaction context */
818
819 ereport(elevel,
821 errmsg("could not fsync file \"%s\": %m", newfile)));
822 return -1;
823 }
824
825 if (CloseTransientFile(fd) != 0)
826 {
827 ereport(elevel,
829 errmsg("could not close file \"%s\": %m", newfile)));
830 return -1;
831 }
832 }
833
834 /* Time to do the real deal... */
835 if (rename(oldfile, newfile) < 0)
836 {
837 ereport(elevel,
839 errmsg("could not rename file \"%s\" to \"%s\": %m",
840 oldfile, newfile)));
841 return -1;
842 }
843
844 /*
845 * To guarantee renaming the file is persistent, fsync the file with its
846 * new name, and its containing directory.
847 */
848 if (fsync_fname_ext(newfile, false, false, elevel) != 0)
849 return -1;
850
851 if (fsync_parent_path(newfile, elevel) != 0)
852 return -1;
853
854 return 0;
855}
#define PG_BINARY
Definition c.h:1376
int errcode_for_file_access(void)
Definition elog.c:897
int CloseTransientFile(int fd)
Definition fd.c:2855
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition fd.c:3847
int pg_fsync(int fd)
Definition fd.c:390
static int fsync_parent_path(const char *fname, int elevel)
Definition fd.c:3923
int OpenTransientFile(const char *fileName, int fileFlags)
Definition fd.c:2678

References CloseTransientFile(), ereport, errcode_for_file_access(), errmsg, fb(), fd(), fsync_fname_ext(), fsync_parent_path(), OpenTransientFile(), PG_BINARY, and pg_fsync().

Referenced by AlterSystemSetConfigFile(), apw_dump_now(), BaseBackup(), basic_archive_file(), bbsink_server_end_manifest(), CheckPointReplicationOrigin(), cleanup_objects_atexit(), CleanupAfterArchiveRecovery(), dir_close(), InitWalRecovery(), InstallXLogFileSegment(), KeepFileRestoredFromArchive(), pgss_shmem_shutdown(), pgstat_write_statsfile(), StartupXLOG(), SummarizeWAL(), write_relmap_file(), writeTimeLineHistory(), writeTimeLineHistoryFile(), and XLogArchiveForceDone().

◆ durable_unlink()

int durable_unlink ( const char fname,
int  elevel 
)
extern

Definition at line 873 of file fd.c.

874{
875 if (unlink(fname) < 0)
876 {
877 ereport(elevel,
879 errmsg("could not remove file \"%s\": %m",
880 fname)));
881 return -1;
882 }
883
884 /*
885 * To guarantee that the removal of the file is persistent, fsync its
886 * parent directory.
887 */
888 if (fsync_parent_path(fname, elevel) != 0)
889 return -1;
890
891 return 0;
892}

References ereport, errcode_for_file_access(), errmsg, fb(), and fsync_parent_path().

Referenced by InstallXLogFileSegment(), RemoveXlogFile(), and StartupXLOG().

◆ FileClose()

void FileClose ( File  file)
extern

Definition at line 1966 of file fd.c.

1967{
1968 Vfd *vfdP;
1969
1970 Assert(FileIsValid(file));
1971
1972 DO_DB(elog(LOG, "FileClose: %d (%s)",
1973 file, VfdCache[file].fileName));
1974
1975 vfdP = &VfdCache[file];
1976
1977 if (!FileIsNotOpen(file))
1978 {
1980
1981 /* close the file */
1982 if (close(vfdP->fd) != 0)
1983 {
1984 /*
1985 * We may need to panic on failure to close non-temporary files;
1986 * see LruDelete.
1987 */
1989 "could not close file \"%s\": %m", vfdP->fileName);
1990 }
1991
1992 --nfile;
1993 vfdP->fd = VFD_CLOSED;
1994
1995 /* remove the file from the lru ring */
1996 Delete(file);
1997 }
1998
1999 if (vfdP->fdstate & FD_TEMP_FILE_LIMIT)
2000 {
2001 /* Subtract its size from current usage (do first in case of error) */
2002 temporary_files_size -= vfdP->fileSize;
2003 vfdP->fileSize = 0;
2004 }
2005
2006 /*
2007 * Delete the file if it was temporary, and make a log entry if wanted
2008 */
2009 if (vfdP->fdstate & FD_DELETE_AT_CLOSE)
2010 {
2011 struct stat filestats;
2012 int stat_errno;
2013
2014 /*
2015 * If we get an error, as could happen within the ereport/elog calls,
2016 * we'll come right back here during transaction abort. Reset the
2017 * flag to ensure that we can't get into an infinite loop. This code
2018 * is arranged to ensure that the worst-case consequence is failing to
2019 * emit log message(s), not failing to attempt the unlink.
2020 */
2021 vfdP->fdstate &= ~FD_DELETE_AT_CLOSE;
2022
2023
2024 /* first try the stat() */
2025 if (stat(vfdP->fileName, &filestats))
2026 stat_errno = errno;
2027 else
2028 stat_errno = 0;
2029
2030 /* in any case do the unlink */
2031 if (unlink(vfdP->fileName))
2032 ereport(LOG,
2034 errmsg("could not delete file \"%s\": %m", vfdP->fileName)));
2035
2036 /* and last report the stat results */
2037 if (stat_errno == 0)
2038 ReportTemporaryFileUsage(vfdP->fileName, filestats.st_size);
2039 else
2040 {
2041 errno = stat_errno;
2042 ereport(LOG,
2044 errmsg("could not stat file \"%s\": %m", vfdP->fileName)));
2045 }
2046 }
2047
2048 /* Unregister it from the resource owner */
2049 if (vfdP->resowner)
2050 ResourceOwnerForgetFile(vfdP->resowner, file);
2051
2052 /*
2053 * Return the Vfd slot to the free list
2054 */
2055 FreeVfd(file);
2056}
#define FD_DELETE_AT_CLOSE
Definition fd.c:196
static void Delete(File file)
Definition fd.c:1254
static void ResourceOwnerForgetFile(ResourceOwner owner, File file)
Definition fd.c:381
#define FileIsValid(file)
Definition fd.c:190
static int nfile
Definition fd.c:226
static void FreeVfd(File file)
Definition fd.c:1460
#define FD_TEMP_FILE_LIMIT
Definition fd.c:198
int data_sync_elevel(int elevel)
Definition fd.c:3986
static void ReportTemporaryFileUsage(const char *path, pgoff_t size)
Definition fd.c:1516
static uint64 temporary_files_size
Definition fd.c:240
#define VFD_CLOSED
Definition fd.c:188
static Vfd * VfdCache
Definition fd.c:220
Definition fd.c:201
#define stat
Definition win32_port.h:74

References Assert, close, data_sync_elevel(), Delete(), DO_DB, elog, ereport, errcode_for_file_access(), errmsg, fb(), FD_DELETE_AT_CLOSE, FD_TEMP_FILE_LIMIT, FileIsNotOpen, FileIsValid, FreeVfd(), LOG, nfile, pgaio_closing_fd(), ReportTemporaryFileUsage(), ResourceOwnerForgetFile(), stat, temporary_files_size, VFD_CLOSED, and VfdCache.

Referenced by bbsink_server_end_archive(), bbsink_server_end_manifest(), BufFileClose(), BufFileTruncateFileSet(), CleanupTempFiles(), logical_end_heap_rewrite(), mdclose(), mdimmedsync(), mdregistersync(), mdsyncfiletag(), mdtruncate(), pg_wal_summary_contents(), PrepareForIncrementalBackup(), ReorderBufferIterTXNFinish(), ReorderBufferRestoreChanges(), ResOwnerReleaseFile(), and SummarizeWAL().

◆ FileFallocate()

int FileFallocate ( File  file,
pgoff_t  offset,
pgoff_t  amount,
uint32  wait_event_info 
)
extern

Definition at line 2408 of file fd.c.

2409{
2410#ifdef HAVE_POSIX_FALLOCATE
2411 int returnCode;
2412
2413 Assert(FileIsValid(file));
2414
2415 DO_DB(elog(LOG, "FileFallocate: %d (%s) " INT64_FORMAT " " INT64_FORMAT,
2416 file, VfdCache[file].fileName,
2417 (int64) offset, (int64) amount));
2418
2419 returnCode = FileAccess(file);
2420 if (returnCode < 0)
2421 return -1;
2422
2423retry:
2424 pgstat_report_wait_start(wait_event_info);
2425 returnCode = posix_fallocate(VfdCache[file].fd, offset, amount);
2427
2428 if (returnCode == 0)
2429 return 0;
2430 else if (returnCode == EINTR)
2431 goto retry;
2432
2433 /* for compatibility with %m printing etc */
2434 errno = returnCode;
2435
2436 /*
2437 * Return in cases of a "real" failure, if fallocate is not supported,
2438 * fall through to the FileZero() backed implementation.
2439 */
2441 return -1;
2442#endif
2443
2444 return FileZero(file, offset, amount, wait_event_info);
2445}
#define INT64_FORMAT
Definition c.h:636
int64_t int64
Definition c.h:615
static int FileAccess(File file)
Definition fd.c:1480
int FileZero(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info)
Definition fd.c:2363
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:69
static void pgstat_report_wait_end(void)
Definition wait_event.h:85
#define EINTR
Definition win32_port.h:361
#define EOPNOTSUPP
Definition win32_port.h:385

References Assert, DO_DB, EINTR, elog, EOPNOTSUPP, fb(), fd(), FileAccess(), FileIsValid, FileZero(), INT64_FORMAT, LOG, pgstat_report_wait_end(), pgstat_report_wait_start(), and VfdCache.

Referenced by mdzeroextend().

◆ FileGetRawDesc()

int FileGetRawDesc ( File  file)
extern

Definition at line 2516 of file fd.c.

2517{
2518 int returnCode;
2519
2520 returnCode = FileAccess(file);
2521 if (returnCode < 0)
2522 return returnCode;
2523
2524 Assert(FileIsValid(file));
2525 return VfdCache[file].fd;
2526}
int fd
Definition fd.c:202

References Assert, fb(), vfd::fd, FileAccess(), FileIsValid, and VfdCache.

Referenced by mdfd().

◆ FileGetRawFlags()

int FileGetRawFlags ( File  file)
extern

Definition at line 2532 of file fd.c.

2533{
2534 Assert(FileIsValid(file));
2535 return VfdCache[file].fileFlags;
2536}
int fileFlags
Definition fd.c:211

References Assert, vfd::fileFlags, FileIsValid, and VfdCache.

◆ FileGetRawMode()

mode_t FileGetRawMode ( File  file)
extern

Definition at line 2542 of file fd.c.

2543{
2544 Assert(FileIsValid(file));
2545 return VfdCache[file].fileMode;
2546}
mode_t fileMode
Definition fd.c:212

References Assert, FileIsValid, vfd::fileMode, and VfdCache.

◆ FilePathName()

◆ FilePrefetch()

int FilePrefetch ( File  file,
pgoff_t  offset,
pgoff_t  amount,
uint32  wait_event_info 
)
extern

Definition at line 2067 of file fd.c.

2068{
2069 Assert(FileIsValid(file));
2070
2071 DO_DB(elog(LOG, "FilePrefetch: %d (%s) " INT64_FORMAT " " INT64_FORMAT,
2072 file, VfdCache[file].fileName,
2073 (int64) offset, (int64) amount));
2074
2075#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
2076 {
2077 int returnCode;
2078
2079 returnCode = FileAccess(file);
2080 if (returnCode < 0)
2081 return returnCode;
2082
2083retry:
2084 pgstat_report_wait_start(wait_event_info);
2085 returnCode = posix_fadvise(VfdCache[file].fd, offset, amount,
2088
2089 if (returnCode == EINTR)
2090 goto retry;
2091
2092 return returnCode;
2093 }
2094#elif defined(__darwin__)
2095 {
2096 struct radvisory
2097 {
2098 off_t ra_offset; /* offset into the file */
2099 int ra_count; /* size of the read */
2100 } ra;
2101 int returnCode;
2102
2103 returnCode = FileAccess(file);
2104 if (returnCode < 0)
2105 return returnCode;
2106
2107 ra.ra_offset = offset;
2108 ra.ra_count = amount;
2109 pgstat_report_wait_start(wait_event_info);
2112 if (returnCode != -1)
2113 return 0;
2114 else
2115 return errno;
2116 }
2117#else
2118 return 0;
2119#endif
2120}

References Assert, DO_DB, EINTR, elog, fb(), fd(), FileAccess(), FileIsValid, INT64_FORMAT, LOG, pgstat_report_wait_end(), pgstat_report_wait_start(), and VfdCache.

Referenced by mdprefetch().

◆ FileRead()

static ssize_t FileRead ( File  file,
void buffer,
size_t  amount,
pgoff_t  offset,
uint32  wait_event_info 
)
inlinestatic

Definition at line 225 of file fd.h.

227{
228 struct iovec iov = {
229 .iov_base = buffer,
230 .iov_len = amount
231 };
232
233 return FileReadV(file, &iov, 1, offset, wait_event_info);
234}
ssize_t FileReadV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info)
Definition fd.c:2149

References fb(), and FileReadV().

Referenced by BufFileLoadBuffer(), ReadWalSummary(), and ReorderBufferRestoreChanges().

◆ FileReadV()

ssize_t FileReadV ( File  file,
const struct iovec iov,
int  iovcnt,
pgoff_t  offset,
uint32  wait_event_info 
)
extern

Definition at line 2149 of file fd.c.

2151{
2153 Vfd *vfdP;
2154
2155 Assert(FileIsValid(file));
2156
2157 DO_DB(elog(LOG, "FileReadV: %d (%s) " INT64_FORMAT " %d",
2158 file, VfdCache[file].fileName,
2159 (int64) offset,
2160 iovcnt));
2161
2162 returnCode = FileAccess(file);
2163 if (returnCode < 0)
2164 return returnCode;
2165
2166 vfdP = &VfdCache[file];
2167
2168retry:
2169 pgstat_report_wait_start(wait_event_info);
2170 returnCode = pg_preadv(vfdP->fd, iov, iovcnt, offset);
2172
2173 if (returnCode < 0)
2174 {
2175 /*
2176 * Windows may run out of kernel buffers and return "Insufficient
2177 * system resources" error. Wait a bit and retry to solve it.
2178 *
2179 * It is rumored that EINTR is also possible on some Unix filesystems,
2180 * in which case immediate retry is indicated.
2181 */
2182#ifdef WIN32
2184
2185 switch (error)
2186 {
2188 pg_usleep(1000L);
2189 errno = EINTR;
2190 break;
2191 default:
2193 break;
2194 }
2195#endif
2196 /* OK to retry if interrupted */
2197 if (errno == EINTR)
2198 goto retry;
2199 }
2200
2201 return returnCode;
2202}
static ssize_t pg_preadv(int fd, const struct iovec *iov, int iovcnt, pgoff_t offset)
Definition pg_iovec.h:54
void pg_usleep(long microsec)
Definition signal.c:53
static void error(void)
void _dosmaperr(unsigned long)
Definition win32error.c:177

References _dosmaperr(), Assert, DO_DB, EINTR, elog, error(), fb(), FileAccess(), FileIsValid, INT64_FORMAT, LOG, pg_preadv(), pg_usleep(), pgstat_report_wait_end(), pgstat_report_wait_start(), and VfdCache.

Referenced by FileRead(), and mdreadv().

◆ FileSize()

pgoff_t FileSize ( File  file)
extern

Definition at line 2448 of file fd.c.

2449{
2450 Assert(FileIsValid(file));
2451
2452 DO_DB(elog(LOG, "FileSize %d (%s)",
2453 file, VfdCache[file].fileName));
2454
2455 if (FileIsNotOpen(file))
2456 {
2457 if (FileAccess(file) < 0)
2458 return (pgoff_t) -1;
2459 }
2460
2461 return lseek(VfdCache[file].fd, 0, SEEK_END);
2462}
off_t pgoff_t
Definition port.h:421

References Assert, DO_DB, elog, fb(), fd(), FileAccess(), FileIsNotOpen, FileIsValid, LOG, and VfdCache.

Referenced by _mdnblocks(), BufFileSeek(), and BufFileSize().

◆ FileStartReadV()

int FileStartReadV ( struct PgAioHandle ioh,
File  file,
int  iovcnt,
pgoff_t  offset,
uint32  wait_event_info 
)
extern

Definition at line 2205 of file fd.c.

2208{
2209 int returnCode;
2210 Vfd *vfdP;
2211
2212 Assert(FileIsValid(file));
2213
2214 DO_DB(elog(LOG, "FileStartReadV: %d (%s) " INT64_FORMAT " %d",
2215 file, VfdCache[file].fileName,
2216 (int64) offset,
2217 iovcnt));
2218
2219 returnCode = FileAccess(file);
2220 if (returnCode < 0)
2221 return returnCode;
2222
2223 vfdP = &VfdCache[file];
2224
2225 pgaio_io_start_readv(ioh, vfdP->fd, iovcnt, offset);
2226
2227 return 0;
2228}
void pgaio_io_start_readv(PgAioHandle *ioh, int fd, int iovcnt, uint64 offset)
Definition aio_io.c:78

References Assert, DO_DB, elog, fb(), FileAccess(), FileIsValid, INT64_FORMAT, LOG, pgaio_io_start_readv(), and VfdCache.

Referenced by mdstartreadv().

◆ FileSync()

int FileSync ( File  file,
uint32  wait_event_info 
)
extern

Definition at line 2336 of file fd.c.

2337{
2338 int returnCode;
2339
2340 Assert(FileIsValid(file));
2341
2342 DO_DB(elog(LOG, "FileSync: %d (%s)",
2343 file, VfdCache[file].fileName));
2344
2345 returnCode = FileAccess(file);
2346 if (returnCode < 0)
2347 return returnCode;
2348
2349 pgstat_report_wait_start(wait_event_info);
2350 returnCode = pg_fsync(VfdCache[file].fd);
2352
2353 return returnCode;
2354}

References Assert, DO_DB, elog, fb(), fd(), FileAccess(), FileIsValid, LOG, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), and VfdCache.

Referenced by bbsink_server_end_archive(), logical_end_heap_rewrite(), mdimmedsync(), mdsyncfiletag(), and register_dirty_segment().

◆ FileTruncate()

int FileTruncate ( File  file,
pgoff_t  offset,
uint32  wait_event_info 
)
extern

Definition at line 2465 of file fd.c.

2466{
2467 int returnCode;
2468
2469 Assert(FileIsValid(file));
2470
2471 DO_DB(elog(LOG, "FileTruncate %d (%s)",
2472 file, VfdCache[file].fileName));
2473
2474 returnCode = FileAccess(file);
2475 if (returnCode < 0)
2476 return returnCode;
2477
2478 pgstat_report_wait_start(wait_event_info);
2479 returnCode = pg_ftruncate(VfdCache[file].fd, offset);
2481
2482 if (returnCode == 0 && VfdCache[file].fileSize > offset)
2483 {
2484 /* adjust our state for truncation of a temp file */
2485 Assert(VfdCache[file].fdstate & FD_TEMP_FILE_LIMIT);
2486 temporary_files_size -= VfdCache[file].fileSize - offset;
2487 VfdCache[file].fileSize = offset;
2488 }
2489
2490 return returnCode;
2491}
static int pg_ftruncate(int fd, pgoff_t length)
Definition fd.c:704
pgoff_t fileSize
Definition fd.c:208

References Assert, DO_DB, elog, fb(), fd(), FD_TEMP_FILE_LIMIT, FileAccess(), FileIsValid, vfd::fileSize, LOG, pg_ftruncate(), pgstat_report_wait_end(), pgstat_report_wait_start(), temporary_files_size, and VfdCache.

Referenced by BufFileTruncateFileSet(), and mdtruncate().

◆ FileWrite()

static ssize_t FileWrite ( File  file,
const void buffer,
size_t  amount,
pgoff_t  offset,
uint32  wait_event_info 
)
inlinestatic

Definition at line 237 of file fd.h.

239{
240 struct iovec iov = {
241 .iov_base = unconstify(void *, buffer),
242 .iov_len = amount
243 };
244
245 return FileWriteV(file, &iov, 1, offset, wait_event_info);
246}
#define unconstify(underlying_type, expr)
Definition c.h:1327
ssize_t FileWriteV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info)
Definition fd.c:2231

References fb(), FileWriteV(), and unconstify.

Referenced by bbsink_server_archive_contents(), bbsink_server_manifest_contents(), BufFileDumpBuffer(), logical_heap_rewrite_flush_mappings(), mdextend(), and WriteWalSummary().

◆ FileWriteback()

void FileWriteback ( File  file,
pgoff_t  offset,
pgoff_t  nbytes,
uint32  wait_event_info 
)
extern

Definition at line 2123 of file fd.c.

2124{
2125 int returnCode;
2126
2127 Assert(FileIsValid(file));
2128
2129 DO_DB(elog(LOG, "FileWriteback: %d (%s) " INT64_FORMAT " " INT64_FORMAT,
2130 file, VfdCache[file].fileName,
2131 (int64) offset, (int64) nbytes));
2132
2133 if (nbytes <= 0)
2134 return;
2135
2136 if (VfdCache[file].fileFlags & PG_O_DIRECT)
2137 return;
2138
2139 returnCode = FileAccess(file);
2140 if (returnCode < 0)
2141 return;
2142
2143 pgstat_report_wait_start(wait_event_info);
2144 pg_flush_data(VfdCache[file].fd, offset, nbytes);
2146}
void pg_flush_data(int fd, pgoff_t offset, pgoff_t nbytes)
Definition fd.c:526

References Assert, DO_DB, elog, fb(), fd(), FileAccess(), FileIsValid, INT64_FORMAT, LOG, pg_flush_data(), PG_O_DIRECT, pgstat_report_wait_end(), pgstat_report_wait_start(), and VfdCache.

Referenced by mdwriteback().

◆ FileWriteV()

ssize_t FileWriteV ( File  file,
const struct iovec iov,
int  iovcnt,
pgoff_t  offset,
uint32  wait_event_info 
)
extern

Definition at line 2231 of file fd.c.

2233{
2235 Vfd *vfdP;
2236
2237 Assert(FileIsValid(file));
2238
2239 DO_DB(elog(LOG, "FileWriteV: %d (%s) " INT64_FORMAT " %d",
2240 file, VfdCache[file].fileName,
2241 (int64) offset,
2242 iovcnt));
2243
2244 returnCode = FileAccess(file);
2245 if (returnCode < 0)
2246 return returnCode;
2247
2248 vfdP = &VfdCache[file];
2249
2250 /*
2251 * If enforcing temp_file_limit and it's a temp file, check to see if the
2252 * write would overrun temp_file_limit, and throw error if so. Note: it's
2253 * really a modularity violation to throw error here; we should set errno
2254 * and return -1. However, there's no way to report a suitable error
2255 * message if we do that. All current callers would just throw error
2256 * immediately anyway, so this is safe at present.
2257 */
2258 if (temp_file_limit >= 0 && (vfdP->fdstate & FD_TEMP_FILE_LIMIT))
2259 {
2260 pgoff_t past_write = offset;
2261
2262 for (int i = 0; i < iovcnt; ++i)
2263 past_write += iov[i].iov_len;
2264
2265 if (past_write > vfdP->fileSize)
2266 {
2268
2270 if (newTotal > (uint64) temp_file_limit * (uint64) 1024)
2271 ereport(ERROR,
2273 errmsg("temporary file size exceeds \"temp_file_limit\" (%dkB)",
2274 temp_file_limit)));
2275 }
2276 }
2277
2278retry:
2279 pgstat_report_wait_start(wait_event_info);
2280 returnCode = pg_pwritev(vfdP->fd, iov, iovcnt, offset);
2282
2283 if (returnCode >= 0)
2284 {
2285 /*
2286 * Some callers expect short writes to set errno, and traditionally we
2287 * have assumed that they imply disk space shortage. We don't want to
2288 * waste CPU cycles adding up the total size here, so we'll just set
2289 * it for all successful writes in case such a caller determines that
2290 * the write was short and ereports "%m".
2291 */
2292 errno = ENOSPC;
2293
2294 /*
2295 * Maintain fileSize and temporary_files_size if it's a temp file.
2296 */
2297 if (vfdP->fdstate & FD_TEMP_FILE_LIMIT)
2298 {
2299 pgoff_t past_write = offset + returnCode;
2300
2301 if (past_write > vfdP->fileSize)
2302 {
2303 temporary_files_size += past_write - vfdP->fileSize;
2304 vfdP->fileSize = past_write;
2305 }
2306 }
2307 }
2308 else
2309 {
2310 /*
2311 * See comments in FileReadV()
2312 */
2313#ifdef WIN32
2315
2316 switch (error)
2317 {
2319 pg_usleep(1000L);
2320 errno = EINTR;
2321 break;
2322 default:
2324 break;
2325 }
2326#endif
2327 /* OK to retry if interrupted */
2328 if (errno == EINTR)
2329 goto retry;
2330 }
2331
2332 return returnCode;
2333}
uint64_t uint64
Definition c.h:619
int temp_file_limit
Definition guc_tables.c:560
static ssize_t pg_pwritev(int fd, const struct iovec *iov, int iovcnt, pgoff_t offset)
Definition pg_iovec.h:93

References _dosmaperr(), Assert, DO_DB, EINTR, elog, ereport, errcode(), errmsg, ERROR, error(), fb(), FD_TEMP_FILE_LIMIT, FileAccess(), FileIsValid, vfd::fileSize, i, INT64_FORMAT, LOG, pg_pwritev(), pg_usleep(), pgstat_report_wait_end(), pgstat_report_wait_start(), temp_file_limit, temporary_files_size, and VfdCache.

Referenced by FileWrite(), and mdwritev().

◆ FileZero()

int FileZero ( File  file,
pgoff_t  offset,
pgoff_t  amount,
uint32  wait_event_info 
)
extern

Definition at line 2363 of file fd.c.

2364{
2365 int returnCode;
2367
2368 Assert(FileIsValid(file));
2369
2370 DO_DB(elog(LOG, "FileZero: %d (%s) " INT64_FORMAT " " INT64_FORMAT,
2371 file, VfdCache[file].fileName,
2372 (int64) offset, (int64) amount));
2373
2374 returnCode = FileAccess(file);
2375 if (returnCode < 0)
2376 return returnCode;
2377
2378 pgstat_report_wait_start(wait_event_info);
2379 written = pg_pwrite_zeros(VfdCache[file].fd, amount, offset);
2381
2382 if (written < 0)
2383 return -1;
2384 else if (written != amount)
2385 {
2386 /* if errno is unset, assume problem is no disk space */
2387 if (errno == 0)
2388 errno = ENOSPC;
2389 return -1;
2390 }
2391
2392 return 0;
2393}
ssize_t pg_pwrite_zeros(int fd, size_t size, pgoff_t offset)
Definition file_utils.c:709

References Assert, DO_DB, elog, fb(), fd(), FileAccess(), FileIsValid, INT64_FORMAT, LOG, pg_pwrite_zeros(), pgstat_report_wait_end(), pgstat_report_wait_start(), and VfdCache.

Referenced by FileFallocate(), and mdzeroextend().

◆ FreeDir()

int FreeDir ( DIR dir)
extern

Definition at line 3009 of file fd.c.

3010{
3011 int i;
3012
3013 /* Nothing to do if AllocateDir failed */
3014 if (dir == NULL)
3015 return 0;
3016
3017 DO_DB(elog(LOG, "FreeDir: Allocated %d", numAllocatedDescs));
3018
3019 /* Remove dir from list of allocated dirs, if it's present */
3020 for (i = numAllocatedDescs; --i >= 0;)
3021 {
3022 AllocateDesc *desc = &allocatedDescs[i];
3023
3024 if (desc->kind == AllocateDescDir && desc->desc.dir == dir)
3025 return FreeDesc(desc);
3026 }
3027
3028 /* Only get here if someone passes us a dir not in allocatedDescs */
3029 elog(WARNING, "dir passed to FreeDir was not obtained from AllocateDir");
3030
3031 return closedir(dir);
3032}
int closedir(DIR *)
Definition dirent.c:127

References allocatedDescs, AllocateDescDir, closedir(), AllocateDesc::desc, AllocateDesc::dir, DO_DB, elog, fb(), FreeDesc(), i, AllocateDesc::kind, LOG, numAllocatedDescs, and WARNING.

Referenced by calculate_database_size(), calculate_tablespace_size(), CheckPointLogicalRewriteHeap(), CheckPointSnapBuild(), CleanupBackupHistory(), copydir(), db_dir_size(), DeleteAllExportedSnapshotFiles(), destroy_tablespace_directories(), directory_is_empty(), do_pg_backup_start(), dsm_cleanup_for_mmap(), extension_file_exists(), get_ext_ver_list(), GetConfFilesInDir(), getInstallationPaths(), GetWalSummaries(), movedb(), ParseTzFile(), perform_base_backup(), pg_available_extension_versions(), pg_available_extensions(), pg_ls_dir(), pg_ls_dir_files(), pg_tablespace_databases(), pg_tzenumerate_end(), pg_tzenumerate_next(), pgarch_readyXlog(), RelationCacheInitFileRemove(), RelationCacheInitFileRemoveInDir(), RemoveNonParentXlogFiles(), RemoveOldXlogFiles(), RemovePgTempFiles(), RemovePgTempFilesInDir(), RemovePgTempRelationFiles(), RemovePgTempRelationFilesInDbspace(), RemoveTempXlogFiles(), ReorderBufferCleanupSerializedTXNs(), ResetUnloggedRelations(), ResetUnloggedRelationsInDbspaceDir(), ResetUnloggedRelationsInTablespaceDir(), restoreTwoPhaseData(), scan_directory_ci(), sendDir(), SlruScanDirectory(), StartupReorderBuffer(), StartupReplicationSlots(), SyncDataDirectory(), UpdateLogicalMappings(), walkdir(), and XLogGetOldestSegno().

◆ FreeFile()

int FreeFile ( FILE file)
extern

Definition at line 2827 of file fd.c.

2828{
2829 int i;
2830
2831 DO_DB(elog(LOG, "FreeFile: Allocated %d", numAllocatedDescs));
2832
2833 /* Remove file from list of allocated files, if it's present */
2834 for (i = numAllocatedDescs; --i >= 0;)
2835 {
2836 AllocateDesc *desc = &allocatedDescs[i];
2837
2838 if (desc->kind == AllocateDescFile && desc->desc.file == file)
2839 return FreeDesc(desc);
2840 }
2841
2842 /* Only get here if someone passes us a file not in allocatedDescs */
2843 elog(WARNING, "file passed to FreeFile was not obtained from AllocateFile");
2844
2845 return fclose(file);
2846}

References allocatedDescs, AllocateDescFile, AllocateDesc::desc, DO_DB, elog, fb(), AllocateDesc::file, FreeDesc(), i, AllocateDesc::kind, LOG, numAllocatedDescs, and WARNING.

Referenced by AlterSystemSetConfigFile(), apw_dump_now(), apw_load_buffers(), checkControlFile(), do_pg_backup_stop(), EndCopy(), EndCopyFrom(), entry_reset(), existsTimeLineHistory(), ExportSnapshot(), free_auth_file(), gc_qtexts(), GetHugePageSize(), ImportSnapshot(), load_dh_file(), load_relcache_init_file(), parse_extension_control_file(), ParseConfigFile(), ParseTzFile(), pg_current_logfile(), pg_promote(), pgss_shmem_shutdown(), pgss_shmem_startup(), pgstat_read_statsfile(), pgstat_write_statsfile(), read_backup_label(), read_binary_file(), read_tablespace_map(), read_whole_file(), readTimeLineHistory(), test_custom_stats_var_finish(), tsearch_readline_end(), ValidatePgVersion(), write_relcache_init_file(), XLogArchiveForceDone(), and XLogArchiveNotify().

◆ fsync_fname()

◆ fsync_fname_ext()

int fsync_fname_ext ( const char fname,
bool  isdir,
bool  ignore_perm,
int  elevel 
)
extern

Definition at line 3847 of file fd.c.

3848{
3849 int fd;
3850 int flags;
3851 int returncode;
3852
3853 /*
3854 * Some OSs require directories to be opened read-only whereas other
3855 * systems don't allow us to fsync files opened read-only; so we need both
3856 * cases here. Using O_RDWR will cause us to fail to fsync files that are
3857 * not writable by our userid, but we assume that's OK.
3858 */
3859 flags = PG_BINARY;
3860 if (!isdir)
3861 flags |= O_RDWR;
3862 else
3863 flags |= O_RDONLY;
3864
3865 fd = OpenTransientFile(fname, flags);
3866
3867 /*
3868 * Some OSs don't allow us to open directories at all (Windows returns
3869 * EACCES), just ignore the error in that case. If desired also silently
3870 * ignoring errors about unreadable files. Log others.
3871 */
3872 if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES))
3873 return 0;
3874 else if (fd < 0 && ignore_perm && errno == EACCES)
3875 return 0;
3876 else if (fd < 0)
3877 {
3878 ereport(elevel,
3880 errmsg("could not open file \"%s\": %m", fname)));
3881 return -1;
3882 }
3883
3885
3886 /*
3887 * Some OSes don't allow us to fsync directories at all, so we can ignore
3888 * those errors. Anything else needs to be logged.
3889 */
3890 if (returncode != 0 && !(isdir && (errno == EBADF || errno == EINVAL)))
3891 {
3892 int save_errno;
3893
3894 /* close file upon error, might not be in transaction context */
3895 save_errno = errno;
3897 errno = save_errno;
3898
3899 ereport(elevel,
3901 errmsg("could not fsync file \"%s\": %m", fname)));
3902 return -1;
3903 }
3904
3905 if (CloseTransientFile(fd) != 0)
3906 {
3907 ereport(elevel,
3909 errmsg("could not close file \"%s\": %m", fname)));
3910 return -1;
3911 }
3912
3913 return 0;
3914}

References CloseTransientFile(), ereport, errcode_for_file_access(), errmsg, fb(), fd(), OpenTransientFile(), PG_BINARY, and pg_fsync().

Referenced by datadir_fsync_fname(), durable_rename(), fsync_fname(), and fsync_parent_path().

◆ GetNextTempTableSpace()

Oid GetNextTempTableSpace ( void  )
extern

Definition at line 3159 of file fd.c.

3160{
3161 if (numTempTableSpaces > 0)
3162 {
3163 /* Advance nextTempTableSpace counter with wraparound */
3167 }
3168 return InvalidOid;
3169}
static int nextTempTableSpace
Definition fd.c:294
#define InvalidOid

References InvalidOid, nextTempTableSpace, numTempTableSpaces, and tempTableSpaces.

Referenced by GetDefaultTablespace(), and OpenTemporaryFile().

◆ GetTempTablespaces()

int GetTempTablespaces ( Oid tableSpaces,
int  numSpaces 
)
extern

Definition at line 3141 of file fd.c.

3142{
3143 int i;
3144
3146 for (i = 0; i < numTempTableSpaces && i < numSpaces; ++i)
3148
3149 return i;
3150}
bool TempTablespacesAreSet(void)
Definition fd.c:3126

References Assert, fb(), i, numTempTableSpaces, tempTableSpaces, and TempTablespacesAreSet().

Referenced by FileSetInit().

◆ InitFileAccess()

void InitFileAccess ( void  )
extern

Definition at line 904 of file fd.c.

905{
906 Assert(SizeVfdCache == 0); /* call me only once */
907
908 /* initialize cache header entry */
909 VfdCache = (Vfd *) malloc(sizeof(Vfd));
910 if (VfdCache == NULL)
913 errmsg("out of memory")));
914
915 MemSet(&(VfdCache[0]), 0, sizeof(Vfd));
917
918 SizeVfdCache = 1;
919}
#define MemSet(start, val, len)
Definition c.h:1109
#define FATAL
Definition elog.h:41
#define malloc(a)

References Assert, ereport, errcode(), errmsg, FATAL, fb(), vfd::fd, malloc, MemSet, SizeVfdCache, VFD_CLOSED, and VfdCache.

Referenced by BaseInit().

◆ InitTemporaryFileAccess()

void InitTemporaryFileAccess ( void  )
extern

Definition at line 934 of file fd.c.

935{
936 Assert(SizeVfdCache != 0); /* InitFileAccess() needs to have run */
937 Assert(!temporary_files_allowed); /* call me only once */
938
939 /*
940 * Register before-shmem-exit hook to ensure temp files are dropped while
941 * we can still report stats.
942 */
944
945#ifdef USE_ASSERT_CHECKING
947#endif
948}
static void BeforeShmemExit_Files(int code, Datum arg)
Definition fd.c:3228
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:344

References Assert, before_shmem_exit(), BeforeShmemExit_Files(), fb(), and SizeVfdCache.

Referenced by BaseInit().

◆ looks_like_temp_rel_name()

bool looks_like_temp_rel_name ( const char name)
extern

Definition at line 3499 of file fd.c.

3500{
3501 int pos;
3502 int savepos;
3503
3504 /* Must start with "t". */
3505 if (name[0] != 't')
3506 return false;
3507
3508 /* Followed by a non-empty string of digits and then an underscore. */
3509 for (pos = 1; isdigit((unsigned char) name[pos]); ++pos)
3510 ;
3511 if (pos == 1 || name[pos] != '_')
3512 return false;
3513
3514 /* Followed by another nonempty string of digits. */
3515 for (savepos = ++pos; isdigit((unsigned char) name[pos]); ++pos)
3516 ;
3517 if (savepos == pos)
3518 return false;
3519
3520 /* We might have _forkname or .segment or both. */
3521 if (name[pos] == '_')
3522 {
3523 int forkchar = forkname_chars(&name[pos + 1], NULL);
3524
3525 if (forkchar <= 0)
3526 return false;
3527 pos += forkchar + 1;
3528 }
3529 if (name[pos] == '.')
3530 {
3531 int segchar;
3532
3533 for (segchar = 1; isdigit((unsigned char) name[pos + segchar]); ++segchar)
3534 ;
3535 if (segchar <= 1)
3536 return false;
3537 pos += segchar;
3538 }
3539
3540 /* Now we should be at the end. */
3541 if (name[pos] != '\0')
3542 return false;
3543 return true;
3544}
int forkname_chars(const char *str, ForkNumber *fork)
Definition relpath.c:81

References fb(), forkname_chars(), and name.

Referenced by RemovePgTempRelationFilesInDbspace(), and sendDir().

◆ MakePGDirectory()

◆ OpenPipeStream()

FILE * OpenPipeStream ( const char command,
const char mode 
)
extern

Definition at line 2731 of file fd.c.

2732{
2733 FILE *file;
2734 int save_errno;
2735
2736 DO_DB(elog(LOG, "OpenPipeStream: Allocated %d (%s)",
2737 numAllocatedDescs, command));
2738
2739 /* Can we allocate another non-virtual FD? */
2740 if (!reserveAllocatedDesc())
2741 ereport(ERROR,
2743 errmsg("exceeded maxAllocatedDescs (%d) while trying to execute command \"%s\"",
2744 maxAllocatedDescs, command)));
2745
2746 /* Close excess kernel FDs. */
2748
2749TryAgain:
2750 fflush(NULL);
2752 errno = 0;
2753 file = popen(command, mode);
2754 save_errno = errno;
2756 errno = save_errno;
2757 if (file != NULL)
2758 {
2760
2761 desc->kind = AllocateDescPipe;
2762 desc->desc.file = file;
2765 return desc->desc.file;
2766 }
2767
2768 if (errno == EMFILE || errno == ENFILE)
2769 {
2770 ereport(LOG,
2772 errmsg("out of file descriptors: %m; release and retry")));
2773 if (ReleaseLruFile())
2774 goto TryAgain;
2775 errno = save_errno;
2776 }
2777
2778 return NULL;
2779}
#define pqsignal
Definition port.h:547
#define SIGPIPE
Definition win32_port.h:163

References allocatedDescs, AllocateDescPipe, AllocateDesc::create_subid, AllocateDesc::desc, DO_DB, elog, ereport, errcode(), errmsg, ERROR, fb(), AllocateDesc::file, GetCurrentSubTransactionId(), AllocateDesc::kind, LOG, maxAllocatedDescs, mode, numAllocatedDescs, pqsignal, ReleaseLruFile(), ReleaseLruFiles(), reserveAllocatedDesc(), and SIGPIPE.

Referenced by BeginCopyFrom(), BeginCopyTo(), pg_import_system_collations(), run_ssl_passphrase_command(), and shell_run_command().

◆ OpenTemporaryFile()

File OpenTemporaryFile ( bool  interXact)
extern

Definition at line 1712 of file fd.c.

1713{
1714 File file = 0;
1715
1716 Assert(temporary_files_allowed); /* check temp file access is up */
1717
1718 /*
1719 * Make sure the current resource owner has space for this File before we
1720 * open it, if we'll be registering it below.
1721 */
1722 if (!interXact)
1724
1725 /*
1726 * If some temp tablespace(s) have been given to us, try to use the next
1727 * one. If a given tablespace can't be found, we silently fall back to
1728 * the database's default tablespace.
1729 *
1730 * BUT: if the temp file is slated to outlive the current transaction,
1731 * force it into the database's default tablespace, so that it will not
1732 * pose a threat to possible tablespace drop attempts.
1733 */
1734 if (numTempTableSpaces > 0 && !interXact)
1735 {
1737
1738 if (OidIsValid(tblspcOid))
1740 }
1741
1742 /*
1743 * If not, or if tablespace is bad, create in database's default
1744 * tablespace. MyDatabaseTableSpace should normally be set before we get
1745 * here, but just in case it isn't, fall back to pg_default tablespace.
1746 */
1747 if (file <= 0)
1751 true);
1752
1753 /* Mark it for deletion at close and temporary file size limit */
1755
1756 /* Register it with the current resource owner */
1757 if (!interXact)
1759
1760 return file;
1761}
#define OidIsValid(objectId)
Definition c.h:860
Oid GetNextTempTableSpace(void)
Definition fd.c:3159
static File OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError)
Definition fd.c:1792
static void RegisterTemporaryFile(File file)
Definition fd.c:1535
int File
Definition fd.h:51
Oid MyDatabaseTableSpace
Definition globals.c:96
return true
Definition isn.c:130
unsigned int Oid
ResourceOwner CurrentResourceOwner
Definition resowner.c:173
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition resowner.c:449
unsigned short fdstate
Definition fd.c:203

References Assert, CurrentResourceOwner, fb(), FD_DELETE_AT_CLOSE, FD_TEMP_FILE_LIMIT, vfd::fdstate, GetNextTempTableSpace(), MyDatabaseTableSpace, numTempTableSpaces, OidIsValid, OpenTemporaryFileInTablespace(), RegisterTemporaryFile(), ResourceOwnerEnlarge(), and VfdCache.

Referenced by BufFileCreateTemp(), and extendBufFile().

◆ OpenTransientFile()

◆ OpenTransientFilePerm()

int OpenTransientFilePerm ( const char fileName,
int  fileFlags,
mode_t  fileMode 
)
extern

Definition at line 2687 of file fd.c.

2688{
2689 int fd;
2690
2691 DO_DB(elog(LOG, "OpenTransientFile: Allocated %d (%s)",
2692 numAllocatedDescs, fileName));
2693
2694 /* Can we allocate another non-virtual FD? */
2695 if (!reserveAllocatedDesc())
2696 ereport(ERROR,
2698 errmsg("exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2699 maxAllocatedDescs, fileName)));
2700
2701 /* Close excess kernel FDs. */
2703
2704 fd = BasicOpenFilePerm(fileName, fileFlags, fileMode);
2705
2706 if (fd >= 0)
2707 {
2709
2710 desc->kind = AllocateDescRawFD;
2711 desc->desc.fd = fd;
2714
2715 return fd;
2716 }
2717
2718 return -1; /* failure */
2719}

References allocatedDescs, AllocateDescRawFD, BasicOpenFilePerm(), AllocateDesc::create_subid, AllocateDesc::desc, DO_DB, elog, ereport, errcode(), errmsg, ERROR, fb(), AllocateDesc::fd, fd(), GetCurrentSubTransactionId(), AllocateDesc::kind, LOG, maxAllocatedDescs, numAllocatedDescs, ReleaseLruFiles(), and reserveAllocatedDesc().

Referenced by be_lo_export(), and OpenTransientFile().

◆ PathNameCreateTemporaryDir()

void PathNameCreateTemporaryDir ( const char basedir,
const char directory 
)
extern

Definition at line 1648 of file fd.c.

1649{
1650 if (MakePGDirectory(directory) < 0)
1651 {
1652 if (errno == EEXIST)
1653 return;
1654
1655 /*
1656 * Failed. Try to create basedir first in case it's missing. Tolerate
1657 * EEXIST to close a race against another process following the same
1658 * algorithm.
1659 */
1660 if (MakePGDirectory(basedir) < 0 && errno != EEXIST)
1661 ereport(ERROR,
1663 errmsg("cannot create temporary directory \"%s\": %m",
1664 basedir)));
1665
1666 /* Try again. */
1667 if (MakePGDirectory(directory) < 0 && errno != EEXIST)
1668 ereport(ERROR,
1670 errmsg("cannot create temporary subdirectory \"%s\": %m",
1671 directory)));
1672 }
1673}
int MakePGDirectory(const char *directoryName)
Definition fd.c:3963
static char * basedir
static const char * directory
Definition zic.c:648

References basedir, directory, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), and MakePGDirectory().

Referenced by FileSetCreate().

◆ PathNameCreateTemporaryFile()

File PathNameCreateTemporaryFile ( const char path,
bool  error_on_failure 
)
extern

Definition at line 1849 of file fd.c.

1850{
1851 File file;
1852
1853 Assert(temporary_files_allowed); /* check temp file access is up */
1854
1856
1857 /*
1858 * Open the file. Note: we don't use O_EXCL, in case there is an orphaned
1859 * temp file that can be reused.
1860 */
1861 file = PathNameOpenFile(path, O_RDWR | O_CREAT | O_TRUNC | PG_BINARY);
1862 if (file <= 0)
1863 {
1864 if (error_on_failure)
1865 ereport(ERROR,
1867 errmsg("could not create temporary file \"%s\": %m",
1868 path)));
1869 else
1870 return file;
1871 }
1872
1873 /* Mark it for temp_file_limit accounting. */
1875
1876 /* Register it for automatic close. */
1878
1879 return file;
1880}
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition fd.c:1563

References Assert, CurrentResourceOwner, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), FD_TEMP_FILE_LIMIT, vfd::fdstate, PathNameOpenFile(), PG_BINARY, RegisterTemporaryFile(), ResourceOwnerEnlarge(), and VfdCache.

Referenced by FileSetCreate().

◆ PathNameDeleteTemporaryDir()

void PathNameDeleteTemporaryDir ( const char dirname)
extern

Definition at line 1679 of file fd.c.

1680{
1681 struct stat statbuf;
1682
1683 /* Silently ignore missing directory. */
1684 if (stat(dirname, &statbuf) != 0 && errno == ENOENT)
1685 return;
1686
1687 /*
1688 * Currently, walkdir doesn't offer a way for our passed in function to
1689 * maintain state. Perhaps it should, so that we could tell the caller
1690 * whether this operation succeeded or failed. Since this operation is
1691 * used in a cleanup path, we wouldn't actually behave differently: we'll
1692 * just log failures.
1693 */
1694 walkdir(dirname, unlink_if_exists_fname, false, LOG);
1695}
static void unlink_if_exists_fname(const char *fname, bool isdir, int elevel)
Definition fd.c:3822
static void walkdir(const char *path, void(*action)(const char *fname, bool isdir, int elevel), bool process_symlinks, int elevel)
Definition fd.c:3708

References fb(), LOG, stat, unlink_if_exists_fname(), and walkdir().

Referenced by FileSetDeleteAll().

◆ PathNameDeleteTemporaryFile()

bool PathNameDeleteTemporaryFile ( const char path,
bool  error_on_failure 
)
extern

Definition at line 1920 of file fd.c.

1921{
1922 struct stat filestats;
1923 int stat_errno;
1924
1925 /* Get the final size for pgstat reporting. */
1926 if (stat(path, &filestats) != 0)
1927 stat_errno = errno;
1928 else
1929 stat_errno = 0;
1930
1931 /*
1932 * Unlike FileClose's automatic file deletion code, we tolerate
1933 * non-existence to support BufFileDeleteFileSet which doesn't know how
1934 * many segments it has to delete until it runs out.
1935 */
1936 if (stat_errno == ENOENT)
1937 return false;
1938
1939 if (unlink(path) < 0)
1940 {
1941 if (errno != ENOENT)
1944 errmsg("could not unlink temporary file \"%s\": %m",
1945 path)));
1946 return false;
1947 }
1948
1949 if (stat_errno == 0)
1950 ReportTemporaryFileUsage(path, filestats.st_size);
1951 else
1952 {
1953 errno = stat_errno;
1954 ereport(LOG,
1956 errmsg("could not stat file \"%s\": %m", path)));
1957 }
1958
1959 return true;
1960}

References ereport, errcode_for_file_access(), errmsg, ERROR, fb(), LOG, ReportTemporaryFileUsage(), and stat.

Referenced by FileSetDelete(), and unlink_if_exists_fname().

◆ PathNameOpenFile()

File PathNameOpenFile ( const char fileName,
int  fileFlags 
)
extern

◆ PathNameOpenFilePerm()

File PathNameOpenFilePerm ( const char fileName,
int  fileFlags,
mode_t  fileMode 
)
extern

Definition at line 1576 of file fd.c.

1577{
1578 char *fnamecopy;
1579 File file;
1580 Vfd *vfdP;
1581
1582 DO_DB(elog(LOG, "PathNameOpenFilePerm: %s %x %o",
1583 fileName, fileFlags, fileMode));
1584
1585 /*
1586 * We need a malloc'd copy of the file name; fail cleanly if no room.
1587 */
1588 fnamecopy = strdup(fileName);
1589 if (fnamecopy == NULL)
1590 ereport(ERROR,
1592 errmsg("out of memory")));
1593
1594 file = AllocateVfd();
1595 vfdP = &VfdCache[file];
1596
1597 /* Close excess kernel FDs. */
1599
1600 /*
1601 * Descriptors managed by VFDs are implicitly marked O_CLOEXEC. The
1602 * client shouldn't be expected to know which kernel descriptors are
1603 * currently open, so it wouldn't make sense for them to be inherited by
1604 * executed subprograms.
1605 */
1606 fileFlags |= O_CLOEXEC;
1607
1608 vfdP->fd = BasicOpenFilePerm(fileName, fileFlags, fileMode);
1609
1610 if (vfdP->fd < 0)
1611 {
1612 int save_errno = errno;
1613
1614 FreeVfd(file);
1615 free(fnamecopy);
1616 errno = save_errno;
1617 return -1;
1618 }
1619 ++nfile;
1620 DO_DB(elog(LOG, "PathNameOpenFile: success %d",
1621 vfdP->fd));
1622
1623 vfdP->fileName = fnamecopy;
1624 /* Saved flags are adjusted to be OK for re-opening file */
1625 vfdP->fileFlags = fileFlags & ~(O_CREAT | O_TRUNC | O_EXCL);
1626 vfdP->fileMode = fileMode;
1627 vfdP->fileSize = 0;
1628 vfdP->fdstate = 0x0;
1629 vfdP->resowner = NULL;
1630
1631 Insert(file);
1632
1633 return file;
1634}
static File AllocateVfd(void)
Definition fd.c:1402
static void Insert(File file)
Definition fd.c:1301
#define free(a)
#define O_CLOEXEC
Definition win32_port.h:344

References AllocateVfd(), BasicOpenFilePerm(), DO_DB, elog, ereport, errcode(), errmsg, ERROR, fb(), free, FreeVfd(), Insert(), LOG, nfile, O_CLOEXEC, ReleaseLruFiles(), and VfdCache.

Referenced by PathNameOpenFile().

◆ PathNameOpenTemporaryFile()

File PathNameOpenTemporaryFile ( const char path,
int  mode 
)
extern

Definition at line 1889 of file fd.c.

1890{
1891 File file;
1892
1893 Assert(temporary_files_allowed); /* check temp file access is up */
1894
1896
1897 file = PathNameOpenFile(path, mode | PG_BINARY);
1898
1899 /* If no such file, then we don't raise an error. */
1900 if (file <= 0 && errno != ENOENT)
1901 ereport(ERROR,
1903 errmsg("could not open temporary file \"%s\": %m",
1904 path)));
1905
1906 if (file > 0)
1907 {
1908 /* Register it for automatic close. */
1910 }
1911
1912 return file;
1913}

References Assert, CurrentResourceOwner, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), mode, PathNameOpenFile(), PG_BINARY, RegisterTemporaryFile(), and ResourceOwnerEnlarge().

Referenced by FileSetOpen().

◆ pg_fdatasync()

int pg_fdatasync ( int  fd)
extern

Definition at line 481 of file fd.c.

482{
483 int rc;
484
485 if (!enableFsync)
486 return 0;
487
488retry:
489 rc = fdatasync(fd);
490
491 if (rc == -1 && errno == EINTR)
492 goto retry;
493
494 return rc;
495}
int fdatasync(int fd)
bool enableFsync
Definition globals.c:129

References EINTR, enableFsync, fb(), fd(), and fdatasync().

Referenced by issue_xlog_fsync().

◆ pg_file_exists()

bool pg_file_exists ( const char name)
extern

Definition at line 504 of file fd.c.

505{
506 struct stat st;
507
508 Assert(name != NULL);
509
510 if (stat(name, &st) == 0)
511 return !S_ISDIR(st.st_mode);
512 else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
515 errmsg("could not access file \"%s\": %m", name)));
516
517 return false;
518}
#define S_ISDIR(m)
Definition win32_port.h:315

References Assert, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), name, S_ISDIR, stat::st_mode, and stat.

Referenced by expand_dynamic_library_name(), find_in_path(), find_in_paths(), and provider_init().

◆ pg_flush_data()

void pg_flush_data ( int  fd,
pgoff_t  offset,
pgoff_t  nbytes 
)
extern

Definition at line 526 of file fd.c.

527{
528 /*
529 * Right now file flushing is primarily used to avoid making later
530 * fsync()/fdatasync() calls have less impact. Thus don't trigger flushes
531 * if fsyncs are disabled - that's a decision we might want to make
532 * configurable at some point.
533 */
534 if (!enableFsync)
535 return;
536
537 /*
538 * We compile all alternatives that are supported on the current platform,
539 * to find portability problems more easily.
540 */
541#if defined(HAVE_SYNC_FILE_RANGE)
542 {
543 int rc;
544 static bool not_implemented_by_kernel = false;
545
547 return;
548
549retry:
550
551 /*
552 * sync_file_range(SYNC_FILE_RANGE_WRITE), currently linux specific,
553 * tells the OS that writeback for the specified blocks should be
554 * started, but that we don't want to wait for completion. Note that
555 * this call might block if too much dirty data exists in the range.
556 * This is the preferable method on OSs supporting it, as it works
557 * reliably when available (contrast to msync()) and doesn't flush out
558 * clean data (like FADV_DONTNEED).
559 */
560 rc = sync_file_range(fd, offset, nbytes,
562 if (rc != 0)
563 {
564 int elevel;
565
566 if (rc == EINTR)
567 goto retry;
568
569 /*
570 * For systems that don't have an implementation of
571 * sync_file_range() such as Windows WSL, generate only one
572 * warning and then suppress all further attempts by this process.
573 */
574 if (errno == ENOSYS)
575 {
576 elevel = WARNING;
578 }
579 else
580 elevel = data_sync_elevel(WARNING);
581
582 ereport(elevel,
584 errmsg("could not flush dirty data: %m")));
585 }
586
587 return;
588 }
589#endif
590#if !defined(WIN32) && defined(MS_ASYNC)
591 {
592 void *p;
593 static int pagesize = 0;
594
595 /*
596 * On several OSs msync(MS_ASYNC) on a mmap'ed file triggers
597 * writeback. On linux it only does so if MS_SYNC is specified, but
598 * then it does the writeback synchronously. Luckily all common linux
599 * systems have sync_file_range(). This is preferable over
600 * FADV_DONTNEED because it doesn't flush out clean data.
601 *
602 * We map the file (mmap()), tell the kernel to sync back the contents
603 * (msync()), and then remove the mapping again (munmap()).
604 */
605
606 /* mmap() needs actual length if we want to map whole file */
607 if (offset == 0 && nbytes == 0)
608 {
609 nbytes = lseek(fd, 0, SEEK_END);
610 if (nbytes < 0)
611 {
614 errmsg("could not determine dirty data size: %m")));
615 return;
616 }
617 }
618
619 /*
620 * Some platforms reject partial-page mmap() attempts. To deal with
621 * that, just truncate the request to a page boundary. If any extra
622 * bytes don't get flushed, well, it's only a hint anyway.
623 */
624
625 /* fetch pagesize only once */
626 if (pagesize == 0)
628
629 /* align length to pagesize, dropping any fractional page */
630 if (pagesize > 0)
631 nbytes = (nbytes / pagesize) * pagesize;
632
633 /* fractional-page request is a no-op */
634 if (nbytes <= 0)
635 return;
636
637 /*
638 * mmap could well fail, particularly on 32-bit platforms where there
639 * may simply not be enough address space. If so, silently fall
640 * through to the next implementation.
641 */
642 if (nbytes <= (pgoff_t) SSIZE_MAX)
643 p = mmap(NULL, nbytes, PROT_READ, MAP_SHARED, fd, offset);
644 else
645 p = MAP_FAILED;
646
647 if (p != MAP_FAILED)
648 {
649 int rc;
650
651 rc = msync(p, (size_t) nbytes, MS_ASYNC);
652 if (rc != 0)
653 {
656 errmsg("could not flush dirty data: %m")));
657 /* NB: need to fall through to munmap()! */
658 }
659
660 rc = munmap(p, (size_t) nbytes);
661 if (rc != 0)
662 {
663 /* FATAL error because mapping would remain */
666 errmsg("could not munmap() while flushing data: %m")));
667 }
668
669 return;
670 }
671 }
672#endif
673#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
674 {
675 int rc;
676
677 /*
678 * Signal the kernel that the passed in range should not be cached
679 * anymore. This has the, desired, side effect of writing out dirty
680 * data, and the, undesired, side effect of likely discarding useful
681 * clean cached blocks. For the latter reason this is the least
682 * preferable method.
683 */
684
685 rc = posix_fadvise(fd, offset, nbytes, POSIX_FADV_DONTNEED);
686
687 if (rc != 0)
688 {
689 /* don't error out, this is just a performance optimization */
692 errmsg("could not flush dirty data: %m")));
693 }
694
695 return;
696 }
697#endif
698}
#define MAP_FAILED
Definition mem.h:43

References data_sync_elevel(), EINTR, enableFsync, ereport, errcode_for_file_access(), errmsg, FATAL, fb(), fd(), MAP_FAILED, and WARNING.

Referenced by copy_file(), and FileWriteback().

◆ pg_fsync()

int pg_fsync ( int  fd)
extern

Definition at line 390 of file fd.c.

391{
392#if !defined(WIN32) && defined(USE_ASSERT_CHECKING)
393 struct stat st;
394
395 /*
396 * Some operating system implementations of fsync() have requirements
397 * about the file access modes that were used when their file descriptor
398 * argument was opened, and these requirements differ depending on whether
399 * the file descriptor is for a directory.
400 *
401 * For any file descriptor that may eventually be handed to fsync(), we
402 * should have opened it with access modes that are compatible with
403 * fsync() on all supported systems, otherwise the code may not be
404 * portable, even if it runs ok on the current system.
405 *
406 * We assert here that a descriptor for a file was opened with write
407 * permissions (i.e., not O_RDONLY) and for a directory without write
408 * permissions (O_RDONLY). Notice that the assertion check is made even
409 * if fsync() is disabled.
410 *
411 * If fstat() fails, ignore it and let the follow-up fsync() complain.
412 */
413 if (fstat(fd, &st) == 0)
414 {
415 int desc_flags = fcntl(fd, F_GETFL);
416
418
419 if (S_ISDIR(st.st_mode))
421 else
423 }
424 errno = 0;
425#endif
426
427 /* #if is to skip the wal_sync_method test if there's no need for it */
428#if defined(HAVE_FSYNC_WRITETHROUGH)
431 else
432#endif
434}
int pg_fsync_no_writethrough(int fd)
Definition fd.c:442
int pg_fsync_writethrough(int fd)
Definition fd.c:462
#define fstat
Definition win32_port.h:73
int wal_sync_method
Definition xlog.c:134
@ WAL_SYNC_METHOD_FSYNC_WRITETHROUGH
Definition xlog.h:28

References Assert, fb(), fd(), fstat, pg_fsync_no_writethrough(), pg_fsync_writethrough(), S_ISDIR, stat::st_mode, wal_sync_method, and WAL_SYNC_METHOD_FSYNC_WRITETHROUGH.

Referenced by AddToDataDirLockFile(), assign_wal_sync_method(), BootStrapXLOG(), CheckPointLogicalRewriteHeap(), CreateDirAndVersionFile(), CreateLockFile(), durable_rename(), FileSync(), fsync_fname_ext(), heap_xlog_logical_rewrite(), readRecoverySignalFile(), RecreateTwoPhaseFile(), RestoreSlotFromDisk(), SaveSlotToPath(), SlruPhysicalWritePage(), SlruSyncFileTag(), SnapBuildSerialize(), update_controlfile(), write_auto_conf_file(), WriteControlFile(), writeTimeLineHistory(), writeTimeLineHistoryFile(), XLogFileCopy(), and XLogFileInitInternal().

◆ pg_fsync_no_writethrough()

int pg_fsync_no_writethrough ( int  fd)
extern

Definition at line 442 of file fd.c.

443{
444 int rc;
445
446 if (!enableFsync)
447 return 0;
448
449retry:
450 rc = fsync(fd);
451
452 if (rc == -1 && errno == EINTR)
453 goto retry;
454
455 return rc;
456}
#define fsync(fd)
Definition win32_port.h:83

References EINTR, enableFsync, fb(), fd(), and fsync.

Referenced by issue_xlog_fsync(), and pg_fsync().

◆ pg_fsync_writethrough()

int pg_fsync_writethrough ( int  fd)
extern

Definition at line 462 of file fd.c.

463{
464 if (enableFsync)
465 {
466#if defined(F_FULLFSYNC)
467 return (fcntl(fd, F_FULLFSYNC, 0) == -1) ? -1 : 0;
468#else
469 errno = ENOSYS;
470 return -1;
471#endif
472 }
473 else
474 return 0;
475}

References enableFsync, fb(), and fd().

Referenced by issue_xlog_fsync(), pg_fsync(), and test_sync().

◆ pg_truncate()

int pg_truncate ( const char path,
pgoff_t  length 
)
extern

Definition at line 721 of file fd.c.

722{
723 int ret;
724#ifdef WIN32
725 int save_errno;
726 int fd;
727
729 if (fd >= 0)
730 {
731 ret = pg_ftruncate(fd, length);
735 }
736 else
737 ret = -1;
738#else
739
740retry:
741 ret = truncate(path, length);
742
743 if (ret == -1 && errno == EINTR)
744 goto retry;
745#endif
746
747 return ret;
748}

References CloseTransientFile(), EINTR, fb(), fd(), OpenTransientFile(), PG_BINARY, and pg_ftruncate().

Referenced by do_truncate().

◆ ReadDir()

◆ ReadDirExtended()

struct dirent * ReadDirExtended ( DIR dir,
const char dirname,
int  elevel 
)
extern

Definition at line 2972 of file fd.c.

2973{
2974 struct dirent *dent;
2975
2976 /* Give a generic message for AllocateDir failure, if caller didn't */
2977 if (dir == NULL)
2978 {
2979 ereport(elevel,
2981 errmsg("could not open directory \"%s\": %m",
2982 dirname)));
2983 return NULL;
2984 }
2985
2986 errno = 0;
2987 if ((dent = readdir(dir)) != NULL)
2988 return dent;
2989
2990 if (errno)
2991 ereport(elevel,
2993 errmsg("could not read directory \"%s\": %m",
2994 dirname)));
2995 return NULL;
2996}
struct dirent * readdir(DIR *)
Definition dirent.c:78

References ereport, errcode_for_file_access(), errmsg, fb(), and readdir().

Referenced by DeleteAllExportedSnapshotFiles(), ReadDir(), RelationCacheInitFileRemove(), RelationCacheInitFileRemoveInDir(), RemovePgTempFiles(), RemovePgTempFilesInDir(), RemovePgTempRelationFiles(), RemovePgTempRelationFilesInDbspace(), ReorderBufferCleanupSerializedTXNs(), scan_directory_ci(), SyncDataDirectory(), and walkdir().

◆ ReleaseExternalFD()

◆ RemovePgTempFiles()

void RemovePgTempFiles ( void  )
extern

Definition at line 3323 of file fd.c.

3324{
3326 DIR *spc_dir;
3327 struct dirent *spc_de;
3328
3329 /*
3330 * First process temp files in pg_default ($PGDATA/base)
3331 */
3332 snprintf(temp_path, sizeof(temp_path), "base/%s", PG_TEMP_FILES_DIR);
3333 RemovePgTempFilesInDir(temp_path, true, false);
3335
3336 /*
3337 * Cycle through temp directories for all non-default tablespaces.
3338 */
3340
3342 {
3343 if (strcmp(spc_de->d_name, ".") == 0 ||
3344 strcmp(spc_de->d_name, "..") == 0)
3345 continue;
3346
3347 snprintf(temp_path, sizeof(temp_path), "%s/%s/%s/%s",
3350 RemovePgTempFilesInDir(temp_path, true, false);
3351
3352 snprintf(temp_path, sizeof(temp_path), "%s/%s/%s",
3355 }
3356
3358
3359 /*
3360 * In EXEC_BACKEND case there is a pgsql_tmp directory at the top level of
3361 * DataDir as well. However, that is *not* cleaned here because doing so
3362 * would create a race condition. It's done separately, earlier in
3363 * postmaster startup.
3364 */
3365}
int FreeDir(DIR *dir)
Definition fd.c:3009
static void RemovePgTempRelationFiles(const char *tsdirname)
Definition fd.c:3443
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
Definition fd.c:3383
DIR * AllocateDir(const char *dirname)
Definition fd.c:2891
#define PG_TEMP_FILES_DIR
Definition file_utils.h:63
#define MAXPGPATH
#define snprintf
Definition port.h:260
#define PG_TBLSPC_DIR
Definition relpath.h:41
#define TABLESPACE_VERSION_DIRECTORY
Definition relpath.h:33

References AllocateDir(), fb(), FreeDir(), LOG, MAXPGPATH, PG_TBLSPC_DIR, PG_TEMP_FILES_DIR, ReadDirExtended(), RemovePgTempFilesInDir(), RemovePgTempRelationFiles(), snprintf, and TABLESPACE_VERSION_DIRECTORY.

Referenced by PostmasterMain(), and PostmasterStateMachine().

◆ RemovePgTempFilesInDir()

void RemovePgTempFilesInDir ( const char tmpdirname,
bool  missing_ok,
bool  unlink_all 
)
extern

Definition at line 3383 of file fd.c.

3384{
3385 DIR *temp_dir;
3386 struct dirent *temp_de;
3387 char rm_path[MAXPGPATH * 2];
3388
3390
3391 if (temp_dir == NULL && errno == ENOENT && missing_ok)
3392 return;
3393
3395 {
3396 if (strcmp(temp_de->d_name, ".") == 0 ||
3397 strcmp(temp_de->d_name, "..") == 0)
3398 continue;
3399
3400 snprintf(rm_path, sizeof(rm_path), "%s/%s",
3401 tmpdirname, temp_de->d_name);
3402
3403 if (unlink_all ||
3404 strncmp(temp_de->d_name,
3407 {
3409
3410 if (type == PGFILETYPE_ERROR)
3411 continue;
3412 else if (type == PGFILETYPE_DIR)
3413 {
3414 /* recursively remove contents, then directory itself */
3415 RemovePgTempFilesInDir(rm_path, false, true);
3416
3417 if (rmdir(rm_path) < 0)
3418 ereport(LOG,
3420 errmsg("could not remove directory \"%s\": %m",
3421 rm_path)));
3422 }
3423 else
3424 {
3425 if (unlink(rm_path) < 0)
3426 ereport(LOG,
3428 errmsg("could not remove file \"%s\": %m",
3429 rm_path)));
3430 }
3431 }
3432 else
3433 ereport(LOG,
3434 (errmsg("unexpected file found in temporary-files directory: \"%s\"",
3435 rm_path)));
3436 }
3437
3439}
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition file_utils.c:547
#define PG_TEMP_FILE_PREFIX
Definition file_utils.h:64
PGFileType
Definition file_utils.h:19
@ PGFILETYPE_DIR
Definition file_utils.h:23
@ PGFILETYPE_ERROR
Definition file_utils.h:20
const char * type

References AllocateDir(), ereport, errcode_for_file_access(), errmsg, fb(), FreeDir(), get_dirent_type(), LOG, MAXPGPATH, PG_TEMP_FILE_PREFIX, PGFILETYPE_DIR, PGFILETYPE_ERROR, ReadDirExtended(), RemovePgTempFilesInDir(), snprintf, and type.

Referenced by PostmasterMain(), RemovePgTempFiles(), and RemovePgTempFilesInDir().

◆ ReserveExternalFD()

void ReserveExternalFD ( void  )
extern

Definition at line 1207 of file fd.c.

1208{
1209 /*
1210 * Release VFDs if needed to stay safe. Because we do this before
1211 * incrementing numExternalFDs, the final state will be as desired, i.e.,
1212 * nfile + numAllocatedDescs + numExternalFDs <= max_safe_fds.
1213 */
1215
1217}

References numExternalFDs, and ReleaseLruFiles().

Referenced by AcquireExternalFD(), BackendInitialize(), dsm_impl_posix(), InitializeWaitEventSupport(), InitPostmasterDeathWatchHandle(), and XLogWrite().

◆ set_max_safe_fds()

void set_max_safe_fds ( void  )
extern

Definition at line 1045 of file fd.c.

1046{
1047 int usable_fds;
1048 int already_open;
1049
1050 /*----------
1051 * We want to set max_safe_fds to
1052 * MIN(usable_fds, max_files_per_process)
1053 * less the slop factor for files that are opened without consulting
1054 * fd.c. This ensures that we won't allow to open more than
1055 * max_files_per_process, or the experimentally-determined EMFILE limit,
1056 * additional files.
1057 *----------
1058 */
1061
1063
1064 /*
1065 * Take off the FDs reserved for system() etc.
1066 */
1068
1069 /*
1070 * Make sure we still have enough to get by.
1071 */
1073 ereport(FATAL,
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.",
1079 already_open)));
1080
1081 elog(DEBUG2, "max_safe_fds = %d, usable_fds = %d, already_open = %d",
1083}
#define Min(x, y)
Definition c.h:1093
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define DEBUG2
Definition elog.h:29
int max_files_per_process
Definition fd.c:147
#define FD_MINFREE
Definition fd.c:139
static void count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
Definition fd.c:965
#define NUM_RESERVED_FDS
Definition fd.c:130

References count_usable_fds(), DEBUG2, elog, ereport, errcode(), errdetail(), errmsg, FATAL, fb(), FD_MINFREE, max_files_per_process, max_safe_fds, Min, and NUM_RESERVED_FDS.

Referenced by BootstrapModeMain(), PostgresSingleUserMain(), and PostmasterMain().

◆ SetTempTablespaces()

void SetTempTablespaces ( Oid tableSpaces,
int  numSpaces 
)
extern

Definition at line 3097 of file fd.c.

3098{
3099 Assert(numSpaces >= 0);
3102
3103 /*
3104 * Select a random starting point in the list. This is to minimize
3105 * conflicts between backends that are most likely sharing the same list
3106 * of temp tablespaces. Note that if we create multiple temp files in the
3107 * same transaction, we'll advance circularly through the list --- this
3108 * ensures that large temporary sort files are nicely spread across all
3109 * available tablespaces.
3110 */
3111 if (numSpaces > 1)
3113 0, numSpaces - 1);
3114 else
3116}
uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax)
Definition pg_prng.c:144
pg_prng_state pg_global_prng_state
Definition pg_prng.c:34

References Assert, fb(), nextTempTableSpace, numTempTableSpaces, pg_global_prng_state, pg_prng_uint64_range(), and tempTableSpaces.

Referenced by assign_temp_tablespaces(), and PrepareTempTablespaces().

◆ SyncDataDirectory()

void SyncDataDirectory ( void  )
extern

Definition at line 3594 of file fd.c.

3595{
3596 bool xlog_is_symlink;
3597
3598 /* We can skip this whole thing if fsync is disabled. */
3599 if (!enableFsync)
3600 return;
3601
3602 /*
3603 * If pg_wal is a symlink, we'll need to recurse into it separately,
3604 * because the first walkdir below will ignore it.
3605 */
3606 xlog_is_symlink = false;
3607
3608 {
3609 struct stat st;
3610
3611 if (lstat("pg_wal", &st) < 0)
3612 ereport(LOG,
3614 errmsg("could not stat file \"%s\": %m",
3615 "pg_wal")));
3616 else if (S_ISLNK(st.st_mode))
3617 xlog_is_symlink = true;
3618 }
3619
3620#ifdef HAVE_SYNCFS
3622 {
3623 DIR *dir;
3624 struct dirent *de;
3625
3626 /*
3627 * On Linux, we don't have to open every single file one by one. We
3628 * can use syncfs() to sync whole filesystems. We only expect
3629 * filesystem boundaries to exist where we tolerate symlinks, namely
3630 * pg_wal and the tablespaces, so we call syncfs() for each of those
3631 * directories.
3632 */
3633
3634 /* Prepare to report progress syncing the data directory via syncfs. */
3636
3637 /* Sync the top level pgdata directory. */
3638 do_syncfs(".");
3639 /* If any tablespaces are configured, sync each of those. */
3641 while ((de = ReadDirExtended(dir, PG_TBLSPC_DIR, LOG)))
3642 {
3643 char path[MAXPGPATH];
3644
3645 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
3646 continue;
3647
3648 snprintf(path, MAXPGPATH, "%s/%s", PG_TBLSPC_DIR, de->d_name);
3649 do_syncfs(path);
3650 }
3651 FreeDir(dir);
3652 /* If pg_wal is a symlink, process that too. */
3653 if (xlog_is_symlink)
3654 do_syncfs("pg_wal");
3655 return;
3656 }
3657#endif /* !HAVE_SYNCFS */
3658
3659#ifdef PG_FLUSH_DATA_WORKS
3660 /* Prepare to report progress of the pre-fsync phase. */
3662
3663 /*
3664 * If possible, hint to the kernel that we're soon going to fsync the data
3665 * directory and its contents. Errors in this step are even less
3666 * interesting than normal, so log them only at DEBUG1.
3667 */
3668 walkdir(".", pre_sync_fname, false, DEBUG1);
3669 if (xlog_is_symlink)
3670 walkdir("pg_wal", pre_sync_fname, false, DEBUG1);
3672#endif
3673
3674 /* Prepare to report progress syncing the data directory via fsync. */
3676
3677 /*
3678 * Now we do the fsync()s in the same order.
3679 *
3680 * The main call ignores symlinks, so in addition to specially processing
3681 * pg_wal if it's a symlink, pg_tblspc has to be visited separately with
3682 * process_symlinks = true. Note that if there are any plain directories
3683 * in pg_tblspc, they'll get fsync'd twice. That's not an expected case
3684 * so we don't worry about optimizing it.
3685 */
3686 walkdir(".", datadir_fsync_fname, false, LOG);
3687 if (xlog_is_symlink)
3688 walkdir("pg_wal", datadir_fsync_fname, false, LOG);
3690}
void begin_startup_progress_phase(void)
Definition startup.c:342
#define DEBUG1
Definition elog.h:30
int recovery_init_sync_method
Definition fd.c:166
static void datadir_fsync_fname(const char *fname, bool isdir, int elevel)
Definition fd.c:3809
@ DATA_DIR_SYNC_METHOD_SYNCFS
Definition file_utils.h:30
#define lstat(path, sb)
Definition win32_port.h:275
#define S_ISLNK(m)
Definition win32_port.h:334

References AllocateDir(), begin_startup_progress_phase(), DATA_DIR_SYNC_METHOD_SYNCFS, datadir_fsync_fname(), DEBUG1, enableFsync, ereport, errcode_for_file_access(), errmsg, fb(), FreeDir(), LOG, lstat, MAXPGPATH, PG_TBLSPC_DIR, ReadDirExtended(), recovery_init_sync_method, S_ISLNK, snprintf, stat::st_mode, and walkdir().

Referenced by StartupXLOG().

◆ TempTablespacePath()

void TempTablespacePath ( char path,
Oid  tablespace 
)
extern

Definition at line 1767 of file fd.c.

1768{
1769 /*
1770 * Identify the tempfile directory for this tablespace.
1771 *
1772 * If someone tries to specify pg_global, use pg_default instead.
1773 */
1774 if (tablespace == InvalidOid ||
1777 snprintf(path, MAXPGPATH, "base/%s", PG_TEMP_FILES_DIR);
1778 else
1779 {
1780 /* All other tablespaces are accessed via symlinks */
1781 snprintf(path, MAXPGPATH, "%s/%u/%s/%s",
1784 }
1785}
static char * tablespace
Definition pgbench.c:217

References fb(), InvalidOid, MAXPGPATH, PG_TBLSPC_DIR, PG_TEMP_FILES_DIR, snprintf, tablespace, and TABLESPACE_VERSION_DIRECTORY.

Referenced by FileSetCreate(), FileSetPath(), OpenTemporaryFileInTablespace(), and pg_ls_tmpdir().

◆ TempTablespacesAreSet()

bool TempTablespacesAreSet ( void  )
extern

Definition at line 3126 of file fd.c.

3127{
3128 return (numTempTableSpaces >= 0);
3129}

References numTempTableSpaces.

Referenced by GetTempTablespaces(), and PrepareTempTablespaces().

Variable Documentation

◆ data_sync_retry

PGDLLIMPORT bool data_sync_retry
extern

Definition at line 163 of file fd.c.

Referenced by data_sync_elevel().

◆ file_extend_method

PGDLLIMPORT int file_extend_method
extern

Definition at line 169 of file fd.c.

Referenced by mdzeroextend().

◆ io_direct_flags

◆ max_files_per_process

PGDLLIMPORT int max_files_per_process
extern

Definition at line 147 of file fd.c.

Referenced by set_max_safe_fds().

◆ max_safe_fds

PGDLLIMPORT int max_safe_fds
extern

Definition at line 160 of file fd.c.

Referenced by AcquireExternalFD(), ReleaseLruFiles(), reserveAllocatedDesc(), and set_max_safe_fds().

◆ recovery_init_sync_method

PGDLLIMPORT int recovery_init_sync_method
extern

Definition at line 166 of file fd.c.

Referenced by SyncDataDirectory().