PostgreSQL Source Code git master
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 FILE_POSSIBLY_DELETED(err)   ((err) == ENOENT)
 
#define PG_O_DIRECT   0
 

Typedefs

typedef int File
 

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)
 
char * FilePathName (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)
 
FILE * AllocateFile (const char *name, const char *mode)
 
int FreeFile (FILE *file)
 
FILE * OpenPipeStream (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 max_safe_fds
 

Macro Definition Documentation

◆ FILE_POSSIBLY_DELETED

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

Definition at line 78 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 112 of file fd.h.

Typedef Documentation

◆ File

typedef int File

Definition at line 51 of file fd.h.

Function Documentation

◆ AcquireExternalFD()

bool AcquireExternalFD ( void  )

Definition at line 1168 of file fd.c.

1169{
1170 /*
1171 * We don't want more than max_safe_fds / 3 FDs to be consumed for
1172 * "external" FDs.
1173 */
1174 if (numExternalFDs < max_safe_fds / 3)
1175 {
1177 return true;
1178 }
1179 errno = EMFILE;
1180 return false;
1181}
int max_safe_fds
Definition: fd.c:159
void ReserveExternalFD(void)
Definition: fd.c:1203
static int numExternalFDs
Definition: fd.c:274

References max_safe_fds, numExternalFDs, and ReserveExternalFD().

Referenced by CreateWaitEventSet(), and libpqsrv_connect_prepare().

◆ AllocateDir()

DIR * AllocateDir ( const char *  dirname)

Definition at line 2887 of file fd.c.

2888{
2889 DIR *dir;
2890
2891 DO_DB(elog(LOG, "AllocateDir: Allocated %d (%s)",
2892 numAllocatedDescs, dirname));
2893
2894 /* Can we allocate another non-virtual FD? */
2895 if (!reserveAllocatedDesc())
2896 ereport(ERROR,
2897 (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2898 errmsg("exceeded maxAllocatedDescs (%d) while trying to open directory \"%s\"",
2899 maxAllocatedDescs, dirname)));
2900
2901 /* Close excess kernel FDs. */
2903
2904TryAgain:
2905 if ((dir = opendir(dirname)) != NULL)
2906 {
2908
2909 desc->kind = AllocateDescDir;
2910 desc->desc.dir = dir;
2913 return desc->desc.dir;
2914 }
2915
2916 if (errno == EMFILE || errno == ENFILE)
2917 {
2918 int save_errno = errno;
2919
2920 ereport(LOG,
2921 (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2922 errmsg("out of file descriptors: %m; release and retry")));
2923 errno = 0;
2924 if (ReleaseLruFile())
2925 goto TryAgain;
2926 errno = save_errno;
2927 }
2928
2929 return NULL;
2930}
DIR * opendir(const char *)
Definition: dirent.c:33
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#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:1366
static int maxAllocatedDescs
Definition: fd.c:268
static int numAllocatedDescs
Definition: fd.c:267
#define DO_DB(A)
Definition: fd.c:180
static AllocateDesc * allocatedDescs
Definition: fd.c:269
@ AllocateDescDir
Definition: fd.c:251
static bool reserveAllocatedDesc(void)
Definition: fd.c:2549
static void ReleaseLruFiles(void)
Definition: fd.c:1388
SubTransactionId create_subid
Definition: fd.c:258
DIR * dir
Definition: fd.c:262
union AllocateDesc::@20 desc
AllocateDescKind kind
Definition: fd.c:257
Definition: dirent.c:26
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:792

References allocatedDescs, AllocateDescDir, AllocateDesc::create_subid, AllocateDesc::desc, AllocateDesc::dir, DO_DB, elog, ereport, errcode(), errmsg(), ERROR, 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 
)

Definition at line 2624 of file fd.c.

