PostgreSQL Source Code  git master
buffile.h File Reference
#include "storage/fileset.h"
Include dependency graph for buffile.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct BufFile BufFile
 

Functions

BufFileBufFileCreateTemp (bool interXact)
 
void BufFileClose (BufFile *file)
 
pg_nodiscard size_t BufFileRead (BufFile *file, void *ptr, size_t size)
 
void BufFileReadExact (BufFile *file, void *ptr, size_t size)
 
size_t BufFileReadMaybeEOF (BufFile *file, void *ptr, size_t size, bool eofOK)
 
void BufFileWrite (BufFile *file, const void *ptr, size_t size)
 
int BufFileSeek (BufFile *file, int fileno, off_t offset, int whence)
 
void BufFileTell (BufFile *file, int *fileno, off_t *offset)
 
int BufFileSeekBlock (BufFile *file, int64 blknum)
 
int64 BufFileSize (BufFile *file)
 
int64 BufFileAppend (BufFile *target, BufFile *source)
 
BufFileBufFileCreateFileSet (FileSet *fileset, const char *name)
 
void BufFileExportFileSet (BufFile *file)
 
BufFileBufFileOpenFileSet (FileSet *fileset, const char *name, int mode, bool missing_ok)
 
void BufFileDeleteFileSet (FileSet *fileset, const char *name, bool missing_ok)
 
void BufFileTruncateFileSet (BufFile *file, int fileno, off_t offset)
 

Typedef Documentation

◆ BufFile

typedef struct BufFile BufFile

Definition at line 1 of file buffile.h.

Function Documentation

◆ BufFileAppend()

int64 BufFileAppend ( BufFile target,
BufFile source 
)

Definition at line 905 of file buffile.c.

906 {
907  int64 startBlock = (int64) target->numFiles * BUFFILE_SEG_SIZE;
908  int newNumFiles = target->numFiles + source->numFiles;
909  int i;
910 
911  Assert(target->fileset != NULL);
912  Assert(source->readOnly);
913  Assert(!source->dirty);
914  Assert(source->fileset != NULL);
915 
916  if (target->resowner != source->resowner)
917  elog(ERROR, "could not append BufFile with non-matching resource owner");
918 
919  target->files = (File *)
920  repalloc(target->files, sizeof(File) * newNumFiles);
921  for (i = target->numFiles; i < newNumFiles; i++)
922  target->files[i] = source->files[i - target->numFiles];
923  target->numFiles = newNumFiles;
924 
925  return startBlock;
926 }
#define BUFFILE_SEG_SIZE
Definition: buffile.c:63
#define Assert(condition)
Definition: c.h:858
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
int File
Definition: fd.h:51
int i
Definition: isn.c:73
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1540
static rewind_source * source
Definition: pg_rewind.c:89
int numFiles
Definition: buffile.c:72
FileSet * fileset
Definition: buffile.c:80
File * files
Definition: buffile.c:74
ResourceOwner resowner
Definition: buffile.c:88

References Assert, BUFFILE_SEG_SIZE, elog, ERROR, BufFile::files, BufFile::fileset, i, BufFile::numFiles, repalloc(), BufFile::resowner, and source.

Referenced by LogicalTapeImport().

◆ BufFileClose()

void BufFileClose ( BufFile file)

Definition at line 412 of file buffile.c.

413 {
414  int i;
415 
416  /* flush any unwritten data */
417  BufFileFlush(file);
418  /* close and delete the underlying file(s) */
419  for (i = 0; i < file->numFiles; i++)
420  FileClose(file->files[i]);
421  /* release the buffer space */
422  pfree(file->files);
423  pfree(file);
424 }
static void BufFileFlush(BufFile *file)
Definition: buffile.c:720
void FileClose(File file)
Definition: fd.c:1978
void pfree(void *pointer)
Definition: mcxt.c:1520

References BufFileFlush(), FileClose(), BufFile::files, i, BufFile::numFiles, and pfree().

Referenced by ensure_last_message(), ExecHashJoinNewBatch(), ExecHashTableDestroy(), gistFreeBuildBuffers(), LogicalTapeSetClose(), SendBackupManifest(), stream_abort_internal(), stream_close_file(), sts_end_parallel_scan(), sts_end_write(), sts_parallel_scan_next(), subxact_info_read(), subxact_info_write(), tuplestore_clear(), and tuplestore_end().

◆ BufFileCreateFileSet()

BufFile* BufFileCreateFileSet ( FileSet fileset,
const char *  name 
)

Definition at line 267 of file buffile.c.

