PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 PG_TEMP_FILES_DIR   "pgsql_tmp"
 
#define PG_TEMP_FILE_PREFIX   "pgsql_tmp"
 

Typedefs

typedef char * FileName
 
typedef int File
 

Functions

File PathNameOpenFile (FileName fileName, int fileFlags, int 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, uint32 wait_event_info)
 
int FileWrite (File file, char *buffer, int amount, uint32 wait_event_info)
 
int FileSync (File file, uint32 wait_event_info)
 
off_t FileSeek (File file, off_t offset, int whence)
 
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)
 
int FileGetRawMode (File file)
 
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)
 
int FreeDir (DIR *dir)
 
int OpenTransientFile (FileName fileName, int fileFlags, int fileMode)
 
int CloseTransientFile (int fd)
 
int BasicOpenFile (FileName fileName, int fileFlags, int fileMode)
 
void InitFileAccess (void)
 
void set_max_safe_fds (void)
 
void closeAllVfds (void)
 
void SetTempTablespaces (Oid *tableSpaces, int numSpaces)
 
bool TempTablespacesAreSet (void)
 
Oid GetNextTempTableSpace (void)
 
void AtEOXact_Files (void)
 
void AtEOSubXact_Files (bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
 
void RemovePgTempFiles (void)
 
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_link_or_rename (const char *oldfile, const char *newfile, int loglevel)
 
void SyncDataDirectory (void)
 

Variables

int max_files_per_process
 
int max_safe_fds
 

Macro Definition Documentation

#define PG_TEMP_FILE_PREFIX   "pgsql_tmp"
#define PG_TEMP_FILES_DIR   "pgsql_tmp"

Definition at line 126 of file fd.h.

Referenced by OpenTemporaryFileInTablespace(), process_source_file(), and RemovePgTempFiles().

Typedef Documentation

Definition at line 51 of file fd.h.

Definition at line 49 of file fd.h.

Function Documentation

DIR* AllocateDir ( const char *  dirname)

Definition at line 2298 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, NULL, 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(), dsm_cleanup_for_mmap(), get_ext_ver_list(), getInstallationPaths(), movedb(), ParseTzFile(), perform_base_backup(), pg_available_extension_versions(), pg_available_extensions(), pg_logdir_ls(), pg_ls_dir(), pg_ls_dir_files(), pg_start_backup(), pg_tablespace_databases(), pg_tzenumerate_next(), pg_tzenumerate_start(), pgarch_readyXlog(), pgstat_reset_remove_files(), PrescanPreparedTransactions(), RecoverPreparedTransactions(), RelationCacheInitFileRemove(), RelationCacheInitFileRemoveInDir(), RemoveNonParentXlogFiles(), RemoveOldXlogFiles(), RemovePgTempFiles(), RemovePgTempFilesInDir(), RemovePgTempRelationFiles(), RemovePgTempRelationFilesInDbspace(), ResetUnloggedRelations(), ResetUnloggedRelationsInDbspaceDir(), ResetUnloggedRelationsInTablespaceDir(), scan_directory_ci(), SendBaseBackup(), sendDir(), SlruScanDirectory(), StandbyRecoverPreparedTransactions(), StartupReorderBuffer(), StartupReplicationSlots(), UpdateLogicalMappings(), and walkdir().

2299 {
2300  DIR *dir;
2301 
2302  DO_DB(elog(LOG, "AllocateDir: Allocated %d (%s)",
2303  numAllocatedDescs, dirname));
2304 
2305  /* Can we allocate another non-virtual FD? */
2306  if (!reserveAllocatedDesc())
2307  ereport(ERROR,
2308  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2309  errmsg("exceeded maxAllocatedDescs (%d) while trying to open directory \"%s\"",
2310  maxAllocatedDescs, dirname)));
2311 
2312  /* Close excess kernel FDs. */
2313  ReleaseLruFiles();
2314 
2315 TryAgain:
2316  if ((dir = opendir(dirname)) != NULL)
2317  {
2319 
2320  desc->kind = AllocateDescDir;
2321  desc->desc.dir = dir;
2324  return desc->desc.dir;
2325  }
2326 
2327  if (errno == EMFILE || errno == ENFILE)
2328  {
2329  int save_errno = errno;
2330 
2331  ereport(LOG,
2332  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2333  errmsg("out of file descriptors: %m; release and retry")));
2334  errno = 0;
2335  if (ReleaseLruFile())
2336  goto TryAgain;
2337  errno = save_errno;
2338  }
2339 
2340  return NULL;
2341 }
static AllocateDesc * allocatedDescs
Definition: fd.c:245
union AllocateDesc::@26 desc
DIR * dir
Definition: fd.c:238
#define DO_DB(A)
Definition: fd.c:152
int errcode(int sqlerrcode)
Definition: elog.c:575
static bool reserveAllocatedDesc(void)
Definition: fd.c:1984
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:233
static bool ReleaseLruFile(void)
Definition: fd.c:1103
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:122
static void ReleaseLruFiles(void)
Definition: fd.c:1125
#define NULL
Definition: c.h:229
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:649
SubTransactionId create_subid
Definition: fd.c:234
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static int maxAllocatedDescs
Definition: fd.c:244
static int numAllocatedDescs
Definition: fd.c:243
FILE* AllocateFile ( const char *  name,
const char *  mode 
)

Definition at line 2057 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, NULL, numAllocatedDescs, ReleaseLruFile(), ReleaseLruFiles(), and reserveAllocatedDesc().

Referenced by AlterSystemSetConfigFile(), BeginCopyFrom(), BeginCopyTo(), checkDataDir(), do_pg_start_backup(), do_pg_stop_backup(), entry_reset(), existsTimeLineHistory(), ExportSnapshot(), fill_hba_view(), gc_qtexts(), ImportSnapshot(), 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(), 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(), read_backup_label(), read_binary_file(), read_tablespace_map(), read_whole_file(), readRecoveryCommandFile(), readTimeLineHistory(), sendFile(), tokenize_inc_file(), tsearch_readline_begin(), ValidatePgVersion(), write_relcache_init_file(), XLogArchiveForceDone(), and XLogArchiveNotify().