2625{
2626 FILE *file;
2627
2628 DO_DB(elog(LOG, "AllocateFile: Allocated %d (%s)",
2630
2631 /* Can we allocate another non-virtual FD? */
2632 if (!reserveAllocatedDesc())
2633 ereport(ERROR,
2634 (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2635 errmsg("exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2637
2638 /* Close excess kernel FDs. */
2640
2641TryAgain:
2642 if ((file = fopen(name, mode)) != NULL)
2643 {
2645
2646 desc->kind = AllocateDescFile;
2647 desc->desc.file = file;
2650 return desc->desc.file;
2651 }
2652
2653 if (errno == EMFILE || errno == ENFILE)
2654 {
2655 int save_errno = errno;
2656
2657 ereport(LOG,
2658 (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2659 errmsg("out of file descriptors: %m; release and retry")));
2660 errno = 0;
2661 if (ReleaseLruFile())
2662 goto TryAgain;
2663 errno = save_errno;
2664 }
2665
2666 return NULL;
2667}
@ AllocateDescFile
Definition: fd.c:249
static PgChecksumMode mode
Definition: pg_checksums.c:56
FILE * file
Definition: fd.c:261
const char * name

References allocatedDescs, AllocateDescFile, AllocateDesc::create_subid, AllocateDesc::desc, DO_DB, elog, ereport, errcode(), errmsg(), ERROR, 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(), tsearch_readline_begin(), ValidatePgVersion(), write_relcache_init_file(), XLogArchiveForceDone(), and XLogArchiveNotify().

◆ AtEOSubXact_Files()

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

Definition at line 3177 of file fd.c.

3179{
3180 Index i;
3181
3182 for (i = 0; i < numAllocatedDescs; i++)
3183 {
3184 if (allocatedDescs[i].create_subid == mySubid)
3185 {
3186 if (isCommit)
3187 allocatedDescs[i].create_subid = parentSubid;
3188 else
3189 {
3190 /* have to recheck the item after FreeDesc (ugly) */
3192 }
3193 }
3194 }
3195}
unsigned int Index
Definition: c.h:633
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2783
int i
Definition: isn.c:77

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

◆ AtEOXact_Files()

void AtEOXact_Files ( bool  isCommit)

Definition at line 3210 of file fd.c.

3211{
3212 CleanupTempFiles(isCommit, false);
3213 tempTableSpaces = NULL;
3214 numTempTableSpaces = -1;
3215}
static int numTempTableSpaces
Definition: fd.c:289
static Oid * tempTableSpaces
Definition: fd.c:288
static void CleanupTempFiles(bool isCommit, bool isProcExit)
Definition: fd.c:3247

References CleanupTempFiles(), numTempTableSpaces, and tempTableSpaces.

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

◆ BasicOpenFile()

int BasicOpenFile ( const char *  fileName,
int  fileFlags 
)

Definition at line 1086 of file fd.c.

1087{
1088 return BasicOpenFilePerm(fileName, fileFlags, pg_file_create_mode);
1089}
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1108
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 
)

Definition at line 1108 of file fd.c.

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

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

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

◆ closeAllVfds()

void closeAllVfds ( void  )

Definition at line 3064 of file fd.c.

3065{
3066 Index i;
3067
3068 if (SizeVfdCache > 0)
3069 {
3070 Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
3071 for (i = 1; i < SizeVfdCache; i++)
3072 {
3073 if (!FileIsNotOpen(i))
3074 LruDelete(i);
3075 }
3076 }
3077}
static void LruDelete(File file)
Definition: fd.c:1269
static Size SizeVfdCache
Definition: fd.c:217
#define FileIsNotOpen(file)
Definition: fd.c:189
Assert(PointerIsAligned(start, uint64))

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

Referenced by standard_ProcessUtility().

◆ ClosePipeStream()

int ClosePipeStream ( FILE *  file)

Definition at line 3035 of file fd.c.

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

References allocatedDescs, AllocateDescPipe, AllocateDesc::desc, DO_DB, elog, 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)

Definition at line 2851 of file fd.c.

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

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 
)

Definition at line 779 of file fd.c.

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

References CloseTransientFile(), ereport, errcode_for_file_access(), errmsg(), 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(), 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 
)

Definition at line 869 of file fd.c.

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

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

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

◆ FileClose()

void FileClose ( File  file)

Definition at line 1962 of file fd.c.

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

References Assert(), close, data_sync_elevel(), Delete(), DO_DB, elog, ereport, errcode_for_file_access(), errmsg(), vfd::fd, FD_DELETE_AT_CLOSE, FD_TEMP_FILE_LIMIT, vfd::fdstate, FileIsNotOpen, FileIsValid, vfd::fileName, vfd::fileSize, FreeVfd(), LOG, nfile, pgaio_closing_fd(), ReportTemporaryFileUsage(), ResourceOwnerForgetFile(), vfd::resowner, stat::st_size, 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 
)

