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, long blknum)
 
int64 BufFileSize (BufFile *file)
 
long 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()

long BufFileAppend ( BufFile target,
BufFile source 
)

Definition at line 923 of file buffile.c.

924 {
925  long startBlock = target->numFiles * BUFFILE_SEG_SIZE;
926  int newNumFiles = target->numFiles + source->numFiles;
927  int i;
928 
929  Assert(target->fileset != NULL);
930  Assert(source->readOnly);
931  Assert(!source->dirty);
932  Assert(source->fileset != NULL);
933 
934  if (target->resowner != source->resowner)
935  elog(ERROR, "could not append BufFile with non-matching resource owner");
936 
937  target->files = (File *)
938  repalloc(target->files, sizeof(File) * newNumFiles);
939  for (i = target->numFiles; i < newNumFiles; i++)
940  target->files[i] = source->files[i - target->numFiles];
941  target->numFiles = newNumFiles;
942 
943  return startBlock;
944 }
#define BUFFILE_SEG_SIZE
Definition: buffile.c:63
#define ERROR
Definition: elog.h:39
int File
Definition: fd.h:49
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1476
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:1930
void pfree(void *pointer)
Definition: mcxt.c:1456

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:1644
void * palloc(Size size)
Definition: mcxt.c:1226
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:1337
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:139
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1676
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:139
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
#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:594
int errcode_for_file_access(void)
Definition: elog.c:881
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
File FileSetOpen(FileSet *fileset, const char *name, int mode)
Definition: fileset.c:122
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

References BufFileReadCommon().

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

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:2406
off_t FileSize(File file)
Definition: fd.c:2354
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,
long  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 884 of file buffile.c.

885 {
886  int64 lastFileSize;
887 
888  Assert(file->fileset != NULL);
889 
890  /* Get the size of the last physical file. */
891  lastFileSize = FileSize(file->files[file->numFiles - 1]);
892  if (lastFileSize < 0)
893  ereport(ERROR,
895  errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
896  FilePathName(file->files[file->numFiles - 1]),
897  file->name)));
898 
899  return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +
900  lastFileSize;
901 }

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 951 of file buffile.c.

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

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:1132

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

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