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/buf_internals.h"
#include "storage/buffile.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 *file, 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)
 
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)
 
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()

long BufFileAppend ( BufFile target,
BufFile source 
)

Definition at line 872 of file buffile.c.

873 {
874  long startBlock = target->numFiles * BUFFILE_SEG_SIZE;
875  int newNumFiles = target->numFiles + source->numFiles;
876  int i;
877 
878  Assert(target->fileset != NULL);
879  Assert(source->readOnly);
880  Assert(!source->dirty);
881  Assert(source->fileset != NULL);
882 
883  if (target->resowner != source->resowner)
884  elog(ERROR, "could not append BufFile with non-matching resource owner");
885 
886  target->files = (File *)
887  repalloc(target->files, sizeof(File) * newNumFiles);
888  for (i = target->numFiles; i < newNumFiles; i++)
889  target->files[i] = source->files[i - target->numFiles];
890  target->numFiles = newNumFiles;
891 
892  return startBlock;
893 }
#define BUFFILE_SEG_SIZE
Definition: buffile.c:63
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
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:1188
static rewind_source * source
Definition: pg_rewind.c:81
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:669
void FileClose(File file)
Definition: fd.c:1961
void pfree(void *pointer)
Definition: mcxt.c:1175

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

◆ 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 *file, int segment)
Definition: buffile.c:226
const char * name
Definition: encode.c:561
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void * palloc(Size size)
Definition: mcxt.c:1068
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:1379
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:134
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1707
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().

◆ BufFileDumpBuffer()

static void BufFileDumpBuffer ( BufFile file)
static

Definition at line 488 of file buffile.c.

489 {
490  int wpos = 0;
491  int bytestowrite;
492  File thisfile;
493 
494  /*
495  * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
496  * crosses a component-file boundary; so we need a loop.
497  */
498  while (wpos < file->nbytes)
499  {
500  off_t availbytes;
501  instr_time io_start;
502  instr_time io_time;
503 
504  /*
505  * Advance to next component file if necessary and possible.
506  */
507  if (file->curOffset >= MAX_PHYSICAL_FILESIZE)
508  {
509  while (file->curFile + 1 >= file->numFiles)
510  extendBufFile(file);
511  file->curFile++;
512  file->curOffset = 0L;
513  }
514 
515  /*
516  * Determine how much we need to write into this file.
517  */
518  bytestowrite = file->nbytes - wpos;
519  availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
520 
521  if ((off_t) bytestowrite > availbytes)
522  bytestowrite = (int) availbytes;
523 
524  thisfile = file->files[file->curFile];
525 
526  if (track_io_timing)
527  INSTR_TIME_SET_CURRENT(io_start);
528 
529  bytestowrite = FileWrite(thisfile,
530  file->buffer.data + wpos,
531  bytestowrite,
532  file->curOffset,
534  if (bytestowrite <= 0)
535  ereport(ERROR,
537  errmsg("could not write to file \"%s\": %m",
538  FilePathName(thisfile))));
539 
540  if (track_io_timing)
541  {
542  INSTR_TIME_SET_CURRENT(io_time);
543  INSTR_TIME_SUBTRACT(io_time, io_start);
545  }
546 
547  file->curOffset += bytestowrite;
548  wpos += bytestowrite;
549 
551  }
552  file->dirty = false;
553 
554  /*
555  * At this point, curOffset has been advanced to the end of the buffer,
556  * ie, its original value + nbytes. We need to make it point to the
557  * logical file position, ie, original value + pos, in case that is less
558  * (as could happen due to a small backwards seek in a dirty buffer!)
559  */
560  file->curOffset -= (file->nbytes - file->pos);
561  if (file->curOffset < 0) /* handle possible segment crossing */
562  {
563  file->curFile--;
564  Assert(file->curFile >= 0);
566  }
567 
568  /*
569  * Now we can set the buffer empty without changing the logical position
570  */
571  file->pos = 0;
572  file->nbytes = 0;
573 }
static void extendBufFile(BufFile *file)
Definition: buffile.c:151
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:62
bool track_io_timing
Definition: bufmgr.c:137
int errcode_for_file_access(void)
Definition: elog.c:716
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ereport(elevel,...)
Definition: elog.h:143
int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2168
char * FilePathName(File file)
Definition: fd.c:2339
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:158
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
struct timeval instr_time
Definition: instr_time.h:150
BufferUsage pgBufferUsage
Definition: instrument.c:20
int nbytes
Definition: buffile.c:97
PGAlignedBlock buffer
Definition: buffile.c:98
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:39
int64 temp_blks_written
Definition: instrument.h:35
#define wpos(wep)
Definition: tsrank.c:26
char data[BLCKSZ]
Definition: c.h:1138
@ WAIT_EVENT_BUFFILE_WRITE
Definition: wait_event.h:164

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_ADD, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, pgBufferUsage, BufFile::pos, BufferUsage::temp_blk_write_time, BufferUsage::temp_blks_written, track_io_timing, WAIT_EVENT_BUFFILE_WRITE, and wpos.

