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)
 
size_t BufFileRead (BufFile *file, void *ptr, size_t size)
 
void BufFileWrite (BufFile *file, 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 33 of file buffile.h.

Function Documentation

◆ BufFileAppend()

long BufFileAppend ( BufFile target,
BufFile source 
)

Definition at line 845 of file buffile.c.

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

Referenced by ltsConcatWorkerTapes().

846 {
847  long startBlock = target->numFiles * BUFFILE_SEG_SIZE;
848  int newNumFiles = target->numFiles + source->numFiles;
849  int i;
850 
851  Assert(target->fileset != NULL);
852  Assert(source->readOnly);
853  Assert(!source->dirty);
854  Assert(source->fileset != NULL);
855 
856  if (target->resowner != source->resowner)
857  elog(ERROR, "could not append BufFile with non-matching resource owner");
858 
859  target->files = (File *)
860  repalloc(target->files, sizeof(File) * newNumFiles);
861  for (i = target->numFiles; i < newNumFiles; i++)
862  target->files[i] = source->files[i - target->numFiles];
863  target->numFiles = newNumFiles;
864 
865  return startBlock;
866 }
bool readOnly
Definition: buffile.c:78
#define BUFFILE_SEG_SIZE
Definition: buffile.c:63
int numFiles
Definition: buffile.c:72
ResourceOwner resowner
Definition: buffile.c:88
FileSet * fileset
Definition: buffile.c:80
#define ERROR
Definition: elog.h:46
File * files
Definition: buffile.c:74
#define Assert(condition)
Definition: c.h:804
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
bool dirty
Definition: buffile.c:77
#define elog(elevel,...)
Definition: elog.h:232
int i
int File
Definition: fd.h:54

◆ BufFileClose()

void BufFileClose ( BufFile file)

Definition at line 407 of file buffile.c.

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

Referenced by apply_handle_stream_abort(), apply_spooled_messages(), ExecHashJoinNewBatch(), ExecHashTableDestroy(), gistFreeBuildBuffers(), LogicalTapeSetClose(), SendBackupManifest(), 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().

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:642
int numFiles
Definition: buffile.c:72
void pfree(void *pointer)
Definition: mcxt.c:1169
File * files
Definition: buffile.c:74
void FileClose(File file)
Definition: fd.c:1959
int i

◆ BufFileCreateFileSet()

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

Definition at line 262 of file buffile.c.

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

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

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 }
char * pstrdup(const char *in)
Definition: mcxt.c:1299
bool readOnly
Definition: buffile.c:78
FileSet * fileset
Definition: buffile.c:80
File * files
Definition: buffile.c:74
const char * name
Definition: buffile.c:81
static File MakeNewFileSetSegment(BufFile *file, int segment)
Definition: buffile.c:226
const char * name
Definition: encode.c:561
void * palloc(Size size)
Definition: mcxt.c:1062
int File
Definition: fd.h:54
static BufFile * makeBufFileCommon(int nfiles)
Definition: buffile.c:113

◆ BufFileCreateTemp()

BufFile* BufFileCreateTemp ( bool  interXact)

Definition at line 188 of file buffile.c.

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

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

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:1332
bool isInterXact
Definition: buffile.c:76
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:134
#define Assert(condition)
Definition: c.h:804
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1705
int File
Definition: fd.h:54

◆ BufFileDeleteFileSet()

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

Definition at line 359 of file buffile.c.

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

Referenced by stream_cleanup_files(), and subxact_info_write().

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
#define ERROR
Definition: elog.h:46
#define MAXPGPATH
bool FileSetDelete(FileSet *fileset, const char *name, bool error_on_failure)
Definition: fileset.c:138
const char * name
Definition: encode.c:561
#define elog(elevel,...)
Definition: elog.h:232
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120

◆ BufFileExportFileSet()

void BufFileExportFileSet ( BufFile file)

Definition at line 389 of file buffile.c.

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

Referenced by LogicalTapeFreeze().

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 }
static void BufFileFlush(BufFile *file)
Definition: buffile.c:642
bool readOnly
Definition: buffile.c:78
FileSet * fileset
Definition: buffile.c:80
#define Assert(condition)
Definition: c.h:804

◆ BufFileOpenFileSet()

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

Definition at line 286 of file buffile.c.

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