Definition at line 2404 of file fd.c.

2405{
2406#ifdef HAVE_POSIX_FALLOCATE
2407 int returnCode;
2408
2409 Assert(FileIsValid(file));
2410
2411 DO_DB(elog(LOG, "FileFallocate: %d (%s) " INT64_FORMAT " " INT64_FORMAT,
2412 file, VfdCache[file].fileName,
2413 (int64) offset, (int64) amount));
2414
2415 returnCode = FileAccess(file);
2416 if (returnCode < 0)
2417 return -1;
2418
2419retry:
2420 pgstat_report_wait_start(wait_event_info);
2421 returnCode = posix_fallocate(VfdCache[file].fd, offset, amount);
2423
2424 if (returnCode == 0)
2425 return 0;
2426 else if (returnCode == EINTR)
2427 goto retry;
2428
2429 /* for compatibility with %m printing etc */
2430 errno = returnCode;
2431
2432 /*
2433 * Return in cases of a "real" failure, if fallocate is not supported,
2434 * fall through to the FileZero() backed implementation.
2435 */
2436 if (returnCode != EINVAL && returnCode != EOPNOTSUPP)
2437 return -1;
2438#endif
2439
2440 return FileZero(file, offset, amount, wait_event_info);
2441}
#define INT64_FORMAT
Definition: c.h:570
int64_t int64
Definition: c.h:549
static int FileAccess(File file)
Definition: fd.c:1476
int FileZero(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info)
Definition: fd.c:2359
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, 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)

Definition at line 2512 of file fd.c.

2513{
2514 int returnCode;
2515
2516 returnCode = FileAccess(file);
2517 if (returnCode < 0)
2518 return returnCode;
2519
2520 Assert(FileIsValid(file));
2521 return VfdCache[file].fd;
2522}

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

Referenced by mdfd().

◆ FileGetRawFlags()

int FileGetRawFlags ( File  file)

Definition at line 2528 of file fd.c.

2529{
2530 Assert(FileIsValid(file));
2531 return VfdCache[file].fileFlags;
2532}
int fileFlags
Definition: fd.c:207

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

◆ FileGetRawMode()

mode_t FileGetRawMode ( File  file)

Definition at line 2538 of file fd.c.

2539{
2540 Assert(FileIsValid(file));
2541 return VfdCache[file].fileMode;
2542}
mode_t fileMode
Definition: fd.c:208

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

◆ FilePathName()

◆ FilePrefetch()

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

Definition at line 2063 of file fd.c.

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

References Assert(), DO_DB, EINTR, elog, 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 214 of file fd.h.

216{
217 struct iovec iov = {
218 .iov_base = buffer,
219 .iov_len = amount
220 };
221
222 return FileReadV(file, &iov, 1, offset, wait_event_info);
223}
ssize_t FileReadV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info)
Definition: fd.c:2145

References 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 
)

Definition at line 2145 of file fd.c.

