PostgreSQL Source Code  git master
fd.h File Reference
#include <dirent.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 FILE_POSSIBLY_DELETED(err)   ((err) == ENOENT)
 
#define PG_TEMP_FILES_DIR   "pgsql_tmp"
 
#define PG_TEMP_FILE_PREFIX   "pgsql_tmp"
 

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, int amount, uint32 wait_event_info)
 
int FileRead (File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
 
int FileWrite (File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
 
int FileSync (File file, 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 *name, bool error_on_failure)
 
File PathNameOpenTemporaryFile (const char *name)
 
bool PathNameDeleteTemporaryFile (const char *name, bool error_on_failure)
 
void PathNameCreateTemporaryDir (const char *base, const char *name)
 
void PathNameDeleteTemporaryDir (const char *name)
 
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)
 
int MakePGDirectory (const char *directoryName)
 
void InitFileAccess (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)
 
void pg_flush_data (int fd, off_t offset, off_t amount)
 
void fsync_fname (const char *fname, bool isdir)
 
int durable_rename (const char *oldfile, const char *newfile, int loglevel)
 
int durable_unlink (const char *fname, int loglevel)
 
int durable_link_or_rename (const char *oldfile, const char *newfile, int loglevel)
 
void SyncDataDirectory (void)
 
int data_sync_elevel (int elevel)
 

Variables

PGDLLIMPORT int max_files_per_process
 
PGDLLIMPORT bool data_sync_retry
 
int max_safe_fds
 

Macro Definition Documentation

◆ FILE_POSSIBLY_DELETED

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

Definition at line 65 of file fd.h.

Referenced by _mdfd_getseg(), mdopenfork(), and ProcessSyncRequests().

◆ PG_TEMP_FILE_PREFIX

#define PG_TEMP_FILE_PREFIX   "pgsql_tmp"

Definition at line 156 of file fd.h.

◆ PG_TEMP_FILES_DIR

#define PG_TEMP_FILES_DIR   "pgsql_tmp"

Definition at line 155 of file fd.h.

Typedef Documentation

◆ File

typedef int File

Definition at line 45 of file fd.h.

Function Documentation

◆ AllocateDir()

DIR* AllocateDir ( const char *  dirname)

Definition at line 2465 of file fd.c.

References 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(), CleanupBackupHistory(), copydir(), db_dir_size(), DeleteAllExportedSnapshotFiles(), destroy_tablespace_directories(), directory_is_empty(), do_pg_start_backup(), dsm_cleanup_for_mmap(), get_ext_ver_list(), getInstallationPaths(), movedb(), ParseTzFile(), perform_base_backup(), pg_available_extension_versions(), pg_available_extensions(), pg_logdir_ls_internal(), pg_ls_dir(), pg_ls_dir_files(), pg_tablespace_databases(), pg_tzenumerate_next(), pg_tzenumerate_start(), pgarch_readyXlog(), pgstat_reset_remove_files(), RelationCacheInitFileRemove(), RelationCacheInitFileRemoveInDir(), RemoveNonParentXlogFiles(), RemoveOldXlogFiles(), RemovePgTempFiles(), RemovePgTempFilesInDir(), RemovePgTempRelationFiles(), RemovePgTempRelationFilesInDbspace(), RemoveTempXlogFiles(), ReorderBufferCleanupSerializedTXNs(), ResetUnloggedRelations(), ResetUnloggedRelationsInDbspaceDir(), ResetUnloggedRelationsInTablespaceDir(), restoreTwoPhaseData(), scan_directory_ci(), sendDir(), SlruScanDirectory(), StartupReorderBuffer(), StartupReplicationSlots(), UpdateLogicalMappings(), and walkdir().

2466 {
2467  DIR *dir;
2468 
2469  DO_DB(elog(LOG, "AllocateDir: Allocated %d (%s)",
2470  numAllocatedDescs, dirname));
2471 
2472  /* Can we allocate another non-virtual FD? */
2473  if (!reserveAllocatedDesc())
2474  ereport(ERROR,
2475  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2476  errmsg("exceeded maxAllocatedDescs (%d) while trying to open directory \"%s\"",
2477  maxAllocatedDescs, dirname)));
2478 
2479  /* Close excess kernel FDs. */
2480  ReleaseLruFiles();
2481 
2482 TryAgain:
2483  if ((dir = opendir(dirname)) != NULL)
2484  {
2486 
2487  desc->kind = AllocateDescDir;
2488  desc->desc.dir = dir;
2491  return desc->desc.dir;
2492  }
2493 
2494  if (errno == EMFILE || errno == ENFILE)
2495  {
2496  int save_errno = errno;
2497 
2498  ereport(LOG,
2499  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2500  errmsg("out of file descriptors: %m; release and retry")));
2501  errno = 0;
2502  if (ReleaseLruFile())
2503  goto TryAgain;
2504  errno = save_errno;
2505  }
2506 
2507  return NULL;
2508 }
static AllocateDesc * allocatedDescs
Definition: fd.c:244
union AllocateDesc::@26 desc
DIR * dir
Definition: fd.c:237
#define DO_DB(A)
Definition: fd.c:160
int errcode(int sqlerrcode)
Definition: elog.c:608
static bool reserveAllocatedDesc(void)
Definition: fd.c:2131
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:232
static bool ReleaseLruFile(void)
Definition: fd.c:1127
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
DIR * opendir(const char *)
Definition: dirent.c:33
#define ereport(elevel, rest)
Definition: elog.h:141
static void ReleaseLruFiles(void)
Definition: fd.c:1149
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:707
SubTransactionId create_subid
Definition: fd.c:233
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
static int maxAllocatedDescs
Definition: fd.c:243
static int numAllocatedDescs
Definition: fd.c:242

◆ AllocateFile()

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

Definition at line 2204 of file fd.c.

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

Referenced by _ShowOption(), AlterSystemSetConfigFile(), apw_dump_now(), apw_load_buffers(), BackendRun(), BeginCopyFrom(), BeginCopyTo(), checkControlFile(), do_pg_start_backup(), do_pg_stop_backup(), entry_reset(), existsTimeLineHistory(), ExportSnapshot(), fill_hba_view(), gc_qtexts(), ImportSnapshot(), load_dh_file(), load_hba(), load_ident(), load_relcache_init_file(), parse_extension_control_file(), ParseTzFile(), perform_base_backup(), pg_backup_start_time(), pg_current_logfile(), pg_file_write_internal(), pg_promote(), PGSharedMemoryAttach(), pgss_shmem_shutdown(), pgss_shmem_startup(), pgstat_read_db_statsfile(), pgstat_read_db_statsfile_timestamp(), pgstat_read_statsfiles(), pgstat_write_db_statsfile(), pgstat_write_statsfiles(), PostmasterMarkPIDForWorkerNotify(), read_backup_label(), read_binary_file(), read_tablespace_map(), read_whole_file(), readTimeLineHistory(), sendFile(), tokenize_inc_file(), tsearch_readline_begin(), ValidatePgVersion(), write_relcache_init_file(), XLogArchiveForceDone(), and XLogArchiveNotify().

