PostgreSQL Source Code  git master
buffile.h File Reference
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)
 
BufFileBufFileCreateShared (SharedFileSet *fileset, const char *name)
 
void BufFileExportShared (BufFile *file)
 
BufFileBufFileOpenShared (SharedFileSet *fileset, const char *name, int mode)
 
void BufFileDeleteShared (SharedFileSet *fileset, const char *name)
 
void BufFileTruncateShared (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 833 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().

834 {
835  long startBlock = target->numFiles * BUFFILE_SEG_SIZE;
836  int newNumFiles = target->numFiles + source->numFiles;
837  int i;
838 
839  Assert(target->fileset != NULL);
840  Assert(source->readOnly);
841  Assert(!source->dirty);
842  Assert(source->fileset != NULL);
843 
844  if (target->resowner != source->resowner)
845  elog(ERROR, "could not append BufFile with non-matching resource owner");
846 
847  target->files = (File *)
848  repalloc(target->files, sizeof(File) * newNumFiles);
849  for (i = target->numFiles; i < newNumFiles; i++)
850  target->files[i] = source->files[i - target->numFiles];
851  target->numFiles = newNumFiles;
852 
853  return startBlock;
854 }
SharedFileSet * fileset
Definition: buffile.c:80
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
#define ERROR
Definition: elog.h:43
File * files
Definition: buffile.c:74
#define Assert(condition)
Definition: c.h:745
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
bool dirty
Definition: buffile.c:77
#define elog(elevel,...)
Definition: elog.h:214
int i
int File
Definition: fd.h:49

◆ BufFileClose()

void BufFileClose ( BufFile file)

Definition at line 395 of file buffile.c.

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

Referenced by apply_handle_stream_abort(), apply_handle_stream_commit(), 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().

396 {
397  int i;
398 
399  /* flush any unwritten data */
400  BufFileFlush(file);
401  /* close and delete the underlying file(s) */
402  for (i = 0; i < file->numFiles; i++)
403  FileClose(file->files[i]);
404  /* release the buffer space */
405  pfree(file->files);
406  pfree(file);
407 }
static void BufFileFlush(BufFile *file)
Definition: buffile.c:630
int numFiles
Definition: buffile.c:72
void pfree(void *pointer)
Definition: mcxt.c:1057
File * files
Definition: buffile.c:74
void FileClose(File file)
Definition: fd.c:1826
int i

◆ BufFileCreateShared()

BufFile* BufFileCreateShared ( SharedFileSet fileset,
const char *  name 
)

Definition at line 262 of file buffile.c.

References BufFile::files, BufFile::fileset, makeBufFileCommon(), MakeNewSharedSegment(), 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] = MakeNewSharedSegment(file, 0);
271  file->readOnly = false;
272 
273  return file;
274 }
SharedFileSet * fileset
Definition: buffile.c:80
char * pstrdup(const char *in)
Definition: mcxt.c:1187
bool readOnly
Definition: buffile.c:78
static File MakeNewSharedSegment(BufFile *file, int segment)
Definition: buffile.c:226
File * files
Definition: buffile.c:74
const char * name
Definition: buffile.c:81
const char * name
Definition: encode.c:561
void * palloc(Size size)
Definition: mcxt.c:950
int File
Definition: fd.h:49
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:1326
bool isInterXact
Definition: buffile.c:76
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:134
#define Assert(condition)
Definition: c.h:745
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1578
int File
Definition: fd.h:49

◆ BufFileDeleteShared()

void BufFileDeleteShared ( SharedFileSet fileset,
const char *  name 
)

Definition at line 347 of file buffile.c.

References CHECK_FOR_INTERRUPTS, elog, ERROR, MAXPGPATH, SharedFileSetDelete(), and SharedSegmentName().

