PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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, off_t offset, off_t amount, uint32 wait_event_info)
 
ssize_t FileReadV (File file, const struct iovec *iov, int iovcnt, off_t offset, uint32 wait_event_info)
 
ssize_t FileWriteV (File file, const struct iovec *iov, int iovcnt, off_t offset, uint32 wait_event_info)
 
int FileSync (File file, uint32 wait_event_info)
 
int FileZero (File file, off_t offset, off_t amount, uint32 wait_event_info)
 
int FileFallocate (File file, off_t offset, off_t amount, uint32 wait_event_info)
 
off_t FileSize (File file)
 
int FileTruncate (File file, off_t offset, uint32 wait_event_info)
 
void FileWriteback (File file, off_t offset, off_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, off_t offset, off_t nbytes)
 
int pg_truncate (const char *path, off_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, off_t offset, uint32 wait_event_info)
 
static ssize_t FileWrite (File file, const void *buffer, size_t amount, off_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 97 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 1185 of file fd.c.

1186 {
1187  /*
1188  * We don't want more than max_safe_fds / 3 FDs to be consumed for
1189  * "external" FDs.
1190  */
1191  if (numExternalFDs < max_safe_fds / 3)
1192  {
1194  return true;
1195  }
1196  errno = EMFILE;
1197  return false;
1198 }
int max_safe_fds
Definition: fd.c:158
void ReserveExternalFD(void)
Definition: fd.c:1220
static int numExternalFDs
Definition: fd.c:273

References max_safe_fds, numExternalFDs, and ReserveExternalFD().

Referenced by CreateWaitEventSet(), and libpqsrv_connect_prepare().

◆ AllocateDir()

DIR* AllocateDir ( const char *  dirname)

Definition at line 2865 of file fd.c.

2866 {
2867  DIR *dir;
2868 
2869  DO_DB(elog(LOG, "AllocateDir: Allocated %d (%s)",
2870  numAllocatedDescs, dirname));
2871 
2872  /* Can we allocate another non-virtual FD? */
2873  if (!reserveAllocatedDesc())
2874  ereport(ERROR,
2875  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2876  errmsg("exceeded maxAllocatedDescs (%d) while trying to open directory \"%s\"",
2877  maxAllocatedDescs, dirname)));
2878 
2879  /* Close excess kernel FDs. */
2880  ReleaseLruFiles();
2881 
2882 TryAgain:
2883  if ((dir = opendir(dirname)) != NULL)
2884  {
2886 
2887  desc->kind = AllocateDescDir;
2888  desc->desc.dir = dir;
2891  return desc->desc.dir;
2892  }
2893 
2894  if (errno == EMFILE || errno == ENFILE)
2895  {
2896  int save_errno = errno;
2897 
2898  ereport(LOG,
2899  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2900  errmsg("out of file descriptors: %m; release and retry")));
2901  errno = 0;
2902  if (ReleaseLruFile())
2903  goto TryAgain;
2904  errno = save_errno;
2905  }
2906 
2907  return NULL;
2908 }
DIR * opendir(const char *)
Definition: dirent.c:33
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define LOG
Definition: elog.h:31
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
static bool ReleaseLruFile(void)
Definition: fd.c:1381
static int maxAllocatedDescs
Definition: fd.c:267
static int numAllocatedDescs
Definition: fd.c:266
#define DO_DB(A)
Definition: fd.c:179
static AllocateDesc * allocatedDescs
Definition: fd.c:268
@ AllocateDescDir
Definition: fd.c:250
static bool reserveAllocatedDesc(void)
Definition: fd.c:2530
static void ReleaseLruFiles(void)
Definition: fd.c:1403
SubTransactionId create_subid
Definition: fd.c:257
DIR * dir
Definition: fd.c:261
union AllocateDesc::@20 desc
AllocateDescKind kind
Definition: fd.c:256
Definition: dirent.c:26
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:790

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 2605 of file fd.c.