2058 {
2059  FILE *file;
2060 
2061  DO_DB(elog(LOG, "AllocateFile: Allocated %d (%s)",
2063 
2064  /* Can we allocate another non-virtual FD? */
2065  if (!reserveAllocatedDesc())
2066  ereport(ERROR,
2067  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2068  errmsg("exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2069  maxAllocatedDescs, name)));
2070 
2071  /* Close excess kernel FDs. */
2072  ReleaseLruFiles();
2073 
2074 TryAgain:
2075  if ((file = fopen(name, mode)) != NULL)
2076  {
2078 
2079  desc->kind = AllocateDescFile;
2080  desc->desc.file = file;
2083  return desc->desc.file;
2084  }
2085 
2086  if (errno == EMFILE || errno == ENFILE)
2087  {
2088  int save_errno = errno;
2089 
2090  ereport(LOG,
2091  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2092  errmsg("out of file descriptors: %m; release and retry")));
2093  errno = 0;
2094  if (ReleaseLruFile())
2095  goto TryAgain;
2096  errno = save_errno;
2097  }
2098 
2099  return NULL;
2100 }
static AllocateDesc * allocatedDescs
Definition: fd.c:245
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:152
int errcode(int sqlerrcode)
Definition: elog.c:575
static bool reserveAllocatedDesc(void)
Definition: fd.c:1984
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:233
static bool ReleaseLruFile(void)
Definition: fd.c:1103
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static void ReleaseLruFiles(void)
Definition: fd.c:1125
FILE * file
Definition: fd.c:237
#define NULL
Definition: c.h:229
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:649
SubTransactionId create_subid
Definition: fd.c:234
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static int maxAllocatedDescs
Definition: fd.c:244
static int numAllocatedDescs
Definition: fd.c:243
void AtEOSubXact_Files ( bool  isCommit,
SubTransactionId  mySubid,
SubTransactionId  parentSubid 
)

Definition at line 2549 of file fd.c.

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

2551 {
2552  Index i;
2553 
2554  for (i = 0; i < numAllocatedDescs; i++)
2555  {
2556  if (allocatedDescs[i].create_subid == mySubid)
2557  {
2558  if (isCommit)
2559  allocatedDescs[i].create_subid = parentSubid;
2560  else
2561  {
2562  /* have to recheck the item after FreeDesc (ugly) */
2563  FreeDesc(&allocatedDescs[i--]);
2564  }
2565  }
2566  }
2567 }
static AllocateDesc * allocatedDescs
Definition: fd.c:245
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2201
unsigned int Index
Definition: c.h:365
SubTransactionId create_subid
Definition: fd.c:234
int i
static int numAllocatedDescs
Definition: fd.c:243
void AtEOXact_Files ( void  )

Definition at line 2580 of file fd.c.

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

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

2581 {
2582  CleanupTempFiles(false);
2584  numTempTableSpaces = -1;
2585 }
static int numTempTableSpaces
Definition: fd.c:258
static void CleanupTempFiles(bool isProcExit)
Definition: fd.c:2609
#define NULL
Definition: c.h:229
static Oid * tempTableSpaces
Definition: fd.c:257
int BasicOpenFile ( FileName  fileName,
int  fileFlags,
int  fileMode 
)

Definition at line 899 of file fd.c.

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

Referenced by AlterSystemSetConfigFile(), GetNewRelFileNode(), LruInsert(), OpenTransientFile(), PathNameOpenFile(), ReadControlFile(), UpdateControlFile(), WriteControlFile(), XLogFileInit(), XLogFileOpen(), XLogFileRead(), and XLogRead().

900 {
901  int fd;
902 
903 tryAgain:
904  fd = open(fileName, fileFlags, fileMode);
905 
906  if (fd >= 0)
907  return fd; /* success! */
908 
909  if (errno == EMFILE || errno == ENFILE)
910  {
911  int save_errno = errno;
912 
913  ereport(LOG,
914  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
915  errmsg("out of file descriptors: %m; release and retry")));
916  errno = 0;
917  if (ReleaseLruFile())
918  goto tryAgain;
919  errno = save_errno;
920  }
921 
922  return -1; /* failure */
923 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#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:1103
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
void closeAllVfds ( void  )

Definition at line 2462 of file fd.c.

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

Referenced by standard_ProcessUtility().

2463 {
2464  Index i;
2465 
2466  if (SizeVfdCache > 0)
2467  {
2468  Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
2469  for (i = 1; i < SizeVfdCache; i++)
2470  {
2471  if (!FileIsNotOpen(i))
2472  LruDelete(i);
2473  }
2474  }
2475 }
static Size SizeVfdCache
Definition: fd.c:198
static void LruDelete(File file)
Definition: fd.c:966
#define FileIsNotOpen(file)
Definition: fd.c:161
unsigned int Index
Definition: c.h:365
#define Assert(condition)
Definition: c.h:675
int i
int ClosePipeStream ( FILE *  file)

Definition at line 2433 of file fd.c.

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

Referenced by ClosePipeToProgram(), and pg_import_system_collations().

2434 {
2435  int i;
2436 
2437  DO_DB(elog(LOG, "ClosePipeStream: Allocated %d", numAllocatedDescs));
2438 
2439  /* Remove file from list of allocated files, if it's present */
2440  for (i = numAllocatedDescs; --i >= 0;)
2441  {
2442  AllocateDesc *desc = &allocatedDescs[i];
2443 
2444  if (desc->kind == AllocateDescPipe && desc->desc.file == file)
2445  return FreeDesc(desc);
2446  }
2447 
2448  /* Only get here if someone passes us a file not in allocatedDescs */
2449  elog(WARNING, "file passed to ClosePipeStream was not obtained from OpenPipeStream");
2450 
2451  return pclose(file);
2452 }
static AllocateDesc * allocatedDescs
Definition: fd.c:245
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:152
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:233
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2201
#define WARNING
Definition: elog.h:40
FILE * file
Definition: fd.c:237
int i
#define elog
Definition: elog.h:219
static int numAllocatedDescs
Definition: fd.c:243
int CloseTransientFile ( int  fd)

Definition at line 2268 of file fd.c.

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

Referenced by be_lo_export(), CheckPointLogicalRewriteHeap(), CheckPointReplicationOrigin(), copy_file(), dsm_impl_mmap(), durable_rename(), fsync_fname_ext(), 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(), write_relmap_file(), writeTimeLineHistory(), writeTimeLineHistoryFile(), and XLogFileCopy().

2269 {
2270  int i;
2271 
2272  DO_DB(elog(LOG, "CloseTransientFile: Allocated %d", numAllocatedDescs));
2273 
2274  /* Remove fd from list of allocated files, if it's present */
2275  for (i = numAllocatedDescs; --i >= 0;)
2276  {
2277  AllocateDesc *desc = &allocatedDescs[i];
2278 
2279  if (desc->kind == AllocateDescRawFD && desc->desc.fd == fd)
2280  return FreeDesc(desc);
2281  }
2282 
2283  /* Only get here if someone passes us a file not in allocatedDescs */
2284  elog(WARNING, "fd passed to CloseTransientFile was not obtained from OpenTransientFile");
2285 
2286  return close(fd);
2287 }
static AllocateDesc * allocatedDescs
Definition: fd.c:245
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:152
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:233
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2201
#define WARNING
Definition: elog.h:40
int fd
Definition: fd.c:239
int i
#define elog
Definition: elog.h:219
#define close(a)
Definition: win32.h:17
static int numAllocatedDescs
Definition: fd.c:243
int durable_link_or_rename ( const char *  oldfile,
const char *  newfile,
int  loglevel 
)

Definition at line 675 of file fd.c.

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

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

676 {
677  /*
678  * Ensure that, if we crash directly after the rename/link, a file with
679  * valid contents is moved into place.
680  */
681  if (fsync_fname_ext(oldfile, false, false, elevel) != 0)
682  return -1;
683 
684 #if HAVE_WORKING_LINK
685  if (link(oldfile, newfile) < 0)
686  {
687  ereport(elevel,
689  errmsg("could not link file \"%s\" to \"%s\": %m",
690  oldfile, newfile)));
691  return -1;
692  }
693  unlink(oldfile);
694 #else
695  /* XXX: Add racy file existence check? */
696  if (rename(oldfile, newfile) < 0)
697  {
698  ereport(elevel,
700  errmsg("could not rename file \"%s\" to \"%s\": %m",
701  oldfile, newfile)));
702  return -1;
703  }
704 #endif
705 
706  /*
707  * Make change persistent in case of an OS crash, both the new entry and
708  * its parent directory need to be flushed.
709  */
710  if (fsync_fname_ext(newfile, false, false, elevel) != 0)
711  return -1;
712 
713  /* Same for parent directory */
714  if (fsync_parent_path(newfile, elevel) != 0)
715  return -1;
716 
717  return 0;
718 }
int errcode_for_file_access(void)
Definition: elog.c:598
int unlink(const char *filename)
#define ereport(elevel, rest)
Definition: elog.h:122
int link(const char *fromname, const char *toname)
static int elevel
Definition: vacuumlazy.c:137
int errmsg(const char *fmt,...)
Definition: elog.c:797
static int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3089
static int fsync_parent_path(const char *fname, int elevel)
Definition: fd.c:3159
int durable_rename ( const char *  oldfile,
const char *  newfile,
int  loglevel 
)

Definition at line 593 of file fd.c.

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

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

594 {
595  int fd;
596 
597  /*
598  * First fsync the old and target path (if it exists), to ensure that they
599  * are properly persistent on disk. Syncing the target file is not
600  * strictly necessary, but it makes it easier to reason about crashes;
601  * because it's then guaranteed that either source or target file exists
602  * after a crash.
603  */
604  if (fsync_fname_ext(oldfile, false, false, elevel) != 0)
605  return -1;
606 
607  fd = OpenTransientFile((char *) newfile, PG_BINARY | O_RDWR, 0);
608  if (fd < 0)
609  {
610  if (errno != ENOENT)
611  {
612  ereport(elevel,
614  errmsg("could not open file \"%s\": %m", newfile)));
615  return -1;
616  }
617  }
618  else
619  {
620  if (pg_fsync(fd) != 0)
621  {
622  int save_errno;
623 
624  /* close file upon error, might not be in transaction context */
625  save_errno = errno;
626  CloseTransientFile(fd);
627  errno = save_errno;
628 
629  ereport(elevel,
631  errmsg("could not fsync file \"%s\": %m", newfile)));
632  return -1;
633  }
634  CloseTransientFile(fd);
635  }
636 
637  /* Time to do the real deal... */
638  if (rename(oldfile, newfile) < 0)
639  {
640  ereport(elevel,
642  errmsg("could not rename file \"%s\" to \"%s\": %m",
643  oldfile, newfile)));
644  return -1;
645  }
646 
647  /*
648  * To guarantee renaming the file is persistent, fsync the file with its
649  * new name, and its containing directory.
650  */
651  if (fsync_fname_ext(newfile, false, false, elevel) != 0)
652  return -1;
653 
654  if (fsync_parent_path(newfile, elevel) != 0)
655  return -1;
656 
657  return 0;
658 }
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1038
int OpenTransientFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:2107
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
int CloseTransientFile(int fd)
Definition: fd.c:2268
static int elevel
Definition: vacuumlazy.c:137
int errmsg(const char *fmt,...)
Definition: elog.c:797
static int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3089
int pg_fsync(int fd)
Definition: fd.c:333
static int fsync_parent_path(const char *fname, int elevel)
Definition: fd.c:3159
void FileClose ( File  file)