2147{
2148 ssize_t returnCode;
2149 Vfd *vfdP;
2150
2151 Assert(FileIsValid(file));
2152
2153 DO_DB(elog(LOG, "FileReadV: %d (%s) " INT64_FORMAT " %d",
2154 file, VfdCache[file].fileName,
2155 (int64) offset,
2156 iovcnt));
2157
2158 returnCode = FileAccess(file);
2159 if (returnCode < 0)
2160 return returnCode;
2161
2162 vfdP = &VfdCache[file];
2163
2164retry:
2165 pgstat_report_wait_start(wait_event_info);
2166 returnCode = pg_preadv(vfdP->fd, iov, iovcnt, offset);
2168
2169 if (returnCode < 0)
2170 {
2171 /*
2172 * Windows may run out of kernel buffers and return "Insufficient
2173 * system resources" error. Wait a bit and retry to solve it.
2174 *
2175 * It is rumored that EINTR is also possible on some Unix filesystems,
2176 * in which case immediate retry is indicated.
2177 */
2178#ifdef WIN32
2179 DWORD error = GetLastError();
2180
2181 switch (error)
2182 {
2183 case ERROR_NO_SYSTEM_RESOURCES:
2184 pg_usleep(1000L);
2185 errno = EINTR;
2186 break;
2187 default:
2189 break;
2190 }
2191#endif
2192 /* OK to retry if interrupted */
2193 if (errno == EINTR)
2194 goto retry;
2195 }
2196
2197 return returnCode;
2198}
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)
Definition: sql-dyntest.c:147
void _dosmaperr(unsigned long)
Definition: win32error.c:177

References _dosmaperr(), Assert(), DO_DB, EINTR, elog, error(), vfd::fd, 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)

Definition at line 2444 of file fd.c.

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

References Assert(), DO_DB, elog, fd(), FileAccess(), FileIsNotOpen, FileIsValid, LOG, pgoff_t, 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 
)

Definition at line 2201 of file fd.c.

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

References Assert(), DO_DB, elog, vfd::fd, FileAccess(), FileIsValid, INT64_FORMAT, LOG, pgaio_io_start_readv(), and VfdCache.

Referenced by mdstartreadv().

◆ FileSync()

int FileSync ( File  file,
uint32  wait_event_info 
)

Definition at line 2332 of file fd.c.

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

References Assert(), DO_DB, elog, 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 
)

Definition at line 2461 of file fd.c.

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

References Assert(), DO_DB, elog, 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 226 of file fd.h.

228{
229 struct iovec iov = {
230 .iov_base = unconstify(void *, buffer),
231 .iov_len = amount
232 };
233
234 return FileWriteV(file, &iov, 1, offset, wait_event_info);
235}
#define unconstify(underlying_type, expr)
Definition: c.h:1243
ssize_t FileWriteV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info)
Definition: fd.c:2227

References 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 
)

Definition at line 2119 of file fd.c.

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

References Assert(), DO_DB, elog, 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 
)

Definition at line 2227 of file fd.c.

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

Definition at line 2359 of file fd.c.

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

References Assert(), DO_DB, elog, 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)

Definition at line 3005 of file fd.c.

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

References allocatedDescs, AllocateDescDir, closedir(), AllocateDesc::desc, AllocateDesc::dir, DO_DB, elog, 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)

Definition at line 2823 of file fd.c.

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

References allocatedDescs, AllocateDescFile, AllocateDesc::desc, DO_DB, elog, 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(), 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 
)

Definition at line 3843 of file fd.c.

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

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

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

◆ GetNextTempTableSpace()

Oid GetNextTempTableSpace ( void  )

Definition at line 3155 of file fd.c.

3156{
3157 if (numTempTableSpaces > 0)
3158 {
3159 /* Advance nextTempTableSpace counter with wraparound */
3163 }
3164 return InvalidOid;
3165}
static int nextTempTableSpace
Definition: fd.c:290
#define InvalidOid
Definition: postgres_ext.h:37

References InvalidOid, nextTempTableSpace, numTempTableSpaces, and tempTableSpaces.

Referenced by GetDefaultTablespace(), and OpenTemporaryFile().

◆ GetTempTablespaces()

int GetTempTablespaces ( Oid tableSpaces,
int  numSpaces 
)

Definition at line 3137 of file fd.c.

3138{
3139 int i;
3140
3142 for (i = 0; i < numTempTableSpaces && i < numSpaces; ++i)
3143 tableSpaces[i] = tempTableSpaces[i];
3144
3145 return i;
3146}
bool TempTablespacesAreSet(void)
Definition: fd.c:3122

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

Referenced by FileSetInit().

◆ InitFileAccess()

void InitFileAccess ( void  )

Definition at line 900 of file fd.c.

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

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