2205 {
2206  FILE *file;
2207 
2208  DO_DB(elog(LOG, "AllocateFile: Allocated %d (%s)",
2210 
2211  /* Can we allocate another non-virtual FD? */
2212  if (!reserveAllocatedDesc())
2213  ereport(ERROR,
2214  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2215  errmsg("exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2216  maxAllocatedDescs, name)));
2217 
2218  /* Close excess kernel FDs. */
2219  ReleaseLruFiles();
2220 
2221 TryAgain:
2222  if ((file = fopen(name, mode)) != NULL)
2223  {
2225 
2226  desc->kind = AllocateDescFile;
2227  desc->desc.file = file;
2230  return desc->desc.file;
2231  }
2232 
2233  if (errno == EMFILE || errno == ENFILE)
2234  {
2235  int save_errno = errno;
2236 
2237  ereport(LOG,
2238  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2239  errmsg("out of file descriptors: %m; release and retry")));
2240  errno = 0;
2241  if (ReleaseLruFile())
2242  goto TryAgain;
2243  errno = save_errno;
2244  }
2245 
2246  return NULL;
2247 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
static AllocateDesc * allocatedDescs
Definition: fd.c:244
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:160
int errcode(int sqlerrcode)
Definition: elog.c:608
static bool reserveAllocatedDesc(void)
Definition: fd.c:2131
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:232
static bool ReleaseLruFile(void)
Definition: fd.c:1127
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
static void ReleaseLruFiles(void)
Definition: fd.c:1149
FILE * file
Definition: fd.c:236
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:707
SubTransactionId create_subid
Definition: fd.c:233
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
static int maxAllocatedDescs
Definition: fd.c:243
static int numAllocatedDescs
Definition: fd.c:242

◆ AtEOSubXact_Files()

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

Definition at line 2747 of file fd.c.

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

2749 {
2750  Index i;
2751 
2752  for (i = 0; i < numAllocatedDescs; i++)
2753  {
2754  if (allocatedDescs[i].create_subid == mySubid)
2755  {
2756  if (isCommit)
2757  allocatedDescs[i].create_subid = parentSubid;
2758  else
2759  {
2760  /* have to recheck the item after FreeDesc (ugly) */
2761  FreeDesc(&allocatedDescs[i--]);
2762  }
2763  }
2764  }
2765 }
static AllocateDesc * allocatedDescs
Definition: fd.c:244
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2364
unsigned int Index
Definition: c.h:476
SubTransactionId create_subid
Definition: fd.c:233
int i
static int numAllocatedDescs
Definition: fd.c:242

◆ AtEOXact_Files()

void AtEOXact_Files ( bool  isCommit)

Definition at line 2780 of file fd.c.

References CleanupTempFiles(), numTempTableSpaces, and tempTableSpaces.

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

2781 {
2782  CleanupTempFiles(isCommit, false);
2783  tempTableSpaces = NULL;
2784  numTempTableSpaces = -1;
2785 }
static int numTempTableSpaces
Definition: fd.c:257
static Oid * tempTableSpaces
Definition: fd.c:256
static void CleanupTempFiles(bool isCommit, bool isProcExit)
Definition: fd.c:2812

◆ BasicOpenFile()

int BasicOpenFile ( const char *  fileName,
int  fileFlags 
)

Definition at line 943 of file fd.c.

References BasicOpenFilePerm(), and pg_file_create_mode.

Referenced by AlterSystemSetConfigFile(), ReadControlFile(), update_controlfile(), WriteControlFile(), XLogFileInit(), XLogFileOpen(), XLogFileRead(), and XLogRead().

944 {
945  return BasicOpenFilePerm(fileName, fileFlags, pg_file_create_mode);
946 }
int pg_file_create_mode
Definition: file_perm.c:19
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:965

◆ BasicOpenFilePerm()

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

Definition at line 965 of file fd.c.

References buf, elog, ereport, errcode(), errmsg(), vfd::fd, LOG, vfd::lruLessRecently, ReleaseLruFile(), and snprintf.

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

966 {
967  int fd;
968 
969 tryAgain:
970  fd = open(fileName, fileFlags, fileMode);
971 
972  if (fd >= 0)
973  return fd; /* success! */
974 
975  if (errno == EMFILE || errno == ENFILE)
976  {
977  int save_errno = errno;
978 
979  ereport(LOG,
980  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
981  errmsg("out of file descriptors: %m; release and retry")));
982  errno = 0;
983  if (ReleaseLruFile())
984  goto tryAgain;
985  errno = save_errno;
986  }
987 
988  return -1; /* failure */
989 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static bool ReleaseLruFile(void)
Definition: fd.c:1127
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ closeAllVfds()

void closeAllVfds ( void  )

Definition at line 2642 of file fd.c.

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

Referenced by standard_ProcessUtility().

2643 {
2644  Index i;
2645 
2646  if (SizeVfdCache > 0)
2647  {
2648  Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
2649  for (i = 1; i < SizeVfdCache; i++)
2650  {
2651  if (!FileIsNotOpen(i))
2652  LruDelete(i);
2653  }
2654  }
2655 }
static Size SizeVfdCache
Definition: fd.c:197
static void LruDelete(File file)
Definition: fd.c:1032
#define FileIsNotOpen(file)
Definition: fd.c:169
unsigned int Index
Definition: c.h:476
#define Assert(condition)
Definition: c.h:733
int i

◆ ClosePipeStream()

int ClosePipeStream ( FILE *  file)

Definition at line 2613 of file fd.c.

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

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

2614 {
2615  int i;
2616 
2617  DO_DB(elog(LOG, "ClosePipeStream: Allocated %d", numAllocatedDescs));
2618 
2619  /* Remove file from list of allocated files, if it's present */
2620  for (i = numAllocatedDescs; --i >= 0;)
2621  {
2622  AllocateDesc *desc = &allocatedDescs[i];
2623 
2624  if (desc->kind == AllocateDescPipe && desc->desc.file == file)
2625  return FreeDesc(desc);
2626  }
2627 
2628  /* Only get here if someone passes us a file not in allocatedDescs */
2629  elog(WARNING, "file passed to ClosePipeStream was not obtained from OpenPipeStream");
2630 
2631  return pclose(file);
2632 }
static AllocateDesc * allocatedDescs
Definition: fd.c:244
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:160
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:232
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2364
#define WARNING
Definition: elog.h:40
FILE * file
Definition: fd.c:236
#define elog(elevel,...)
Definition: elog.h:228
int i
static int numAllocatedDescs
Definition: fd.c:242

◆ CloseTransientFile()

int CloseTransientFile ( int  fd)

Definition at line 2431 of file fd.c.

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

Referenced by ApplyLogicalMappingFile(), be_lo_export(), CheckPointLogicalRewriteHeap(), CheckPointReplicationOrigin(), copy_file(), dsm_impl_mmap(), durable_rename(), fsync_fname_ext(), get_controlfile(), heap_xlog_logical_rewrite(), lo_import_internal(), load_relmap_file(), mdunlinkfork(), qtext_load_file(), qtext_store(), ReadTwoPhaseFile(), RecreateTwoPhaseFile(), ReorderBufferIterTXNFinish(), ReorderBufferRestoreChanges(), ReorderBufferSerializeChange(), ReorderBufferSerializeTXN(), RestoreSlotFromDisk(), SaveSlotToPath(), SendTimeLineHistory(), SimpleLruDoesPhysicalPageExist(), SimpleLruFlush(), SlruInternalWritePage(), SlruPhysicalReadPage(), SlruPhysicalWritePage(), SnapBuildRestore(), SnapBuildSerialize(), StartupReplicationOrigin(), walkdir(), write_relmap_file(), writeTimeLineHistory(), writeTimeLineHistoryFile(), and XLogFileCopy().

2432 {
2433  int i;
2434 
2435  DO_DB(elog(LOG, "CloseTransientFile: Allocated %d", numAllocatedDescs));
2436 
2437  /* Remove fd from list of allocated files, if it's present */
2438  for (i = numAllocatedDescs; --i >= 0;)
2439  {
2440  AllocateDesc *desc = &allocatedDescs[i];
2441 
2442  if (desc->kind == AllocateDescRawFD && desc->desc.fd == fd)
2443  return FreeDesc(desc);
2444  }
2445 
2446  /* Only get here if someone passes us a file not in allocatedDescs */
2447  elog(WARNING, "fd passed to CloseTransientFile was not obtained from OpenTransientFile");
2448 
2449  return close(fd);
2450 }
static AllocateDesc * allocatedDescs
Definition: fd.c:244
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:160
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:232
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2364
#define WARNING
Definition: elog.h:40
int fd
Definition: fd.c:238
#define elog(elevel,...)
Definition: elog.h:228
int i
#define close(a)
Definition: win32.h:12
static int numAllocatedDescs
Definition: fd.c:242

◆ data_sync_elevel()

◆ durable_link_or_rename()

int durable_link_or_rename ( const char *  oldfile,
const char *  newfile,
int  loglevel 
)

Definition at line 731 of file fd.c.

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

Referenced by InstallXLogFileSegment(), writeTimeLineHistory(), and writeTimeLineHistoryFile().

732 {
733  /*
734  * Ensure that, if we crash directly after the rename/link, a file with
735  * valid contents is moved into place.
736  */
737  if (fsync_fname_ext(oldfile, false, false, elevel) != 0)
738  return -1;
739 
740 #ifdef HAVE_WORKING_LINK
741  if (link(oldfile, newfile) < 0)
742  {
743  ereport(elevel,
745  errmsg("could not link file \"%s\" to \"%s\": %m",
746  oldfile, newfile)));
747  return -1;
748  }
749  unlink(oldfile);
750 #else
751  /* XXX: Add racy file existence check? */
752  if (rename(oldfile, newfile) < 0)
753  {
754  ereport(elevel,
756  errmsg("could not rename file \"%s\" to \"%s\": %m",
757  oldfile, newfile)));
758  return -1;
759  }
760 #endif
761 
762  /*
763  * Make change persistent in case of an OS crash, both the new entry and
764  * its parent directory need to be flushed.
765  */
766  if (fsync_fname_ext(newfile, false, false, elevel) != 0)
767  return -1;
768 
769  /* Same for parent directory */
770  if (fsync_parent_path(newfile, elevel) != 0)
771  return -1;
772 
773  return 0;
774 }
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
int link(const char *fromname, const char *toname)
static int elevel
Definition: vacuumlazy.c:143
int errmsg(const char *fmt,...)
Definition: elog.c:822
static int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3342
static int fsync_parent_path(const char *fname, int elevel)
Definition: fd.c:3418

◆ durable_rename()

int durable_rename ( const char *  oldfile,
const char *  newfile,
int  loglevel 
)

Definition at line 605 of file fd.c.

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

Referenced by AlterSystemSetConfigFile(), apw_dump_now(), CancelBackup(), CheckPointReplicationOrigin(), dir_close(), KeepFileRestoredFromArchive(), pgarch_archiveDone(), pgss_shmem_shutdown(), StartupXLOG(), and XLogArchiveForceDone().

606 {
607  int fd;
608 
609  /*
610  * First fsync the old and target path (if it exists), to ensure that they
611  * are properly persistent on disk. Syncing the target file is not
612  * strictly necessary, but it makes it easier to reason about crashes;
613  * because it's then guaranteed that either source or target file exists
614  * after a crash.
615  */
616  if (fsync_fname_ext(oldfile, false, false, elevel) != 0)
617  return -1;
618 
619  fd = OpenTransientFile(newfile, PG_BINARY | O_RDWR);
620  if (fd < 0)
621  {
622  if (errno != ENOENT)
623  {
624  ereport(elevel,
626  errmsg("could not open file \"%s\": %m", newfile)));
627  return -1;
628  }
629  }
630  else
631  {
632  if (pg_fsync(fd) != 0)
633  {
634  int save_errno;
635 
636  /* close file upon error, might not be in transaction context */
637  save_errno = errno;
638  CloseTransientFile(fd);
639  errno = save_errno;
640 
641  ereport(elevel,
643  errmsg("could not fsync file \"%s\": %m", newfile)));
644  return -1;
645  }
646 
647  if (CloseTransientFile(fd) != 0)
648  {
649  ereport(elevel,
651  errmsg("could not close file \"%s\": %m", newfile)));
652  return -1;
653  }
654  }
655 
656  /* Time to do the real deal... */
657  if (rename(oldfile, newfile) < 0)
658  {
659  ereport(elevel,
661  errmsg("could not rename file \"%s\" to \"%s\": %m",
662  oldfile, newfile)));
663  return -1;
664  }
665 
666  /*
667  * To guarantee renaming the file is persistent, fsync the file with its
668  * new name, and its containing directory.
669  */
670  if (fsync_fname_ext(newfile, false, false, elevel) != 0)
671  return -1;
672 
673  if (fsync_parent_path(newfile, elevel) != 0)
674  return -1;
675 
676  return 0;
677 }
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1216
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2254
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
int CloseTransientFile(int fd)
Definition: fd.c:2431
static int elevel
Definition: vacuumlazy.c:143
int errmsg(const char *fmt,...)
Definition: elog.c:822
static int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3342
int pg_fsync(int fd)
Definition: fd.c:330
static int fsync_parent_path(const char *fname, int elevel)
Definition: fd.c:3418

◆ durable_unlink()

int durable_unlink ( const char *  fname,
int  loglevel 
)

Definition at line 695 of file fd.c.

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

Referenced by do_pg_stop_backup(), exitArchiveRecovery(), InstallXLogFileSegment(), and RemoveXlogFile().

696 {
697  if (unlink(fname) < 0)
698  {
699  ereport(elevel,
701  errmsg("could not remove file \"%s\": %m",
702  fname)));
703  return -1;
704  }
705 
706  /*
707  * To guarantee that the removal of the file is persistent, fsync its
708  * parent directory.
709  */
710  if (fsync_parent_path(fname, elevel) != 0)
711  return -1;
712 
713  return 0;
714 }
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
static int elevel
Definition: vacuumlazy.c:143
int errmsg(const char *fmt,...)
Definition: elog.c:822
static int fsync_parent_path(const char *fname, int elevel)
Definition: fd.c:3418

◆ FileClose()

void FileClose ( File  file)

Definition at line 1710 of file fd.c.

References Assert, close, data_sync_elevel(), Delete(), DO_DB, elog, 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, temporary_files_size, and VFD_CLOSED.

Referenced by BufFileClose(), CleanupTempFiles(), logical_end_heap_rewrite(), mdclose(), mdtruncate(), and ResourceOwnerReleaseInternal().

