PostgreSQL Source Code  git master
buffile.c File Reference
#include "postgres.h"
#include "commands/tablespace.h"
#include "executor/instrument.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/buffile.h"
#include "storage/bufmgr.h"
#include "storage/fd.h"
#include "utils/resowner.h"
Include dependency graph for buffile.c:

Go to the source code of this file.

Data Structures

struct  BufFile
 

Macros

#define MAX_PHYSICAL_FILESIZE   0x40000000
 
#define BUFFILE_SEG_SIZE   (MAX_PHYSICAL_FILESIZE / BLCKSZ)
 

Functions

static BufFilemakeBufFileCommon (int nfiles)
 
static BufFilemakeBufFile (File firstfile)
 
static void extendBufFile (BufFile *file)
 
static void BufFileLoadBuffer (BufFile *file)
 
static void BufFileDumpBuffer (BufFile *file)
 
static void BufFileFlush (BufFile *file)
 
static File MakeNewFileSetSegment (BufFile *buffile, int segment)
 
BufFileBufFileCreateTemp (bool interXact)
 
static void FileSetSegmentName (char *name, const char *buffile_name, int segment)
 
BufFileBufFileCreateFileSet (FileSet *fileset, const char *name)
 
BufFileBufFileOpenFileSet (FileSet *fileset, const char *name, int mode, bool missing_ok)
 
void BufFileDeleteFileSet (FileSet *fileset, const char *name, bool missing_ok)
 
void BufFileExportFileSet (BufFile *file)
 
void BufFileClose (BufFile *file)
 
static size_t BufFileReadCommon (BufFile *file, void *ptr, size_t size, bool exact, bool eofOK)
 
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)
 
void BufFileTruncateFileSet (BufFile *file, int fileno, off_t offset)
 

Macro Definition Documentation

◆ BUFFILE_SEG_SIZE

#define BUFFILE_SEG_SIZE   (MAX_PHYSICAL_FILESIZE / BLCKSZ)

Definition at line 63 of file buffile.c.

◆ MAX_PHYSICAL_FILESIZE

#define MAX_PHYSICAL_FILESIZE   0x40000000

Definition at line 62 of file buffile.c.

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

◆ BufFileDumpBuffer()

static void BufFileDumpBuffer ( BufFile file)
static

Definition at line 494 of file buffile.c.