268 {
269  BufFile *file;
270 
271  file = makeBufFileCommon(1);
272  file->fileset = fileset;
273  file->name = pstrdup(name);
274  file->files = (File *) palloc(sizeof(File));
275  file->files[0] = MakeNewFileSetSegment(file, 0);
276  file->readOnly = false;
277 
278  return file;
279 }
static BufFile * makeBufFileCommon(int nfiles)
Definition: buffile.c:118
static File MakeNewFileSetSegment(BufFile *buffile, int segment)
Definition: buffile.c:231
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void * palloc(Size size)
Definition: mcxt.c:1316
bool readOnly
Definition: buffile.c:78
const char * name
Definition: buffile.c:81
const char * name

References BufFile::files, BufFile::fileset, makeBufFileCommon(), MakeNewFileSetSegment(), BufFile::name, name, palloc(), pstrdup(), and BufFile::readOnly.

Referenced by LogicalTapeSetCreate(), stream_open_file(), sts_puttuple(), and subxact_info_write().

◆ BufFileCreateTemp()

BufFile* BufFileCreateTemp ( bool  interXact)

Definition at line 193 of file buffile.c.

194 {
195  BufFile *file;
196  File pfile;
197 
198  /*
199  * Ensure that temp tablespaces are set up for OpenTemporaryFile to use.
200  * Possibly the caller will have done this already, but it seems useful to
201  * double-check here. Failure to do this at all would result in the temp
202  * files always getting placed in the default tablespace, which is a
203  * pretty hard-to-detect bug. Callers may prefer to do it earlier if they
204  * want to be sure that any required catalog access is done in some other
205  * resource context.
206  */
208 
209  pfile = OpenTemporaryFile(interXact);
210  Assert(pfile >= 0);
211 
212  file = makeBufFile(pfile);
213  file->isInterXact = interXact;
214 
215  return file;
216 }
void PrepareTempTablespaces(void)
Definition: tablespace.c:1331
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:139
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1724
bool isInterXact
Definition: buffile.c:76

References Assert, BufFile::isInterXact, makeBufFile(), OpenTemporaryFile(), and PrepareTempTablespaces().

Referenced by ExecHashJoinSaveTuple(), gistInitBuildBuffers(), InitializeBackupManifest(), LogicalTapeSetCreate(), and tuplestore_puttuple_common().

◆ BufFileDeleteFileSet()

void BufFileDeleteFileSet ( FileSet fileset,
const char *  name,
bool  missing_ok 
)

Definition at line 364 of file buffile.c.

365 {
366  char segment_name[MAXPGPATH];
367  int segment = 0;
368  bool found = false;
369 
370  /*
371  * We don't know how many segments the file has. We'll keep deleting
372  * until we run out. If we don't manage to find even an initial segment,
373  * raise an error.
374  */
375  for (;;)
376  {
377  FileSetSegmentName(segment_name, name, segment);
378  if (!FileSetDelete(fileset, segment_name, true))
379  break;
380  found = true;
381  ++segment;
382 
384  }
385 
386  if (!found && !missing_ok)
387  elog(ERROR, "could not delete unknown BufFile \"%s\"", name);
388 }
static void FileSetSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:222
bool FileSetDelete(FileSet *fileset, const char *name, bool error_on_failure)
Definition: fileset.c:136
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define MAXPGPATH

References CHECK_FOR_INTERRUPTS, elog, ERROR, FileSetDelete(), FileSetSegmentName(), MAXPGPATH, and name.

Referenced by stream_cleanup_files(), and subxact_info_write().

◆ BufFileExportFileSet()

void BufFileExportFileSet ( BufFile file)

Definition at line 394 of file buffile.c.

395 {
396  /* Must be a file belonging to a FileSet. */
397  Assert(file->fileset != NULL);
398 
399  /* It's probably a bug if someone calls this twice. */
400  Assert(!file->readOnly);
401 
402  BufFileFlush(file);
403  file->readOnly = true;
404 }

References Assert, BufFileFlush(), BufFile::fileset, and BufFile::readOnly.

Referenced by LogicalTapeFreeze().

◆ BufFileOpenFileSet()

BufFile* BufFileOpenFileSet ( FileSet fileset,
const char *  name,
int  mode,
bool  missing_ok 
)

Definition at line 291 of file buffile.c.