Referenced by BaseInit().

◆ InitTemporaryFileAccess()

void InitTemporaryFileAccess ( void  )

Definition at line 930 of file fd.c.

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

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

Referenced by BaseInit().

◆ looks_like_temp_rel_name()

bool looks_like_temp_rel_name ( const char *  name)

Definition at line 3495 of file fd.c.

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

References forkname_chars(), and name.

Referenced by RemovePgTempRelationFilesInDbspace(), and sendDir().

◆ MakePGDirectory()

int MakePGDirectory ( const char *  directoryName)

◆ OpenPipeStream()

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

Definition at line 2727 of file fd.c.

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

References allocatedDescs, AllocateDescPipe, AllocateDesc::create_subid, AllocateDesc::desc, DO_DB, elog, ereport, errcode(), errmsg(), ERROR, 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)

Definition at line 1708 of file fd.c.

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

References Assert(), CurrentResourceOwner, 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 
)

Definition at line 2683 of file fd.c.

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

References allocatedDescs, AllocateDescRawFD, BasicOpenFilePerm(), AllocateDesc::create_subid, AllocateDesc::desc, DO_DB, elog, ereport, errcode(), errmsg(), ERROR, 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 
)

Definition at line 1644 of file fd.c.

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

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

Referenced by FileSetCreate().

◆ PathNameCreateTemporaryFile()

File PathNameCreateTemporaryFile ( const char *  path,
bool  error_on_failure 
)

Definition at line 1845 of file fd.c.

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

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

Referenced by FileSetCreate().

◆ PathNameDeleteTemporaryDir()

void PathNameDeleteTemporaryDir ( const char *  dirname)

Definition at line 1675 of file fd.c.

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

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

Referenced by FileSetDeleteAll().

◆ PathNameDeleteTemporaryFile()

bool PathNameDeleteTemporaryFile ( const char *  path,
bool  error_on_failure 
)

Definition at line 1916 of file fd.c.

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

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

Referenced by FileSetDelete(), and unlink_if_exists_fname().

◆ PathNameOpenFile()

File PathNameOpenFile ( const char *  fileName,
int  fileFlags 
)

◆ PathNameOpenFilePerm()

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

Definition at line 1572 of file fd.c.

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

References AllocateVfd(), BasicOpenFilePerm(), DO_DB, elog, ereport, errcode(), errmsg(), ERROR, vfd::fd, vfd::fdstate, vfd::fileFlags, vfd::fileMode, vfd::fileName, vfd::fileSize, free, FreeVfd(), Insert(), LOG, nfile, O_CLOEXEC, ReleaseLruFiles(), vfd::resowner, and VfdCache.

Referenced by PathNameOpenFile().

◆ PathNameOpenTemporaryFile()

File PathNameOpenTemporaryFile ( const char *  path,
int  mode 
)

Definition at line 1885 of file fd.c.

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

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

Referenced by FileSetOpen().

◆ pg_fdatasync()

int pg_fdatasync ( int  fd)

Definition at line 477 of file fd.c.

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

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

Referenced by issue_xlog_fsync().

◆ pg_file_exists()

bool pg_file_exists ( const char *  name)

Definition at line 500 of file fd.c.

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

References Assert(), ereport, errcode_for_file_access(), errmsg(), ERROR, 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 
)

Definition at line 522 of file fd.c.

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

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

Referenced by copy_file(), and FileWriteback().

◆ pg_fsync()

int pg_fsync ( int  fd)

Definition at line 386 of file fd.c.

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

References Assert(), 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)

Definition at line 438 of file fd.c.

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

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

Referenced by issue_xlog_fsync(), and pg_fsync().

◆ pg_fsync_writethrough()

int pg_fsync_writethrough ( int  fd)

Definition at line 458 of file fd.c.

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

References enableFsync, and fd().

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

◆ pg_truncate()

int pg_truncate ( const char *  path,
pgoff_t  length 
)

Definition at line 717 of file fd.c.