2606 {
2607  FILE *file;
2608 
2609  DO_DB(elog(LOG, "AllocateFile: Allocated %d (%s)",
2611 
2612  /* Can we allocate another non-virtual FD? */
2613  if (!reserveAllocatedDesc())
2614  ereport(ERROR,
2615  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2616  errmsg("exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2617  maxAllocatedDescs, name)));
2618 
2619  /* Close excess kernel FDs. */
2620  ReleaseLruFiles();
2621 
2622 TryAgain:
2623  if ((file = fopen(name, mode)) != NULL)
2624  {
2626 
2627  desc->kind = AllocateDescFile;
2628  desc->desc.file = file;
2631  return desc->desc.file;
2632  }
2633 
2634  if (errno == EMFILE || errno == ENFILE)
2635  {
2636  int save_errno = errno;
2637 
2638  ereport(LOG,
2639  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2640  errmsg("out of file descriptors: %m; release and retry")));
2641  errno = 0;
2642  if (ReleaseLruFile())
2643  goto TryAgain;
2644  errno = save_errno;
2645  }
2646 
2647  return NULL;
2648 }
@ AllocateDescFile
Definition: fd.c:248
static PgChecksumMode mode
Definition: pg_checksums.c:55
FILE * file
Definition: fd.c:260
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(), 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 3154 of file fd.c.

3156 {
3157  Index i;
3158 
3159  for (i = 0; i < numAllocatedDescs; i++)
3160  {
3161  if (allocatedDescs[i].create_subid == mySubid)
3162  {
3163  if (isCommit)
3164  allocatedDescs[i].create_subid = parentSubid;
3165  else
3166  {
3167  /* have to recheck the item after FreeDesc (ugly) */
3168  FreeDesc(&allocatedDescs[i--]);
3169  }
3170  }
3171  }
3172 }
unsigned int Index
Definition: c.h:619
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2764
int i
Definition: isn.c:72

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

◆ AtEOXact_Files()

void AtEOXact_Files ( bool  isCommit)

Definition at line 3187 of file fd.c.

3188 {
3189  CleanupTempFiles(isCommit, false);
3190  tempTableSpaces = NULL;
3191  numTempTableSpaces = -1;
3192 }
static int numTempTableSpaces
Definition: fd.c:288
static Oid * tempTableSpaces
Definition: fd.c:287
static void CleanupTempFiles(bool isCommit, bool isProcExit)
Definition: fd.c:3224

References CleanupTempFiles(), numTempTableSpaces, and tempTableSpaces.

Referenced by AbortTransaction(), 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 
1112 tryAgain:
1113 #ifdef PG_O_DIRECT_USE_F_NOCACHE
1114 
1115  /*
1116  * The value we defined to stand in for O_DIRECT when simulating it with
1117  * F_NOCACHE had better not collide with any of the standard flags.
1118  */
1120  (O_APPEND |
1121  O_CLOEXEC |
1122  O_CREAT |
1123  O_DSYNC |
1124  O_EXCL |
1125  O_RDWR |
1126  O_RDONLY |
1127  O_SYNC |
1128  O_TRUNC |
1129  O_WRONLY)) == 0,
1130  "PG_O_DIRECT value collides with standard flag");
1131  fd = open(fileName, fileFlags & ~PG_O_DIRECT, fileMode);
1132 #else
1133  fd = open(fileName, fileFlags, fileMode);
1134 #endif
1135 
1136  if (fd >= 0)
1137  {
1138 #ifdef PG_O_DIRECT_USE_F_NOCACHE
1139  if (fileFlags & PG_O_DIRECT)
1140  {
1141  if (fcntl(fd, F_NOCACHE, 1) < 0)
1142  {
1143  int save_errno = errno;
1144 
1145  close(fd);
1146  errno = save_errno;
1147  return -1;
1148  }
1149  }
1150 #endif
1151 
1152  return fd; /* success! */
1153  }
1154 
1155  if (errno == EMFILE || errno == ENFILE)
1156  {
1157  int save_errno = errno;
1158 
1159  ereport(LOG,
1160  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
1161  errmsg("out of file descriptors: %m; release and retry")));
1162  errno = 0;
1163  if (ReleaseLruFile())
1164  goto tryAgain;
1165  errno = save_errno;
1166  }
1167 
1168  return -1; /* failure */
1169 }
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:943
#define PG_O_DIRECT
Definition: fd.h:97
#define close(a)
Definition: win32.h:12
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define O_CLOEXEC
Definition: win32_port.h:359
#define O_DSYNC
Definition: win32_port.h:352

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

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

◆ closeAllVfds()

void closeAllVfds ( void  )

Definition at line 3042 of file fd.c.

3043 {
3044  Index i;
3045 
3046  if (SizeVfdCache > 0)
3047  {
3048  Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
3049  for (i = 1; i < SizeVfdCache; i++)
3050  {
3051  if (!FileIsNotOpen(i))
3052  LruDelete(i);
3053  }
3054  }
3055 }
#define Assert(condition)
Definition: c.h:863
static void LruDelete(File file)
Definition: fd.c:1286
static Size SizeVfdCache
Definition: fd.c:216
#define FileIsNotOpen(file)
Definition: fd.c:188

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

Referenced by standard_ProcessUtility().

◆ ClosePipeStream()

int ClosePipeStream ( FILE *  file)

Definition at line 3013 of file fd.c.

3014 {
3015  int i;
3016 
3017  DO_DB(elog(LOG, "ClosePipeStream: Allocated %d", numAllocatedDescs));
3018 
3019  /* Remove file from list of allocated files, if it's present */
3020  for (i = numAllocatedDescs; --i >= 0;)
3021  {
3022  AllocateDesc *desc = &allocatedDescs[i];
3023 
3024  if (desc->kind == AllocateDescPipe && desc->desc.file == file)
3025  return FreeDesc(desc);
3026  }
3027 
3028  /* Only get here if someone passes us a file not in allocatedDescs */
3029  elog(WARNING, "file passed to ClosePipeStream was not obtained from OpenPipeStream");
3030 
3031  return pclose(file);
3032 }
#define WARNING
Definition: elog.h:36
@ AllocateDescPipe
Definition: fd.c:249

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 2831 of file fd.c.

2832 {
2833  int i;
2834 
2835  DO_DB(elog(LOG, "CloseTransientFile: Allocated %d", numAllocatedDescs));
2836 
2837  /* Remove fd from list of allocated files, if it's present */
2838  for (i = numAllocatedDescs; --i >= 0;)
2839  {
2840  AllocateDesc *desc = &allocatedDescs[i];
2841 
2842  if (desc->kind == AllocateDescRawFD && desc->desc.fd == fd)
2843  return FreeDesc(desc);
2844  }
2845 
2846  /* Only get here if someone passes us a file not in allocatedDescs */
2847  elog(WARNING, "fd passed to CloseTransientFile was not obtained from OpenTransientFile");
2848 
2849  return close(fd);
2850 }
@ AllocateDescRawFD
Definition: fd.c:251
int fd
Definition: fd.c:262

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

Referenced by ApplyLogicalMappingFile(), be_lo_export(), CheckPointLogicalRewriteHeap(), CheckPointReplicationOrigin(), 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 781 of file fd.c.

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

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 871 of file fd.c.

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

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 1977 of file fd.c.

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

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, 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,
off_t  offset,
off_t  amount,
uint32  wait_event_info 
)

Definition at line 2391 of file fd.c.

2392 {
2393 #ifdef HAVE_POSIX_FALLOCATE
2394  int returnCode;
2395 
2396  Assert(FileIsValid(file));
2397 
2398  DO_DB(elog(LOG, "FileFallocate: %d (%s) " INT64_FORMAT " " INT64_FORMAT,
2399  file, VfdCache[file].fileName,
2400  (int64) offset, (int64) amount));
2401 
2402  returnCode = FileAccess(file);
2403  if (returnCode < 0)
2404  return -1;
2405 
2406 retry:
2407  pgstat_report_wait_start(wait_event_info);
2408  returnCode = posix_fallocate(VfdCache[file].fd, offset, amount);
2410 
2411  if (returnCode == 0)
2412  return 0;
2413  else if (returnCode == EINTR)
2414  goto retry;
2415 
2416  /* for compatibility with %m printing etc */
2417  errno = returnCode;
2418 
2419  /*
2420  * Return in cases of a "real" failure, if fallocate is not supported,
2421  * fall through to the FileZero() backed implementation.
2422  */
2423  if (returnCode != EINVAL && returnCode != EOPNOTSUPP)
2424  return -1;
2425 #endif
2426 
2427  return FileZero(file, offset, amount, wait_event_info);
2428 }
#define INT64_FORMAT
Definition: c.h:551
static int FileAccess(File file)
Definition: fd.c:1491
int FileZero(File file, off_t offset, off_t amount, uint32 wait_event_info)
Definition: fd.c:2346
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:85
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101
#define EINTR
Definition: win32_port.h:374
#define EOPNOTSUPP
Definition: win32_port.h:398

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 2499 of file fd.c.