293 {
294  BufFile *file;
295  char segment_name[MAXPGPATH];
296  Size capacity = 16;
297  File *files;
298  int nfiles = 0;
299 
300  files = palloc(sizeof(File) * capacity);
301 
302  /*
303  * We don't know how many segments there are, so we'll probe the
304  * filesystem to find out.
305  */
306  for (;;)
307  {
308  /* See if we need to expand our file segment array. */
309  if (nfiles + 1 > capacity)
310  {
311  capacity *= 2;
312  files = repalloc(files, sizeof(File) * capacity);
313  }
314  /* Try to load a segment. */
315  FileSetSegmentName(segment_name, name, nfiles);
316  files[nfiles] = FileSetOpen(fileset, segment_name, mode);
317  if (files[nfiles] <= 0)
318  break;
319  ++nfiles;
320 
322  }
323 
324  /*
325  * If we didn't find any files at all, then no BufFile exists with this
326  * name.
327  */
328  if (nfiles == 0)
329  {
330  /* free the memory */
331  pfree(files);
332 
333  if (missing_ok)
334  return NULL;
335 
336  ereport(ERROR,
338  errmsg("could not open temporary file \"%s\" from BufFile \"%s\": %m",
339  segment_name, name)));
340  }
341 
342  file = makeBufFileCommon(nfiles);
343  file->files = files;
344  file->readOnly = (mode == O_RDONLY);
345  file->fileset = fileset;
346  file->name = pstrdup(name);
347 
348  return file;
349 }
size_t Size
Definition: c.h:605
int errcode_for_file_access(void)
Definition: elog.c:882
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ereport(elevel,...)
Definition: elog.h:149
File FileSetOpen(FileSet *fileset, const char *name, int mode)
Definition: fileset.c:119
static PgChecksumMode mode
Definition: pg_checksums.c:56

References CHECK_FOR_INTERRUPTS, ereport, errcode_for_file_access(), errmsg(), ERROR, BufFile::files, BufFile::fileset, FileSetOpen(), FileSetSegmentName(), makeBufFileCommon(), MAXPGPATH, mode, BufFile::name, name, palloc(), pfree(), pstrdup(), BufFile::readOnly, and repalloc().

Referenced by apply_spooled_messages(), ensure_last_message(), LogicalTapeImport(), stream_abort_internal(), stream_open_file(), sts_parallel_scan_next(), subxact_info_read(), and subxact_info_write().

◆ BufFileRead()

pg_nodiscard size_t BufFileRead ( BufFile file,
void *  ptr,
size_t  size 
)

Definition at line 645 of file buffile.c.

646 {
647  return BufFileReadCommon(file, ptr, size, false, false);
648 }
static size_t BufFileReadCommon(BufFile *file, void *ptr, size_t size, bool exact, bool eofOK)
Definition: buffile.c:593
static pg_noinline void Size size
Definition: slab.c:607

References BufFileReadCommon(), and size.

◆ BufFileReadExact()

void BufFileReadExact ( BufFile file,
void *  ptr,
size_t  size 
)

◆ BufFileReadMaybeEOF()

size_t BufFileReadMaybeEOF ( BufFile file,
void *  ptr,
size_t  size,
bool  eofOK 
)

Definition at line 664 of file buffile.c.

665 {
666  return BufFileReadCommon(file, ptr, size, true, eofOK);
667 }

References BufFileReadCommon(), and size.

Referenced by apply_spooled_messages(), ExecHashJoinGetSavedTuple(), and getlen().

◆ BufFileSeek()

int BufFileSeek ( BufFile file,
int  fileno,
off_t  offset,
int  whence 
)

Definition at line 740 of file buffile.c.