Definition at line 1456 of file fd.c.

References Assert, close, Delete(), DO_DB, elog, ereport, errmsg(), vfd::fd, FD_TEMPORARY, vfd::fdstate, FileIsNotOpen, FileIsValid, vfd::fileName, vfd::fileSize, FreeVfd(), LOG, log_temp_files, nfile, pgstat_report_tempfile(), ResourceOwnerForgetFile(), vfd::resowner, temporary_files_size, unlink(), and VFD_CLOSED.

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

1457 {
1458  Vfd *vfdP;
1459 
1460  Assert(FileIsValid(file));
1461 
1462  DO_DB(elog(LOG, "FileClose: %d (%s)",
1463  file, VfdCache[file].fileName));
1464 
1465  vfdP = &VfdCache[file];
1466 
1467  if (!FileIsNotOpen(file))
1468  {
1469  /* close the file */
1470  if (close(vfdP->fd))
1471  elog(LOG, "could not close file \"%s\": %m", vfdP->fileName);
1472 
1473  --nfile;
1474  vfdP->fd = VFD_CLOSED;
1475 
1476  /* remove the file from the lru ring */
1477  Delete(file);
1478  }
1479 
1480  /*
1481  * Delete the file if it was temporary, and make a log entry if wanted
1482  */
1483  if (vfdP->fdstate & FD_TEMPORARY)
1484  {
1485  struct stat filestats;
1486  int stat_errno;
1487 
1488  /*
1489  * If we get an error, as could happen within the ereport/elog calls,
1490  * we'll come right back here during transaction abort. Reset the
1491  * flag to ensure that we can't get into an infinite loop. This code
1492  * is arranged to ensure that the worst-case consequence is failing to
1493  * emit log message(s), not failing to attempt the unlink.
1494  */
1495  vfdP->fdstate &= ~FD_TEMPORARY;
1496 
1497  /* Subtract its size from current usage (do first in case of error) */
1498  temporary_files_size -= vfdP->fileSize;
1499  vfdP->fileSize = 0;
1500 
1501  /* first try the stat() */
1502  if (stat(vfdP->fileName, &filestats))
1503  stat_errno = errno;
1504  else
1505  stat_errno = 0;
1506 
1507  /* in any case do the unlink */
1508  if (unlink(vfdP->fileName))
1509  elog(LOG, "could not unlink file \"%s\": %m", vfdP->fileName);
1510 
1511  /* and last report the stat results */
1512  if (stat_errno == 0)
1513  {
1514  pgstat_report_tempfile(filestats.st_size);
1515 
1516  if (log_temp_files >= 0)
1517  {
1518  if ((filestats.st_size / 1024) >= log_temp_files)
1519  ereport(LOG,
1520  (errmsg("temporary file: path \"%s\", size %lu",
1521  vfdP->fileName,
1522  (unsigned long) filestats.st_size)));
1523  }
1524  }
1525  else
1526  {
1527  errno = stat_errno;
1528  elog(LOG, "could not stat file \"%s\": %m", vfdP->fileName);
1529  }
1530  }
1531 
1532  /* Unregister it from the resource owner */
1533  if (vfdP->resowner)
1534  ResourceOwnerForgetFile(vfdP->resowner, file);
1535 
1536  /*
1537  * Return the Vfd slot to the free list
1538  */
1539  FreeVfd(file);
1540 }
#define DO_DB(A)
Definition: fd.c:152
int log_temp_files
Definition: guc.c:457
static Vfd * VfdCache
Definition: fd.c:197
static void Delete(File file)
Definition: fd.c:947
void pgstat_report_tempfile(size_t filesize)
Definition: pgstat.c:1450
#define LOG
Definition: elog.h:26
char * fileName
Definition: fd.c:186
static int nfile
Definition: fd.c:203
unsigned short fdstate
Definition: fd.c:179
Definition: fd.c:176
off_t fileSize
Definition: fd.c:185
int fd
Definition: fd.c:178
int unlink(const char *filename)
#define ereport(elevel, rest)
Definition: elog.h:122
ResourceOwner resowner
Definition: fd.c:180
#define FileIsNotOpen(file)
Definition: fd.c:161
#define FD_TEMPORARY
Definition: fd.c:173
#define FileIsValid(file)
Definition: fd.c:158
#define VFD_CLOSED
Definition: fd.c:156
static uint64 temporary_files_size
Definition: fd.c:217
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void FreeVfd(File file)
Definition: fd.c:1193
#define elog
Definition: elog.h:219
#define close(a)
Definition: win32.h:17
void ResourceOwnerForgetFile(ResourceOwner owner, File file)
Definition: resowner.c:1209
int FileGetRawDesc ( File  file)

Definition at line 1953 of file fd.c.

References Assert, vfd::fd, and FileIsValid.

1954 {
1955  Assert(FileIsValid(file));
1956  return VfdCache[file].fd;
1957 }
static Vfd * VfdCache
Definition: fd.c:197
int fd
Definition: fd.c:178
#define FileIsValid(file)
Definition: fd.c:158
#define Assert(condition)
Definition: c.h:675
int FileGetRawFlags ( File  file)

Definition at line 1963 of file fd.c.

References Assert, vfd::fileFlags, and FileIsValid.

1964 {
1965  Assert(FileIsValid(file));
1966  return VfdCache[file].fileFlags;
1967 }
static Vfd * VfdCache
Definition: fd.c:197
#define FileIsValid(file)
Definition: fd.c:158
#define Assert(condition)
Definition: c.h:675
int fileFlags
Definition: fd.c:188
int FileGetRawMode ( File  file)

Definition at line 1973 of file fd.c.

References Assert, FileIsValid, and vfd::fileMode.

1974 {
1975  Assert(FileIsValid(file));
1976  return VfdCache[file].fileMode;
1977 }
static Vfd * VfdCache
Definition: fd.c:197
int fileMode
Definition: fd.c:189
#define FileIsValid(file)
Definition: fd.c:158
#define Assert(condition)
Definition: c.h:675
char* FilePathName ( File  file)

Definition at line 1937 of file fd.c.

References Assert, FileIsValid, and vfd::fileName.

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

1938 {
1939  Assert(FileIsValid(file));
1940 
1941  return VfdCache[file].fileName;
1942 }
static Vfd * VfdCache
Definition: fd.c:197
char * fileName
Definition: fd.c:186
#define FileIsValid(file)
Definition: fd.c:158
#define Assert(condition)
Definition: c.h:675
int FilePrefetch ( File  file,
off_t  offset,
int  amount,
uint32  wait_event_info 
)

Definition at line 1553 of file fd.c.

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

Referenced by mdprefetch().

1554 {
1555 #if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
1556  int returnCode;
1557 
1558  Assert(FileIsValid(file));
1559 
1560  DO_DB(elog(LOG, "FilePrefetch: %d (%s) " INT64_FORMAT " %d",
1561  file, VfdCache[file].fileName,
1562  (int64) offset, amount));
1563 
1564  returnCode = FileAccess(file);
1565  if (returnCode < 0)
1566  return returnCode;
1567 
1568  pgstat_report_wait_start(wait_event_info);
1569  returnCode = posix_fadvise(VfdCache[file].fd, offset, amount,
1570  POSIX_FADV_WILLNEED);
1572 
1573  return returnCode;
1574 #else
1575  Assert(FileIsValid(file));
1576  return 0;
1577 #endif
1578 }
#define DO_DB(A)
Definition: fd.c:152
static Vfd * VfdCache
Definition: fd.c:197
#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:1205
#define FileIsValid(file)
Definition: fd.c:158
static int FileAccess(File file)
Definition: fd.c:1213
#define Assert(condition)
Definition: c.h:675
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1181
#define INT64_FORMAT
Definition: c.h:315
#define elog
Definition: elog.h:219
int FileRead ( File  file,
char *  buffer,
int  amount,
uint32  wait_event_info 
)

Definition at line 1608 of file fd.c.

References _dosmaperr(), Assert, DO_DB, EINTR, elog, error(), vfd::fd, FileAccess(), FileIsValid, FilePosIsUnknown, FileUnknownPos, INT64_FORMAT, LOG, pg_usleep(), pgstat_report_wait_end(), pgstat_report_wait_start(), read, and vfd::seekPos.