2500 {
2501  Assert(FileIsValid(file));
2502  return VfdCache[file].fd;
2503 }

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

◆ FileGetRawFlags()

int FileGetRawFlags ( File  file)

Definition at line 2509 of file fd.c.

2510 {
2511  Assert(FileIsValid(file));
2512  return VfdCache[file].fileFlags;
2513 }
int fileFlags
Definition: fd.c:206

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

◆ FileGetRawMode()

mode_t FileGetRawMode ( File  file)

Definition at line 2519 of file fd.c.

2520 {
2521  Assert(FileIsValid(file));
2522  return VfdCache[file].fileMode;
2523 }
mode_t fileMode
Definition: fd.c:207

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

◆ FilePathName()

◆ FilePrefetch()

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

Definition at line 2076 of file fd.c.

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

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,
off_t  offset,
uint32  wait_event_info 
)
inlinestatic

Definition at line 196 of file fd.h.

198 {
199  struct iovec iov = {
200  .iov_base = buffer,
201  .iov_len = amount
202  };
203 
204  return FileReadV(file, &iov, 1, offset, wait_event_info);
205 }
ssize_t FileReadV(File file, const struct iovec *iov, int iovcnt, off_t offset, uint32 wait_event_info)
Definition: fd.c:2158

References FileReadV().

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

◆ FileReadV()

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

Definition at line 2158 of file fd.c.

2160 {
2161  ssize_t returnCode;
2162  Vfd *vfdP;
2163 
2164  Assert(FileIsValid(file));
2165 
2166  DO_DB(elog(LOG, "FileReadV: %d (%s) " INT64_FORMAT " %d",
2167  file, VfdCache[file].fileName,
2168  (int64) offset,
2169  iovcnt));
2170 
2171  returnCode = FileAccess(file);
2172  if (returnCode < 0)
2173  return returnCode;
2174 
2175  vfdP = &VfdCache[file];
2176 
2177 retry:
2178  pgstat_report_wait_start(wait_event_info);
2179  returnCode = pg_preadv(vfdP->fd, iov, iovcnt, offset);
2181 
2182  if (returnCode < 0)
2183  {
2184  /*
2185  * Windows may run out of kernel buffers and return "Insufficient
2186  * system resources" error. Wait a bit and retry to solve it.
2187  *
2188  * It is rumored that EINTR is also possible on some Unix filesystems,
2189  * in which case immediate retry is indicated.
2190  */
2191 #ifdef WIN32
2192  DWORD error = GetLastError();
2193 
2194  switch (error)
2195  {
2196  case ERROR_NO_SYSTEM_RESOURCES:
2197  pg_usleep(1000L);
2198  errno = EINTR;
2199  break;
2200  default:
2201  _dosmaperr(error);
2202  break;
2203  }
2204 #endif
2205  /* OK to retry if interrupted */
2206  if (errno == EINTR)
2207  goto retry;
2208  }
2209 
2210  return returnCode;
2211 }
static ssize_t pg_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: pg_iovec.h:44
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()

off_t FileSize ( File  file)

Definition at line 2431 of file fd.c.

2432 {
2433  Assert(FileIsValid(file));
2434 
2435  DO_DB(elog(LOG, "FileSize %d (%s)",
2436  file, VfdCache[file].fileName));
2437 
2438  if (FileIsNotOpen(file))
2439  {
2440  if (FileAccess(file) < 0)
2441  return (off_t) -1;
2442  }
2443 
2444  return lseek(VfdCache[file].fd, 0, SEEK_END);
2445 }

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

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

◆ FileSync()

int FileSync ( File  file,
uint32  wait_event_info 
)

Definition at line 2319 of file fd.c.

2320 {
2321  int returnCode;
2322 
2323  Assert(FileIsValid(file));
2324 
2325  DO_DB(elog(LOG, "FileSync: %d (%s)",
2326  file, VfdCache[file].fileName));
2327 
2328  returnCode = FileAccess(file);
2329  if (returnCode < 0)
2330  return returnCode;
2331 
2332  pgstat_report_wait_start(wait_event_info);
2333  returnCode = pg_fsync(VfdCache[file].fd);
2335 
2336  return returnCode;
2337 }

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,
off_t  offset,
uint32  wait_event_info 
)

Definition at line 2448 of file fd.c.

2449 {
2450  int returnCode;
2451 
2452  Assert(FileIsValid(file));
2453 
2454  DO_DB(elog(LOG, "FileTruncate %d (%s)",
2455  file, VfdCache[file].fileName));
2456 
2457  returnCode = FileAccess(file);
2458  if (returnCode < 0)
2459  return returnCode;
2460 
2461  pgstat_report_wait_start(wait_event_info);
2462  returnCode = pg_ftruncate(VfdCache[file].fd, offset);
2464 
2465  if (returnCode == 0 && VfdCache[file].fileSize > offset)
2466  {
2467  /* adjust our state for truncation of a temp file */
2468  Assert(VfdCache[file].fdstate & FD_TEMP_FILE_LIMIT);
2469  temporary_files_size -= VfdCache[file].fileSize - offset;
2470  VfdCache[file].fileSize = offset;
2471  }
2472 
2473  return returnCode;
2474 }
static int pg_ftruncate(int fd, off_t length)
Definition: fd.c:702

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,
off_t  offset,
uint32  wait_event_info 
)
inlinestatic