1711 {
1712  Vfd *vfdP;
1713 
1714  Assert(FileIsValid(file));
1715 
1716  DO_DB(elog(LOG, "FileClose: %d (%s)",
1717  file, VfdCache[file].fileName));
1718 
1719  vfdP = &VfdCache[file];
1720 
1721  if (!FileIsNotOpen(file))
1722  {
1723  /* close the file */
1724  if (close(vfdP->fd) != 0)
1725  {
1726  /*
1727  * We may need to panic on failure to close non-temporary files;
1728  * see LruDelete.
1729  */
1731  "could not close file \"%s\": %m", vfdP->fileName);
1732  }
1733 
1734  --nfile;
1735  vfdP->fd = VFD_CLOSED;
1736 
1737  /* remove the file from the lru ring */
1738  Delete(file);
1739  }
1740 
1741  if (vfdP->fdstate & FD_TEMP_FILE_LIMIT)
1742  {
1743  /* Subtract its size from current usage (do first in case of error) */
1744  temporary_files_size -= vfdP->fileSize;
1745  vfdP->fileSize = 0;
1746  }
1747 
1748  /*
1749  * Delete the file if it was temporary, and make a log entry if wanted
1750  */
1751  if (vfdP->fdstate & FD_DELETE_AT_CLOSE)
1752  {
1753  struct stat filestats;
1754  int stat_errno;
1755 
1756  /*
1757  * If we get an error, as could happen within the ereport/elog calls,
1758  * we'll come right back here during transaction abort. Reset the
1759  * flag to ensure that we can't get into an infinite loop. This code
1760  * is arranged to ensure that the worst-case consequence is failing to
1761  * emit log message(s), not failing to attempt the unlink.
1762  */
1763  vfdP->fdstate &= ~FD_DELETE_AT_CLOSE;
1764 
1765 
1766  /* first try the stat() */
1767  if (stat(vfdP->fileName, &filestats))
1768  stat_errno = errno;
1769  else
1770  stat_errno = 0;
1771 
1772  /* in any case do the unlink */
1773  if (unlink(vfdP->fileName))
1774  elog(LOG, "could not unlink file \"%s\": %m", vfdP->fileName);
1775 
1776  /* and last report the stat results */
1777  if (stat_errno == 0)
1778  ReportTemporaryFileUsage(vfdP->fileName, filestats.st_size);
1779  else
1780  {
1781  errno = stat_errno;
1782  elog(LOG, "could not stat file \"%s\": %m", vfdP->fileName);
1783  }
1784  }
1785 
1786  /* Unregister it from the resource owner */
1787  if (vfdP->resowner)
1788  ResourceOwnerForgetFile(vfdP->resowner, file);
1789 
1790  /*
1791  * Return the Vfd slot to the free list
1792  */
1793  FreeVfd(file);
1794 }
#define FD_TEMP_FILE_LIMIT
Definition: fd.c:174
#define DO_DB(A)
Definition: fd.c:160
#define FD_DELETE_AT_CLOSE
Definition: fd.c:172
static Vfd * VfdCache
Definition: fd.c:196
static void Delete(File file)
Definition: fd.c:1013
#define LOG
Definition: elog.h:26
char * fileName
Definition: fd.c:185
static int nfile
Definition: fd.c:202
unsigned short fdstate
Definition: fd.c:179
Definition: fd.c:176
off_t fileSize
Definition: fd.c:184
int fd
Definition: fd.c:178
ResourceOwner resowner
Definition: fd.c:180
static void ReportTemporaryFileUsage(const char *path, off_t size)
Definition: fd.c:1273
#define stat(a, b)
Definition: win32_port.h:255
#define FileIsNotOpen(file)
Definition: fd.c:169
int data_sync_elevel(int elevel)
Definition: fd.c:3481
#define FileIsValid(file)
Definition: fd.c:166
#define VFD_CLOSED
Definition: fd.c:164
static uint64 temporary_files_size
Definition: fd.c:216
#define Assert(condition)
Definition: c.h:733
#define elog(elevel,...)
Definition: elog.h:228
static void FreeVfd(File file)
Definition: fd.c:1217
#define close(a)
Definition: win32.h:12
void ResourceOwnerForgetFile(ResourceOwner owner, File file)
Definition: resowner.c:1253

◆ FileGetRawDesc()

int FileGetRawDesc ( File  file)

Definition at line 2100 of file fd.c.

References Assert, vfd::fd, and FileIsValid.

2101 {
2102  Assert(FileIsValid(file));
2103  return VfdCache[file].fd;
2104 }
static Vfd * VfdCache
Definition: fd.c:196
int fd
Definition: fd.c:178
#define FileIsValid(file)
Definition: fd.c:166
#define Assert(condition)
Definition: c.h:733

◆ FileGetRawFlags()

int FileGetRawFlags ( File  file)

Definition at line 2110 of file fd.c.

References Assert, vfd::fileFlags, and FileIsValid.

2111 {
2112  Assert(FileIsValid(file));
2113  return VfdCache[file].fileFlags;
2114 }
static Vfd * VfdCache
Definition: fd.c:196
#define FileIsValid(file)
Definition: fd.c:166
#define Assert(condition)
Definition: c.h:733
int fileFlags
Definition: fd.c:187

◆ FileGetRawMode()

mode_t FileGetRawMode ( File  file)

Definition at line 2120 of file fd.c.

References Assert, FileIsValid, and vfd::fileMode.

2121 {
2122  Assert(FileIsValid(file));
2123  return VfdCache[file].fileMode;
2124 }
static Vfd * VfdCache
Definition: fd.c:196
mode_t fileMode
Definition: fd.c:188
#define FileIsValid(file)
Definition: fd.c:166
#define Assert(condition)
Definition: c.h:733

◆ FilePathName()

char* FilePathName ( File  file)

Definition at line 2084 of file fd.c.

References Assert, FileIsValid, and vfd::fileName.

Referenced by _mdnblocks(), BufFileSize(), mdextend(), mdimmedsync(), mdread(), mdtruncate(), mdwrite(), and register_dirty_segment().

2085 {
2086  Assert(FileIsValid(file));
2087 
2088  return VfdCache[file].fileName;
2089 }
static Vfd * VfdCache
Definition: fd.c:196
char * fileName
Definition: fd.c:185
#define FileIsValid(file)
Definition: fd.c:166
#define Assert(condition)
Definition: c.h:733

◆ FilePrefetch()

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

Definition at line 1806 of file fd.c.

References Assert, DO_DB, elog, vfd::fd, FileAccess(), FileIsValid, vfd::fileName, INT64_FORMAT, LOG, pgstat_report_wait_end(), and pgstat_report_wait_start().

Referenced by mdprefetch().

1807 {
1808 #if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
1809  int returnCode;
1810 
1811  Assert(FileIsValid(file));
1812 
1813  DO_DB(elog(LOG, "FilePrefetch: %d (%s) " INT64_FORMAT " %d",
1814  file, VfdCache[file].fileName,
1815  (int64) offset, amount));
1816 
1817  returnCode = FileAccess(file);
1818  if (returnCode < 0)
1819  return returnCode;
1820 
1821  pgstat_report_wait_start(wait_event_info);
1822  returnCode = posix_fadvise(VfdCache[file].fd, offset, amount,
1823  POSIX_FADV_WILLNEED);
1825 
1826  return returnCode;
1827 #else
1828  Assert(FileIsValid(file));
1829  return 0;
1830 #endif
1831 }
#define DO_DB(A)
Definition: fd.c:160
static Vfd * VfdCache
Definition: fd.c:196
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
#define FileIsValid(file)
Definition: fd.c:166
static int FileAccess(File file)
Definition: fd.c:1237
#define Assert(condition)
Definition: c.h:733
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1318
#define INT64_FORMAT
Definition: c.h:401
#define elog(elevel,...)
Definition: elog.h:228

◆ FileRead()

int FileRead ( File  file,
char *  buffer,
int  amount,
off_t  offset,
uint32  wait_event_info 
)

Definition at line 1857 of file fd.c.

References _dosmaperr(), Assert, DO_DB, EINTR, elog, error(), vfd::fd, FileAccess(), FileIsValid, vfd::fileName, INT64_FORMAT, LOG, pg_pread(), pg_usleep(), pgstat_report_wait_end(), and pgstat_report_wait_start().

Referenced by BufFileLoadBuffer(), and mdread().

1859 {
1860  int returnCode;
1861  Vfd *vfdP;
1862 
1863  Assert(FileIsValid(file));
1864 
1865  DO_DB(elog(LOG, "FileRead: %d (%s) " INT64_FORMAT " %d %p",
1866  file, VfdCache[file].fileName,
1867  (int64) offset,
1868  amount, buffer));
1869 
1870  returnCode = FileAccess(file);
1871  if (returnCode < 0)
1872  return returnCode;
1873 
1874  vfdP = &VfdCache[file];
1875 
1876 retry:
1877  pgstat_report_wait_start(wait_event_info);
1878  returnCode = pg_pread(vfdP->fd, buffer, amount, offset);
1880 
1881  if (returnCode < 0)
1882  {
1883  /*
1884  * Windows may run out of kernel buffers and return "Insufficient
1885  * system resources" error. Wait a bit and retry to solve it.
1886  *
1887  * It is rumored that EINTR is also possible on some Unix filesystems,
1888  * in which case immediate retry is indicated.
1889  */
1890 #ifdef WIN32
1891  DWORD error = GetLastError();
1892 
1893  switch (error)
1894  {
1895  case ERROR_NO_SYSTEM_RESOURCES:
1896  pg_usleep(1000L);
1897  errno = EINTR;
1898  break;
1899  default:
1900  _dosmaperr(error);
1901  break;
1902  }
1903 #endif
1904  /* OK to retry if interrupted */
1905  if (errno == EINTR)
1906  goto retry;
1907  }
1908 
1909  return returnCode;
1910 }
static void error(void)
Definition: sql-dyntest.c:147
#define DO_DB(A)
Definition: fd.c:160
void _dosmaperr(unsigned long)
Definition: win32error.c:171
static Vfd * VfdCache
Definition: fd.c:196
#define LOG
Definition: elog.h:26
ssize_t pg_pread(int fd, void *buf, size_t nbyte, off_t offset)
Definition: pread.c:27
void pg_usleep(long microsec)
Definition: signal.c:53
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
Definition: fd.c:176
int fd
Definition: fd.c:178
#define FileIsValid(file)
Definition: fd.c:166
static int FileAccess(File file)
Definition: fd.c:1237
#define Assert(condition)
Definition: c.h:733
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1318
#define INT64_FORMAT
Definition: c.h:401
#define elog(elevel,...)
Definition: elog.h:228
#define EINTR
Definition: win32_port.h:323

◆ FileSize()

off_t FileSize ( File  file)

Definition at line 2032 of file fd.c.

References Assert, DO_DB, elog, vfd::fd, FileAccess(), FileIsNotOpen, FileIsValid, vfd::fileName, and LOG.

Referenced by _mdnblocks(), and BufFileSize().

2033 {
2034  Assert(FileIsValid(file));
2035 
2036  DO_DB(elog(LOG, "FileSize %d (%s)",
2037  file, VfdCache[file].fileName));
2038 
2039  if (FileIsNotOpen(file))
2040  {
2041  if (FileAccess(file) < 0)
2042  return (off_t) -1;
2043  }
2044 
2045  return lseek(VfdCache[file].fd, 0, SEEK_END);
2046 }
#define DO_DB(A)
Definition: fd.c:160
static Vfd * VfdCache
Definition: fd.c:196
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define FileIsNotOpen(file)
Definition: fd.c:169
#define FileIsValid(file)
Definition: fd.c:166
static int FileAccess(File file)
Definition: fd.c:1237
#define Assert(condition)
Definition: c.h:733
#define elog(elevel,...)
Definition: elog.h:228

◆ FileSync()

int FileSync ( File  file,
uint32  wait_event_info 
)

Definition at line 2011 of file fd.c.

References Assert, DO_DB, elog, vfd::fd, FileAccess(), FileIsValid, vfd::fileName, LOG, pg_fsync(), pgstat_report_wait_end(), and pgstat_report_wait_start().

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

2012 {
2013  int returnCode;
2014 
2015  Assert(FileIsValid(file));
2016 
2017  DO_DB(elog(LOG, "FileSync: %d (%s)",
2018  file, VfdCache[file].fileName));
2019 
2020  returnCode = FileAccess(file);
2021  if (returnCode < 0)
2022  return returnCode;
2023 
2024  pgstat_report_wait_start(wait_event_info);
2025  returnCode = pg_fsync(VfdCache[file].fd);
2027 
2028  return returnCode;
2029 }
#define DO_DB(A)
Definition: fd.c:160
static Vfd * VfdCache
Definition: fd.c:196
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
#define FileIsValid(file)
Definition: fd.c:166
static int FileAccess(File file)
Definition: fd.c:1237
#define Assert(condition)
Definition: c.h:733
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1318
#define elog(elevel,...)
Definition: elog.h:228
int pg_fsync(int fd)
Definition: fd.c:330

◆ FileTruncate()

int FileTruncate ( File  file,
off_t  offset,
uint32  wait_event_info 
)

Definition at line 2049 of file fd.c.

References Assert, DO_DB, elog, vfd::fd, FD_TEMP_FILE_LIMIT, vfd::fdstate, FileAccess(), FileIsValid, vfd::fileName, vfd::fileSize, ftruncate, LOG, pgstat_report_wait_end(), pgstat_report_wait_start(), and temporary_files_size.