Referenced by BufFileLoadBuffer(), and mdread().

1609 {
1610  int returnCode;
1611  Vfd *vfdP;
1612 
1613  Assert(FileIsValid(file));
1614 
1615  DO_DB(elog(LOG, "FileRead: %d (%s) " INT64_FORMAT " %d %p",
1616  file, VfdCache[file].fileName,
1617  (int64) VfdCache[file].seekPos,
1618  amount, buffer));
1619 
1620  returnCode = FileAccess(file);
1621  if (returnCode < 0)
1622  return returnCode;
1623 
1624  vfdP = &VfdCache[file];
1625 
1626 retry:
1627  pgstat_report_wait_start(wait_event_info);
1628  returnCode = read(vfdP->fd, buffer, amount);
1630 
1631  if (returnCode >= 0)
1632  {
1633  /* if seekPos is unknown, leave it that way */
1634  if (!FilePosIsUnknown(vfdP->seekPos))
1635  vfdP->seekPos += returnCode;
1636  }
1637  else
1638  {
1639  /*
1640  * Windows may run out of kernel buffers and return "Insufficient
1641  * system resources" error. Wait a bit and retry to solve it.
1642  *
1643  * It is rumored that EINTR is also possible on some Unix filesystems,
1644  * in which case immediate retry is indicated.
1645  */
1646 #ifdef WIN32
1647  DWORD error = GetLastError();
1648 
1649  switch (error)
1650  {
1651  case ERROR_NO_SYSTEM_RESOURCES:
1652  pg_usleep(1000L);
1653  errno = EINTR;
1654  break;
1655  default:
1656  _dosmaperr(error);
1657  break;
1658  }
1659 #endif
1660  /* OK to retry if interrupted */
1661  if (errno == EINTR)
1662  goto retry;
1663 
1664  /* Trouble, so assume we don't know the file position anymore */
1665  vfdP->seekPos = FileUnknownPos;
1666  }
1667 
1668  return returnCode;
1669 }
static void error(void)
Definition: sql-dyntest.c:147
#define DO_DB(A)
Definition: fd.c:152
static Vfd * VfdCache
Definition: fd.c:197
#define LOG
Definition: elog.h:26
#define FilePosIsUnknown(pos)
Definition: fd.c:170
void pg_usleep(long microsec)
Definition: signal.c:53
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1205
off_t seekPos
Definition: fd.c:184
Definition: fd.c:176
int fd
Definition: fd.c:178
#define EINTR
Definition: win32.h:295
#define FileIsValid(file)
Definition: fd.c:158
static int FileAccess(File file)
Definition: fd.c:1213
#define Assert(condition)
Definition: c.h:675
void _dosmaperr(unsigned long)
Definition: win32error.c:171
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:207
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1181
#define INT64_FORMAT
Definition: c.h:315
#define elog
Definition: elog.h:219
#define FileUnknownPos
Definition: fd.c:169
#define read(a, b, c)
Definition: win32.h:18
off_t FileSeek ( File  file,
off_t  offset,
int  whence 
)

Definition at line 1814 of file fd.c.

References Assert, DO_DB, elog, ERROR, vfd::fd, FileAccess(), FileIsNotOpen, FileIsValid, FilePosIsUnknown, INT64_FORMAT, LOG, and vfd::seekPos.

Referenced by _mdnblocks(), BufFileDumpBuffer(), BufFileLoadBuffer(), mdextend(), mdread(), and mdwrite().

1815 {
1816  Vfd *vfdP;
1817 
1818  Assert(FileIsValid(file));
1819 
1820  DO_DB(elog(LOG, "FileSeek: %d (%s) " INT64_FORMAT " " INT64_FORMAT " %d",
1821  file, VfdCache[file].fileName,
1822  (int64) VfdCache[file].seekPos,
1823  (int64) offset, whence));
1824 
1825  vfdP = &VfdCache[file];
1826 
1827  if (FileIsNotOpen(file))
1828  {
1829  switch (whence)
1830  {
1831  case SEEK_SET:
1832  if (offset < 0)
1833  {
1834  errno = EINVAL;
1835  return (off_t) -1;
1836  }
1837  vfdP->seekPos = offset;
1838  break;
1839  case SEEK_CUR:
1840  if (FilePosIsUnknown(vfdP->seekPos) ||
1841  vfdP->seekPos + offset < 0)
1842  {
1843  errno = EINVAL;
1844  return (off_t) -1;
1845  }
1846  vfdP->seekPos += offset;
1847  break;
1848  case SEEK_END:
1849  if (FileAccess(file) < 0)
1850  return (off_t) -1;
1851  vfdP->seekPos = lseek(vfdP->fd, offset, whence);
1852  break;
1853  default:
1854  elog(ERROR, "invalid whence: %d", whence);
1855  break;
1856  }
1857  }
1858  else
1859  {
1860  switch (whence)
1861  {
1862  case SEEK_SET:
1863  if (offset < 0)
1864  {
1865  errno = EINVAL;
1866  return (off_t) -1;
1867  }
1868  if (vfdP->seekPos != offset)
1869  vfdP->seekPos = lseek(vfdP->fd, offset, whence);
1870  break;
1871  case SEEK_CUR:
1872  if (offset != 0 || FilePosIsUnknown(vfdP->seekPos))
1873  vfdP->seekPos = lseek(vfdP->fd, offset, whence);
1874  break;
1875  case SEEK_END:
1876  vfdP->seekPos = lseek(vfdP->fd, offset, whence);
1877  break;
1878  default:
1879  elog(ERROR, "invalid whence: %d", whence);
1880  break;
1881  }
1882  }
1883 
1884  return vfdP->seekPos;
1885 }
#define DO_DB(A)
Definition: fd.c:152
static Vfd * VfdCache
Definition: fd.c:197
#define LOG
Definition: elog.h:26
#define FilePosIsUnknown(pos)
Definition: fd.c:170
#define ERROR
Definition: elog.h:43
off_t seekPos
Definition: fd.c:184
Definition: fd.c:176
int fd
Definition: fd.c:178
#define FileIsNotOpen(file)
Definition: fd.c:161
#define FileIsValid(file)
Definition: fd.c:158
static int FileAccess(File file)
Definition: fd.c:1213
#define Assert(condition)
Definition: c.h:675
#define INT64_FORMAT
Definition: c.h:315
#define elog
Definition: elog.h:219
int FileSync ( File  file,
uint32  wait_event_info 
)

Definition at line 1793 of file fd.c.

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

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

1794 {
1795  int returnCode;
1796 
1797  Assert(FileIsValid(file));
1798 
1799  DO_DB(elog(LOG, "FileSync: %d (%s)",
1800  file, VfdCache[file].fileName));
1801 
1802  returnCode = FileAccess(file);
1803  if (returnCode < 0)
1804  return returnCode;
1805 
1806  pgstat_report_wait_start(wait_event_info);
1807  returnCode = pg_fsync(VfdCache[file].fd);
1809 
1810  return returnCode;
1811 }
#define DO_DB(A)
Definition: fd.c:152
static Vfd * VfdCache
Definition: fd.c:197
#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:1205
#define FileIsValid(file)
Definition: fd.c:158
static int FileAccess(File file)
Definition: fd.c:1213
#define Assert(condition)
Definition: c.h:675
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1181
int pg_fsync(int fd)
Definition: fd.c:333
#define elog
Definition: elog.h:219
int FileTruncate ( File  file,
off_t  offset,
uint32  wait_event_info 
)

Definition at line 1902 of file fd.c.

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

Referenced by mdtruncate().

1903 {
1904  int returnCode;
1905 
1906  Assert(FileIsValid(file));
1907 
1908  DO_DB(elog(LOG, "FileTruncate %d (%s)",
1909  file, VfdCache[file].fileName));
1910 
1911  returnCode = FileAccess(file);
1912  if (returnCode < 0)
1913  return returnCode;
1914 
1915  pgstat_report_wait_start(wait_event_info);
1916  returnCode = ftruncate(VfdCache[file].fd, offset);
1918 
1919  if (returnCode == 0 && VfdCache[file].fileSize > offset)
1920  {
1921  /* adjust our state for truncation of a temp file */
1922  Assert(VfdCache[file].fdstate & FD_TEMPORARY);
1923  temporary_files_size -= VfdCache[file].fileSize - offset;
1924  VfdCache[file].fileSize = offset;
1925  }
1926 
1927  return returnCode;
1928 }
#define DO_DB(A)
Definition: fd.c:152
static Vfd * VfdCache
Definition: fd.c:197
#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:1205
off_t fileSize
Definition: fd.c:185
#define FD_TEMPORARY
Definition: fd.c:173
#define FileIsValid(file)
Definition: fd.c:158
static uint64 temporary_files_size
Definition: fd.c:217
#define ftruncate(a, b)
Definition: win32.h:67
static int FileAccess(File file)
Definition: fd.c:1213
#define Assert(condition)
Definition: c.h:675
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1181
#define elog
Definition: elog.h:219
int FileWrite ( File  file,
char *  buffer,
int  amount,
uint32  wait_event_info 
)