Definition at line 208 of file fd.h.

210 {
211  struct iovec iov = {
212  .iov_base = unconstify(void *, buffer),
213  .iov_len = amount
214  };
215 
216  return FileWriteV(file, &iov, 1, offset, wait_event_info);
217 }
#define unconstify(underlying_type, expr)
Definition: c.h:1250
ssize_t FileWriteV(File file, const struct iovec *iov, int iovcnt, off_t offset, uint32 wait_event_info)
Definition: fd.c:2214

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,
off_t  offset,
off_t  nbytes,
uint32  wait_event_info 
)

Definition at line 2132 of file fd.c.

2133 {
2134  int returnCode;
2135 
2136  Assert(FileIsValid(file));
2137 
2138  DO_DB(elog(LOG, "FileWriteback: %d (%s) " INT64_FORMAT " " INT64_FORMAT,
2139  file, VfdCache[file].fileName,
2140  (int64) offset, (int64) nbytes));
2141 
2142  if (nbytes <= 0)
2143  return;
2144 
2145  if (VfdCache[file].fileFlags & PG_O_DIRECT)
2146  return;
2147 
2148  returnCode = FileAccess(file);
2149  if (returnCode < 0)
2150  return;
2151 
2152  pgstat_report_wait_start(wait_event_info);
2153  pg_flush_data(VfdCache[file].fd, offset, nbytes);
2155 }
void pg_flush_data(int fd, off_t offset, off_t nbytes)
Definition: fd.c:524

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,
off_t  offset,
uint32  wait_event_info 
)

Definition at line 2214 of file fd.c.

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

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(), 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,
off_t  offset,
off_t  amount,
uint32  wait_event_info 
)

Definition at line 2346 of file fd.c.

2347 {
2348  int returnCode;
2349  ssize_t written;
2350 
2351  Assert(FileIsValid(file));
2352 
2353  DO_DB(elog(LOG, "FileZero: %d (%s) " INT64_FORMAT " " INT64_FORMAT,
2354  file, VfdCache[file].fileName,
2355  (int64) offset, (int64) amount));
2356 
2357  returnCode = FileAccess(file);
2358  if (returnCode < 0)
2359  return returnCode;
2360 
2361  pgstat_report_wait_start(wait_event_info);
2362  written = pg_pwrite_zeros(VfdCache[file].fd, amount, offset);
2364 
2365  if (written < 0)
2366  return -1;
2367  else if (written != amount)
2368  {
2369  /* if errno is unset, assume problem is no disk space */
2370  if (errno == 0)
2371  errno = ENOSPC;
2372  return -1;
2373  }
2374 
2375  return 0;
2376 }
ssize_t pg_pwrite_zeros(int fd, size_t size, off_t offset)
Definition: file_utils.c:688

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 2983 of file fd.c.

2984 {
2985  int i;
2986 
2987  /* Nothing to do if AllocateDir failed */
2988  if (dir == NULL)
2989  return 0;
2990 
2991  DO_DB(elog(LOG, "FreeDir: Allocated %d", numAllocatedDescs));
2992 
2993  /* Remove dir from list of allocated dirs, if it's present */
2994  for (i = numAllocatedDescs; --i >= 0;)
2995  {
2996  AllocateDesc *desc = &allocatedDescs[i];
2997 
2998  if (desc->kind == AllocateDescDir && desc->desc.dir == dir)
2999  return FreeDesc(desc);
3000  }
3001 
3002  /* Only get here if someone passes us a dir not in allocatedDescs */
3003  elog(WARNING, "dir passed to FreeDir was not obtained from AllocateDir");
3004 
3005  return closedir(dir);
3006 }
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 2803 of file fd.c.

2804 {
2805  int i;
2806 
2807  DO_DB(elog(LOG, "FreeFile: Allocated %d", numAllocatedDescs));
2808 
2809  /* Remove file from list of allocated files, if it's present */
2810  for (i = numAllocatedDescs; --i >= 0;)
2811  {
2812  AllocateDesc *desc = &allocatedDescs[i];
2813 
2814  if (desc->kind == AllocateDescFile && desc->desc.file == file)
2815  return FreeDesc(desc);
2816  }
2817 
2818  /* Only get here if someone passes us a file not in allocatedDescs */
2819  elog(WARNING, "file passed to FreeFile was not obtained from AllocateFile");
2820 
2821  return fclose(file);
2822 }

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(), 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 3820 of file fd.c.

3821 {
3822  int fd;
3823  int flags;
3824  int returncode;
3825 
3826  /*
3827  * Some OSs require directories to be opened read-only whereas other
3828  * systems don't allow us to fsync files opened read-only; so we need both
3829  * cases here. Using O_RDWR will cause us to fail to fsync files that are
3830  * not writable by our userid, but we assume that's OK.
3831  */
3832  flags = PG_BINARY;
3833  if (!isdir)
3834  flags |= O_RDWR;
3835  else
3836  flags |= O_RDONLY;
3837 
3838  fd = OpenTransientFile(fname, flags);
3839 
3840  /*
3841  * Some OSs don't allow us to open directories at all (Windows returns
3842  * EACCES), just ignore the error in that case. If desired also silently
3843  * ignoring errors about unreadable files. Log others.
3844  */
3845  if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES))
3846  return 0;
3847  else if (fd < 0 && ignore_perm && errno == EACCES)
3848  return 0;
3849  else if (fd < 0)
3850  {
3851  ereport(elevel,
3853  errmsg("could not open file \"%s\": %m", fname)));
3854  return -1;
3855  }
3856 
3857  returncode = pg_fsync(fd);
3858 
3859  /*
3860  * Some OSes don't allow us to fsync directories at all, so we can ignore
3861  * those errors. Anything else needs to be logged.
3862  */
3863  if (returncode != 0 && !(isdir && (errno == EBADF || errno == EINVAL)))
3864  {
3865  int save_errno;
3866 
3867  /* close file upon error, might not be in transaction context */
3868  save_errno = errno;
3869  (void) CloseTransientFile(fd);
3870  errno = save_errno;
3871 
3872  ereport(elevel,
3874  errmsg("could not fsync file \"%s\": %m", fname)));
3875  return -1;
3876  }
3877 
3878  if (CloseTransientFile(fd) != 0)
3879  {
3880  ereport(elevel,
3882  errmsg("could not close file \"%s\": %m", fname)));
3883  return -1;
3884  }
3885 
3886  return 0;
3887 }

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 3133 of file fd.c.