Referenced by mdtruncate().

2050 {
2051  int returnCode;
2052 
2053  Assert(FileIsValid(file));
2054 
2055  DO_DB(elog(LOG, "FileTruncate %d (%s)",
2056  file, VfdCache[file].fileName));
2057 
2058  returnCode = FileAccess(file);
2059  if (returnCode < 0)
2060  return returnCode;
2061 
2062  pgstat_report_wait_start(wait_event_info);
2063  returnCode = ftruncate(VfdCache[file].fd, offset);
2065 
2066  if (returnCode == 0 && VfdCache[file].fileSize > offset)
2067  {
2068  /* adjust our state for truncation of a temp file */
2069  Assert(VfdCache[file].fdstate & FD_TEMP_FILE_LIMIT);
2070  temporary_files_size -= VfdCache[file].fileSize - offset;
2071  VfdCache[file].fileSize = offset;
2072  }
2073 
2074  return returnCode;
2075 }
#define FD_TEMP_FILE_LIMIT
Definition: fd.c:174
#define DO_DB(A)
Definition: fd.c:160
static Vfd * VfdCache
Definition: fd.c:196
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
off_t fileSize
Definition: fd.c:184
#define FileIsValid(file)
Definition: fd.c:166
static uint64 temporary_files_size
Definition: fd.c:216
static int FileAccess(File file)
Definition: fd.c:1237
#define Assert(condition)
Definition: c.h:733
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1318
#define elog(elevel,...)
Definition: elog.h:228
#define ftruncate(a, b)
Definition: win32_port.h:60

◆ FileWrite()

int FileWrite ( File  file,
char *  buffer,
int  amount,
off_t  offset,
uint32  wait_event_info 
)

Definition at line 1913 of file fd.c.

References _dosmaperr(), Assert, DO_DB, EINTR, elog, ereport, errcode(), errmsg(), ERROR, error(), vfd::fd, FD_TEMP_FILE_LIMIT, vfd::fdstate, FileAccess(), FileIsValid, vfd::fileName, vfd::fileSize, INT64_FORMAT, LOG, pg_pwrite(), pg_usleep(), pgstat_report_wait_end(), pgstat_report_wait_start(), temp_file_limit, and temporary_files_size.

Referenced by BufFileDumpBuffer(), logical_heap_rewrite_flush_mappings(), mdextend(), and mdwrite().

1915 {
1916  int returnCode;
1917  Vfd *vfdP;
1918 
1919  Assert(FileIsValid(file));
1920 
1921  DO_DB(elog(LOG, "FileWrite: %d (%s) " INT64_FORMAT " %d %p",
1922  file, VfdCache[file].fileName,
1923  (int64) offset,
1924  amount, buffer));
1925 
1926  returnCode = FileAccess(file);
1927  if (returnCode < 0)
1928  return returnCode;
1929 
1930  vfdP = &VfdCache[file];
1931 
1932  /*
1933  * If enforcing temp_file_limit and it's a temp file, check to see if the
1934  * write would overrun temp_file_limit, and throw error if so. Note: it's
1935  * really a modularity violation to throw error here; we should set errno
1936  * and return -1. However, there's no way to report a suitable error
1937  * message if we do that. All current callers would just throw error
1938  * immediately anyway, so this is safe at present.
1939  */
1940  if (temp_file_limit >= 0 && (vfdP->fdstate & FD_TEMP_FILE_LIMIT))
1941  {
1942  off_t past_write = offset + amount;
1943 
1944  if (past_write > vfdP->fileSize)
1945  {
1946  uint64 newTotal = temporary_files_size;
1947 
1948  newTotal += past_write - vfdP->fileSize;
1949  if (newTotal > (uint64) temp_file_limit * (uint64) 1024)
1950  ereport(ERROR,
1951  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
1952  errmsg("temporary file size exceeds temp_file_limit (%dkB)",
1953  temp_file_limit)));
1954  }
1955  }
1956 
1957 retry:
1958  errno = 0;
1959  pgstat_report_wait_start(wait_event_info);
1960  returnCode = pg_pwrite(VfdCache[file].fd, buffer, amount, offset);
1962 
1963  /* if write didn't set errno, assume problem is no disk space */
1964  if (returnCode != amount && errno == 0)
1965  errno = ENOSPC;
1966 
1967  if (returnCode >= 0)
1968  {
1969  /*
1970  * Maintain fileSize and temporary_files_size if it's a temp file.
1971  */
1972  if (vfdP->fdstate & FD_TEMP_FILE_LIMIT)
1973  {
1974  off_t past_write = offset + amount;
1975 
1976  if (past_write > vfdP->fileSize)
1977  {
1978  temporary_files_size += past_write - vfdP->fileSize;
1979  vfdP->fileSize = past_write;
1980  }
1981  }
1982  }
1983  else
1984  {
1985  /*
1986  * See comments in FileRead()
1987  */
1988 #ifdef WIN32
1989  DWORD error = GetLastError();
1990 
1991  switch (error)
1992  {
1993  case ERROR_NO_SYSTEM_RESOURCES:
1994  pg_usleep(1000L);
1995  errno = EINTR;
1996  break;
1997  default:
1998  _dosmaperr(error);
1999  break;
2000  }
2001 #endif
2002  /* OK to retry if interrupted */
2003  if (errno == EINTR)
2004  goto retry;
2005  }
2006 
2007  return returnCode;
2008 }
static void error(void)
Definition: sql-dyntest.c:147
#define FD_TEMP_FILE_LIMIT
Definition: fd.c:174
#define DO_DB(A)
Definition: fd.c:160
void _dosmaperr(unsigned long)
Definition: win32error.c:171
static Vfd * VfdCache
Definition: fd.c:196
int errcode(int sqlerrcode)
Definition: elog.c:608
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
ssize_t pg_pwrite(int fd, const void *buf, size_t nbyte, off_t offset)
Definition: pwrite.c:27
void pg_usleep(long microsec)
Definition: signal.c:53
#define ERROR
Definition: elog.h:43
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
unsigned short fdstate
Definition: fd.c:179
Definition: fd.c:176
off_t fileSize
Definition: fd.c:184
#define ereport(elevel, rest)
Definition: elog.h:141
#define FileIsValid(file)
Definition: fd.c:166
static uint64 temporary_files_size
Definition: fd.c:216
static int FileAccess(File file)
Definition: fd.c:1237
#define Assert(condition)
Definition: c.h:733
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1318
#define INT64_FORMAT
Definition: c.h:401
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
#define EINTR
Definition: win32_port.h:323
int temp_file_limit
Definition: guc.c:523

◆ FileWriteback()

void FileWriteback ( File  file,
off_t  offset,
off_t  nbytes,
uint32  wait_event_info 
)

Definition at line 1834 of file fd.c.

References Assert, DO_DB, elog, vfd::fd, FileAccess(), FileIsValid, vfd::fileName, INT64_FORMAT, LOG, pg_flush_data(), pgstat_report_wait_end(), and pgstat_report_wait_start().

Referenced by mdwriteback().

1835 {
1836  int returnCode;
1837 
1838  Assert(FileIsValid(file));
1839 
1840  DO_DB(elog(LOG, "FileWriteback: %d (%s) " INT64_FORMAT " " INT64_FORMAT,
1841  file, VfdCache[file].fileName,
1842  (int64) offset, (int64) nbytes));
1843 
1844  if (nbytes <= 0)
1845  return;
1846 
1847  returnCode = FileAccess(file);
1848  if (returnCode < 0)
1849  return;
1850 
1851  pgstat_report_wait_start(wait_event_info);
1852  pg_flush_data(VfdCache[file].fd, offset, nbytes);
1854 }
#define DO_DB(A)
Definition: fd.c:160
static Vfd * VfdCache
Definition: fd.c:196
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
void pg_flush_data(int fd, off_t offset, off_t nbytes)
Definition: fd.c:402
#define FileIsValid(file)
Definition: fd.c:166
static int FileAccess(File file)
Definition: fd.c:1237
#define Assert(condition)
Definition: c.h:733
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1318
#define INT64_FORMAT
Definition: c.h:401
#define elog(elevel,...)
Definition: elog.h:228

◆ FreeDir()

int FreeDir ( DIR dir)

Definition at line 2583 of file fd.c.

References 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_start_backup(), dsm_cleanup_for_mmap(), get_ext_ver_list(), getInstallationPaths(), movedb(), ParseTzFile(), perform_base_backup(), pg_available_extension_versions(), pg_available_extensions(), pg_logdir_ls_internal(), pg_ls_dir(), pg_ls_dir_files(), pg_tablespace_databases(), pg_tzenumerate_end(), pg_tzenumerate_next(), pgarch_readyXlog(), pgstat_reset_remove_files(), RelationCacheInitFileRemove(), RelationCacheInitFileRemoveInDir(), RemoveNonParentXlogFiles(), RemoveOldXlogFiles(), RemovePgTempFiles(), RemovePgTempFilesInDir(), RemovePgTempRelationFiles(), RemovePgTempRelationFilesInDbspace(), RemoveTempXlogFiles(), ReorderBufferCleanupSerializedTXNs(), ResetUnloggedRelations(), ResetUnloggedRelationsInDbspaceDir(), ResetUnloggedRelationsInTablespaceDir(), restoreTwoPhaseData(), scan_directory_ci(), sendDir(), SlruScanDirectory(), StartupReorderBuffer(), StartupReplicationSlots(), UpdateLogicalMappings(), and walkdir().

2584 {
2585  int i;
2586 
2587  /* Nothing to do if AllocateDir failed */
2588  if (dir == NULL)
2589  return 0;
2590 
2591  DO_DB(elog(LOG, "FreeDir: Allocated %d", numAllocatedDescs));
2592 
2593  /* Remove dir from list of allocated dirs, if it's present */
2594  for (i = numAllocatedDescs; --i >= 0;)
2595  {
2596  AllocateDesc *desc = &allocatedDescs[i];
2597 
2598  if (desc->kind == AllocateDescDir && desc->desc.dir == dir)
2599  return FreeDesc(desc);
2600  }
2601 
2602  /* Only get here if someone passes us a dir not in allocatedDescs */
2603  elog(WARNING, "dir passed to FreeDir was not obtained from AllocateDir");
2604 
2605  return closedir(dir);
2606 }
static AllocateDesc * allocatedDescs
Definition: fd.c:244
union AllocateDesc::@26 desc
DIR * dir
Definition: fd.c:237
#define DO_DB(A)
Definition: fd.c:160
int closedir(DIR *)
Definition: dirent.c:113
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:232
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2364
#define WARNING
Definition: elog.h:40
#define elog(elevel,...)
Definition: elog.h:228
int i
static int numAllocatedDescs
Definition: fd.c:242

◆ FreeFile()

int FreeFile ( FILE *  file)

Definition at line 2403 of file fd.c.

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

Referenced by _ShowOption(), AlterSystemSetConfigFile(), apw_dump_now(), apw_load_buffers(), BackendRun(), checkControlFile(), do_pg_start_backup(), do_pg_stop_backup(), EndCopy(), entry_reset(), existsTimeLineHistory(), ExportSnapshot(), fill_hba_view(), gc_qtexts(), ImportSnapshot(), load_dh_file(), load_hba(), load_ident(), load_relcache_init_file(), parse_extension_control_file(), ParseTzFile(), perform_base_backup(), pg_backup_start_time(), pg_current_logfile(), pg_file_write_internal(), pg_promote(), PGSharedMemoryAttach(), pgss_shmem_shutdown(), pgss_shmem_startup(), pgstat_read_db_statsfile(), pgstat_read_db_statsfile_timestamp(), pgstat_read_statsfiles(), pgstat_write_db_statsfile(), pgstat_write_statsfiles(), PostmasterMarkPIDForWorkerNotify(), read_backup_label(), read_binary_file(), read_tablespace_map(), read_whole_file(), readTimeLineHistory(), sendFile(), tokenize_inc_file(), tsearch_readline_end(), ValidatePgVersion(), write_relcache_init_file(), XLogArchiveForceDone(), and XLogArchiveNotify().