Definition at line 1672 of file fd.c.

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

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

1673 {
1674  int returnCode;
1675  Vfd *vfdP;
1676 
1677  Assert(FileIsValid(file));
1678 
1679  DO_DB(elog(LOG, "FileWrite: %d (%s) " INT64_FORMAT " %d %p",
1680  file, VfdCache[file].fileName,
1681  (int64) VfdCache[file].seekPos,
1682  amount, buffer));
1683 
1684  returnCode = FileAccess(file);
1685  if (returnCode < 0)
1686  return returnCode;
1687 
1688  vfdP = &VfdCache[file];
1689 
1690  /*
1691  * If enforcing temp_file_limit and it's a temp file, check to see if the
1692  * write would overrun temp_file_limit, and throw error if so. Note: it's
1693  * really a modularity violation to throw error here; we should set errno
1694  * and return -1. However, there's no way to report a suitable error
1695  * message if we do that. All current callers would just throw error
1696  * immediately anyway, so this is safe at present.
1697  */
1698  if (temp_file_limit >= 0 && (vfdP->fdstate & FD_TEMPORARY))
1699  {
1700  off_t newPos;
1701 
1702  /*
1703  * Normally we should know the seek position, but if for some reason
1704  * we have lost track of it, try again to get it. Here, it's fine to
1705  * throw an error if we still can't get it.
1706  */
1707  if (FilePosIsUnknown(vfdP->seekPos))
1708  {
1709  vfdP->seekPos = lseek(vfdP->fd, (off_t) 0, SEEK_CUR);
1710  if (FilePosIsUnknown(vfdP->seekPos))
1711  elog(ERROR, "could not seek file \"%s\": %m", vfdP->fileName);
1712  }
1713 
1714  newPos = vfdP->seekPos + amount;
1715  if (newPos > vfdP->fileSize)
1716  {
1717  uint64 newTotal = temporary_files_size;
1718 
1719  newTotal += newPos - vfdP->fileSize;
1720  if (newTotal > (uint64) temp_file_limit * (uint64) 1024)
1721  ereport(ERROR,
1722  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
1723  errmsg("temporary file size exceeds temp_file_limit (%dkB)",
1724  temp_file_limit)));
1725  }
1726  }
1727 
1728 retry:
1729  errno = 0;
1730  pgstat_report_wait_start(wait_event_info);
1731  returnCode = write(vfdP->fd, buffer, amount);
1733 
1734  /* if write didn't set errno, assume problem is no disk space */
1735  if (returnCode != amount && errno == 0)
1736  errno = ENOSPC;
1737 
1738  if (returnCode >= 0)
1739  {
1740  /* if seekPos is unknown, leave it that way */
1741  if (!FilePosIsUnknown(vfdP->seekPos))
1742  vfdP->seekPos += returnCode;
1743 
1744  /*
1745  * Maintain fileSize and temporary_files_size if it's a temp file.
1746  *
1747  * If seekPos is -1 (unknown), this will do nothing; but we could only
1748  * get here in that state if we're not enforcing temporary_files_size,
1749  * so we don't care.
1750  */
1751  if (vfdP->fdstate & FD_TEMPORARY)
1752  {
1753  off_t newPos = vfdP->seekPos;
1754 
1755  if (newPos > vfdP->fileSize)
1756  {
1757  temporary_files_size += newPos - vfdP->fileSize;
1758  vfdP->fileSize = newPos;
1759  }
1760  }
1761  }
1762  else
1763  {
1764  /*
1765  * See comments in FileRead()
1766  */
1767 #ifdef WIN32
1768  DWORD error = GetLastError();
1769 
1770  switch (error)
1771  {
1772  case ERROR_NO_SYSTEM_RESOURCES:
1773  pg_usleep(1000L);
1774  errno = EINTR;
1775  break;
1776  default:
1777  _dosmaperr(error);
1778  break;
1779  }
1780 #endif
1781  /* OK to retry if interrupted */
1782  if (errno == EINTR)
1783  goto retry;
1784 
1785  /* Trouble, so assume we don't know the file position anymore */
1786  vfdP->seekPos = FileUnknownPos;
1787  }
1788 
1789  return returnCode;
1790 }
static void error(void)
Definition: sql-dyntest.c:147
#define write(a, b, c)
Definition: win32.h:19
#define DO_DB(A)
Definition: fd.c:152
static Vfd * VfdCache
Definition: fd.c:197
int errcode(int sqlerrcode)
Definition: elog.c:575
#define LOG
Definition: elog.h:26
#define FilePosIsUnknown(pos)
Definition: fd.c:170
void pg_usleep(long microsec)
Definition: signal.c:53
#define ERROR
Definition: elog.h:43
char * fileName
Definition: fd.c:186
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1205
off_t seekPos
Definition: fd.c:184
unsigned short fdstate
Definition: fd.c:179
Definition: fd.c:176
off_t fileSize
Definition: fd.c:185
int fd
Definition: fd.c:178
#define ereport(elevel, rest)
Definition: elog.h:122
#define FD_TEMPORARY
Definition: fd.c:173
#define EINTR
Definition: win32.h:295
#define FileIsValid(file)
Definition: fd.c:158
static uint64 temporary_files_size
Definition: fd.c:217
static int FileAccess(File file)
Definition: fd.c:1213
#define Assert(condition)
Definition: c.h:675
void _dosmaperr(unsigned long)
Definition: win32error.c:171
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:207
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1181
#define INT64_FORMAT
Definition: c.h:315
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
#define FileUnknownPos
Definition: fd.c:169
int temp_file_limit
Definition: guc.c:460
void FileWriteback ( File  file,
off_t  offset,
off_t  nbytes,
uint32  wait_event_info 
)

Definition at line 1581 of file fd.c.

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

Referenced by mdwriteback().

1582 {
1583  int returnCode;
1584 
1585  Assert(FileIsValid(file));
1586 
1587  DO_DB(elog(LOG, "FileWriteback: %d (%s) " INT64_FORMAT " " INT64_FORMAT,
1588  file, VfdCache[file].fileName,
1589  (int64) offset, (int64) nbytes));
1590 
1591  /*
1592  * Caution: do not call pg_flush_data with nbytes = 0, it could trash the
1593  * file's seek position. We prefer to define that as a no-op here.
1594  */
1595  if (nbytes <= 0)
1596  return;
1597 
1598  returnCode = FileAccess(file);
1599  if (returnCode < 0)
1600  return;
1601 
1602  pgstat_report_wait_start(wait_event_info);
1603  pg_flush_data(VfdCache[file].fd, offset, nbytes);
1605 }
#define DO_DB(A)
Definition: fd.c:152
static Vfd * VfdCache
Definition: fd.c:197
#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:1205
void pg_flush_data(int fd, off_t offset, off_t nbytes)
Definition: fd.c:407
#define FileIsValid(file)
Definition: fd.c:158
static int FileAccess(File file)
Definition: fd.c:1213
#define Assert(condition)
Definition: c.h:675
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1181
#define INT64_FORMAT
Definition: c.h:315
#define elog
Definition: elog.h:219
int FreeDir ( DIR dir)

Definition at line 2407 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(), dsm_cleanup_for_mmap(), get_ext_ver_list(), getInstallationPaths(), movedb(), ParseTzFile(), perform_base_backup(), pg_available_extension_versions(), pg_available_extensions(), pg_logdir_ls(), pg_ls_dir(), pg_ls_dir_files(), pg_start_backup(), pg_tablespace_databases(), pg_tzenumerate_end(), pg_tzenumerate_next(), pgarch_readyXlog(), pgstat_reset_remove_files(), PrescanPreparedTransactions(), RecoverPreparedTransactions(), RelationCacheInitFileRemove(), RelationCacheInitFileRemoveInDir(), RemoveNonParentXlogFiles(), RemoveOldXlogFiles(), RemovePgTempFiles(), RemovePgTempFilesInDir(), RemovePgTempRelationFiles(), RemovePgTempRelationFilesInDbspace(), ResetUnloggedRelations(), ResetUnloggedRelationsInDbspaceDir(), ResetUnloggedRelationsInTablespaceDir(), scan_directory_ci(), SendBaseBackup(), sendDir(), SlruScanDirectory(), StandbyRecoverPreparedTransactions(), StartupReorderBuffer(), StartupReplicationSlots(), UpdateLogicalMappings(), and walkdir().