3134 {
3135  if (numTempTableSpaces > 0)
3136  {
3137  /* Advance nextTempTableSpace counter with wraparound */
3139  nextTempTableSpace = 0;
3141  }
3142  return InvalidOid;
3143 }
static int nextTempTableSpace
Definition: fd.c:289
#define InvalidOid
Definition: postgres_ext.h:36

References InvalidOid, nextTempTableSpace, numTempTableSpaces, and tempTableSpaces.

Referenced by GetDefaultTablespace(), and OpenTemporaryFile().

◆ GetTempTablespaces()

int GetTempTablespaces ( Oid tableSpaces,
int  numSpaces 
)

Definition at line 3115 of file fd.c.

3116 {
3117  int i;
3118 
3120  for (i = 0; i < numTempTableSpaces && i < numSpaces; ++i)
3121  tableSpaces[i] = tempTableSpaces[i];
3122 
3123  return i;
3124 }
bool TempTablespacesAreSet(void)
Definition: fd.c:3100

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

Referenced by FileSetInit().

◆ InitFileAccess()

void InitFileAccess ( void  )

Definition at line 902 of file fd.c.

903 {
904  Assert(SizeVfdCache == 0); /* call me only once */
905 
906  /* initialize cache header entry */
907  VfdCache = (Vfd *) malloc(sizeof(Vfd));
908  if (VfdCache == NULL)
909  ereport(FATAL,
910  (errcode(ERRCODE_OUT_OF_MEMORY),
911  errmsg("out of memory")));
912 
913  MemSet((char *) &(VfdCache[0]), 0, sizeof(Vfd));
915 
916  SizeVfdCache = 1;
917 }
#define MemSet(start, val, len)
Definition: c.h:1025
#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 932 of file fd.c.

933 {
934  Assert(SizeVfdCache != 0); /* InitFileAccess() needs to have run */
935  Assert(!temporary_files_allowed); /* call me only once */
936 
937  /*
938  * Register before-shmem-exit hook to ensure temp files are dropped while
939  * we can still report stats.
940  */
942 
943 #ifdef USE_ASSERT_CHECKING
944  temporary_files_allowed = true;
945 #endif
946 }
static void BeforeShmemExit_Files(int code, Datum arg)
Definition: fd.c:3201
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 3472 of file fd.c.

3473 {
3474  int pos;
3475  int savepos;
3476 
3477  /* Must start with "t". */
3478  if (name[0] != 't')
3479  return false;
3480 
3481  /* Followed by a non-empty string of digits and then an underscore. */
3482  for (pos = 1; isdigit((unsigned char) name[pos]); ++pos)
3483  ;
3484  if (pos == 1 || name[pos] != '_')
3485  return false;
3486 
3487  /* Followed by another nonempty string of digits. */
3488  for (savepos = ++pos; isdigit((unsigned char) name[pos]); ++pos)
3489  ;
3490  if (savepos == pos)
3491  return false;
3492 
3493  /* We might have _forkname or .segment or both. */
3494  if (name[pos] == '_')
3495  {
3496  int forkchar = forkname_chars(&name[pos + 1], NULL);
3497 
3498  if (forkchar <= 0)
3499  return false;
3500  pos += forkchar + 1;
3501  }
3502  if (name[pos] == '.')
3503  {
3504  int segchar;
3505 
3506  for (segchar = 1; isdigit((unsigned char) name[pos + segchar]); ++segchar)
3507  ;
3508  if (segchar <= 1)
3509  return false;
3510  pos += segchar;
3511  }
3512 
3513  /* Now we should be at the end. */
3514  if (name[pos] != '\0')
3515  return false;
3516  return true;
3517 }
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 2708 of file fd.c.

2709 {
2710  FILE *file;
2711  int save_errno;
2712 
2713  DO_DB(elog(LOG, "OpenPipeStream: Allocated %d (%s)",
2714  numAllocatedDescs, command));
2715 
2716  /* Can we allocate another non-virtual FD? */
2717  if (!reserveAllocatedDesc())
2718  ereport(ERROR,
2719  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2720  errmsg("exceeded maxAllocatedDescs (%d) while trying to execute command \"%s\"",
2721  maxAllocatedDescs, command)));
2722 
2723  /* Close excess kernel FDs. */
2724  ReleaseLruFiles();
2725 
2726 TryAgain:
2727  fflush(NULL);
2729  errno = 0;
2730  file = popen(command, mode);
2731  save_errno = errno;
2733  errno = save_errno;
2734  if (file != NULL)
2735  {
2737 
2738  desc->kind = AllocateDescPipe;
2739  desc->desc.file = file;
2742  return desc->desc.file;
2743  }
2744 
2745  if (errno == EMFILE || errno == ENFILE)
2746  {
2747  ereport(LOG,
2748  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2749  errmsg("out of file descriptors: %m; release and retry")));
2750  if (ReleaseLruFile())
2751  goto TryAgain;
2752  errno = save_errno;
2753  }
2754 
2755  return NULL;
2756 }
static void const char fflush(stdout)
pqsigfunc pqsignal(int signo, pqsigfunc func)
#define SIG_DFL
Definition: win32_port.h:163
#define SIGPIPE
Definition: win32_port.h:173
#define SIG_IGN
Definition: win32_port.h:165

References allocatedDescs, AllocateDescPipe, AllocateDesc::create_subid, AllocateDesc::desc, DO_DB, elog, ereport, errcode(), errmsg(), ERROR, fflush(), AllocateDesc::file, GetCurrentSubTransactionId(), AllocateDesc::kind, LOG, maxAllocatedDescs, mode, numAllocatedDescs, pqsignal(), ReleaseLruFile(), ReleaseLruFiles(), reserveAllocatedDesc(), SIG_DFL, SIG_IGN, 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 1723 of file fd.c.