2404 {
2405  int i;
2406 
2407  DO_DB(elog(LOG, "FreeFile: Allocated %d", numAllocatedDescs));
2408 
2409  /* Remove file from list of allocated files, if it's present */
2410  for (i = numAllocatedDescs; --i >= 0;)
2411  {
2412  AllocateDesc *desc = &allocatedDescs[i];
2413 
2414  if (desc->kind == AllocateDescFile && desc->desc.file == file)
2415  return FreeDesc(desc);
2416  }
2417 
2418  /* Only get here if someone passes us a file not in allocatedDescs */
2419  elog(WARNING, "file passed to FreeFile was not obtained from AllocateFile");
2420 
2421  return fclose(file);
2422 }
static AllocateDesc * allocatedDescs
Definition: fd.c:244
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:160
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:232
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2364
#define WARNING
Definition: elog.h:40
FILE * file
Definition: fd.c:236
#define elog(elevel,...)
Definition: elog.h:228
int i
static int numAllocatedDescs
Definition: fd.c:242

◆ fsync_fname()

◆ GetNextTempTableSpace()

Oid GetNextTempTableSpace ( void  )

Definition at line 2726 of file fd.c.

References InvalidOid, nextTempTableSpace, numTempTableSpaces, and tempTableSpaces.

Referenced by GetDefaultTablespace(), and OpenTemporaryFile().

2727 {
2728  if (numTempTableSpaces > 0)
2729  {
2730  /* Advance nextTempTableSpace counter with wraparound */
2732  nextTempTableSpace = 0;
2734  }
2735  return InvalidOid;
2736 }
static int numTempTableSpaces
Definition: fd.c:257
static int nextTempTableSpace
Definition: fd.c:258
#define InvalidOid
Definition: postgres_ext.h:36
static Oid * tempTableSpaces
Definition: fd.c:256

◆ GetTempTablespaces()

int GetTempTablespaces ( Oid tableSpaces,
int  numSpaces 
)

Definition at line 2708 of file fd.c.

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

Referenced by SharedFileSetInit().

2709 {
2710  int i;
2711 
2713  for (i = 0; i < numTempTableSpaces && i < numSpaces; ++i)
2714  tableSpaces[i] = tempTableSpaces[i];
2715 
2716  return i;
2717 }
static int numTempTableSpaces
Definition: fd.c:257
bool TempTablespacesAreSet(void)
Definition: fd.c:2696
#define Assert(condition)
Definition: c.h:733
static Oid * tempTableSpaces
Definition: fd.c:256
int i

◆ InitFileAccess()

void InitFileAccess ( void  )

Definition at line 783 of file fd.c.

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

Referenced by BaseInit().

784 {
785  Assert(SizeVfdCache == 0); /* call me only once */
786 
787  /* initialize cache header entry */
788  VfdCache = (Vfd *) malloc(sizeof(Vfd));
789  if (VfdCache == NULL)
790  ereport(FATAL,
791  (errcode(ERRCODE_OUT_OF_MEMORY),
792  errmsg("out of memory")));
793 
794  MemSet((char *) &(VfdCache[0]), 0, sizeof(Vfd));
796 
797  SizeVfdCache = 1;
798 
799  /* register proc-exit hook to ensure temp files are dropped at exit */
801 }
static void AtProcExit_Files(int code, Datum arg)
Definition: fd.c:2794
static Size SizeVfdCache
Definition: fd.c:197
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:305
static Vfd * VfdCache
Definition: fd.c:196
int errcode(int sqlerrcode)
Definition: elog.c:608
#define MemSet(start, val, len)
Definition: c.h:956
#define malloc(a)
Definition: header.h:50
#define FATAL
Definition: elog.h:52
Definition: fd.c:176
int fd
Definition: fd.c:178
#define ereport(elevel, rest)
Definition: elog.h:141
#define VFD_CLOSED
Definition: fd.c:164
#define Assert(condition)
Definition: c.h:733
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ looks_like_temp_rel_name()

bool looks_like_temp_rel_name ( const char *  name)

Definition at line 3063 of file fd.c.

References forkname_chars().

Referenced by RemovePgTempRelationFilesInDbspace(), and sendDir().

3064 {
3065  int pos;
3066  int savepos;
3067 
3068  /* Must start with "t". */
3069  if (name[0] != 't')
3070  return false;
3071 
3072  /* Followed by a non-empty string of digits and then an underscore. */
3073  for (pos = 1; isdigit((unsigned char) name[pos]); ++pos)
3074  ;
3075  if (pos == 1 || name[pos] != '_')
3076  return false;
3077 
3078  /* Followed by another nonempty string of digits. */
3079  for (savepos = ++pos; isdigit((unsigned char) name[pos]); ++pos)
3080  ;
3081  if (savepos == pos)
3082  return false;
3083 
3084  /* We might have _forkname or .segment or both. */
3085  if (name[pos] == '_')
3086  {
3087  int forkchar = forkname_chars(&name[pos + 1], NULL);
3088 
3089  if (forkchar <= 0)
3090  return false;
3091  pos += forkchar + 1;
3092  }
3093  if (name[pos] == '.')
3094  {
3095  int segchar;
3096 
3097  for (segchar = 1; isdigit((unsigned char) name[pos + segchar]); ++segchar)
3098  ;
3099  if (segchar <= 1)
3100  return false;
3101  pos += segchar;
3102  }
3103 
3104  /* Now we should be at the end. */
3105  if (name[pos] != '\0')
3106  return false;
3107  return true;
3108 }
int forkname_chars(const char *str, ForkNumber *fork)
Definition: relpath.c:78
const char * name
Definition: encode.c:521

◆ MakePGDirectory()

int MakePGDirectory ( const char *  directoryName)

◆ OpenPipeStream()

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

Definition at line 2307 of file fd.c.

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

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

2308 {
2309  FILE *file;
2310  int save_errno;
2311 
2312  DO_DB(elog(LOG, "OpenPipeStream: Allocated %d (%s)",
2313  numAllocatedDescs, command));
2314 
2315  /* Can we allocate another non-virtual FD? */
2316  if (!reserveAllocatedDesc())
2317  ereport(ERROR,
2318  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2319  errmsg("exceeded maxAllocatedDescs (%d) while trying to execute command \"%s\"",
2320  maxAllocatedDescs, command)));
2321 
2322  /* Close excess kernel FDs. */
2323  ReleaseLruFiles();
2324 
2325 TryAgain:
2326  fflush(stdout);
2327  fflush(stderr);
2329  errno = 0;
2330  file = popen(command, mode);
2331  save_errno = errno;
2333  errno = save_errno;
2334  if (file != NULL)
2335  {
2337 
2338  desc->kind = AllocateDescPipe;
2339  desc->desc.file = file;
2342  return desc->desc.file;
2343  }
2344 
2345  if (errno == EMFILE || errno == ENFILE)
2346  {
2347  ereport(LOG,
2348  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2349  errmsg("out of file descriptors: %m; release and retry")));
2350  if (ReleaseLruFile())
2351  goto TryAgain;
2352  errno = save_errno;
2353  }
2354 
2355  return NULL;
2356 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
static AllocateDesc * allocatedDescs
Definition: fd.c:244
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:160
int errcode(int sqlerrcode)
Definition: elog.c:608
static bool reserveAllocatedDesc(void)
Definition: fd.c:2131
#define SIGPIPE
Definition: win32_port.h:159
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:232
static bool ReleaseLruFile(void)
Definition: fd.c:1127
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define SIG_IGN
Definition: win32_port.h:151
static void ReleaseLruFiles(void)
Definition: fd.c:1149
FILE * file
Definition: fd.c:236
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
#define SIG_DFL
Definition: win32_port.h:149
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:707
SubTransactionId create_subid
Definition: fd.c:233
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
static int maxAllocatedDescs
Definition: fd.c:243
static int numAllocatedDescs
Definition: fd.c:242

◆ OpenTemporaryFile()

File OpenTemporaryFile ( bool  interXact)

Definition at line 1461 of file fd.c.

References CurrentResourceOwner, FD_DELETE_AT_CLOSE, FD_TEMP_FILE_LIMIT, vfd::fdstate, GetNextTempTableSpace(), MyDatabaseTableSpace, numTempTableSpaces, OidIsValid, OpenTemporaryFileInTablespace(), RegisterTemporaryFile(), and ResourceOwnerEnlargeFiles().

Referenced by BufFileCreateTemp(), and extendBufFile().

1462 {
1463  File file = 0;
1464 
1465  /*
1466  * Make sure the current resource owner has space for this File before we
1467  * open it, if we'll be registering it below.
1468  */
1469  if (!interXact)
1471 
1472  /*
1473  * If some temp tablespace(s) have been given to us, try to use the next
1474  * one. If a given tablespace can't be found, we silently fall back to
1475  * the database's default tablespace.
1476  *
1477  * BUT: if the temp file is slated to outlive the current transaction,
1478  * force it into the database's default tablespace, so that it will not
1479  * pose a threat to possible tablespace drop attempts.
1480  */
1481  if (numTempTableSpaces > 0 && !interXact)
1482  {
1483  Oid tblspcOid = GetNextTempTableSpace();
1484 
1485  if (OidIsValid(tblspcOid))
1486  file = OpenTemporaryFileInTablespace(tblspcOid, false);
1487  }
1488 
1489  /*
1490  * If not, or if tablespace is bad, create in database's default
1491  * tablespace. MyDatabaseTableSpace should normally be set before we get
1492  * here, but just in case it isn't, fall back to pg_default tablespace.
1493  */
1494  if (file <= 0)
1497  DEFAULTTABLESPACE_OID,
1498  true);
1499 
1500  /* Mark it for deletion at close and temporary file size limit */
1502 
1503  /* Register it with the current resource owner */
1504  if (!interXact)
1505  RegisterTemporaryFile(file);
1506 
1507  return file;
1508 }
static File OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError)
Definition: fd.c:1539
#define FD_TEMP_FILE_LIMIT
Definition: fd.c:174
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
#define FD_DELETE_AT_CLOSE
Definition: fd.c:172
static Vfd * VfdCache
Definition: fd.c:196
static int numTempTableSpaces
Definition: fd.c:257
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:639
Oid MyDatabaseTableSpace
Definition: globals.c:87
Oid GetNextTempTableSpace(void)
Definition: fd.c:2726
unsigned short fdstate
Definition: fd.c:179
static void RegisterTemporaryFile(File file)
Definition: fd.c:1292
void ResourceOwnerEnlargeFiles(ResourceOwner owner)
Definition: resowner.c:1233
int File
Definition: fd.h:45

◆ OpenTransientFile()

◆ OpenTransientFilePerm()

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

Definition at line 2263 of file fd.c.

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

Referenced by be_lo_export(), and OpenTransientFile().

