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

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

408 {
409  int i;
410 
411  /* flush any unwritten data */
412  BufFileFlush(file);
413  /* close and delete the underlying file(s) */
414  for (i = 0; i < file->numFiles; i++)
415  FileClose(file->files[i]);
416  /* release the buffer space */
417  pfree(file->files);
418  pfree(file);
419 }
static void BufFileFlush(BufFile *file)
Definition: buffile.c:717
void FileClose(File file)
Definition: fd.c:1884
void pfree(void *pointer)
Definition: mcxt.c:1436

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

263 {
264  BufFile *file;
265 
266  file = makeBufFileCommon(1);
267  file->fileset = fileset;
268  file->name = pstrdup(name);
269  file->files = (File *) palloc(sizeof(File));
270  file->files[0] = MakeNewFileSetSegment(file, 0);
271  file->readOnly = false;
272 
273  return file;
274 }
static BufFile * makeBufFileCommon(int nfiles)
Definition: buffile.c:113
static File MakeNewFileSetSegment(BufFile *buffile, int segment)
Definition: buffile.c:226
const char * name
Definition: encode.c:571
char * pstrdup(const char *in)
Definition: mcxt.c:1624
void * palloc(Size size)
Definition: mcxt.c:1210
bool readOnly
Definition: buffile.c:78
const char * name
Definition: buffile.c:81

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

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

360 {
361  char segment_name[MAXPGPATH];
362  int segment = 0;
363  bool found = false;
364 
365  /*
366  * We don't know how many segments the file has. We'll keep deleting
367  * until we run out. If we don't manage to find even an initial segment,
368  * raise an error.
369  */
370  for (;;)
371  {
372  FileSetSegmentName(segment_name, name, segment);
373  if (!FileSetDelete(fileset, segment_name, true))
374  break;
375  found = true;
376  ++segment;
377 
379  }
380 
381  if (!found && !missing_ok)
382  elog(ERROR, "could not delete unknown BufFile \"%s\"", name);
383 }
static void FileSetSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:217
bool FileSetDelete(FileSet *fileset, const char *name, bool error_on_failure)
Definition: fileset.c:138
#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 389 of file buffile.c.

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

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

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

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

643 {
644  return BufFileReadCommon(file, ptr, size, false, false);
645 }
static size_t BufFileReadCommon(BufFile *file, void *ptr, size_t size, bool exact, bool eofOK)
Definition: buffile.c:590

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

662 {
663  return BufFileReadCommon(file, ptr, size, true, eofOK);
664 }

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

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

849 {
850  return BufFileSeek(file,
851  (int) (blknum / BUFFILE_SEG_SIZE),
852  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
853  SEEK_SET);
854 }
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:737

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

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

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

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

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, BufFile::pos, and WAIT_EVENT_BUFFILE_TRUNCATE.

Referenced by stream_abort_internal().

◆ BufFileWrite()

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

Definition at line 673 of file buffile.c.

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

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