1724 {
1725  File file = 0;
1726 
1727  Assert(temporary_files_allowed); /* check temp file access is up */
1728 
1729  /*
1730  * Make sure the current resource owner has space for this File before we
1731  * open it, if we'll be registering it below.
1732  */
1733  if (!interXact)
1735 
1736  /*
1737  * If some temp tablespace(s) have been given to us, try to use the next
1738  * one. If a given tablespace can't be found, we silently fall back to
1739  * the database's default tablespace.
1740  *
1741  * BUT: if the temp file is slated to outlive the current transaction,
1742  * force it into the database's default tablespace, so that it will not
1743  * pose a threat to possible tablespace drop attempts.
1744  */
1745  if (numTempTableSpaces > 0 && !interXact)
1746  {
1747  Oid tblspcOid = GetNextTempTableSpace();
1748 
1749  if (OidIsValid(tblspcOid))
1750  file = OpenTemporaryFileInTablespace(tblspcOid, false);
1751  }
1752 
1753  /*
1754  * If not, or if tablespace is bad, create in database's default
1755  * tablespace. MyDatabaseTableSpace should normally be set before we get
1756  * here, but just in case it isn't, fall back to pg_default tablespace.
1757  */
1758  if (file <= 0)
1761  DEFAULTTABLESPACE_OID,
1762  true);
1763 
1764  /* Mark it for deletion at close and temporary file size limit */
1766 
1767  /* Register it with the current resource owner */
1768  if (!interXact)
1769  RegisterTemporaryFile(file);
1770 
1771  return file;
1772 }
#define OidIsValid(objectId)
Definition: c.h:780
Oid GetNextTempTableSpace(void)
Definition: fd.c:3133
static File OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError)
Definition: fd.c:1803
static void RegisterTemporaryFile(File file)
Definition: fd.c:1546
int File
Definition: fd.h:51
Oid MyDatabaseTableSpace
Definition: globals.c:95
unsigned int Oid
Definition: postgres_ext.h:31
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition: resowner.c:442

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 2664 of file fd.c.

2665 {
2666  int fd;
2667 
2668  DO_DB(elog(LOG, "OpenTransientFile: Allocated %d (%s)",
2669  numAllocatedDescs, fileName));
2670 
2671  /* Can we allocate another non-virtual FD? */
2672  if (!reserveAllocatedDesc())
2673  ereport(ERROR,
2674  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2675  errmsg("exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2676  maxAllocatedDescs, fileName)));
2677 
2678  /* Close excess kernel FDs. */
2679  ReleaseLruFiles();
2680 
2681  fd = BasicOpenFilePerm(fileName, fileFlags, fileMode);
2682 
2683  if (fd >= 0)
2684  {
2686 
2687  desc->kind = AllocateDescRawFD;
2688  desc->desc.fd = fd;
2691 
2692  return fd;
2693  }
2694 
2695  return -1; /* failure */
2696 }

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 1659 of file fd.c.

1660 {
1661  if (MakePGDirectory(directory) < 0)
1662  {
1663  if (errno == EEXIST)
1664  return;
1665 
1666  /*
1667  * Failed. Try to create basedir first in case it's missing. Tolerate
1668  * EEXIST to close a race against another process following the same
1669  * algorithm.
1670  */
1671  if (MakePGDirectory(basedir) < 0 && errno != EEXIST)
1672  ereport(ERROR,
1674  errmsg("cannot create temporary directory \"%s\": %m",
1675  basedir)));
1676 
1677  /* Try again. */
1678  if (MakePGDirectory(directory) < 0 && errno != EEXIST)
1679  ereport(ERROR,
1681  errmsg("cannot create temporary subdirectory \"%s\": %m",
1682  directory)));
1683  }
1684 }
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3936
static char * basedir
static const char * directory
Definition: zic.c:634

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 1860 of file fd.c.

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

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 1690 of file fd.c.

1691 {
1692  struct stat statbuf;
1693 
1694  /* Silently ignore missing directory. */
1695  if (stat(dirname, &statbuf) != 0 && errno == ENOENT)
1696  return;
1697 
1698  /*
1699  * Currently, walkdir doesn't offer a way for our passed in function to
1700  * maintain state. Perhaps it should, so that we could tell the caller
1701  * whether this operation succeeded or failed. Since this operation is
1702  * used in a cleanup path, we wouldn't actually behave differently: we'll
1703  * just log failures.
1704  */
1705  walkdir(dirname, unlink_if_exists_fname, false, LOG);
1706 }
static void unlink_if_exists_fname(const char *fname, bool isdir, int elevel)
Definition: fd.c:3795
static void walkdir(const char *path, void(*action)(const char *fname, bool isdir, int elevel), bool process_symlinks, int elevel)
Definition: fd.c:3681

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 1931 of file fd.c.

1932 {
1933  struct stat filestats;
1934  int stat_errno;
1935 
1936  /* Get the final size for pgstat reporting. */
1937  if (stat(path, &filestats) != 0)
1938  stat_errno = errno;
1939  else
1940  stat_errno = 0;
1941 
1942  /*
1943  * Unlike FileClose's automatic file deletion code, we tolerate
1944  * non-existence to support BufFileDeleteFileSet which doesn't know how
1945  * many segments it has to delete until it runs out.
1946  */
1947  if (stat_errno == ENOENT)
1948  return false;
1949 
1950  if (unlink(path) < 0)
1951  {
1952  if (errno != ENOENT)
1953  ereport(error_on_failure ? ERROR : LOG,
1955  errmsg("could not unlink temporary file \"%s\": %m",
1956  path)));
1957  return false;
1958  }
1959 
1960  if (stat_errno == 0)
1961  ReportTemporaryFileUsage(path, filestats.st_size);
1962  else
1963  {
1964  errno = stat_errno;
1965  ereport(LOG,
1967  errmsg("could not stat file \"%s\": %m", path)));
1968  }
1969 
1970  return true;
1971 }

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 1587 of file fd.c.

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

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 1900 of file fd.c.