348 {
349  char segment_name[MAXPGPATH];
350  int segment = 0;
351  bool found = false;
352 
353  /*
354  * We don't know how many segments the file has. We'll keep deleting
355  * until we run out. If we don't manage to find even an initial segment,
356  * raise an error.
357  */
358  for (;;)
359  {
360  SharedSegmentName(segment_name, name, segment);
361  if (!SharedFileSetDelete(fileset, segment_name, true))
362  break;
363  found = true;
364  ++segment;
365 
367  }
368 
369  if (!found)
370  elog(ERROR, "could not delete unknown shared BufFile \"%s\"", name);
371 }
bool SharedFileSetDelete(SharedFileSet *fileset, const char *name, bool error_on_failure)
#define ERROR
Definition: elog.h:43
static void SharedSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:217
#define MAXPGPATH
const char * name
Definition: encode.c:561
#define elog(elevel,...)
Definition: elog.h:214
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ BufFileExportShared()

void BufFileExportShared ( BufFile file)

Definition at line 377 of file buffile.c.

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

Referenced by LogicalTapeFreeze().

378 {
379  /* Must be a file belonging to a SharedFileSet. */
380  Assert(file->fileset != NULL);
381 
382  /* It's probably a bug if someone calls this twice. */
383  Assert(!file->readOnly);
384 
385  BufFileFlush(file);
386  file->readOnly = true;
387 }
SharedFileSet * fileset
Definition: buffile.c:80
static void BufFileFlush(BufFile *file)
Definition: buffile.c:630
bool readOnly
Definition: buffile.c:78
#define Assert(condition)
Definition: c.h:745

◆ BufFileOpenShared()

BufFile* BufFileOpenShared ( SharedFileSet fileset,
const char *  name,
int  mode 
)

Definition at line 284 of file buffile.c.

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

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

285 {
286  BufFile *file;
287  char segment_name[MAXPGPATH];
288  Size capacity = 16;
289  File *files;
290  int nfiles = 0;
291 
292  files = palloc(sizeof(File) * capacity);
293 
294  /*
295  * We don't know how many segments there are, so we'll probe the
296  * filesystem to find out.
297  */
298  for (;;)
299  {
300  /* See if we need to expand our file segment array. */
301  if (nfiles + 1 > capacity)
302  {
303  capacity *= 2;
304  files = repalloc(files, sizeof(File) * capacity);
305  }
306  /* Try to load a segment. */
307  SharedSegmentName(segment_name, name, nfiles);
308  files[nfiles] = SharedFileSetOpen(fileset, segment_name, mode);
309  if (files[nfiles] <= 0)
310  break;
311  ++nfiles;
312 
314  }
315 
316  /*
317  * If we didn't find any files at all, then no BufFile exists with this
318  * name.
319  */
320  if (nfiles == 0)
321  ereport(ERROR,
323  errmsg("could not open temporary file \"%s\" from BufFile \"%s\": %m",
324  segment_name, name)));
325 
326  file = makeBufFileCommon(nfiles);
327  file->files = files;
328  file->readOnly = (mode == O_RDONLY) ? true : false;
329  file->fileset = fileset;
330  file->name = pstrdup(name);
331 
332  return file;
333 }
SharedFileSet * fileset
Definition: buffile.c:80
static PgChecksumMode mode
Definition: pg_checksums.c:61
char * pstrdup(const char *in)
Definition: mcxt.c:1187
bool readOnly
Definition: buffile.c:78
static int64 files
Definition: pg_checksums.c:34
#define ERROR
Definition: elog.h:43
static void SharedSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:217
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:633
File * files
Definition: buffile.c:74
const char * name
Definition: buffile.c:81
#define ereport(elevel,...)
Definition: elog.h:144
size_t Size
Definition: c.h:473
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
const char * name
Definition: encode.c:561
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
File SharedFileSetOpen(SharedFileSet *fileset, const char *name, int mode)
int File
Definition: fd.h:49
static BufFile * makeBufFileCommon(int nfiles)
Definition: buffile.c:113

◆ BufFileRead()

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