2264 {
2265  int fd;
2266 
2267  DO_DB(elog(LOG, "OpenTransientFile: Allocated %d (%s)",
2268  numAllocatedDescs, fileName));
2269 
2270  /* Can we allocate another non-virtual FD? */
2271  if (!reserveAllocatedDesc())
2272  ereport(ERROR,
2273  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2274  errmsg("exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2275  maxAllocatedDescs, fileName)));
2276 
2277  /* Close excess kernel FDs. */
2278  ReleaseLruFiles();
2279 
2280  fd = BasicOpenFilePerm(fileName, fileFlags, fileMode);
2281 
2282  if (fd >= 0)
2283  {
2285 
2286  desc->kind = AllocateDescRawFD;
2287  desc->desc.fd = fd;
2290 
2291  return fd;
2292  }
2293 
2294  return -1; /* failure */
2295 }
static AllocateDesc * allocatedDescs
Definition: fd.c:244
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:160
int errcode(int sqlerrcode)
Definition: elog.c:608
static bool reserveAllocatedDesc(void)
Definition: fd.c:2131
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:232
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
static void ReleaseLruFiles(void)
Definition: fd.c:1149
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:707
SubTransactionId create_subid
Definition: fd.c:233
int fd
Definition: fd.c:238
int errmsg(const char *fmt,...)
Definition: elog.c:822
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:965
#define elog(elevel,...)
Definition: elog.h:228
static int maxAllocatedDescs
Definition: fd.c:243
static int numAllocatedDescs
Definition: fd.c:242

◆ PathNameCreateTemporaryDir()

void PathNameCreateTemporaryDir ( const char *  base,
const char *  name 
)

Definition at line 1397 of file fd.c.

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

Referenced by SharedFileSetCreate().

1398 {
1399  if (MakePGDirectory(directory) < 0)
1400  {
1401  if (errno == EEXIST)
1402  return;
1403 
1404  /*
1405  * Failed. Try to create basedir first in case it's missing. Tolerate
1406  * EEXIST to close a race against another process following the same
1407  * algorithm.
1408  */
1409  if (MakePGDirectory(basedir) < 0 && errno != EEXIST)
1410  ereport(ERROR,
1412  errmsg("cannot create temporary directory \"%s\": %m",
1413  basedir)));
1414 
1415  /* Try again. */
1416  if (MakePGDirectory(directory) < 0 && errno != EEXIST)
1417  ereport(ERROR,
1419  errmsg("cannot create temporary subdirectory \"%s\": %m",
1420  directory)));
1421  }
1422 }
static char * basedir
Definition: pg_basebackup.c:82
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3458
static const char * directory
Definition: zic.c:622
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ PathNameCreateTemporaryFile()

File PathNameCreateTemporaryFile ( const char *  name,
bool  error_on_failure 
)

Definition at line 1596 of file fd.c.

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

Referenced by SharedFileSetCreate().

1597 {
1598  File file;
1599 
1601 
1602  /*
1603  * Open the file. Note: we don't use O_EXCL, in case there is an orphaned
1604  * temp file that can be reused.
1605  */
1606  file = PathNameOpenFile(path, O_RDWR | O_CREAT | O_TRUNC | PG_BINARY);
1607  if (file <= 0)
1608  {
1609  if (error_on_failure)
1610  ereport(ERROR,
1612  errmsg("could not create temporary file \"%s\": %m",
1613  path)));
1614  else
1615  return file;
1616  }
1617 
1618  /* Mark it for temp_file_limit accounting. */
1620 
1621  /* Register it for automatic close. */
1622  RegisterTemporaryFile(file);
1623 
1624  return file;
1625 }
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1320
#define FD_TEMP_FILE_LIMIT
Definition: fd.c:174
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
static Vfd * VfdCache
Definition: fd.c:196
#define PG_BINARY
Definition: c.h:1216
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:631
unsigned short fdstate
Definition: fd.c:179
#define ereport(elevel, rest)
Definition: elog.h:141
static void RegisterTemporaryFile(File file)
Definition: fd.c:1292
int errmsg(const char *fmt,...)
Definition: elog.c:822
void ResourceOwnerEnlargeFiles(ResourceOwner owner)
Definition: resowner.c:1233
int File
Definition: fd.h:45

◆ PathNameDeleteTemporaryDir()

void PathNameDeleteTemporaryDir ( const char *  name)

Definition at line 1428 of file fd.c.

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

Referenced by SharedFileSetDeleteAll().

1429 {
1430  struct stat statbuf;
1431 
1432  /* Silently ignore missing directory. */
1433  if (stat(dirname, &statbuf) != 0 && errno == ENOENT)
1434  return;
1435 
1436  /*
1437  * Currently, walkdir doesn't offer a way for our passed in function to
1438  * maintain state. Perhaps it should, so that we could tell the caller
1439  * whether this operation succeeded or failed. Since this operation is
1440  * used in a cleanup path, we wouldn't actually behave differently: we'll
1441  * just log failures.
1442  */
1443  walkdir(dirname, unlink_if_exists_fname, false, LOG);
1444 }
static void walkdir(const char *path, void(*action)(const char *fname, bool isdir, int elevel), bool process_symlinks, int elevel)
Definition: fd.c:3207
#define LOG
Definition: elog.h:26
static void unlink_if_exists_fname(const char *fname, bool isdir, int elevel)
Definition: fd.c:3317
#define stat(a, b)
Definition: win32_port.h:255

◆ PathNameDeleteTemporaryFile()

bool PathNameDeleteTemporaryFile ( const char *  name,
bool  error_on_failure 
)

Definition at line 1664 of file fd.c.

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

Referenced by SharedFileSetDelete(), and unlink_if_exists_fname().

1665 {
1666  struct stat filestats;
1667  int stat_errno;
1668 
1669  /* Get the final size for pgstat reporting. */
1670  if (stat(path, &filestats) != 0)
1671  stat_errno = errno;
1672  else
1673  stat_errno = 0;
1674 
1675  /*
1676  * Unlike FileClose's automatic file deletion code, we tolerate
1677  * non-existence to support BufFileDeleteShared which doesn't know how
1678  * many segments it has to delete until it runs out.
1679  */
1680  if (stat_errno == ENOENT)
1681  return false;
1682 
1683  if (unlink(path) < 0)
1684  {
1685  if (errno != ENOENT)
1686  ereport(error_on_failure ? ERROR : LOG,
1688  errmsg("could not unlink temporary file \"%s\": %m",
1689  path)));
1690  return false;
1691  }
1692 
1693  if (stat_errno == 0)
1694  ReportTemporaryFileUsage(path, filestats.st_size);
1695  else
1696  {
1697  errno = stat_errno;
1698  ereport(LOG,
1700  errmsg("could not stat file \"%s\": %m", path)));
1701  }
1702 
1703  return true;
1704 }
#define LOG
Definition: elog.h:26
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
static void ReportTemporaryFileUsage(const char *path, off_t size)
Definition: fd.c:1273
#define stat(a, b)
Definition: win32_port.h:255
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ PathNameOpenFile()

File PathNameOpenFile ( const char *  fileName,
int  fileFlags 
)

Definition at line 1320 of file fd.c.

References PathNameOpenFilePerm(), and pg_file_create_mode.

Referenced by _mdfd_openseg(), logical_rewrite_log_mapping(), mdcreate(), mdopenfork(), OpenTemporaryFileInTablespace(), PathNameCreateTemporaryFile(), and PathNameOpenTemporaryFile().

1321 {
1322  return PathNameOpenFilePerm(fileName, fileFlags, pg_file_create_mode);
1323 }
File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1333
int pg_file_create_mode
Definition: file_perm.c:19

◆ PathNameOpenFilePerm()

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

Definition at line 1333 of file fd.c.

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, ReleaseLruFiles(), and vfd::resowner.

Referenced by PathNameOpenFile().

1334 {
1335  char *fnamecopy;
1336  File file;
1337  Vfd *vfdP;
1338 
1339  DO_DB(elog(LOG, "PathNameOpenFilePerm: %s %x %o",
1340  fileName, fileFlags, fileMode));
1341 
1342  /*
1343  * We need a malloc'd copy of the file name; fail cleanly if no room.
1344  */
1345  fnamecopy = strdup(fileName);
1346  if (fnamecopy == NULL)
1347  ereport(ERROR,
1348  (errcode(ERRCODE_OUT_OF_MEMORY),
1349  errmsg("out of memory")));
1350 
1351  file = AllocateVfd();
1352  vfdP = &VfdCache[file];
1353 
1354  /* Close excess kernel FDs. */
1355  ReleaseLruFiles();
1356 
1357  vfdP->fd = BasicOpenFilePerm(fileName, fileFlags, fileMode);
1358 
1359  if (vfdP->fd < 0)
1360  {
1361  int save_errno = errno;
1362 
1363  FreeVfd(file);
1364  free(fnamecopy);
1365  errno = save_errno;
1366  return -1;
1367  }
1368  ++nfile;
1369  DO_DB(elog(LOG, "PathNameOpenFile: success %d",
1370  vfdP->fd));
1371 
1372  Insert(file);
1373 
1374  vfdP->fileName = fnamecopy;
1375  /* Saved flags are adjusted to be OK for re-opening file */
1376  vfdP->fileFlags = fileFlags & ~(O_CREAT | O_TRUNC | O_EXCL);
1377  vfdP->fileMode = fileMode;
1378  vfdP->fileSize = 0;
1379  vfdP->fdstate = 0x0;
1380  vfdP->resowner = NULL;
1381 
1382  return file;
1383 }
#define DO_DB(A)
Definition: fd.c:160
static Vfd * VfdCache
Definition: fd.c:196
int errcode(int sqlerrcode)
Definition: elog.c:608
#define LOG
Definition: elog.h:26
mode_t fileMode
Definition: fd.c:188
#define ERROR
Definition: elog.h:43
char * fileName
Definition: fd.c:185
static int nfile
Definition: fd.c:202
static File AllocateVfd(void)
Definition: fd.c:1159
unsigned short fdstate
Definition: fd.c:179
Definition: fd.c:176
off_t fileSize
Definition: fd.c:184
int fd
Definition: fd.c:178
#define ereport(elevel, rest)
Definition: elog.h:141
static void Insert(File file)
Definition: fd.c:1058
ResourceOwner resowner
Definition: fd.c:180
static void ReleaseLruFiles(void)
Definition: fd.c:1149
#define free(a)
Definition: header.h:65
int errmsg(const char *fmt,...)
Definition: elog.c:822
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:965
#define elog(elevel,...)
Definition: elog.h:228
static void FreeVfd(File file)
Definition: fd.c:1217
int fileFlags
Definition: fd.c:187
int File
Definition: fd.h:45

◆ PathNameOpenTemporaryFile()

File PathNameOpenTemporaryFile ( const char *  name)

Definition at line 1634 of file fd.c.

References CurrentResourceOwner, ereport, errcode_for_file_access(), errmsg(), ERROR, PathNameOpenFile(), PG_BINARY, RegisterTemporaryFile(), and ResourceOwnerEnlargeFiles().

Referenced by SharedFileSetOpen().

1635 {
1636  File file;
1637 
1639 
1640  /* We open the file read-only. */
1641  file = PathNameOpenFile(path, O_RDONLY | PG_BINARY);
1642 
1643  /* If no such file, then we don't raise an error. */
1644  if (file <= 0 && errno != ENOENT)
1645  ereport(ERROR,
1647  errmsg("could not open temporary file \"%s\": %m",
1648  path)));
1649 
1650  if (file > 0)
1651  {
1652  /* Register it for automatic close. */
1653  RegisterTemporaryFile(file);
1654  }
1655 
1656  return file;
1657 }
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1320
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
#define PG_BINARY
Definition: c.h:1216
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
static void RegisterTemporaryFile(File file)
Definition: fd.c:1292
int errmsg(const char *fmt,...)
Definition: elog.c:822
void ResourceOwnerEnlargeFiles(ResourceOwner owner)
Definition: resowner.c:1233
int File
Definition: fd.h:45

◆ pg_fdatasync()

int pg_fdatasync ( int  fd)

Definition at line 382 of file fd.c.

References enableFsync, and fsync.

Referenced by issue_xlog_fsync().