1901 {
1902  File file;
1903 
1904  Assert(temporary_files_allowed); /* check temp file access is up */
1905 
1907 
1908  file = PathNameOpenFile(path, mode | PG_BINARY);
1909 
1910  /* If no such file, then we don't raise an error. */
1911  if (file <= 0 && errno != ENOENT)
1912  ereport(ERROR,
1914  errmsg("could not open temporary file \"%s\": %m",
1915  path)));
1916 
1917  if (file > 0)
1918  {
1919  /* Register it for automatic close. */
1920  RegisterTemporaryFile(file);
1921  }
1922 
1923  return file;
1924 }

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 479 of file fd.c.

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

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

Referenced by issue_xlog_fsync().

◆ pg_file_exists()

bool pg_file_exists ( const char *  name)

Definition at line 502 of file fd.c.

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

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_dynamic_libpath(), and provider_init().

◆ pg_flush_data()

void pg_flush_data ( int  fd,
off_t  offset,
off_t  nbytes 
)

Definition at line 524 of file fd.c.

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

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

Referenced by copy_file(), and FileWriteback().

◆ pg_fsync()

int pg_fsync ( int  fd)

Definition at line 385 of file fd.c.

386 {
387 #if !defined(WIN32) && defined(USE_ASSERT_CHECKING)
388  struct stat st;
389 
390  /*
391  * Some operating system implementations of fsync() have requirements
392  * about the file access modes that were used when their file descriptor
393  * argument was opened, and these requirements differ depending on whether
394  * the file descriptor is for a directory.
395  *
396  * For any file descriptor that may eventually be handed to fsync(), we
397  * should have opened it with access modes that are compatible with
398  * fsync() on all supported systems, otherwise the code may not be
399  * portable, even if it runs ok on the current system.
400  *
401  * We assert here that a descriptor for a file was opened with write
402  * permissions (either O_RDWR or O_WRONLY) and for a directory without
403  * write permissions (O_RDONLY).
404  *
405  * Ignore any fstat errors and let the follow-up fsync() do its work.
406  * Doing this sanity check here counts for the case where fsync() is
407  * disabled.
408  */
409  if (fstat(fd, &st) == 0)
410  {
411  int desc_flags = fcntl(fd, F_GETFL);
412 
413  /*
414  * O_RDONLY is historically 0, so just make sure that for directories
415  * no write flags are used.
416  */
417  if (S_ISDIR(st.st_mode))
418  Assert((desc_flags & (O_RDWR | O_WRONLY)) == 0);
419  else
420  Assert((desc_flags & (O_RDWR | O_WRONLY)) != 0);
421  }
422  errno = 0;
423 #endif
424 
425  /* #if is to skip the wal_sync_method test if there's no need for it */
426 #if defined(HAVE_FSYNC_WRITETHROUGH)
428  return pg_fsync_writethrough(fd);
429  else
430 #endif
432 }
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:440
int pg_fsync_writethrough(int fd)
Definition: fd.c:460
#define fstat
Definition: win32_port.h:283
int wal_sync_method
Definition: xlog.c:130
@ 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 440 of file fd.c.

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

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 460 of file fd.c.

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

References enableFsync, and fd().

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

◆ pg_truncate()

int pg_truncate ( const char *  path,
off_t  length 
)

Definition at line 719 of file fd.c.

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

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 2946 of file fd.c.

2947 {
2948  struct dirent *dent;
2949 
2950  /* Give a generic message for AllocateDir failure, if caller didn't */
2951  if (dir == NULL)
2952  {
2953  ereport(elevel,
2955  errmsg("could not open directory \"%s\": %m",
2956  dirname)));
2957  return NULL;
2958  }
2959 
2960  errno = 0;
2961  if ((dent = readdir(dir)) != NULL)
2962  return dent;
2963 
2964  if (errno)
2965  ereport(elevel,
2967  errmsg("could not read directory \"%s\": %m",
2968  dirname)));
2969  return NULL;
2970 }
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 3296 of file fd.c.

3297 {
3298  char temp_path[MAXPGPATH + sizeof(PG_TBLSPC_DIR) + sizeof(TABLESPACE_VERSION_DIRECTORY) + sizeof(PG_TEMP_FILES_DIR)];
3299  DIR *spc_dir;
3300  struct dirent *spc_de;
3301 
3302  /*
3303  * First process temp files in pg_default ($PGDATA/base)
3304  */
3305  snprintf(temp_path, sizeof(temp_path), "base/%s", PG_TEMP_FILES_DIR);
3306  RemovePgTempFilesInDir(temp_path, true, false);
3307  RemovePgTempRelationFiles("base");
3308 
3309  /*
3310  * Cycle through temp directories for all non-default tablespaces.
3311  */
3312  spc_dir = AllocateDir(PG_TBLSPC_DIR);
3313 
3314  while ((spc_de = ReadDirExtended(spc_dir, PG_TBLSPC_DIR, LOG)) != NULL)
3315  {
3316  if (strcmp(spc_de->d_name, ".") == 0 ||
3317  strcmp(spc_de->d_name, "..") == 0)
3318  continue;
3319 
3320  snprintf(temp_path, sizeof(temp_path), "%s/%s/%s/%s",
3323  RemovePgTempFilesInDir(temp_path, true, false);
3324 
3325  snprintf(temp_path, sizeof(temp_path), "%s/%s/%s",
3327  RemovePgTempRelationFiles(temp_path);
3328  }
3329 
3330  FreeDir(spc_dir);
3331 
3332  /*
3333  * In EXEC_BACKEND case there is a pgsql_tmp directory at the top level of
3334  * DataDir as well. However, that is *not* cleaned here because doing so
3335  * would create a race condition. It's done separately, earlier in
3336  * postmaster startup.
3337  */
3338 }
int FreeDir(DIR *dir)
Definition: fd.c:2983
static void RemovePgTempRelationFiles(const char *tsdirname)
Definition: fd.c:3416
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
Definition: fd.c:3356
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2865
#define PG_TEMP_FILES_DIR
Definition: file_utils.h:62
#define MAXPGPATH
#define snprintf
Definition: port.h:238
#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 3356 of file fd.c.

3357 {
3358  DIR *temp_dir;
3359  struct dirent *temp_de;
3360  char rm_path[MAXPGPATH * 2];
3361 
3362  temp_dir = AllocateDir(tmpdirname);
3363 
3364  if (temp_dir == NULL && errno == ENOENT && missing_ok)
3365  return;
3366 
3367  while ((temp_de = ReadDirExtended(temp_dir, tmpdirname, LOG)) != NULL)
3368  {
3369  if (strcmp(temp_de->d_name, ".") == 0 ||
3370  strcmp(temp_de->d_name, "..") == 0)
3371  continue;
3372 
3373  snprintf(rm_path, sizeof(rm_path), "%s/%s",
3374  tmpdirname, temp_de->d_name);
3375 
3376  if (unlink_all ||
3377  strncmp(temp_de->d_name,
3379  strlen(PG_TEMP_FILE_PREFIX)) == 0)
3380  {
3381  PGFileType type = get_dirent_type(rm_path, temp_de, false, LOG);
3382 
3383  if (type == PGFILETYPE_ERROR)
3384  continue;
3385  else if (type == PGFILETYPE_DIR)
3386  {
3387  /* recursively remove contents, then directory itself */
3388  RemovePgTempFilesInDir(rm_path, false, true);
3389 
3390  if (rmdir(rm_path) < 0)
3391  ereport(LOG,
3393  errmsg("could not remove directory \"%s\": %m",
3394  rm_path)));
3395  }
3396  else
3397  {
3398  if (unlink(rm_path) < 0)
3399  ereport(LOG,
3401  errmsg("could not remove file \"%s\": %m",
3402  rm_path)));
3403  }
3404  }
3405  else
3406  ereport(LOG,
3407  (errmsg("unexpected file found in temporary-files directory: \"%s\"",
3408  rm_path)));
3409  }
3410 
3411  FreeDir(temp_dir);
3412 }
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition: file_utils.c:526
#define PG_TEMP_FILE_PREFIX
Definition: file_utils.h:63
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(), snprintf, and type.