495 {
496  int wpos = 0;
497  int bytestowrite;
498  File thisfile;
499 
500  /*
501  * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
502  * crosses a component-file boundary; so we need a loop.
503  */
504  while (wpos < file->nbytes)
505  {
506  off_t availbytes;
507  instr_time io_start;
508  instr_time io_time;
509 
510  /*
511  * Advance to next component file if necessary and possible.
512  */
513  if (file->curOffset >= MAX_PHYSICAL_FILESIZE)
514  {
515  while (file->curFile + 1 >= file->numFiles)
516  extendBufFile(file);
517  file->curFile++;
518  file->curOffset = 0;
519  }
520 
521  /*
522  * Determine how much we need to write into this file.
523  */
524  bytestowrite = file->nbytes - wpos;
525  availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
526 
527  if ((off_t) bytestowrite > availbytes)
528  bytestowrite = (int) availbytes;
529 
530  thisfile = file->files[file->curFile];
531 
532  if (track_io_timing)
533  INSTR_TIME_SET_CURRENT(io_start);
534  else
535  INSTR_TIME_SET_ZERO(io_start);
536 
537  bytestowrite = FileWrite(thisfile,
538  file->buffer.data + wpos,
539  bytestowrite,
540  file->curOffset,
541  WAIT_EVENT_BUFFILE_WRITE);
542  if (bytestowrite <= 0)
543  ereport(ERROR,
545  errmsg("could not write to file \"%s\": %m",
546  FilePathName(thisfile))));
547 
548  if (track_io_timing)
549  {
550  INSTR_TIME_SET_CURRENT(io_time);
552  }
553 
554  file->curOffset += bytestowrite;
555  wpos += bytestowrite;
556 
558  }
559  file->dirty = false;
560 
561  /*
562  * At this point, curOffset has been advanced to the end of the buffer,
563  * ie, its original value + nbytes. We need to make it point to the
564  * logical file position, ie, original value + pos, in case that is less
565  * (as could happen due to a small backwards seek in a dirty buffer!)
566  */
567  file->curOffset -= (file->nbytes - file->pos);
568  if (file->curOffset < 0) /* handle possible segment crossing */
569  {
570  file->curFile--;
571  Assert(file->curFile >= 0);
573  }
574 
575  /*
576  * Now we can set the buffer empty without changing the logical position
577  */
578  file->pos = 0;
579  file->nbytes = 0;
580 }
static void extendBufFile(BufFile *file)
Definition: buffile.c:156
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:62
bool track_io_timing
Definition: bufmgr.c:142
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
char * FilePathName(File file)
Definition: fd.c:2461
static ssize_t FileWrite(File file, const void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
Definition: fd.h:208
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:172
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition: instr_time.h:184
BufferUsage pgBufferUsage
Definition: instrument.c:20
int nbytes
Definition: buffile.c:97
PGAlignedBlock buffer
Definition: buffile.c:103
bool dirty
Definition: buffile.c:77
int pos
Definition: buffile.c:96
int curFile
Definition: buffile.c:94
off_t curOffset
Definition: buffile.c:95
instr_time temp_blk_write_time
Definition: instrument.h:41
int64 temp_blks_written
Definition: instrument.h:35
#define wpos(wep)
Definition: tsrank.c:26
char data[BLCKSZ]
Definition: c.h:1119

References Assert, BufFile::buffer, BufFile::curFile, BufFile::curOffset, PGAlignedBlock::data, BufFile::dirty, ereport, errcode_for_file_access(), errmsg(), ERROR, extendBufFile(), FilePathName(), BufFile::files, FileWrite(), INSTR_TIME_ACCUM_DIFF, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, pgBufferUsage, BufFile::pos, BufferUsage::temp_blk_write_time, BufferUsage::temp_blks_written, track_io_timing, and wpos.

Referenced by BufFileFlush(), and BufFileWrite().

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

◆ BufFileFlush()

static void BufFileFlush ( BufFile file)
static

Definition at line 720 of file buffile.c.

721 {
722  if (file->dirty)
723  BufFileDumpBuffer(file);
724 
725  Assert(!file->dirty);
726 }
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:494

References Assert, BufFileDumpBuffer(), and BufFile::dirty.

Referenced by BufFileClose(), BufFileExportFileSet(), BufFileReadCommon(), and BufFileSeek().

◆ BufFileLoadBuffer()

static void BufFileLoadBuffer ( BufFile file)
static

Definition at line 434 of file buffile.c.

435 {
436  File thisfile;
437  instr_time io_start;
438  instr_time io_time;
439 
440  /*
441  * Advance to next component file if necessary and possible.
442  */
443  if (file->curOffset >= MAX_PHYSICAL_FILESIZE &&
444  file->curFile + 1 < file->numFiles)
445  {
446  file->curFile++;
447  file->curOffset = 0;
448  }
449 
450  thisfile = file->files[file->curFile];
451 
452  if (track_io_timing)
453  INSTR_TIME_SET_CURRENT(io_start);
454  else
455  INSTR_TIME_SET_ZERO(io_start);
456 
457  /*
458  * Read whatever we can get, up to a full bufferload.
459  */
460  file->nbytes = FileRead(thisfile,
461  file->buffer.data,
462  sizeof(file->buffer),
463  file->curOffset,
464  WAIT_EVENT_BUFFILE_READ);
465  if (file->nbytes < 0)
466  {
467  file->nbytes = 0;
468  ereport(ERROR,
470  errmsg("could not read file \"%s\": %m",
471  FilePathName(thisfile))));
472  }
473 
474  if (track_io_timing)
475  {
476  INSTR_TIME_SET_CURRENT(io_time);
478  }
479 
480  /* we choose not to advance curOffset here */
481 
482  if (file->nbytes > 0)
484 }
static ssize_t FileRead(File file, void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
Definition: fd.h:196
instr_time temp_blk_read_time
Definition: instrument.h:40
int64 temp_blks_read
Definition: instrument.h:34

References BufFile::buffer, BufFile::curFile, BufFile::curOffset, PGAlignedBlock::data, ereport, errcode_for_file_access(), errmsg(), ERROR, FilePathName(), FileRead(), BufFile::files, INSTR_TIME_ACCUM_DIFF, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, pgBufferUsage, BufferUsage::temp_blk_read_time, BufferUsage::temp_blks_read, and track_io_timing.

Referenced by BufFileReadCommon().

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

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.

◆ BufFileReadCommon()

static size_t BufFileReadCommon ( BufFile file,
void *  ptr,
size_t  size,
bool  exact,
bool  eofOK 
)
static

Definition at line 593 of file buffile.c.

594 {
595  size_t start_size = size;
596  size_t nread = 0;
597  size_t nthistime;
598 
599  BufFileFlush(file);
600 
601  while (size > 0)
602  {
603  if (file->pos >= file->nbytes)
604  {
605  /* Try to load more data into buffer. */
606  file->curOffset += file->pos;
607  file->pos = 0;
608  file->nbytes = 0;
609  BufFileLoadBuffer(file);
610  if (file->nbytes <= 0)
611  break; /* no more data available */
612  }
613 
614  nthistime = file->nbytes - file->pos;
615  if (nthistime > size)
616  nthistime = size;
617  Assert(nthistime > 0);
618 
619  memcpy(ptr, file->buffer.data + file->pos, nthistime);
620 
621  file->pos += nthistime;
622  ptr = (char *) ptr + nthistime;
623  size -= nthistime;
624  nread += nthistime;
625  }
626 
627  if (exact &&
628  (nread != start_size && !(nread == 0 && eofOK)))
629  ereport(ERROR,
631  file->name ?
632  errmsg("could not read from file set \"%s\": read only %zu of %zu bytes",
633  file->name, nread, start_size) :
634  errmsg("could not read from temporary file: read only %zu of %zu bytes",
635  nread, start_size));
636 
637  return nread;
638 }
static void BufFileLoadBuffer(BufFile *file)
Definition: buffile.c:434

References Assert, BufFile::buffer, BufFileFlush(), BufFileLoadBuffer(), BufFile::curOffset, PGAlignedBlock::data, ereport, errcode_for_file_access(), errmsg(), ERROR, BufFile::name, BufFile::nbytes, BufFile::pos, and size.

Referenced by BufFileRead(), BufFileReadExact(), and BufFileReadMaybeEOF().

◆ 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 }
off_t FileSize(File file)
Definition: fd.c:2409

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 }

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