741 {
742  int newFile;
743  off_t newOffset;
744 
745  switch (whence)
746  {
747  case SEEK_SET:
748  if (fileno < 0)
749  return EOF;
750  newFile = fileno;
751  newOffset = offset;
752  break;
753  case SEEK_CUR:
754 
755  /*
756  * Relative seek considers only the signed offset, ignoring
757  * fileno. Note that large offsets (> 1 GB) risk overflow in this
758  * add, unless we have 64-bit off_t.
759  */
760  newFile = file->curFile;
761  newOffset = (file->curOffset + file->pos) + offset;
762  break;
763  case SEEK_END:
764 
765  /*
766  * The file size of the last file gives us the end offset of that
767  * file.
768  */
769  newFile = file->numFiles - 1;
770  newOffset = FileSize(file->files[file->numFiles - 1]);
771  if (newOffset < 0)
772  ereport(ERROR,
774  errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
775  FilePathName(file->files[file->numFiles - 1]),
776  file->name)));
777  break;
778  default:
779  elog(ERROR, "invalid whence: %d", whence);
780  return EOF;
781  }
782  while (newOffset < 0)
783  {
784  if (--newFile < 0)
785  return EOF;
786  newOffset += MAX_PHYSICAL_FILESIZE;
787  }
788  if (newFile == file->curFile &&
789  newOffset >= file->curOffset &&
790  newOffset <= file->curOffset + file->nbytes)
791  {
792  /*
793  * Seek is to a point within existing buffer; we can just adjust
794  * pos-within-buffer, without flushing buffer. Note this is OK
795  * whether reading or writing, but buffer remains dirty if we were
796  * writing.
797  */
798  file->pos = (int) (newOffset - file->curOffset);
799  return 0;
800  }
801  /* Otherwise, must reposition buffer, so flush any dirty data */
802  BufFileFlush(file);
803 
804  /*
805  * At this point and no sooner, check for seek past last segment. The
806  * above flush could have created a new segment, so checking sooner would
807  * not work (at least not with this code).
808  */
809 
810  /* convert seek to "start of next seg" to "end of last seg" */
811  if (newFile == file->numFiles && newOffset == 0)
812  {
813  newFile--;
814  newOffset = MAX_PHYSICAL_FILESIZE;
815  }
816  while (newOffset > MAX_PHYSICAL_FILESIZE)
817  {
818  if (++newFile >= file->numFiles)
819  return EOF;
820  newOffset -= MAX_PHYSICAL_FILESIZE;
821  }
822  if (newFile >= file->numFiles)
823  return EOF;
824  /* Seek is OK! */
825  file->curFile = newFile;
826  file->curOffset = newOffset;
827  file->pos = 0;
828  file->nbytes = 0;
829  return 0;
830 }
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:62
char * FilePathName(File file)
Definition: fd.c:2461
off_t FileSize(File file)
Definition: fd.c:2409
int nbytes
Definition: buffile.c:97
int pos
Definition: buffile.c:96
int curFile
Definition: buffile.c:94
off_t curOffset
Definition: buffile.c:95

References BufFileFlush(), BufFile::curFile, BufFile::curOffset, elog, ereport, errcode_for_file_access(), errmsg(), ERROR, FilePathName(), BufFile::files, FileSize(), MAX_PHYSICAL_FILESIZE, BufFile::name, BufFile::nbytes, BufFile::numFiles, and BufFile::pos.

Referenced by BufFileSeekBlock(), ensure_last_message(), ExecHashJoinNewBatch(), SendBackupManifest(), stream_open_file(), tuplestore_copy_read_pointer(), tuplestore_gettuple(), tuplestore_puttuple_common(), tuplestore_rescan(), and tuplestore_select_read_pointer().

◆ BufFileSeekBlock()

int BufFileSeekBlock ( BufFile file,
int64  blknum 
)

Definition at line 851 of file buffile.c.

852 {
853  return BufFileSeek(file,
854  (int) (blknum / BUFFILE_SEG_SIZE),
855  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
856  SEEK_SET);
857 }
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:740

References BUFFILE_SEG_SIZE, and BufFileSeek().

Referenced by ltsReadBlock(), ltsWriteBlock(), ReadTempFileBlock(), sts_parallel_scan_next(), and WriteTempFileBlock().

◆ BufFileSize()

int64 BufFileSize ( BufFile file)

Definition at line 866 of file buffile.c.

867 {
868  int64 lastFileSize;
869 
870  Assert(file->fileset != NULL);
871 
872  /* Get the size of the last physical file. */
873  lastFileSize = FileSize(file->files[file->numFiles - 1]);
874  if (lastFileSize < 0)
875  ereport(ERROR,
877  errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
878  FilePathName(file->files[file->numFiles - 1]),
879  file->name)));
880 
881  return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +
882  lastFileSize;
883 }

References Assert, ereport, errcode_for_file_access(), errmsg(), ERROR, FilePathName(), BufFile::files, BufFile::fileset, FileSize(), MAX_PHYSICAL_FILESIZE, BufFile::name, and BufFile::numFiles.

Referenced by LogicalTapeImport().

◆ BufFileTell()

void BufFileTell ( BufFile file,
int *  fileno,
off_t *  offset 
)

◆ BufFileTruncateFileSet()

void BufFileTruncateFileSet ( BufFile file,
int  fileno,
off_t  offset 
)

Definition at line 933 of file buffile.c.