2408 {
2409  int i;
2410 
2411  DO_DB(elog(LOG, "FreeDir: Allocated %d", numAllocatedDescs));
2412 
2413  /* Remove dir from list of allocated dirs, if it's present */
2414  for (i = numAllocatedDescs; --i >= 0;)
2415  {
2416  AllocateDesc *desc = &allocatedDescs[i];
2417 
2418  if (desc->kind == AllocateDescDir && desc->desc.dir == dir)
2419  return FreeDesc(desc);
2420  }
2421 
2422  /* Only get here if someone passes us a dir not in allocatedDescs */
2423  elog(WARNING, "dir passed to FreeDir was not obtained from AllocateDir");
2424 
2425  return closedir(dir);
2426 }
static AllocateDesc * allocatedDescs
Definition: fd.c:245
union AllocateDesc::@26 desc
DIR * dir
Definition: fd.c:238
#define DO_DB(A)
Definition: fd.c:152
int closedir(DIR *)
Definition: dirent.c:113
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:233
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2201
#define WARNING
Definition: elog.h:40
int i
#define elog
Definition: elog.h:219
static int numAllocatedDescs
Definition: fd.c:243
int FreeFile ( FILE *  file)

Definition at line 2240 of file fd.c.

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

Referenced by AlterSystemSetConfigFile(), checkDataDir(), do_pg_start_backup(), do_pg_stop_backup(), EndCopy(), entry_reset(), existsTimeLineHistory(), ExportSnapshot(), fill_hba_view(), gc_qtexts(), ImportSnapshot(), 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(), 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(), read_backup_label(), read_binary_file(), read_tablespace_map(), read_whole_file(), readRecoveryCommandFile(), readTimeLineHistory(), sendFile(), tokenize_inc_file(), tsearch_readline_end(), ValidatePgVersion(), write_relcache_init_file(), XLogArchiveForceDone(), and XLogArchiveNotify().

2241 {
2242  int i;
2243 
2244  DO_DB(elog(LOG, "FreeFile: Allocated %d", numAllocatedDescs));
2245 
2246  /* Remove file from list of allocated files, if it's present */
2247  for (i = numAllocatedDescs; --i >= 0;)
2248  {
2249  AllocateDesc *desc = &allocatedDescs[i];
2250 
2251  if (desc->kind == AllocateDescFile && desc->desc.file == file)
2252  return FreeDesc(desc);
2253  }
2254 
2255  /* Only get here if someone passes us a file not in allocatedDescs */
2256  elog(WARNING, "file passed to FreeFile was not obtained from AllocateFile");
2257 
2258  return fclose(file);
2259 }
static AllocateDesc * allocatedDescs
Definition: fd.c:245
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:152
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:233
static int FreeDesc(AllocateDesc *desc)
Definition: fd.c:2201
#define WARNING
Definition: elog.h:40
FILE * file
Definition: fd.c:237
int i
#define elog
Definition: elog.h:219
static int numAllocatedDescs
Definition: fd.c:243
void fsync_fname ( const char *  fname,
bool  isdir 
)

Definition at line 567 of file fd.c.

References ERROR, and fsync_fname_ext().

Referenced by _CloseArchive(), BaseBackup(), copydir(), CreateSlotOnDisk(), dir_close(), dir_finish(), dir_open_for_write(), main(), ReceiveTarFile(), ReplicationSlotDropPtr(), ResetUnloggedRelationsInDbspaceDir(), RestoreSlotFromDisk(), SaveSlotToPath(), SnapBuildRestore(), SnapBuildSerialize(), StartupReplicationSlots(), and tar_finish().

568 {
569  fsync_fname_ext(fname, isdir, false, ERROR);
570 }
#define ERROR
Definition: elog.h:43
static int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3089
Oid GetNextTempTableSpace ( void  )

Definition at line 2528 of file fd.c.

References InvalidOid, nextTempTableSpace, numTempTableSpaces, and tempTableSpaces.

Referenced by GetDefaultTablespace(), and OpenTemporaryFile().

2529 {
2530  if (numTempTableSpaces > 0)
2531  {
2532  /* Advance nextTempTableSpace counter with wraparound */
2534  nextTempTableSpace = 0;
2536  }
2537  return InvalidOid;
2538 }
static int numTempTableSpaces
Definition: fd.c:258
static int nextTempTableSpace
Definition: fd.c:259
#define InvalidOid
Definition: postgres_ext.h:36
static Oid * tempTableSpaces
Definition: fd.c:257
void InitFileAccess ( void  )

Definition at line 727 of file fd.c.

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

Referenced by BaseInit().

728 {
729  Assert(SizeVfdCache == 0); /* call me only once */
730 
731  /* initialize cache header entry */
732  VfdCache = (Vfd *) malloc(sizeof(Vfd));
733  if (VfdCache == NULL)
734  ereport(FATAL,
735  (errcode(ERRCODE_OUT_OF_MEMORY),
736  errmsg("out of memory")));
737 
738  MemSet((char *) &(VfdCache[0]), 0, sizeof(Vfd));
740 
741  SizeVfdCache = 1;
742 
743  /* register proc-exit hook to ensure temp files are dropped at exit */
745 }
static void AtProcExit_Files(int code, Datum arg)
Definition: fd.c:2594
static Size SizeVfdCache
Definition: fd.c:198
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:292
static Vfd * VfdCache
Definition: fd.c:197
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:857
#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:122
#define VFD_CLOSED
Definition: fd.c:156
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
FILE* OpenPipeStream ( const char *  command,
const char *  mode 
)

Definition at line 2147 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, NULL, numAllocatedDescs, ReleaseLruFile(), ReleaseLruFiles(), and reserveAllocatedDesc().

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

2148 {
2149  FILE *file;
2150 
2151  DO_DB(elog(LOG, "OpenPipeStream: Allocated %d (%s)",
2152  numAllocatedDescs, command));
2153 
2154  /* Can we allocate another non-virtual FD? */
2155  if (!reserveAllocatedDesc())
2156  ereport(ERROR,
2157  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2158  errmsg("exceeded maxAllocatedDescs (%d) while trying to execute command \"%s\"",
2159  maxAllocatedDescs, command)));
2160 
2161  /* Close excess kernel FDs. */
2162  ReleaseLruFiles();
2163 
2164 TryAgain:
2165  fflush(stdout);
2166  fflush(stderr);
2167  errno = 0;
2168  if ((file = popen(command, mode)) != NULL)
2169  {
2171 
2172  desc->kind = AllocateDescPipe;
2173  desc->desc.file = file;
2176  return desc->desc.file;
2177  }
2178 
2179  if (errno == EMFILE || errno == ENFILE)
2180  {
2181  int save_errno = errno;
2182 
2183  ereport(LOG,
2184  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2185  errmsg("out of file descriptors: %m; release and retry")));
2186  errno = 0;
2187  if (ReleaseLruFile())
2188  goto TryAgain;
2189  errno = save_errno;
2190  }
2191 
2192  return NULL;
2193 }
static AllocateDesc * allocatedDescs
Definition: fd.c:245
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:152
int errcode(int sqlerrcode)
Definition: elog.c:575
static bool reserveAllocatedDesc(void)
Definition: fd.c:1984
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:233
static bool ReleaseLruFile(void)
Definition: fd.c:1103
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static void ReleaseLruFiles(void)
Definition: fd.c:1125
FILE * file
Definition: fd.c:237
#define NULL
Definition: c.h:229
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:649
SubTransactionId create_subid
Definition: fd.c:234
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static int maxAllocatedDescs
Definition: fd.c:244
static int numAllocatedDescs
Definition: fd.c:243
File OpenTemporaryFile ( bool  interXact)

Definition at line 1334 of file fd.c.

References CurrentResourceOwner, DEFAULTTABLESPACE_OID, FD_TEMPORARY, FD_XACT_TEMPORARY, vfd::fdstate, GetNextTempTableSpace(), have_xact_temporary_files, MyDatabaseTableSpace, numTempTableSpaces, OidIsValid, OpenTemporaryFileInTablespace(), ResourceOwnerEnlargeFiles(), ResourceOwnerRememberFile(), and vfd::resowner.

Referenced by BufFileCreateTemp(), and extendBufFile().