Referenced by apply_handle_stream_abort(), apply_spooled_messages(), ltsConcatWorkerTapes(), stream_open_file(), sts_parallel_scan_next(), subxact_info_read(), and subxact_info_write().

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 }
static PgChecksumMode mode
Definition: pg_checksums.c:65
File FileSetOpen(FileSet *fileset, const char *name, int mode)
Definition: fileset.c:121
char * pstrdup(const char *in)
Definition: mcxt.c:1299
bool readOnly
Definition: buffile.c:78
static void FileSetSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:217
FileSet * fileset
Definition: buffile.c:80
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:721
File * files
Definition: buffile.c:74
const char * name
Definition: buffile.c:81
#define ereport(elevel,...)
Definition: elog.h:157
size_t Size
Definition: c.h:540
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
const char * name
Definition: encode.c:561
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
int File
Definition: fd.h:54
static BufFile * makeBufFileCommon(int nfiles)
Definition: buffile.c:113

◆ BufFileRead()

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

Definition at line 555 of file buffile.c.

References Assert, BufFile::buffer, BufFileFlush(), BufFileLoadBuffer(), BufFile::curOffset, PGAlignedBlock::data, BufFile::nbytes, and BufFile::pos.

Referenced by apply_spooled_messages(), ExecHashJoinGetSavedTuple(), getlen(), ltsReadBlock(), ReadTempFileBlock(), readtup_heap(), SendBackupManifest(), sts_parallel_scan_next(), sts_read_tuple(), and subxact_info_read().

556 {
557  size_t nread = 0;
558  size_t nthistime;
559 
560  BufFileFlush(file);
561 
562  while (size > 0)
563  {
564  if (file->pos >= file->nbytes)
565  {
566  /* Try to load more data into buffer. */
567  file->curOffset += file->pos;
568  file->pos = 0;
569  file->nbytes = 0;
570  BufFileLoadBuffer(file);
571  if (file->nbytes <= 0)
572  break; /* no more data available */
573  }
574 
575  nthistime = file->nbytes - file->pos;
576  if (nthistime > size)
577  nthistime = size;
578  Assert(nthistime > 0);
579 
580  memcpy(ptr, file->buffer.data + file->pos, nthistime);
581 
582  file->pos += nthistime;
583  ptr = (void *) ((char *) ptr + nthistime);
584  size -= nthistime;
585  nread += nthistime;
586  }
587 
588  return nread;
589 }
static void BufFileFlush(BufFile *file)
Definition: buffile.c:642
int nbytes
Definition: buffile.c:97
PGAlignedBlock buffer
Definition: buffile.c:98
char data[BLCKSZ]
Definition: c.h:1141
off_t curOffset
Definition: buffile.c:95
#define Assert(condition)
Definition: c.h:804
static void BufFileLoadBuffer(BufFile *file)
Definition: buffile.c:429
int pos
Definition: buffile.c:96

◆ BufFileSeek()

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

Definition at line 662 of file buffile.c.

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(), ExecHashJoinNewBatch(), SendBackupManifest(), stream_open_file(), tuplestore_copy_read_pointer(), tuplestore_gettuple(), tuplestore_puttuple_common(), tuplestore_rescan(), and tuplestore_select_read_pointer().