934 {
935  int numFiles = file->numFiles;
936  int newFile = fileno;
937  off_t newOffset = file->curOffset;
938  char segment_name[MAXPGPATH];
939  int i;
940 
941  /*
942  * Loop over all the files up to the given fileno and remove the files
943  * that are greater than the fileno and truncate the given file up to the
944  * offset. Note that we also remove the given fileno if the offset is 0
945  * provided it is not the first file in which we truncate it.
946  */
947  for (i = file->numFiles - 1; i >= fileno; i--)
948  {
949  if ((i != fileno || offset == 0) && i != 0)
950  {
951  FileSetSegmentName(segment_name, file->name, i);
952  FileClose(file->files[i]);
953  if (!FileSetDelete(file->fileset, segment_name, true))
954  ereport(ERROR,
956  errmsg("could not delete fileset \"%s\": %m",
957  segment_name)));
958  numFiles--;
959  newOffset = MAX_PHYSICAL_FILESIZE;
960 
961  /*
962  * This is required to indicate that we have deleted the given
963  * fileno.
964  */
965  if (i == fileno)
966  newFile--;
967  }
968  else
969  {
970  if (FileTruncate(file->files[i], offset,
971  WAIT_EVENT_BUFFILE_TRUNCATE) < 0)
972  ereport(ERROR,
974  errmsg("could not truncate file \"%s\": %m",
975  FilePathName(file->files[i]))));
976  newOffset = offset;
977  }
978  }
979 
980  file->numFiles = numFiles;
981 
982  /*
983  * If the truncate point is within existing buffer then we can just adjust
984  * pos within buffer.
985  */
986  if (newFile == file->curFile &&
987  newOffset >= file->curOffset &&
988  newOffset <= file->curOffset + file->nbytes)
989  {
990  /* No need to reset the current pos if the new pos is greater. */
991  if (newOffset <= file->curOffset + file->pos)
992  file->pos = (int) (newOffset - file->curOffset);
993 
994  /* Adjust the nbytes for the current buffer. */
995  file->nbytes = (int) (newOffset - file->curOffset);
996  }
997  else if (newFile == file->curFile &&
998  newOffset < file->curOffset)
999  {
1000  /*
1001  * The truncate point is within the existing file but prior to the
1002  * current position, so we can forget the current buffer and reset the
1003  * current position.
1004  */
1005  file->curOffset = newOffset;
1006  file->pos = 0;
1007  file->nbytes = 0;
1008  }
1009  else if (newFile < file->curFile)
1010  {
1011  /*
1012  * The truncate point is prior to the current file, so need to reset
1013  * the current position accordingly.
1014  */
1015  file->curFile = newFile;
1016  file->curOffset = newOffset;
1017  file->pos = 0;
1018  file->nbytes = 0;
1019  }
1020  /* Nothing to do, if the truncate point is beyond current file. */
1021 }
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2426

References BufFile::curFile, BufFile::curOffset, ereport, errcode_for_file_access(), errmsg(), ERROR, FileClose(), FilePathName(), BufFile::files, BufFile::fileset, FileSetDelete(), FileSetSegmentName(), FileTruncate(), i, MAX_PHYSICAL_FILESIZE, MAXPGPATH, BufFile::name, BufFile::nbytes, BufFile::numFiles, and BufFile::pos.

Referenced by stream_abort_internal().

◆ BufFileWrite()

void BufFileWrite ( BufFile file,
const void *  ptr,
size_t  size 
)

Definition at line 676 of file buffile.c.

677 {
678  size_t nthistime;
679 
680  Assert(!file->readOnly);
681 
682  while (size > 0)
683  {
684  if (file->pos >= BLCKSZ)
685  {
686  /* Buffer full, dump it out */
687  if (file->dirty)
688  BufFileDumpBuffer(file);
689  else
690  {
691  /* Hmm, went directly from reading to writing? */
692  file->curOffset += file->pos;
693  file->pos = 0;
694  file->nbytes = 0;
695  }
696  }
697 
698  nthistime = BLCKSZ - file->pos;
699  if (nthistime > size)
700  nthistime = size;
701  Assert(nthistime > 0);
702 
703  memcpy(file->buffer.data + file->pos, ptr, nthistime);
704 
705  file->dirty = true;
706  file->pos += nthistime;
707  if (file->nbytes < file->pos)
708  file->nbytes = file->pos;
709  ptr = (const char *) ptr + nthistime;
710  size -= nthistime;
711  }
712 }
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:494
PGAlignedBlock buffer
Definition: buffile.c:103
bool dirty
Definition: buffile.c:77
char data[BLCKSZ]
Definition: c.h:1119

References Assert, BufFile::buffer, BufFileDumpBuffer(), BufFile::curOffset, PGAlignedBlock::data, BufFile::dirty, BufFile::nbytes, BufFile::pos, BufFile::readOnly, and size.

Referenced by AppendStringToManifest(), ExecHashJoinSaveTuple(), ltsWriteBlock(), stream_write_change(), sts_flush_chunk(), subxact_info_write(), WriteTempFileBlock(), and writetup_heap().