718{
719 int ret;
720#ifdef WIN32
721 int save_errno;
722 int fd;
723
724 fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
725 if (fd >= 0)
726 {
727 ret = pg_ftruncate(fd, length);
728 save_errno = errno;
730 errno = save_errno;
731 }
732 else
733 ret = -1;
734#else
735
736retry:
737 ret = truncate(path, length);
738
739 if (ret == -1 && errno == EINTR)
740 goto retry;
741#endif
742
743 return ret;
744}

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

Referenced by do_truncate().

◆ ReadDir()

◆ ReadDirExtended()

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

Definition at line 2968 of file fd.c.

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

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

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

◆ ReleaseExternalFD()

◆ RemovePgTempFiles()

void RemovePgTempFiles ( void  )

Definition at line 3319 of file fd.c.

3320{
3321 char temp_path[MAXPGPATH + sizeof(PG_TBLSPC_DIR) + sizeof(TABLESPACE_VERSION_DIRECTORY) + sizeof(PG_TEMP_FILES_DIR)];
3322 DIR *spc_dir;
3323 struct dirent *spc_de;
3324
3325 /*
3326 * First process temp files in pg_default ($PGDATA/base)
3327 */
3328 snprintf(temp_path, sizeof(temp_path), "base/%s", PG_TEMP_FILES_DIR);
3329 RemovePgTempFilesInDir(temp_path, true, false);
3331
3332 /*
3333 * Cycle through temp directories for all non-default tablespaces.
3334 */
3335 spc_dir = AllocateDir(PG_TBLSPC_DIR);
3336
3337 while ((spc_de = ReadDirExtended(spc_dir, PG_TBLSPC_DIR, LOG)) != NULL)
3338 {
3339 if (strcmp(spc_de->d_name, ".") == 0 ||
3340 strcmp(spc_de->d_name, "..") == 0)
3341 continue;
3342
3343 snprintf(temp_path, sizeof(temp_path), "%s/%s/%s/%s",
3346 RemovePgTempFilesInDir(temp_path, true, false);
3347
3348 snprintf(temp_path, sizeof(temp_path), "%s/%s/%s",
3350 RemovePgTempRelationFiles(temp_path);
3351 }
3352
3353 FreeDir(spc_dir);
3354
3355 /*
3356 * In EXEC_BACKEND case there is a pgsql_tmp directory at the top level of
3357 * DataDir as well. However, that is *not* cleaned here because doing so
3358 * would create a race condition. It's done separately, earlier in
3359 * postmaster startup.
3360 */
3361}
int FreeDir(DIR *dir)
Definition: fd.c:3005
static void RemovePgTempRelationFiles(const char *tsdirname)
Definition: fd.c:3439
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
Definition: fd.c:3379
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2887
#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
char d_name[MAX_PATH]
Definition: dirent.h:15

References AllocateDir(), dirent::d_name, 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 
)

Definition at line 3379 of file fd.c.

3380{
3381 DIR *temp_dir;
3382 struct dirent *temp_de;
3383 char rm_path[MAXPGPATH * 2];
3384
3385 temp_dir = AllocateDir(tmpdirname);
3386
3387 if (temp_dir == NULL && errno == ENOENT && missing_ok)
3388 return;
3389
3390 while ((temp_de = ReadDirExtended(temp_dir, tmpdirname, LOG)) != NULL)
3391 {
3392 if (strcmp(temp_de->d_name, ".") == 0 ||
3393 strcmp(temp_de->d_name, "..") == 0)
3394 continue;
3395
3396 snprintf(rm_path, sizeof(rm_path), "%s/%s",
3397 tmpdirname, temp_de->d_name);
3398
3399 if (unlink_all ||
3400 strncmp(temp_de->d_name,
3402 strlen(PG_TEMP_FILE_PREFIX)) == 0)
3403 {
3404 PGFileType type = get_dirent_type(rm_path, temp_de, false, LOG);
3405
3406 if (type == PGFILETYPE_ERROR)
3407 continue;
3408 else if (type == PGFILETYPE_DIR)
3409 {
3410 /* recursively remove contents, then directory itself */
3411 RemovePgTempFilesInDir(rm_path, false, true);
3412
3413 if (rmdir(rm_path) < 0)
3414 ereport(LOG,
3416 errmsg("could not remove directory \"%s\": %m",
3417 rm_path)));
3418 }
3419 else
3420 {
3421 if (unlink(rm_path) < 0)
3422 ereport(LOG,
3424 errmsg("could not remove file \"%s\": %m",
3425 rm_path)));
3426 }
3427 }
3428 else
3429 ereport(LOG,
3430 (errmsg("unexpected file found in temporary-files directory: \"%s\"",
3431 rm_path)));
3432 }
3433
3434 FreeDir(temp_dir);
3435}
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(), dirent::d_name, ereport, errcode_for_file_access(), errmsg(), 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  )