663 {
664  int newFile;
665  off_t newOffset;
666 
667  switch (whence)
668  {
669  case SEEK_SET:
670  if (fileno < 0)
671  return EOF;
672  newFile = fileno;
673  newOffset = offset;
674  break;
675  case SEEK_CUR:
676 
677  /*
678  * Relative seek considers only the signed offset, ignoring
679  * fileno. Note that large offsets (> 1 GB) risk overflow in this
680  * add, unless we have 64-bit off_t.
681  */
682  newFile = file->curFile;
683  newOffset = (file->curOffset + file->pos) + offset;
684  break;
685  case SEEK_END:
686 
687  /*
688  * The file size of the last file gives us the end offset of that
689  * file.
690  */
691  newFile = file->numFiles - 1;
692  newOffset = FileSize(file->files[file->numFiles - 1]);
693  if (newOffset < 0)
694  ereport(ERROR,
696  errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
697  FilePathName(file->files[file->numFiles - 1]),
698  file->name)));
699  break;
700  default:
701  elog(ERROR, "invalid whence: %d", whence);
702  return EOF;
703  }
704  while (newOffset < 0)
705  {
706  if (--newFile < 0)
707  return EOF;
708  newOffset += MAX_PHYSICAL_FILESIZE;
709  }
710  if (newFile == file->curFile &&
711  newOffset >= file->curOffset &&
712  newOffset <= file->curOffset + file->nbytes)
713  {
714  /*
715  * Seek is to a point within existing buffer; we can just adjust
716  * pos-within-buffer, without flushing buffer. Note this is OK
717  * whether reading or writing, but buffer remains dirty if we were
718  * writing.
719  */
720  file->pos = (int) (newOffset - file->curOffset);
721  return 0;
722  }
723  /* Otherwise, must reposition buffer, so flush any dirty data */
724  BufFileFlush(file);
725 
726  /*
727  * At this point and no sooner, check for seek past last segment. The
728  * above flush could have created a new segment, so checking sooner would
729  * not work (at least not with this code).
730  */
731 
732  /* convert seek to "start of next seg" to "end of last seg" */
733  if (newFile == file->numFiles && newOffset == 0)
734  {
735  newFile--;
736  newOffset = MAX_PHYSICAL_FILESIZE;
737  }
738  while (newOffset > MAX_PHYSICAL_FILESIZE)
739  {
740  if (++newFile >= file->numFiles)
741  return EOF;
742  newOffset -= MAX_PHYSICAL_FILESIZE;
743  }
744  if (newFile >= file->numFiles)
745  return EOF;
746  /* Seek is OK! */
747  file->curFile = newFile;
748  file->curOffset = newOffset;
749  file->pos = 0;
750  file->nbytes = 0;
751  return 0;
752 }
static void BufFileFlush(BufFile *file)
Definition: buffile.c:642
int nbytes
Definition: buffile.c:97
off_t FileSize(File file)
Definition: fd.c:2285
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:62
int numFiles
Definition: buffile.c:72
char * FilePathName(File file)
Definition: fd.c:2337
#define ERROR
Definition: elog.h:46
int errcode_for_file_access(void)
Definition: elog.c:721
File * files
Definition: buffile.c:74
off_t curOffset
Definition: buffile.c:95
const char * name
Definition: buffile.c:81
int curFile
Definition: buffile.c:94
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
int pos
Definition: buffile.c:96

◆ BufFileSeekBlock()

int BufFileSeekBlock ( BufFile file,
long  blknum 
)

Definition at line 773 of file buffile.c.

References BUFFILE_SEG_SIZE, BufFileSeek(), BufFile::curFile, BufFile::curOffset, and BufFile::pos.

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

774 {
775  return BufFileSeek(file,
776  (int) (blknum / BUFFILE_SEG_SIZE),
777  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
778  SEEK_SET);
779 }
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:662
#define BUFFILE_SEG_SIZE
Definition: buffile.c:63

◆ BufFileSize()

int64 BufFileSize ( BufFile file)

Definition at line 806 of file buffile.c.

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

807 {
808  int64 lastFileSize;
809 
810  Assert(file->fileset != NULL);
811 
812  /* Get the size of the last physical file. */
813  lastFileSize = FileSize(file->files[file->numFiles - 1]);
814  if (lastFileSize < 0)
815  ereport(ERROR,
817  errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
818  FilePathName(file->files[file->numFiles - 1]),
819  file->name)));
820 
821  return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +
822  lastFileSize;
823 }
off_t FileSize(File file)
Definition: fd.c:2285
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:62
int numFiles
Definition: buffile.c:72
char * FilePathName(File file)
Definition: fd.c:2337
FileSet * fileset
Definition: buffile.c:80
#define ERROR
Definition: elog.h:46
int errcode_for_file_access(void)
Definition: elog.c:721
File * files
Definition: buffile.c:74
const char * name
Definition: buffile.c:81
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ BufFileTell()

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

Definition at line 755 of file buffile.c.

References BufFile::curFile, BufFile::curOffset, and BufFile::pos.

Referenced by dumptuples(), subxact_info_add(), tuplestore_copy_read_pointer(), tuplestore_gettuple(), tuplestore_puttuple_common(), and tuplestore_select_read_pointer().

756 {
757  *fileno = file->curFile;
758  *offset = file->curOffset + file->pos;
759 }
off_t curOffset
Definition: buffile.c:95
int curFile
Definition: buffile.c:94
int pos
Definition: buffile.c:96

◆ BufFileTruncateFileSet()

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

Definition at line 873 of file buffile.c.

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