Definition at line 543 of file buffile.c.

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

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

544 {
545  size_t nread = 0;
546  size_t nthistime;
547 
548  BufFileFlush(file);
549 
550  while (size > 0)
551  {
552  if (file->pos >= file->nbytes)
553  {
554  /* Try to load more data into buffer. */
555  file->curOffset += file->pos;
556  file->pos = 0;
557  file->nbytes = 0;
558  BufFileLoadBuffer(file);
559  if (file->nbytes <= 0)
560  break; /* no more data available */
561  }
562 
563  nthistime = file->nbytes - file->pos;
564  if (nthistime > size)
565  nthistime = size;
566  Assert(nthistime > 0);
567 
568  memcpy(ptr, file->buffer.data + file->pos, nthistime);
569 
570  file->pos += nthistime;
571  ptr = (void *) ((char *) ptr + nthistime);
572  size -= nthistime;
573  nread += nthistime;
574  }
575 
576  return nread;
577 }
static void BufFileFlush(BufFile *file)
Definition: buffile.c:630
int nbytes
Definition: buffile.c:97
PGAlignedBlock buffer
Definition: buffile.c:98
char data[BLCKSZ]
Definition: c.h:1082
off_t curOffset
Definition: buffile.c:95
#define Assert(condition)
Definition: c.h:745
static void BufFileLoadBuffer(BufFile *file)
Definition: buffile.c:417
int pos
Definition: buffile.c:96

◆ BufFileSeek()

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

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

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

◆ BufFileSeekBlock()

int BufFileSeekBlock ( BufFile file,
long  blknum 
)

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

762 {
763  return BufFileSeek(file,
764  (int) (blknum / BUFFILE_SEG_SIZE),
765  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
766  SEEK_SET);
767 }
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:650
#define BUFFILE_SEG_SIZE
Definition: buffile.c:63

◆ BufFileSize()

int64 BufFileSize ( BufFile file)

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

795 {
796  int64 lastFileSize;
797 
798  Assert(file->fileset != NULL);
799 
800  /* Get the size of the last physical file. */
801  lastFileSize = FileSize(file->files[file->numFiles - 1]);
802  if (lastFileSize < 0)
803  ereport(ERROR,
805  errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
806  FilePathName(file->files[file->numFiles - 1]),
807  file->name)));
808 
809  return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +
810  lastFileSize;
811 }
SharedFileSet * fileset
Definition: buffile.c:80
off_t FileSize(File file)
Definition: fd.c:2148
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:62
int numFiles
Definition: buffile.c:72
char * FilePathName(File file)
Definition: fd.c:2200
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:633
File * files
Definition: buffile.c:74
const char * name
Definition: buffile.c:81
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ BufFileTell()

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

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

744 {
745  *fileno = file->curFile;
746  *offset = file->curOffset + file->pos;
747 }
off_t curOffset
Definition: buffile.c:95
int curFile
Definition: buffile.c:94
int pos
Definition: buffile.c:96

◆ BufFileTruncateShared()

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

Definition at line 861 of file buffile.c.

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

Referenced by apply_handle_stream_abort().