Referenced by BufFileFlush(), and BufFileWrite().

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

◆ BufFileFlush()

static void BufFileFlush ( BufFile file)
static

Definition at line 669 of file buffile.c.

670 {
671  if (file->dirty)
672  BufFileDumpBuffer(file);
673 
674  Assert(!file->dirty);
675 }
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:488

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

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

◆ BufFileLoadBuffer()

static void BufFileLoadBuffer ( BufFile file)
static

Definition at line 429 of file buffile.c.

430 {
431  File thisfile;
432  instr_time io_start;
433  instr_time io_time;
434 
435  /*
436  * Advance to next component file if necessary and possible.
437  */
438  if (file->curOffset >= MAX_PHYSICAL_FILESIZE &&
439  file->curFile + 1 < file->numFiles)
440  {
441  file->curFile++;
442  file->curOffset = 0L;
443  }
444 
445  thisfile = file->files[file->curFile];
446 
447  if (track_io_timing)
448  INSTR_TIME_SET_CURRENT(io_start);
449 
450  /*
451  * Read whatever we can get, up to a full bufferload.
452  */
453  file->nbytes = FileRead(thisfile,
454  file->buffer.data,
455  sizeof(file->buffer),
456  file->curOffset,
458  if (file->nbytes < 0)
459  {
460  file->nbytes = 0;
461  ereport(ERROR,
463  errmsg("could not read file \"%s\": %m",
464  FilePathName(thisfile))));
465  }
466 
467  if (track_io_timing)
468  {
469  INSTR_TIME_SET_CURRENT(io_time);
470  INSTR_TIME_SUBTRACT(io_time, io_start);
472  }
473 
474  /* we choose not to advance curOffset here */
475 
476  if (file->nbytes > 0)
478 }
int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2112
instr_time temp_blk_read_time
Definition: instrument.h:38
int64 temp_blks_read
Definition: instrument.h:34
@ WAIT_EVENT_BUFFILE_READ
Definition: wait_event.h:163

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

Referenced by BufFileRead().

◆ 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:540
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_handle_stream_abort(), apply_spooled_messages(), LogicalTapeImport(), 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 582 of file buffile.c.

583 {
584  size_t nread = 0;
585  size_t nthistime;
586 
587  BufFileFlush(file);
588 
589  while (size > 0)
590  {
591  if (file->pos >= file->nbytes)
592  {
593  /* Try to load more data into buffer. */
594  file->curOffset += file->pos;
595  file->pos = 0;
596  file->nbytes = 0;
597  BufFileLoadBuffer(file);
598  if (file->nbytes <= 0)
599  break; /* no more data available */
600  }
601 
602  nthistime = file->nbytes - file->pos;
603  if (nthistime > size)
604  nthistime = size;
605  Assert(nthistime > 0);
606 
607  memcpy(ptr, file->buffer.data + file->pos, nthistime);
608 
609  file->pos += nthistime;
610  ptr = (void *) ((char *) ptr + nthistime);
611  size -= nthistime;
612  nread += nthistime;
613  }
614 
615  return nread;
616 }
static void BufFileLoadBuffer(BufFile *file)
Definition: buffile.c:429

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

◆ BufFileSeek()

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

Definition at line 689 of file buffile.c.