Definition at line 1203 of file fd.c.

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

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  )

Definition at line 1041 of file fd.c.

1042{
1043 int usable_fds;
1044 int already_open;
1045
1046 /*----------
1047 * We want to set max_safe_fds to
1048 * MIN(usable_fds, max_files_per_process)
1049 * less the slop factor for files that are opened without consulting
1050 * fd.c. This ensures that we won't allow to open more than
1051 * max_files_per_process, or the experimentally-determined EMFILE limit,
1052 * additional files.
1053 *----------
1054 */
1056 &usable_fds, &already_open);
1057
1058 max_safe_fds = Min(usable_fds, max_files_per_process);
1059
1060 /*
1061 * Take off the FDs reserved for system() etc.
1062 */
1064
1065 /*
1066 * Make sure we still have enough to get by.
1067 */
1069 ereport(FATAL,
1070 (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
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.",
1075 already_open)));
1076
1077 elog(DEBUG2, "max_safe_fds = %d, usable_fds = %d, already_open = %d",
1078 max_safe_fds, usable_fds, already_open);
1079}
#define Min(x, y)
Definition: c.h:1016
int errdetail(const char *fmt,...)
Definition: elog.c:1216
#define DEBUG2
Definition: elog.h:29
int max_files_per_process
Definition: fd.c:146
#define FD_MINFREE
Definition: fd.c:138
static void count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
Definition: fd.c:961
#define NUM_RESERVED_FDS
Definition: fd.c:129

References count_usable_fds(), DEBUG2, elog, ereport, errcode(), errdetail(), errmsg(), FATAL, 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 
)

Definition at line 3093 of file fd.c.

3094{
3095 Assert(numSpaces >= 0);
3096 tempTableSpaces = tableSpaces;
3097 numTempTableSpaces = numSpaces;
3098
3099 /*
3100 * Select a random starting point in the list. This is to minimize
3101 * conflicts between backends that are most likely sharing the same list
3102 * of temp tablespaces. Note that if we create multiple temp files in the
3103 * same transaction, we'll advance circularly through the list --- this
3104 * ensures that large temporary sort files are nicely spread across all
3105 * available tablespaces.
3106 */
3107 if (numSpaces > 1)
3109 0, numSpaces - 1);
3110 else
3112}
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(), nextTempTableSpace, numTempTableSpaces, pg_global_prng_state, pg_prng_uint64_range(), and tempTableSpaces.

Referenced by assign_temp_tablespaces(), and PrepareTempTablespaces().

◆ SyncDataDirectory()

void SyncDataDirectory ( void  )

Definition at line 3590 of file fd.c.

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

Definition at line 1763 of file fd.c.

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

References 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  )

Definition at line 3122 of file fd.c.

3123{
3124 return (numTempTableSpaces >= 0);
3125}

References numTempTableSpaces.

Referenced by GetTempTablespaces(), and PrepareTempTablespaces().

Variable Documentation

◆ data_sync_retry

PGDLLIMPORT bool data_sync_retry
extern

Definition at line 162 of file fd.c.

Referenced by data_sync_elevel().

◆ io_direct_flags

◆ max_files_per_process

PGDLLIMPORT int max_files_per_process
extern

Definition at line 146 of file fd.c.

Referenced by set_max_safe_fds().

◆ max_safe_fds

PGDLLIMPORT int max_safe_fds
extern

Definition at line 159 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 165 of file fd.c.

Referenced by SyncDataDirectory().