Referenced by PostmasterMain(), and RemovePgTempFiles().

◆ ReserveExternalFD()

void ReserveExternalFD ( void  )

Definition at line 1220 of file fd.c.

1221 {
1222  /*
1223  * Release VFDs if needed to stay safe. Because we do this before
1224  * incrementing numExternalFDs, the final state will be as desired, i.e.,
1225  * nfile + numAllocatedDescs + numExternalFDs <= max_safe_fds.
1226  */
1227  ReleaseLruFiles();
1228 
1229  numExternalFDs++;
1230 }

References numExternalFDs, and ReleaseLruFiles().

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

◆ set_max_safe_fds()

void set_max_safe_fds ( void  )

Definition at line 1043 of file fd.c.

1044 {
1045  int usable_fds;
1046  int already_open;
1047 
1048  /*----------
1049  * We want to set max_safe_fds to
1050  * MIN(usable_fds, max_files_per_process - already_open)
1051  * less the slop factor for files that are opened without consulting
1052  * fd.c. This ensures that we won't exceed either max_files_per_process
1053  * or the experimentally-determined EMFILE limit.
1054  *----------
1055  */
1057  &usable_fds, &already_open);
1058 
1059  max_safe_fds = Min(usable_fds, max_files_per_process - already_open);
1060 
1061  /*
1062  * Take off the FDs reserved for system() etc.
1063  */
1065 
1066  /*
1067  * Make sure we still have enough to get by.
1068  */
1069  if (max_safe_fds < FD_MINFREE)
1070  ereport(FATAL,
1071  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
1072  errmsg("insufficient file descriptors available to start server process"),
1073  errdetail("System allows %d, server needs at least %d.",
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:1009
int errdetail(const char *fmt,...)
Definition: elog.c:1203
#define DEBUG2
Definition: elog.h:29
int max_files_per_process
Definition: fd.c:145
#define FD_MINFREE
Definition: fd.c:137
static void count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
Definition: fd.c:963
#define NUM_RESERVED_FDS
Definition: fd.c:128

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 PostmasterMain().

◆ SetTempTablespaces()

void SetTempTablespaces ( Oid tableSpaces,
int  numSpaces 
)

Definition at line 3071 of file fd.c.

3072 {
3073  Assert(numSpaces >= 0);
3074  tempTableSpaces = tableSpaces;
3075  numTempTableSpaces = numSpaces;
3076 
3077  /*
3078  * Select a random starting point in the list. This is to minimize
3079  * conflicts between backends that are most likely sharing the same list
3080  * of temp tablespaces. Note that if we create multiple temp files in the
3081  * same transaction, we'll advance circularly through the list --- this
3082  * ensures that large temporary sort files are nicely spread across all
3083  * available tablespaces.
3084  */
3085  if (numSpaces > 1)
3087  0, numSpaces - 1);
3088  else
3089  nextTempTableSpace = 0;
3090 }
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 3567 of file fd.c.

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

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 1778 of file fd.c.

1779 {
1780  /*
1781  * Identify the tempfile directory for this tablespace.
1782  *
1783  * If someone tries to specify pg_global, use pg_default instead.
1784  */
1785  if (tablespace == InvalidOid ||
1786  tablespace == DEFAULTTABLESPACE_OID ||
1787  tablespace == GLOBALTABLESPACE_OID)
1788  snprintf(path, MAXPGPATH, "base/%s", PG_TEMP_FILES_DIR);
1789  else
1790  {
1791  /* All other tablespaces are accessed via symlinks */
1792  snprintf(path, MAXPGPATH, "%s/%u/%s/%s",
1795  }
1796 }
static char * tablespace
Definition: pgbench.c:216

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 3100 of file fd.c.

3101 {
3102  return (numTempTableSpaces >= 0);
3103 }

References numTempTableSpaces.

Referenced by GetTempTablespaces(), and PrepareTempTablespaces().

Variable Documentation

◆ data_sync_retry

PGDLLIMPORT bool data_sync_retry
extern

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

Referenced by set_max_safe_fds().

◆ max_safe_fds

PGDLLIMPORT int max_safe_fds
extern

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

Referenced by SyncDataDirectory().