690 {
691  int newFile;
692  off_t newOffset;
693 
694  switch (whence)
695  {
696  case SEEK_SET:
697  if (fileno < 0)
698  return EOF;
699  newFile = fileno;
700  newOffset = offset;
701  break;
702  case SEEK_CUR:
703 
704  /*
705  * Relative seek considers only the signed offset, ignoring
706  * fileno. Note that large offsets (> 1 GB) risk overflow in this
707  * add, unless we have 64-bit off_t.
708  */
709  newFile = file->curFile;
710  newOffset = (file->curOffset + file->pos) + offset;
711  break;
712  case SEEK_END:
713 
714  /*
715  * The file size of the last file gives us the end offset of that
716  * file.
717  */
718  newFile = file->numFiles - 1;
719  newOffset = FileSize(file->files[file->numFiles - 1]);
720  if (newOffset < 0)
721  ereport(ERROR,
723  errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
724  FilePathName(file->files[file->numFiles - 1]),
725  file->name)));
726  break;
727  default:
728  elog(ERROR, "invalid whence: %d", whence);
729  return EOF;
730  }
731  while (newOffset < 0)
732  {
733  if (--newFile < 0)
734  return EOF;
735  newOffset += MAX_PHYSICAL_FILESIZE;
736  }
737  if (newFile == file->curFile &&
738  newOffset >= file->curOffset &&
739  newOffset <= file->curOffset + file->nbytes)
740  {
741  /*
742  * Seek is to a point within existing buffer; we can just adjust
743  * pos-within-buffer, without flushing buffer. Note this is OK
744  * whether reading or writing, but buffer remains dirty if we were
745  * writing.
746  */
747  file->pos = (int) (newOffset - file->curOffset);
748  return 0;
749  }
750  /* Otherwise, must reposition buffer, so flush any dirty data */
751  BufFileFlush(file);
752 
753  /*
754  * At this point and no sooner, check for seek past last segment. The
755  * above flush could have created a new segment, so checking sooner would
756  * not work (at least not with this code).
757  */
758 
759  /* convert seek to "start of next seg" to "end of last seg" */
760  if (newFile == file->numFiles && newOffset == 0)
761  {
762  newFile--;
763  newOffset = MAX_PHYSICAL_FILESIZE;
764  }
765  while (newOffset > MAX_PHYSICAL_FILESIZE)
766  {
767  if (++newFile >= file->numFiles)
768  return EOF;
769  newOffset -= MAX_PHYSICAL_FILESIZE;
770  }
771  if (newFile >= file->numFiles)
772  return EOF;
773  /* Seek is OK! */
774  file->curFile = newFile;
775  file->curOffset = newOffset;
776  file->pos = 0;
777  file->nbytes = 0;
778  return 0;
779 }
off_t FileSize(File file)
Definition: fd.c:2287

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

◆ BufFileSeekBlock()

int BufFileSeekBlock ( BufFile file,
long  blknum 
)

Definition at line 800 of file buffile.c.

801 {
802  return BufFileSeek(file,
803  (int) (blknum / BUFFILE_SEG_SIZE),
804  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
805  SEEK_SET);
806 }
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:689

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

834 {
835  int64 lastFileSize;
836 
837  Assert(file->fileset != NULL);
838 
839  /* Get the size of the last physical file. */
840  lastFileSize = FileSize(file->files[file->numFiles - 1]);
841  if (lastFileSize < 0)
842  ereport(ERROR,
844  errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
845  FilePathName(file->files[file->numFiles - 1]),
846  file->name)));
847 
848  return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +
849  lastFileSize;
850 }

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 
)

Definition at line 782 of file buffile.c.

783 {
784  *fileno = file->curFile;
785  *offset = file->curOffset + file->pos;
786 }

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

◆ BufFileTruncateFileSet()

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

Definition at line 900 of file buffile.c.