383 {
384  if (enableFsync)
385  {
386 #ifdef HAVE_FDATASYNC
387  return fdatasync(fd);
388 #else
389  return fsync(fd);
390 #endif
391  }
392  else
393  return 0;
394 }
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define fsync(fd)
Definition: win32_port.h:63
bool enableFsync
Definition: globals.c:119

◆ pg_flush_data()

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

Definition at line 402 of file fd.c.

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

Referenced by copy_file(), FileWriteback(), and walkdir().

403 {
404  /*
405  * Right now file flushing is primarily used to avoid making later
406  * fsync()/fdatasync() calls have less impact. Thus don't trigger flushes
407  * if fsyncs are disabled - that's a decision we might want to make
408  * configurable at some point.
409  */
410  if (!enableFsync)
411  return;
412 
413  /*
414  * We compile all alternatives that are supported on the current platform,
415  * to find portability problems more easily.
416  */
417 #if defined(HAVE_SYNC_FILE_RANGE)
418  {
419  int rc;
420  static bool not_implemented_by_kernel = false;
421 
422  if (not_implemented_by_kernel)
423  return;
424 
425  /*
426  * sync_file_range(SYNC_FILE_RANGE_WRITE), currently linux specific,
427  * tells the OS that writeback for the specified blocks should be
428  * started, but that we don't want to wait for completion. Note that
429  * this call might block if too much dirty data exists in the range.
430  * This is the preferable method on OSs supporting it, as it works
431  * reliably when available (contrast to msync()) and doesn't flush out
432  * clean data (like FADV_DONTNEED).
433  */
434  rc = sync_file_range(fd, offset, nbytes,
435  SYNC_FILE_RANGE_WRITE);
436  if (rc != 0)
437  {
438  int elevel;
439 
440  /*
441  * For systems that don't have an implementation of
442  * sync_file_range() such as Windows WSL, generate only one
443  * warning and then suppress all further attempts by this process.
444  */
445  if (errno == ENOSYS)
446  {
447  elevel = WARNING;
448  not_implemented_by_kernel = true;
449  }
450  else
451  elevel = data_sync_elevel(WARNING);
452 
453  ereport(elevel,
455  errmsg("could not flush dirty data: %m")));
456  }
457 
458  return;
459  }
460 #endif
461 #if !defined(WIN32) && defined(MS_ASYNC)
462  {
463  void *p;
464  static int pagesize = 0;
465 
466  /*
467  * On several OSs msync(MS_ASYNC) on a mmap'ed file triggers
468  * writeback. On linux it only does so if MS_SYNC is specified, but
469  * then it does the writeback synchronously. Luckily all common linux
470  * systems have sync_file_range(). This is preferable over
471  * FADV_DONTNEED because it doesn't flush out clean data.
472  *
473  * We map the file (mmap()), tell the kernel to sync back the contents
474  * (msync()), and then remove the mapping again (munmap()).
475  */
476 
477  /* mmap() needs actual length if we want to map whole file */
478  if (offset == 0 && nbytes == 0)
479  {
480  nbytes = lseek(fd, 0, SEEK_END);
481  if (nbytes < 0)
482  {
485  errmsg("could not determine dirty data size: %m")));
486  return;
487  }
488  }
489 
490  /*
491  * Some platforms reject partial-page mmap() attempts. To deal with
492  * that, just truncate the request to a page boundary. If any extra
493  * bytes don't get flushed, well, it's only a hint anyway.
494  */
495 
496  /* fetch pagesize only once */
497  if (pagesize == 0)
498  pagesize = sysconf(_SC_PAGESIZE);
499 
500  /* align length to pagesize, dropping any fractional page */
501  if (pagesize > 0)
502  nbytes = (nbytes / pagesize) * pagesize;
503 
504  /* fractional-page request is a no-op */
505  if (nbytes <= 0)
506  return;
507 
508  /*
509  * mmap could well fail, particularly on 32-bit platforms where there
510  * may simply not be enough address space. If so, silently fall
511  * through to the next implementation.
512  */
513  if (nbytes <= (off_t) SSIZE_MAX)
514  p = mmap(NULL, nbytes, PROT_READ, MAP_SHARED, fd, offset);
515  else
516  p = MAP_FAILED;
517 
518  if (p != MAP_FAILED)
519  {
520  int rc;
521 
522  rc = msync(p, (size_t) nbytes, MS_ASYNC);
523  if (rc != 0)
524  {
527  errmsg("could not flush dirty data: %m")));
528  /* NB: need to fall through to munmap()! */
529  }
530 
531  rc = munmap(p, (size_t) nbytes);
532  if (rc != 0)
533  {
534  /* FATAL error because mapping would remain */
535  ereport(FATAL,
537  errmsg("could not munmap() while flushing data: %m")));
538  }
539 
540  return;
541  }
542  }
543 #endif
544 #if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
545  {
546  int rc;
547 
548  /*
549  * Signal the kernel that the passed in range should not be cached
550  * anymore. This has the, desired, side effect of writing out dirty
551  * data, and the, undesired, side effect of likely discarding useful
552  * clean cached blocks. For the latter reason this is the least
553  * preferable method.
554  */
555 
556  rc = posix_fadvise(fd, offset, nbytes, POSIX_FADV_DONTNEED);
557 
558  if (rc != 0)
559  {
560  /* don't error out, this is just a performance optimization */
563  errmsg("could not flush dirty data: %m")));
564  }
565 
566  return;
567  }
568 #endif
569 }
#define MAP_FAILED
Definition: mem.h:45
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define FATAL
Definition: elog.h:52
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
static int elevel
Definition: vacuumlazy.c:143
int data_sync_elevel(int elevel)
Definition: fd.c:3481
bool enableFsync
Definition: globals.c:119
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_fsync()

int pg_fsync ( int  fd)

Definition at line 330 of file fd.c.

References pg_fsync_no_writethrough(), pg_fsync_writethrough(), sync_method, and SYNC_METHOD_FSYNC_WRITETHROUGH.

Referenced by AddToDataDirLockFile(), assign_xlog_sync_method(), BootStrapXLOG(), CheckPointLogicalRewriteHeap(), CreateLockFile(), do_pg_start_backup(), durable_rename(), FileSync(), fsync_fname_ext(), heap_xlog_logical_rewrite(), readRecoverySignalFile(), RecreateTwoPhaseFile(), RestoreSlotFromDisk(), SaveSlotToPath(), SimpleLruFlush(), SlruPhysicalWritePage(), SnapBuildSerialize(), update_controlfile(), write_auto_conf_file(), write_relmap_file(), WriteControlFile(), writeTimeLineHistory(), writeTimeLineHistoryFile(), XLogFileCopy(), and XLogFileInit().

331 {
332  /* #if is to skip the sync_method test if there's no need for it */
333 #if defined(HAVE_FSYNC_WRITETHROUGH) && !defined(FSYNC_WRITETHROUGH_IS_FSYNC)
335  return pg_fsync_writethrough(fd);
336  else
337 #endif
339 }
#define SYNC_METHOD_FSYNC_WRITETHROUGH
Definition: xlog.h:28
int pg_fsync_writethrough(int fd)
Definition: fd.c:359
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:347
static int fd(const char *x, int i)
Definition: preproc-init.c:105
int sync_method
Definition: xlog.c:102

◆ pg_fsync_no_writethrough()

int pg_fsync_no_writethrough ( int  fd)

Definition at line 347 of file fd.c.

References enableFsync, and fsync.

Referenced by issue_xlog_fsync(), and pg_fsync().

348 {
349  if (enableFsync)
350  return fsync(fd);
351  else
352  return 0;
353 }
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define fsync(fd)
Definition: win32_port.h:63
bool enableFsync
Definition: globals.c:119

◆ pg_fsync_writethrough()

int pg_fsync_writethrough ( int  fd)

Definition at line 359 of file fd.c.

References enableFsync.

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

360 {
361  if (enableFsync)
362  {
363 #ifdef WIN32
364  return _commit(fd);
365 #elif defined(F_FULLFSYNC)
366  return (fcntl(fd, F_FULLFSYNC, 0) == -1) ? -1 : 0;
367 #else
368  errno = ENOSYS;
369  return -1;
370 #endif
371  }
372  else
373  return 0;
374 }
static int fd(const char *x, int i)
Definition: preproc-init.c:105
bool enableFsync
Definition: globals.c:119

◆ ReadDir()

◆ ReadDirExtended()

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

Definition at line 2546 of file fd.c.

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

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

2547 {
2548  struct dirent *dent;
2549 
2550  /* Give a generic message for AllocateDir failure, if caller didn't */
2551  if (dir == NULL)
2552  {
2553  ereport(elevel,
2555  errmsg("could not open directory \"%s\": %m",
2556  dirname)));
2557  return NULL;
2558  }
2559 
2560  errno = 0;
2561  if ((dent = readdir(dir)) != NULL)
2562  return dent;
2563 
2564  if (errno)
2565  ereport(elevel,
2567  errmsg("could not read directory \"%s\": %m",
2568  dirname)));
2569  return NULL;
2570 }
Definition: dirent.h:9
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
static int elevel
Definition: vacuumlazy.c:143
struct dirent * readdir(DIR *)
Definition: dirent.c:77
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ RemovePgTempFiles()

void RemovePgTempFiles ( void  )

Definition at line 2882 of file fd.c.

References AllocateDir(), dirent::d_name, FreeDir(), LOG, MAXPGPATH, PG_TEMP_FILES_DIR, ReadDirExtended(), RemovePgTempFilesInDir(), RemovePgTempRelationFiles(), snprintf, and TABLESPACE_VERSION_DIRECTORY.

Referenced by PostmasterMain().

2883 {
2884  char temp_path[MAXPGPATH + 10 + sizeof(TABLESPACE_VERSION_DIRECTORY) + sizeof(PG_TEMP_FILES_DIR)];
2885  DIR *spc_dir;
2886  struct dirent *spc_de;
2887 
2888  /*
2889  * First process temp files in pg_default ($PGDATA/base)
2890  */
2891  snprintf(temp_path, sizeof(temp_path), "base/%s", PG_TEMP_FILES_DIR);
2892  RemovePgTempFilesInDir(temp_path, true, false);
2893  RemovePgTempRelationFiles("base");
2894 
2895  /*
2896  * Cycle through temp directories for all non-default tablespaces.
2897  */
2898  spc_dir = AllocateDir("pg_tblspc");
2899 
2900  while ((spc_de = ReadDirExtended(spc_dir, "pg_tblspc", LOG)) != NULL)
2901  {
2902  if (strcmp(spc_de->d_name, ".") == 0 ||
2903  strcmp(spc_de->d_name, "..") == 0)
2904  continue;
2905 
2906  snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s/%s",
2908  RemovePgTempFilesInDir(temp_path, true, false);
2909 
2910  snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s",
2912  RemovePgTempRelationFiles(temp_path);
2913  }
2914 
2915  FreeDir(spc_dir);
2916 
2917  /*
2918  * In EXEC_BACKEND case there is a pgsql_tmp directory at the top level of
2919  * DataDir as well. However, that is *not* cleaned here because doing so
2920  * would create a race condition. It's done separately, earlier in
2921  * postmaster startup.
2922  */
2923 }
struct dirent * ReadDirExtended(DIR *dir, const char *dirname, int elevel)
Definition: fd.c:2546
#define PG_TEMP_FILES_DIR
Definition: pg_checksums.c:58
#define LOG
Definition: elog.h:26
Definition: dirent.h:9
static void RemovePgTempRelationFiles(const char *tsdirname)
Definition: fd.c:3007
Definition: dirent.c:25
#define MAXPGPATH
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:26
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
Definition: fd.c:2941
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2465
char d_name[MAX_PATH]
Definition: dirent.h:14
#define snprintf
Definition: port.h:192
int FreeDir(DIR *dir)
Definition: fd.c:2583