1335 {
1336  File file = 0;
1337 
1338  /*
1339  * If some temp tablespace(s) have been given to us, try to use the next
1340  * one. If a given tablespace can't be found, we silently fall back to
1341  * the database's default tablespace.
1342  *
1343  * BUT: if the temp file is slated to outlive the current transaction,
1344  * force it into the database's default tablespace, so that it will not
1345  * pose a threat to possible tablespace drop attempts.
1346  */
1347  if (numTempTableSpaces > 0 && !interXact)
1348  {
1349  Oid tblspcOid = GetNextTempTableSpace();
1350 
1351  if (OidIsValid(tblspcOid))
1352  file = OpenTemporaryFileInTablespace(tblspcOid, false);
1353  }
1354 
1355  /*
1356  * If not, or if tablespace is bad, create in database's default
1357  * tablespace. MyDatabaseTableSpace should normally be set before we get
1358  * here, but just in case it isn't, fall back to pg_default tablespace.
1359  */
1360  if (file <= 0)
1364  true);
1365 
1366  /* Mark it for deletion at close */
1367  VfdCache[file].fdstate |= FD_TEMPORARY;
1368 
1369  /* Register it with the current resource owner */
1370  if (!interXact)
1371  {
1373 
1377 
1378  /* ensure cleanup happens at eoxact */
1380  }
1381 
1382  return file;
1383 }
static File OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError)
Definition: fd.c:1390
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static Vfd * VfdCache
Definition: fd.c:197
static int numTempTableSpaces
Definition: fd.c:258
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
Oid MyDatabaseTableSpace
Definition: globals.c:78
static bool have_xact_temporary_files
Definition: fd.c:209
Oid GetNextTempTableSpace(void)
Definition: fd.c:2528
void ResourceOwnerRememberFile(ResourceOwner owner, File file)
Definition: resowner.c:1200
#define DEFAULTTABLESPACE_OID
Definition: pg_tablespace.h:63
unsigned short fdstate
Definition: fd.c:179
ResourceOwner resowner
Definition: fd.c:180
#define FD_TEMPORARY
Definition: fd.c:173
#define FD_XACT_TEMPORARY
Definition: fd.c:174
void ResourceOwnerEnlargeFiles(ResourceOwner owner)
Definition: resowner.c:1189
int File
Definition: fd.h:51
int OpenTransientFile ( FileName  fileName,
int  fileFlags,
int  fileMode 
)

Definition at line 2107 of file fd.c.

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

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

2108 {
2109  int fd;
2110 
2111  DO_DB(elog(LOG, "OpenTransientFile: Allocated %d (%s)",
2112  numAllocatedDescs, fileName));
2113 
2114  /* Can we allocate another non-virtual FD? */
2115  if (!reserveAllocatedDesc())
2116  ereport(ERROR,
2117  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
2118  errmsg("exceeded maxAllocatedDescs (%d) while trying to open file \"%s\"",
2119  maxAllocatedDescs, fileName)));
2120 
2121  /* Close excess kernel FDs. */
2122  ReleaseLruFiles();
2123 
2124  fd = BasicOpenFile(fileName, fileFlags, fileMode);
2125 
2126  if (fd >= 0)
2127  {
2129 
2130  desc->kind = AllocateDescRawFD;
2131  desc->desc.fd = fd;
2134 
2135  return fd;
2136  }
2137 
2138  return -1; /* failure */
2139 }
static AllocateDesc * allocatedDescs
Definition: fd.c:245
union AllocateDesc::@26 desc
#define DO_DB(A)
Definition: fd.c:152
int errcode(int sqlerrcode)
Definition: elog.c:575
static bool reserveAllocatedDesc(void)
Definition: fd.c:1984
#define LOG
Definition: elog.h:26
AllocateDescKind kind
Definition: fd.c:233
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:122
static void ReleaseLruFiles(void)
Definition: fd.c:1125
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:649
SubTransactionId create_subid
Definition: fd.c:234
int fd
Definition: fd.c:239
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static int maxAllocatedDescs
Definition: fd.c:244
static int numAllocatedDescs
Definition: fd.c:243
int BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:899
File PathNameOpenFile ( FileName  fileName,
int  fileFlags,
int  fileMode 
)

Definition at line 1266 of file fd.c.

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

Referenced by _mdfd_openseg(), logical_rewrite_log_mapping(), mdcreate(), mdopen(), and OpenTemporaryFileInTablespace().

1267 {
1268  char *fnamecopy;
1269  File file;
1270  Vfd *vfdP;
1271 
1272  DO_DB(elog(LOG, "PathNameOpenFile: %s %x %o",
1273  fileName, fileFlags, fileMode));
1274 
1275  /*
1276  * We need a malloc'd copy of the file name; fail cleanly if no room.
1277  */
1278  fnamecopy = strdup(fileName);
1279  if (fnamecopy == NULL)
1280  ereport(ERROR,
1281  (errcode(ERRCODE_OUT_OF_MEMORY),
1282  errmsg("out of memory")));
1283 
1284  file = AllocateVfd();
1285  vfdP = &VfdCache[file];
1286 
1287  /* Close excess kernel FDs. */
1288  ReleaseLruFiles();
1289 
1290  vfdP->fd = BasicOpenFile(fileName, fileFlags, fileMode);
1291 
1292  if (vfdP->fd < 0)
1293  {
1294  int save_errno = errno;
1295 
1296  FreeVfd(file);
1297  free(fnamecopy);
1298  errno = save_errno;
1299  return -1;
1300  }
1301  ++nfile;
1302  DO_DB(elog(LOG, "PathNameOpenFile: success %d",
1303  vfdP->fd));
1304 
1305  Insert(file);
1306 
1307  vfdP->fileName = fnamecopy;
1308  /* Saved flags are adjusted to be OK for re-opening file */
1309  vfdP->fileFlags = fileFlags & ~(O_CREAT | O_TRUNC | O_EXCL);
1310  vfdP->fileMode = fileMode;
1311  vfdP->seekPos = 0;
1312  vfdP->fileSize = 0;
1313  vfdP->fdstate = 0x0;
1314  vfdP->resowner = NULL;
1315 
1316  return file;
1317 }
#define DO_DB(A)
Definition: fd.c:152
static Vfd * VfdCache
Definition: fd.c:197
int errcode(int sqlerrcode)
Definition: elog.c:575
#define LOG
Definition: elog.h:26
int fileMode
Definition: fd.c:189
#define ERROR
Definition: elog.h:43
char * fileName
Definition: fd.c:186
static int nfile
Definition: fd.c:203
static File AllocateVfd(void)
Definition: fd.c:1135
off_t seekPos
Definition: fd.c:184
unsigned short fdstate
Definition: fd.c:179
Definition: fd.c:176
off_t fileSize
Definition: fd.c:185
int fd
Definition: fd.c:178
#define ereport(elevel, rest)
Definition: elog.h:122
static void Insert(File file)
Definition: fd.c:1007
ResourceOwner resowner
Definition: fd.c:180
static void ReleaseLruFiles(void)
Definition: fd.c:1125
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void FreeVfd(File file)
Definition: fd.c:1193
#define elog
Definition: elog.h:219
int fileFlags
Definition: fd.c:188
int File
Definition: fd.h:51
int BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:899
int pg_fdatasync ( int  fd)

Definition at line 385 of file fd.c.

References enableFsync, and fsync.

Referenced by issue_xlog_fsync().

386 {
387  if (enableFsync)
388  {
389 #ifdef HAVE_FDATASYNC
390  return fdatasync(fd);
391 #else
392  return fsync(fd);
393 #endif
394  }
395  else
396  return 0;
397 }
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define fsync(fd)
Definition: win32.h:70
bool enableFsync
Definition: globals.c:110
void pg_flush_data ( int  fd,
off_t  offset,
off_t  amount 
)

Definition at line 407 of file fd.c.

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

Referenced by copy_file(), and FileWriteback().

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

Definition at line 333 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(), RecreateTwoPhaseFile(), RestoreSlotFromDisk(), SaveSlotToPath(), SimpleLruFlush(), SlruPhysicalWritePage(), SnapBuildSerialize(), UpdateControlFile(), write_auto_conf_file(), write_relmap_file(), WriteControlFile(), writeTimeLineHistory(), writeTimeLineHistoryFile(), XLogFileCopy(), and XLogFileInit().

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

Definition at line 350 of file fd.c.

References enableFsync, and fsync.

Referenced by issue_xlog_fsync(), and pg_fsync().

351 {
352  if (enableFsync)
353  return fsync(fd);
354  else
355  return 0;
356 }
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define fsync(fd)
Definition: win32.h:70
bool enableFsync
Definition: globals.c:110
int pg_fsync_writethrough ( int  fd)

Definition at line 362 of file fd.c.