901 {
902  int numFiles = file->numFiles;
903  int newFile = fileno;
904  off_t newOffset = file->curOffset;
905  char segment_name[MAXPGPATH];
906  int i;
907 
908  /*
909  * Loop over all the files up to the given fileno and remove the files
910  * that are greater than the fileno and truncate the given file up to the
911  * offset. Note that we also remove the given fileno if the offset is 0
912  * provided it is not the first file in which we truncate it.
913  */
914  for (i = file->numFiles - 1; i >= fileno; i--)
915  {
916  if ((i != fileno || offset == 0) && i != 0)
917  {
918  FileSetSegmentName(segment_name, file->name, i);
919  FileClose(file->files[i]);
920  if (!FileSetDelete(file->fileset, segment_name, true))
921  ereport(ERROR,
923  errmsg("could not delete fileset \"%s\": %m",
924  segment_name)));
925  numFiles--;
926  newOffset = MAX_PHYSICAL_FILESIZE;
927 
928  /*
929  * This is required to indicate that we have deleted the given
930  * fileno.
931  */
932  if (i == fileno)
933  newFile--;
934  }
935  else
936  {
937  if (FileTruncate(file->files[i], offset,
939  ereport(ERROR,
941  errmsg("could not truncate file \"%s\": %m",
942  FilePathName(file->files[i]))));
943  newOffset = offset;
944  }
945  }
946 
947  file->numFiles = numFiles;
948 
949  /*
950  * If the truncate point is within existing buffer then we can just adjust
951  * pos within buffer.
952  */
953  if (newFile == file->curFile &&
954  newOffset >= file->curOffset &&
955  newOffset <= file->curOffset + file->nbytes)
956  {
957  /* No need to reset the current pos if the new pos is greater. */
958  if (newOffset <= file->curOffset + file->pos)
959  file->pos = (int) (newOffset - file->curOffset);
960 
961  /* Adjust the nbytes for the current buffer. */
962  file->nbytes = (int) (newOffset - file->curOffset);
963  }
964  else if (newFile == file->curFile &&
965  newOffset < file->curOffset)
966  {
967  /*
968  * The truncate point is within the existing file but prior to the
969  * current position, so we can forget the current buffer and reset the
970  * current position.
971  */
972  file->curOffset = newOffset;
973  file->pos = 0;
974  file->nbytes = 0;
975  }
976  else if (newFile < file->curFile)
977  {
978  /*
979  * The truncate point is prior to the current file, so need to reset
980  * the current position accordingly.
981  */
982  file->curFile = newFile;
983  file->curOffset = newOffset;
984  file->pos = 0;
985  file->nbytes = 0;
986  }
987  /* Nothing to do, if the truncate point is beyond current file. */
988 }
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2304
@ WAIT_EVENT_BUFFILE_TRUNCATE
Definition: wait_event.h:165

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

◆ BufFileWrite()

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

Definition at line 625 of file buffile.c.

626 {
627  size_t nthistime;
628 
629  Assert(!file->readOnly);
630 
631  while (size > 0)
632  {
633  if (file->pos >= BLCKSZ)
634  {
635  /* Buffer full, dump it out */
636  if (file->dirty)
637  BufFileDumpBuffer(file);
638  else
639  {
640  /* Hmm, went directly from reading to writing? */
641  file->curOffset += file->pos;
642  file->pos = 0;
643  file->nbytes = 0;
644  }
645  }
646 
647  nthistime = BLCKSZ - file->pos;
648  if (nthistime > size)
649  nthistime = size;
650  Assert(nthistime > 0);
651 
652  memcpy(file->buffer.data + file->pos, ptr, nthistime);
653 
654  file->dirty = true;
655  file->pos += nthistime;
656  if (file->nbytes < file->pos)
657  file->nbytes = file->pos;
658  ptr = (void *) ((char *) ptr + nthistime);
659  size -= nthistime;
660  }
661 }

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

◆ extendBufFile()

static void extendBufFile ( BufFile file)
static

Definition at line 151 of file buffile.c.

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

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

218 {
219  snprintf(name, MAXPGPATH, "%s.%d", buffile_name, segment);
220 }
#define snprintf
Definition: port.h:225

References MAXPGPATH, name, and snprintf.

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

◆ makeBufFile()

static BufFile * makeBufFile ( File  firstfile)
static

Definition at line 134 of file buffile.c.

135 {
136  BufFile *file = makeBufFileCommon(1);
137 
138  file->files = (File *) palloc(sizeof(File));
139  file->files[0] = firstfile;
140  file->readOnly = false;
141  file->fileset = NULL;
142  file->name = NULL;
143 
144  return file;
145 }

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

114 {
115  BufFile *file = (BufFile *) palloc(sizeof(BufFile));
116 
117  file->numFiles = nfiles;
118  file->isInterXact = false;
119  file->dirty = false;
121  file->curFile = 0;
122  file->curOffset = 0L;
123  file->pos = 0;
124  file->nbytes = 0;
125 
126  return file;
127 }

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 file,
int  segment 
)
static

Definition at line 226 of file buffile.c.

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

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

Referenced by BufFileCreateFileSet(), and extendBufFile().