◆ RemovePgTempFilesInDir()

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

Definition at line 2941 of file fd.c.

References AllocateDir(), dirent::d_name, ereport, errcode_for_file_access(), errmsg(), FreeDir(), LOG, lstat, MAXPGPATH, PG_TEMP_FILE_PREFIX, ReadDirExtended(), RemovePgTempFilesInDir(), S_ISDIR, snprintf, and stat.

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

2942 {
2943  DIR *temp_dir;
2944  struct dirent *temp_de;
2945  char rm_path[MAXPGPATH * 2];
2946 
2947  temp_dir = AllocateDir(tmpdirname);
2948 
2949  if (temp_dir == NULL && errno == ENOENT && missing_ok)
2950  return;
2951 
2952  while ((temp_de = ReadDirExtended(temp_dir, tmpdirname, LOG)) != NULL)
2953  {
2954  if (strcmp(temp_de->d_name, ".") == 0 ||
2955  strcmp(temp_de->d_name, "..") == 0)
2956  continue;
2957 
2958  snprintf(rm_path, sizeof(rm_path), "%s/%s",
2959  tmpdirname, temp_de->d_name);
2960 
2961  if (unlink_all ||
2962  strncmp(temp_de->d_name,
2964  strlen(PG_TEMP_FILE_PREFIX)) == 0)
2965  {
2966  struct stat statbuf;
2967 
2968  if (lstat(rm_path, &statbuf) < 0)
2969  {
2970  ereport(LOG,
2972  errmsg("could not stat file \"%s\": %m", rm_path)));
2973  continue;
2974  }
2975 
2976  if (S_ISDIR(statbuf.st_mode))
2977  {
2978  /* recursively remove contents, then directory itself */
2979  RemovePgTempFilesInDir(rm_path, false, true);
2980 
2981  if (rmdir(rm_path) < 0)
2982  ereport(LOG,
2984  errmsg("could not remove directory \"%s\": %m",
2985  rm_path)));
2986  }
2987  else
2988  {
2989  if (unlink(rm_path) < 0)
2990  ereport(LOG,
2992  errmsg("could not remove file \"%s\": %m",
2993  rm_path)));
2994  }
2995  }
2996  else
2997  ereport(LOG,
2998  (errmsg("unexpected file found in temporary-files directory: \"%s\"",
2999  rm_path)));
3000  }
3001 
3002  FreeDir(temp_dir);
3003 }
struct dirent * ReadDirExtended(DIR *dir, const char *dirname, int elevel)
Definition: fd.c:2546
#define LOG
Definition: elog.h:26
Definition: dirent.h:9
Definition: dirent.c:25
#define PG_TEMP_FILE_PREFIX
Definition: pg_checksums.c:59
#define MAXPGPATH
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
Definition: fd.c:2941
int errcode_for_file_access(void)
Definition: elog.c:631
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2465
#define ereport(elevel, rest)
Definition: elog.h:141
#define stat(a, b)
Definition: win32_port.h:255
#define S_ISDIR(m)
Definition: win32_port.h:296
#define lstat(path, sb)
Definition: win32_port.h:244
int errmsg(const char *fmt,...)
Definition: elog.c:822
char d_name[MAX_PATH]
Definition: dirent.h:14
#define snprintf
Definition: port.h:192
int FreeDir(DIR *dir)
Definition: fd.c:2583

◆ set_max_safe_fds()

void set_max_safe_fds ( void  )

Definition at line 900 of file fd.c.

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

901 {
902  int usable_fds;
903  int already_open;
904 
905  /*----------
906  * We want to set max_safe_fds to
907  * MIN(usable_fds, max_files_per_process - already_open)
908  * less the slop factor for files that are opened without consulting
909  * fd.c. This ensures that we won't exceed either max_files_per_process
910  * or the experimentally-determined EMFILE limit.
911  *----------
912  */
914  &usable_fds, &already_open);
915 
916  max_safe_fds = Min(usable_fds, max_files_per_process - already_open);
917 
918  /*
919  * Take off the FDs reserved for system() etc.
920  */
922 
923  /*
924  * Make sure we still have enough to get by.
925  */
926  if (max_safe_fds < FD_MINFREE)
927  ereport(FATAL,
928  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
929  errmsg("insufficient file descriptors available to start server process"),
930  errdetail("System allows %d, we need at least %d.",
933 
934  elog(DEBUG2, "max_safe_fds = %d, usable_fds = %d, already_open = %d",
935  max_safe_fds, usable_fds, already_open);
936 }
static void count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
Definition: fd.c:816
#define NUM_RESERVED_FDS
Definition: fd.c:118
int max_safe_fds
Definition: fd.c:145
#define Min(x, y)
Definition: c.h:905
int errcode(int sqlerrcode)
Definition: elog.c:608
#define FATAL
Definition: elog.h:52
#define DEBUG2
Definition: elog.h:24
int errdetail(const char *fmt,...)
Definition: elog.c:957
int max_files_per_process
Definition: fd.c:132
#define ereport(elevel, rest)
Definition: elog.h:141
#define FD_MINFREE
Definition: fd.c:124
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ SetTempTablespaces()

void SetTempTablespaces ( Oid tableSpaces,
int  numSpaces 
)

Definition at line 2668 of file fd.c.

References Assert, nextTempTableSpace, numTempTableSpaces, random(), and tempTableSpaces.

Referenced by assign_temp_tablespaces(), and PrepareTempTablespaces().

2669 {
2670  Assert(numSpaces >= 0);
2671  tempTableSpaces = tableSpaces;
2672  numTempTableSpaces = numSpaces;
2673 
2674  /*
2675  * Select a random starting point in the list. This is to minimize
2676  * conflicts between backends that are most likely sharing the same list
2677  * of temp tablespaces. Note that if we create multiple temp files in the
2678  * same transaction, we'll advance circularly through the list --- this
2679  * ensures that large temporary sort files are nicely spread across all
2680  * available tablespaces.
2681  */
2682  if (numSpaces > 1)
2683  nextTempTableSpace = random() % numSpaces;
2684  else
2685  nextTempTableSpace = 0;
2686 }
long random(void)
Definition: random.c:22
static int numTempTableSpaces
Definition: fd.c:257
static int nextTempTableSpace
Definition: fd.c:258
#define Assert(condition)
Definition: c.h:733
static Oid * tempTableSpaces
Definition: fd.c:256

◆ SyncDataDirectory()

void SyncDataDirectory ( void  )

Definition at line 3133 of file fd.c.

References datadir_fsync_fname(), DEBUG1, enableFsync, ereport, errcode_for_file_access(), errmsg(), LOG, lstat, pgwin32_is_junction(), stat, and walkdir().

Referenced by StartupXLOG().

3134 {
3135  bool xlog_is_symlink;
3136 
3137  /* We can skip this whole thing if fsync is disabled. */
3138  if (!enableFsync)
3139  return;
3140 
3141  /*
3142  * If pg_wal is a symlink, we'll need to recurse into it separately,
3143  * because the first walkdir below will ignore it.
3144  */
3145  xlog_is_symlink = false;
3146 
3147 #ifndef WIN32
3148  {
3149  struct stat st;
3150 
3151  if (lstat("pg_wal", &st) < 0)
3152  ereport(LOG,
3154  errmsg("could not stat file \"%s\": %m",
3155  "pg_wal")));
3156  else if (S_ISLNK(st.st_mode))
3157  xlog_is_symlink = true;
3158  }
3159 #else
3160  if (pgwin32_is_junction("pg_wal"))
3161  xlog_is_symlink = true;
3162 #endif
3163 
3164  /*
3165  * If possible, hint to the kernel that we're soon going to fsync the data
3166  * directory and its contents. Errors in this step are even less
3167  * interesting than normal, so log them only at DEBUG1.
3168  */
3169 #ifdef PG_FLUSH_DATA_WORKS
3170  walkdir(".", pre_sync_fname, false, DEBUG1);
3171  if (xlog_is_symlink)
3172  walkdir("pg_wal", pre_sync_fname, false, DEBUG1);
3173  walkdir("pg_tblspc", pre_sync_fname, true, DEBUG1);
3174 #endif
3175 
3176  /*
3177  * Now we do the fsync()s in the same order.
3178  *
3179  * The main call ignores symlinks, so in addition to specially processing
3180  * pg_wal if it's a symlink, pg_tblspc has to be visited separately with
3181  * process_symlinks = true. Note that if there are any plain directories
3182  * in pg_tblspc, they'll get fsync'd twice. That's not an expected case
3183  * so we don't worry about optimizing it.
3184  */
3185  walkdir(".", datadir_fsync_fname, false, LOG);
3186  if (xlog_is_symlink)
3187  walkdir("pg_wal", datadir_fsync_fname, false, LOG);
3188  walkdir("pg_tblspc", datadir_fsync_fname, true, LOG);
3189 }
#define DEBUG1
Definition: elog.h:25
static void walkdir(const char *path, void(*action)(const char *fname, bool isdir, int elevel), bool process_symlinks, int elevel)
Definition: fd.c:3207
#define LOG
Definition: elog.h:26
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
static void datadir_fsync_fname(const char *fname, bool isdir, int elevel)
Definition: fd.c:3307
#define stat(a, b)
Definition: win32_port.h:255
#define lstat(path, sb)
Definition: win32_port.h:244
bool enableFsync
Definition: globals.c:119
int errmsg(const char *fmt,...)
Definition: elog.c:822
bool pgwin32_is_junction(const char *path)

◆ TempTablespacePath()

void TempTablespacePath ( char *  path,
Oid  tablespace 
)

Definition at line 1514 of file fd.c.

References InvalidOid, MAXPGPATH, PG_TEMP_FILES_DIR, snprintf, and TABLESPACE_VERSION_DIRECTORY.

Referenced by OpenTemporaryFileInTablespace(), pg_ls_tmpdir(), SharedFileSetCreate(), and SharedFileSetPath().

1515 {
1516  /*
1517  * Identify the tempfile directory for this tablespace.
1518  *
1519  * If someone tries to specify pg_global, use pg_default instead.
1520  */
1521  if (tablespace == InvalidOid ||
1522  tablespace == DEFAULTTABLESPACE_OID ||
1523  tablespace == GLOBALTABLESPACE_OID)
1524  snprintf(path, MAXPGPATH, "base/%s", PG_TEMP_FILES_DIR);
1525  else
1526  {
1527  /* All other tablespaces are accessed via symlinks */
1528  snprintf(path, MAXPGPATH, "pg_tblspc/%u/%s/%s",
1531  }
1532 }
#define PG_TEMP_FILES_DIR
Definition: pg_checksums.c:58
#define MAXPGPATH
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:26
char * tablespace
Definition: pgbench.c:188
#define InvalidOid
Definition: postgres_ext.h:36
#define snprintf
Definition: port.h:192

◆ TempTablespacesAreSet()

bool TempTablespacesAreSet ( void  )

Definition at line 2696 of file fd.c.

References numTempTableSpaces.

Referenced by GetTempTablespaces(), and PrepareTempTablespaces().

2697 {
2698  return (numTempTableSpaces >= 0);
2699 }
static int numTempTableSpaces
Definition: fd.c:257

Variable Documentation

◆ data_sync_retry

PGDLLIMPORT bool data_sync_retry

Definition at line 148 of file fd.c.

Referenced by data_sync_elevel().

◆ max_files_per_process

PGDLLIMPORT int max_files_per_process

Definition at line 132 of file fd.c.

Referenced by set_max_safe_fds().

◆ max_safe_fds

int max_safe_fds