References enableFsync.

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

363 {
364  if (enableFsync)
365  {
366 #ifdef WIN32
367  return _commit(fd);
368 #elif defined(F_FULLFSYNC)
369  return (fcntl(fd, F_FULLFSYNC, 0) == -1) ? -1 : 0;
370 #else
371  errno = ENOSYS;
372  return -1;
373 #endif
374  }
375  else
376  return 0;
377 }
static int fd(const char *x, int i)
Definition: preproc-init.c:105
bool enableFsync
Definition: globals.c:110
struct dirent* ReadDir ( DIR dir,
const char *  dirname 
)
void RemovePgTempFiles ( void  )

Definition at line 2669 of file fd.c.

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

Referenced by PostmasterMain().

2670 {
2671  char temp_path[MAXPGPATH];
2672  DIR *spc_dir;
2673  struct dirent *spc_de;
2674 
2675  /*
2676  * First process temp files in pg_default ($PGDATA/base)
2677  */
2678  snprintf(temp_path, sizeof(temp_path), "base/%s", PG_TEMP_FILES_DIR);
2679  RemovePgTempFilesInDir(temp_path);
2680  RemovePgTempRelationFiles("base");
2681 
2682  /*
2683  * Cycle through temp directories for all non-default tablespaces.
2684  */
2685  spc_dir = AllocateDir("pg_tblspc");
2686 
2687  while ((spc_de = ReadDir(spc_dir, "pg_tblspc")) != NULL)
2688  {
2689  if (strcmp(spc_de->d_name, ".") == 0 ||
2690  strcmp(spc_de->d_name, "..") == 0)
2691  continue;
2692 
2693  snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s/%s",
2695  RemovePgTempFilesInDir(temp_path);
2696 
2697  snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s",
2699  RemovePgTempRelationFiles(temp_path);
2700  }
2701 
2702  FreeDir(spc_dir);
2703 
2704  /*
2705  * In EXEC_BACKEND case there is a pgsql_tmp directory at the top level of
2706  * DataDir as well.
2707  */
2708 #ifdef EXEC_BACKEND
2710 #endif
2711 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static void RemovePgTempFilesInDir(const char *tmpdirname)
Definition: fd.c:2715
Definition: dirent.h:9
static void RemovePgTempRelationFiles(const char *tsdirname)
Definition: fd.c:2756
Definition: dirent.c:25
#define MAXPGPATH
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2298
#define PG_TEMP_FILES_DIR
Definition: fd.h:126
#define NULL
Definition: c.h:229
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2364
#define TABLESPACE_VERSION_DIRECTORY
Definition: catalog.h:26
char d_name[MAX_PATH]
Definition: dirent.h:14
int FreeDir(DIR *dir)
Definition: fd.c:2407
void set_max_safe_fds ( void  )

Definition at line 844 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().

845 {
846  int usable_fds;
847  int already_open;
848 
849  /*----------
850  * We want to set max_safe_fds to
851  * MIN(usable_fds, max_files_per_process - already_open)
852  * less the slop factor for files that are opened without consulting
853  * fd.c. This ensures that we won't exceed either max_files_per_process
854  * or the experimentally-determined EMFILE limit.
855  *----------
856  */
858  &usable_fds, &already_open);
859 
860  max_safe_fds = Min(usable_fds, max_files_per_process - already_open);
861 
862  /*
863  * Take off the FDs reserved for system() etc.
864  */
866 
867  /*
868  * Make sure we still have enough to get by.
869  */
870  if (max_safe_fds < FD_MINFREE)
871  ereport(FATAL,
872  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
873  errmsg("insufficient file descriptors available to start server process"),
874  errdetail("System allows %d, we need at least %d.",
877 
878  elog(DEBUG2, "max_safe_fds = %d, usable_fds = %d, already_open = %d",
879  max_safe_fds, usable_fds, already_open);
880 }
static void count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
Definition: fd.c:760
#define NUM_RESERVED_FDS
Definition: fd.c:111
int max_safe_fds
Definition: fd.c:139
#define Min(x, y)
Definition: c.h:806
int errcode(int sqlerrcode)
Definition: elog.c:575
#define FATAL
Definition: elog.h:52
#define DEBUG2
Definition: elog.h:24
int errdetail(const char *fmt,...)
Definition: elog.c:873
int max_files_per_process
Definition: fd.c:126
#define ereport(elevel, rest)
Definition: elog.h:122
#define FD_MINFREE
Definition: fd.c:117
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
void SetTempTablespaces ( Oid tableSpaces,
int  numSpaces 
)

Definition at line 2488 of file fd.c.

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

Referenced by assign_temp_tablespaces(), and PrepareTempTablespaces().

2489 {
2490  Assert(numSpaces >= 0);
2491  tempTableSpaces = tableSpaces;
2492  numTempTableSpaces = numSpaces;
2493 
2494  /*
2495  * Select a random starting point in the list. This is to minimize
2496  * conflicts between backends that are most likely sharing the same list
2497  * of temp tablespaces. Note that if we create multiple temp files in the
2498  * same transaction, we'll advance circularly through the list --- this
2499  * ensures that large temporary sort files are nicely spread across all
2500  * available tablespaces.
2501  */
2502  if (numSpaces > 1)
2503  nextTempTableSpace = random() % numSpaces;
2504  else
2505  nextTempTableSpace = 0;
2506 }
long random(void)
Definition: random.c:22
static int numTempTableSpaces
Definition: fd.c:258
static int nextTempTableSpace
Definition: fd.c:259
#define Assert(condition)
Definition: c.h:675
static Oid * tempTableSpaces
Definition: fd.c:257
void SyncDataDirectory ( void  )

Definition at line 2896 of file fd.c.

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

Referenced by StartupXLOG().

2897 {
2898  bool xlog_is_symlink;
2899 
2900  /* We can skip this whole thing if fsync is disabled. */
2901  if (!enableFsync)
2902  return;
2903 
2904  /*
2905  * If pg_wal is a symlink, we'll need to recurse into it separately,
2906  * because the first walkdir below will ignore it.
2907  */
2908  xlog_is_symlink = false;
2909 
2910 #ifndef WIN32
2911  {
2912  struct stat st;
2913 
2914  if (lstat("pg_wal", &st) < 0)
2915  ereport(LOG,
2917  errmsg("could not stat file \"%s\": %m",
2918  "pg_wal")));
2919  else if (S_ISLNK(st.st_mode))
2920  xlog_is_symlink = true;
2921  }
2922 #else
2923  if (pgwin32_is_junction("pg_wal"))
2924  xlog_is_symlink = true;
2925 #endif
2926 
2927  /*
2928  * If possible, hint to the kernel that we're soon going to fsync the data
2929  * directory and its contents. Errors in this step are even less
2930  * interesting than normal, so log them only at DEBUG1.
2931  */
2932 #ifdef PG_FLUSH_DATA_WORKS
2933  walkdir(".", pre_sync_fname, false, DEBUG1);
2934  if (xlog_is_symlink)
2935  walkdir("pg_wal", pre_sync_fname, false, DEBUG1);
2936  walkdir("pg_tblspc", pre_sync_fname, true, DEBUG1);
2937 #endif
2938 
2939  /*
2940  * Now we do the fsync()s in the same order.
2941  *
2942  * The main call ignores symlinks, so in addition to specially processing
2943  * pg_wal if it's a symlink, pg_tblspc has to be visited separately with
2944  * process_symlinks = true. Note that if there are any plain directories
2945  * in pg_tblspc, they'll get fsync'd twice. That's not an expected case
2946  * so we don't worry about optimizing it.
2947  */
2948  walkdir(".", datadir_fsync_fname, false, LOG);
2949  if (xlog_is_symlink)
2950  walkdir("pg_wal", datadir_fsync_fname, false, LOG);
2951  walkdir("pg_tblspc", datadir_fsync_fname, true, LOG);
2952 }
#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:2969
#define LOG
Definition: elog.h:26
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
static void datadir_fsync_fname(const char *fname, bool isdir, int elevel)
Definition: fd.c:3071
bool enableFsync
Definition: globals.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define lstat(path, sb)
Definition: win32.h:272
bool TempTablespacesAreSet ( void  )

Definition at line 2516 of file fd.c.

References numTempTableSpaces.

Referenced by PrepareTempTablespaces().

2517 {
2518  return (numTempTableSpaces >= 0);
2519 }
static int numTempTableSpaces
Definition: fd.c:258

Variable Documentation

int max_files_per_process

Definition at line 126 of file fd.c.

Referenced by set_max_safe_fds().

int max_safe_fds

Definition at line 139 of file fd.c.

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