874 {
875  int numFiles = file->numFiles;
876  int newFile = fileno;
877  off_t newOffset = file->curOffset;
878  char segment_name[MAXPGPATH];
879  int i;
880 
881  /*
882  * Loop over all the files up to the given fileno and remove the files
883  * that are greater than the fileno and truncate the given file up to the
884  * offset. Note that we also remove the given fileno if the offset is 0
885  * provided it is not the first file in which we truncate it.
886  */
887  for (i = file->numFiles - 1; i >= fileno; i--)
888  {
889  if ((i != fileno || offset == 0) && i != 0)
890  {
891  FileSetSegmentName(segment_name, file->name, i);
892  FileClose(file->files[i]);
893  if (!FileSetDelete(file->fileset, segment_name, true))
894  ereport(ERROR,
896  errmsg("could not delete fileset \"%s\": %m",
897  segment_name)));
898  numFiles--;
899  newOffset = MAX_PHYSICAL_FILESIZE;
900 
901  /*
902  * This is required to indicate that we have deleted the given
903  * fileno.
904  */
905  if (i == fileno)
906  newFile--;
907  }
908  else
909  {
910  if (FileTruncate(file->files[i], offset,
912  ereport(ERROR,
914  errmsg("could not truncate file \"%s\": %m",
915  FilePathName(file->files[i]))));
916  newOffset = offset;
917  }
918  }
919 
920  file->numFiles = numFiles;
921 
922  /*
923  * If the truncate point is within existing buffer then we can just adjust
924  * pos within buffer.
925  */
926  if (newFile == file->curFile &&
927  newOffset >= file->curOffset &&
928  newOffset <= file->curOffset + file->nbytes)
929  {
930  /* No need to reset the current pos if the new pos is greater. */
931  if (newOffset <= file->curOffset + file->pos)
932  file->pos = (int) (newOffset - file->curOffset);
933 
934  /* Adjust the nbytes for the current buffer. */
935  file->nbytes = (int) (newOffset - file->curOffset);
936  }
937  else if (newFile == file->curFile &&
938  newOffset < file->curOffset)
939  {
940  /*
941  * The truncate point is within the existing file but prior to the
942  * current position, so we can forget the current buffer and reset the
943  * current position.
944  */
945  file->curOffset = newOffset;
946  file->pos = 0;
947  file->nbytes = 0;
948  }
949  else if (newFile < file->curFile)
950  {
951  /*
952  * The truncate point is prior to the current file, so need to reset
953  * the current position accordingly.
954  */
955  file->curFile = newFile;
956  file->curOffset = newOffset;
957  file->pos = 0;
958  file->nbytes = 0;
959  }
960  /* Nothing to do, if the truncate point is beyond current file. */
961 }
int nbytes
Definition: buffile.c:97
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:62
int numFiles
Definition: buffile.c:72
char * FilePathName(File file)
Definition: fd.c:2337
static void FileSetSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:217
FileSet * fileset
Definition: buffile.c:80
#define ERROR
Definition: elog.h:46
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:721
File * files
Definition: buffile.c:74
off_t curOffset
Definition: buffile.c:95
const char * name
Definition: buffile.c:81
int curFile
Definition: buffile.c:94
#define ereport(elevel,...)
Definition: elog.h:157
void FileClose(File file)
Definition: fd.c:1959
bool FileSetDelete(FileSet *fileset, const char *name, bool error_on_failure)
Definition: fileset.c:138
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2302
int pos
Definition: buffile.c:96

◆ BufFileWrite()

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

Definition at line 598 of file buffile.c.

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

599 {
600  size_t nthistime;
601 
602  Assert(!file->readOnly);
603 
604  while (size > 0)
605  {
606  if (file->pos >= BLCKSZ)
607  {
608  /* Buffer full, dump it out */
609  if (file->dirty)
610  BufFileDumpBuffer(file);
611  else
612  {
613  /* Hmm, went directly from reading to writing? */
614  file->curOffset += file->pos;
615  file->pos = 0;
616  file->nbytes = 0;
617  }
618  }
619 
620  nthistime = BLCKSZ - file->pos;
621  if (nthistime > size)
622  nthistime = size;
623  Assert(nthistime > 0);
624 
625  memcpy(file->buffer.data + file->pos, ptr, nthistime);
626 
627  file->dirty = true;
628  file->pos += nthistime;
629  if (file->nbytes < file->pos)
630  file->nbytes = file->pos;
631  ptr = (void *) ((char *) ptr + nthistime);
632  size -= nthistime;
633  }
634 }
int nbytes
Definition: buffile.c:97
bool readOnly
Definition: buffile.c:78
PGAlignedBlock buffer
Definition: buffile.c:98
char data[BLCKSZ]
Definition: c.h:1141
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:475
off_t curOffset
Definition: buffile.c:95
#define Assert(condition)
Definition: c.h:804
bool dirty
Definition: buffile.c:77
int pos
Definition: buffile.c:96