862 {
863  int numFiles = file->numFiles;
864  int newFile = fileno;
865  off_t newOffset = file->curOffset;
866  char segment_name[MAXPGPATH];
867  int i;
868 
869  /*
870  * Loop over all the files up to the given fileno and remove the files
871  * that are greater than the fileno and truncate the given file up to the
872  * offset. Note that we also remove the given fileno if the offset is 0
873  * provided it is not the first file in which we truncate it.
874  */
875  for (i = file->numFiles - 1; i >= fileno; i--)
876  {
877  if ((i != fileno || offset == 0) && i != 0)
878  {
879  SharedSegmentName(segment_name, file->name, i);
880  FileClose(file->files[i]);
881  if (!SharedFileSetDelete(file->fileset, segment_name, true))
882  ereport(ERROR,
884  errmsg("could not delete shared fileset \"%s\": %m",
885  segment_name)));
886  numFiles--;
887  newOffset = MAX_PHYSICAL_FILESIZE;
888 
889  /*
890  * This is required to indicate that we have deleted the given
891  * fileno.
892  */
893  if (i == fileno)
894  newFile--;
895  }
896  else
897  {
898  if (FileTruncate(file->files[i], offset,
900  ereport(ERROR,
902  errmsg("could not truncate file \"%s\": %m",
903  FilePathName(file->files[i]))));
904  newOffset = offset;
905  }
906  }
907 
908  file->numFiles = numFiles;
909 
910  /*
911  * If the truncate point is within existing buffer then we can just adjust
912  * pos within buffer.
913  */
914  if (newFile == file->curFile &&
915  newOffset >= file->curOffset &&
916  newOffset <= file->curOffset + file->nbytes)
917  {
918  /* No need to reset the current pos if the new pos is greater. */
919  if (newOffset <= file->curOffset + file->pos)
920  file->pos = (int) (newOffset - file->curOffset);
921 
922  /* Adjust the nbytes for the current buffer. */
923  file->nbytes = (int) (newOffset - file->curOffset);
924  }
925  else if (newFile == file->curFile &&
926  newOffset < file->curOffset)
927  {
928  /*
929  * The truncate point is within the existing file but prior to the
930  * current position, so we can forget the current buffer and reset the
931  * current position.
932  */
933  file->curOffset = newOffset;
934  file->pos = 0;
935  file->nbytes = 0;
936  }
937  else if (newFile < file->curFile)
938  {
939  /*
940  * The truncate point is prior to the current file, so need to reset
941  * the current position accordingly.
942  */
943  file->curFile = newFile;
944  file->curOffset = newOffset;
945  file->pos = 0;
946  file->nbytes = 0;
947  }
948  /* Nothing to do, if the truncate point is beyond current file. */
949 }
SharedFileSet * fileset
Definition: buffile.c:80
int nbytes
Definition: buffile.c:97
bool SharedFileSetDelete(SharedFileSet *fileset, const char *name, bool error_on_failure)
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:62
int numFiles
Definition: buffile.c:72
char * FilePathName(File file)
Definition: fd.c:2200
#define ERROR
Definition: elog.h:43
static void SharedSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:217
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:633
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:144
void FileClose(File file)
Definition: fd.c:1826
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2165
int pos
Definition: buffile.c:96

◆ BufFileWrite()

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

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

587 {
588  size_t nthistime;
589 
590  Assert(!file->readOnly);
591 
592  while (size > 0)
593  {
594  if (file->pos >= BLCKSZ)
595  {
596  /* Buffer full, dump it out */
597  if (file->dirty)
598  BufFileDumpBuffer(file);
599  else
600  {
601  /* Hmm, went directly from reading to writing? */
602  file->curOffset += file->pos;
603  file->pos = 0;
604  file->nbytes = 0;
605  }
606  }
607 
608  nthistime = BLCKSZ - file->pos;
609  if (nthistime > size)
610  nthistime = size;
611  Assert(nthistime > 0);
612 
613  memcpy(file->buffer.data + file->pos, ptr, nthistime);
614 
615  file->dirty = true;
616  file->pos += nthistime;
617  if (file->nbytes < file->pos)
618  file->nbytes = file->pos;
619  ptr = (void *) ((char *) ptr + nthistime);
620  size -= nthistime;
621  }
622 }
int nbytes
Definition: buffile.c:97
bool readOnly
Definition: buffile.c:78
PGAlignedBlock buffer
Definition: buffile.c:98
char data[BLCKSZ]
Definition: c.h:1082
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:463
off_t curOffset
Definition: buffile.c:95
#define Assert(condition)
Definition: c.h:745
bool dirty
Definition: buffile.c:77
int pos
Definition: buffile.c:96