◆ extendBufFile()

static void extendBufFile ( BufFile file)
static

Definition at line 156 of file buffile.c.

157 {
158  File pfile;
159  ResourceOwner oldowner;
160 
161  /* Be sure to associate the file with the BufFile's resource owner */
162  oldowner = CurrentResourceOwner;
164 
165  if (file->fileset == NULL)
166  pfile = OpenTemporaryFile(file->isInterXact);
167  else
168  pfile = MakeNewFileSetSegment(file, file->numFiles);
169 
170  Assert(pfile >= 0);
171 
172  CurrentResourceOwner = oldowner;
173 
174  file->files = (File *) repalloc(file->files,
175  (file->numFiles + 1) * sizeof(File));
176  file->files[file->numFiles] = pfile;
177  file->numFiles++;
178 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165

References Assert, CurrentResourceOwner, BufFile::files, BufFile::fileset, BufFile::isInterXact, MakeNewFileSetSegment(), BufFile::numFiles, OpenTemporaryFile(), repalloc(), and BufFile::resowner.

Referenced by BufFileDumpBuffer().

◆ FileSetSegmentName()

static void FileSetSegmentName ( char *  name,
const char *  buffile_name,
int  segment 
)
static

Definition at line 222 of file buffile.c.

223 {
224  snprintf(name, MAXPGPATH, "%s.%d", buffile_name, segment);
225 }
#define snprintf
Definition: port.h:238

References MAXPGPATH, name, and snprintf.

Referenced by BufFileDeleteFileSet(), BufFileOpenFileSet(), BufFileTruncateFileSet(), and MakeNewFileSetSegment().

◆ makeBufFile()

static BufFile * makeBufFile ( File  firstfile)
static

Definition at line 139 of file buffile.c.

140 {
141  BufFile *file = makeBufFileCommon(1);
142 
143  file->files = (File *) palloc(sizeof(File));
144  file->files[0] = firstfile;
145  file->readOnly = false;
146  file->fileset = NULL;
147  file->name = NULL;
148 
149  return file;
150 }

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

Referenced by BufFileCreateTemp().

◆ makeBufFileCommon()

static BufFile * makeBufFileCommon ( int  nfiles)
static

Definition at line 118 of file buffile.c.

119 {
120  BufFile *file = (BufFile *) palloc(sizeof(BufFile));
121 
122  file->numFiles = nfiles;
123  file->isInterXact = false;
124  file->dirty = false;
126  file->curFile = 0;
127  file->curOffset = 0;
128  file->pos = 0;
129  file->nbytes = 0;
130 
131  return file;
132 }

References BufFile::curFile, BufFile::curOffset, CurrentResourceOwner, BufFile::dirty, BufFile::isInterXact, BufFile::nbytes, BufFile::numFiles, palloc(), BufFile::pos, and BufFile::resowner.

Referenced by BufFileCreateFileSet(), BufFileOpenFileSet(), and makeBufFile().

◆ MakeNewFileSetSegment()

static File MakeNewFileSetSegment ( BufFile buffile,
int  segment 
)
static

Definition at line 231 of file buffile.c.

232 {
233  char name[MAXPGPATH];
234  File file;
235 
236  /*
237  * It is possible that there are files left over from before a crash
238  * restart with the same name. In order for BufFileOpenFileSet() not to
239  * get confused about how many segments there are, we'll unlink the next
240  * segment number if it already exists.
241  */
242  FileSetSegmentName(name, buffile->name, segment + 1);
243  FileSetDelete(buffile->fileset, name, true);
244 
245  /* Create the new segment. */
246  FileSetSegmentName(name, buffile->name, segment);
247  file = FileSetCreate(buffile->fileset, name);
248 
249  /* FileSetCreate would've errored out */
250  Assert(file > 0);
251 
252  return file;
253 }
File FileSetCreate(FileSet *fileset, const char *name)
Definition: fileset.c:92

References Assert, BufFile::fileset, FileSetCreate(), FileSetDelete(), FileSetSegmentName(), MAXPGPATH, BufFile::name, and name.

Referenced by BufFileCreateFileSet(), and extendBufFile().