PostgreSQL Source Code  git master
buffile.c File Reference
#include "postgres.h"
#include "executor/instrument.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/fd.h"
#include "storage/buffile.h"
#include "storage/buf_internals.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 BufFilemakeBufFile (File firstfile)
 
static void extendBufFile (BufFile *file)
 
static void BufFileLoadBuffer (BufFile *file)
 
static void BufFileDumpBuffer (BufFile *file)
 
static int BufFileFlush (BufFile *file)
 
static File MakeNewSharedSegment (BufFile *file, int segment)
 
BufFileBufFileCreateTemp (bool interXact)
 
static void SharedSegmentName (char *name, const char *buffile_name, int segment)
 
BufFileBufFileCreateShared (SharedFileSet *fileset, const char *name)
 
BufFileBufFileOpenShared (SharedFileSet *fileset, const char *name)
 
void BufFileDeleteShared (SharedFileSet *fileset, const char *name)
 
void BufFileExportShared (BufFile *file)
 
void BufFileClose (BufFile *file)
 
size_t BufFileRead (BufFile *file, void *ptr, size_t size)
 
size_t 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)
 

Macro Definition Documentation

◆ BUFFILE_SEG_SIZE

#define BUFFILE_SEG_SIZE   (MAX_PHYSICAL_FILESIZE / BLCKSZ)

Definition at line 58 of file buffile.c.

Referenced by BufFileSeekBlock().

◆ MAX_PHYSICAL_FILESIZE

#define MAX_PHYSICAL_FILESIZE   0x40000000

Definition at line 57 of file buffile.c.

Referenced by BufFileDumpBuffer(), BufFileLoadBuffer(), and BufFileSeek().

Function Documentation

◆ BufFileClose()

void BufFileClose ( BufFile file)

Definition at line 397 of file buffile.c.

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

Referenced by ExecHashJoinNewBatch(), ExecHashTableDestroy(), gistFreeBuildBuffers(), LogicalTapeSetClose(), sts_end_parallel_scan(), sts_end_write(), sts_parallel_scan_next(), tuplestore_clear(), and tuplestore_end().

398 {
399  int i;
400 
401  /* flush any unwritten data */
402  BufFileFlush(file);
403  /* close and delete the underlying file(s) */
404  for (i = 0; i < file->numFiles; i++)
405  FileClose(file->files[i]);
406  /* release the buffer space */
407  pfree(file->files);
408  pfree(file->offsets);
409  pfree(file);
410 }
off_t * offsets
Definition: buffile.c:70
static int BufFileFlush(BufFile *file)
Definition: buffile.c:653
int numFiles
Definition: buffile.c:67
void pfree(void *pointer)
Definition: mcxt.c:936
File * files
Definition: buffile.c:69
void FileClose(File file)
Definition: fd.c:1749
int i

◆ BufFileCreateShared()

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

Definition at line 245 of file buffile.c.

References BufFile::curFile, BufFile::curOffset, CurrentResourceOwner, BufFile::dirty, BufFile::files, BufFile::fileset, BufFile::isInterXact, MakeNewSharedSegment(), BufFile::name, BufFile::nbytes, BufFile::numFiles, BufFile::offsets, palloc(), BufFile::pos, pstrdup(), BufFile::readOnly, and BufFile::resowner.

Referenced by sts_puttuple().

246 {
247  BufFile *file;
248 
249  file = (BufFile *) palloc(sizeof(BufFile));
250  file->fileset = fileset;
251  file->name = pstrdup(name);
252  file->numFiles = 1;
253  file->files = (File *) palloc(sizeof(File));
254  file->files[0] = MakeNewSharedSegment(file, 0);
255  file->offsets = (off_t *) palloc(sizeof(off_t));
256  file->offsets[0] = 0L;
257  file->isInterXact = false;
258  file->dirty = false;
260  file->curFile = 0;
261  file->curOffset = 0L;
262  file->pos = 0;
263  file->nbytes = 0;
264  file->readOnly = false;
265  file->name = pstrdup(name);
266 
267  return file;
268 }
SharedFileSet * fileset
Definition: buffile.c:81
off_t * offsets
Definition: buffile.c:70
int nbytes
Definition: buffile.c:98
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
char * pstrdup(const char *in)
Definition: mcxt.c:1063
bool readOnly
Definition: buffile.c:79
int numFiles
Definition: buffile.c:67
ResourceOwner resowner
Definition: buffile.c:89
bool isInterXact
Definition: buffile.c:77
static File MakeNewSharedSegment(BufFile *file, int segment)
Definition: buffile.c:209
File * files
Definition: buffile.c:69
off_t curOffset
Definition: buffile.c:96
const char * name
Definition: buffile.c:82
int curFile
Definition: buffile.c:95
const char * name
Definition: encode.c:521
bool dirty
Definition: buffile.c:78
void * palloc(Size size)
Definition: mcxt.c:835
int File
Definition: fd.h:49
int pos
Definition: buffile.c:97

◆ BufFileCreateTemp()

BufFile* BufFileCreateTemp ( bool  interXact)

Definition at line 182 of file buffile.c.

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

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

183 {
184  BufFile *file;
185  File pfile;
186 
187  pfile = OpenTemporaryFile(interXact);
188  Assert(pfile >= 0);
189 
190  file = makeBufFile(pfile);
191  file->isInterXact = interXact;
192 
193  return file;
194 }
bool isInterXact
Definition: buffile.c:77
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:115
#define Assert(condition)
Definition: c.h:680
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1500
int File
Definition: fd.h:49

◆ BufFileDeleteShared()

void BufFileDeleteShared ( SharedFileSet fileset,
const char *  name 
)

Definition at line 349 of file buffile.c.

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

350 {
351  char segment_name[MAXPGPATH];
352  int segment = 0;
353  bool found = false;
354 
355  /*
356  * We don't know how many segments the file has. We'll keep deleting
357  * until we run out. If we don't manage to find even an initial segment,
358  * raise an error.
359  */
360  for (;;)
361  {
362  SharedSegmentName(segment_name, name, segment);
363  if (!SharedFileSetDelete(fileset, segment_name, true))
364  break;
365  found = true;
366  ++segment;
367 
369  }
370 
371  if (!found)
372  elog(ERROR, "could not delete unknown shared BufFile \"%s\"", name);
373 }
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:200
#define MAXPGPATH
const char * name
Definition: encode.c:521
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219

◆ BufFileDumpBuffer()

static void BufFileDumpBuffer ( BufFile file)
static

Definition at line 469 of file buffile.c.

References Assert, BufFile::buffer, BufFile::curFile, BufFile::curOffset, BufFile::dirty, extendBufFile(), BufFile::files, FileSeek(), FileWrite(), MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, BufFile::offsets, pgBufferUsage, BufFile::pos, BufferUsage::temp_blks_written, WAIT_EVENT_BUFFILE_WRITE, and wpos.

Referenced by BufFileFlush(), and BufFileWrite().

470 {
471  int wpos = 0;
472  int bytestowrite;
473  File thisfile;
474 
475  /*
476  * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
477  * crosses a component-file boundary; so we need a loop.
478  */
479  while (wpos < file->nbytes)
480  {
481  off_t availbytes;
482 
483  /*
484  * Advance to next component file if necessary and possible.
485  */
486  if (file->curOffset >= MAX_PHYSICAL_FILESIZE)
487  {
488  while (file->curFile + 1 >= file->numFiles)
489  extendBufFile(file);
490  file->curFile++;
491  file->curOffset = 0L;
492  }
493 
494  /*
495  * Determine how much we need to write into this file.
496  */
497  bytestowrite = file->nbytes - wpos;
498  availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
499 
500  if ((off_t) bytestowrite > availbytes)
501  bytestowrite = (int) availbytes;
502 
503  /*
504  * May need to reposition physical file.
505  */
506  thisfile = file->files[file->curFile];
507  if (file->curOffset != file->offsets[file->curFile])
508  {
509  if (FileSeek(thisfile, file->curOffset, SEEK_SET) != file->curOffset)
510  return; /* seek failed, give up */
511  file->offsets[file->curFile] = file->curOffset;
512  }
513  bytestowrite = FileWrite(thisfile,
514  file->buffer + wpos,
515  bytestowrite,
517  if (bytestowrite <= 0)
518  return; /* failed to write */
519  file->offsets[file->curFile] += bytestowrite;
520  file->curOffset += bytestowrite;
521  wpos += bytestowrite;
522 
524  }
525  file->dirty = false;
526 
527  /*
528  * At this point, curOffset has been advanced to the end of the buffer,
529  * ie, its original value + nbytes. We need to make it point to the
530  * logical file position, ie, original value + pos, in case that is less
531  * (as could happen due to a small backwards seek in a dirty buffer!)
532  */
533  file->curOffset -= (file->nbytes - file->pos);
534  if (file->curOffset < 0) /* handle possible segment crossing */
535  {
536  file->curFile--;
537  Assert(file->curFile >= 0);
539  }
540 
541  /*
542  * Now we can set the buffer empty without changing the logical position
543  */
544  file->pos = 0;
545  file->nbytes = 0;
546 }
off_t * offsets
Definition: buffile.c:70
int nbytes
Definition: buffile.c:98
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:57
long temp_blks_written
Definition: instrument.h:30
static void extendBufFile(BufFile *file)
Definition: buffile.c:142
int numFiles
Definition: buffile.c:67
int FileWrite(File file, char *buffer, int amount, uint32 wait_event_info)
Definition: fd.c:1958
File * files
Definition: buffile.c:69
#define wpos(wep)
Definition: tsrank.c:27
off_t curOffset
Definition: buffile.c:96
int curFile
Definition: buffile.c:95
#define Assert(condition)
Definition: c.h:680
bool dirty
Definition: buffile.c:78
char buffer[BLCKSZ]
Definition: buffile.c:99
off_t FileSeek(File file, off_t offset, int whence)
Definition: fd.c:2100
int File
Definition: fd.h:49
BufferUsage pgBufferUsage
Definition: instrument.c:20
int pos
Definition: buffile.c:97

◆ BufFileExportShared()

void BufFileExportShared ( BufFile file)

Definition at line 379 of file buffile.c.

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

380 {
381  /* Must be a file belonging to a SharedFileSet. */
382  Assert(file->fileset != NULL);
383 
384  /* It's probably a bug if someone calls this twice. */
385  Assert(!file->readOnly);
386 
387  BufFileFlush(file);
388  file->readOnly = true;
389 }
SharedFileSet * fileset
Definition: buffile.c:81
static int BufFileFlush(BufFile *file)
Definition: buffile.c:653
bool readOnly
Definition: buffile.c:79
#define Assert(condition)
Definition: c.h:680

◆ BufFileFlush()

static int BufFileFlush ( BufFile file)
static

Definition at line 653 of file buffile.c.

References BufFileDumpBuffer(), and BufFile::dirty.

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

654 {
655  if (file->dirty)
656  {
657  BufFileDumpBuffer(file);
658  if (file->dirty)
659  return EOF;
660  }
661 
662  return 0;
663 }
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:469
bool dirty
Definition: buffile.c:78

◆ BufFileLoadBuffer()

static void BufFileLoadBuffer ( BufFile file)
static

Definition at line 420 of file buffile.c.

References BufFile::buffer, BufFile::curFile, BufFile::curOffset, FileRead(), BufFile::files, FileSeek(), MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, BufFile::offsets, pgBufferUsage, BufferUsage::temp_blks_read, and WAIT_EVENT_BUFFILE_READ.

Referenced by BufFileRead().

421 {
422  File thisfile;
423 
424  /*
425  * Advance to next component file if necessary and possible.
426  */
427  if (file->curOffset >= MAX_PHYSICAL_FILESIZE &&
428  file->curFile + 1 < file->numFiles)
429  {
430  file->curFile++;
431  file->curOffset = 0L;
432  }
433 
434  /*
435  * May need to reposition physical file.
436  */
437  thisfile = file->files[file->curFile];
438  if (file->curOffset != file->offsets[file->curFile])
439  {
440  if (FileSeek(thisfile, file->curOffset, SEEK_SET) != file->curOffset)
441  return; /* seek failed, read nothing */
442  file->offsets[file->curFile] = file->curOffset;
443  }
444 
445  /*
446  * Read whatever we can get, up to a full bufferload.
447  */
448  file->nbytes = FileRead(thisfile,
449  file->buffer,
450  sizeof(file->buffer),
452  if (file->nbytes < 0)
453  file->nbytes = 0;
454  file->offsets[file->curFile] += file->nbytes;
455  /* we choose not to advance curOffset here */
456 
457  if (file->nbytes > 0)
459 }
off_t * offsets
Definition: buffile.c:70
int nbytes
Definition: buffile.c:98
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:57
int numFiles
Definition: buffile.c:67
long temp_blks_read
Definition: instrument.h:29
File * files
Definition: buffile.c:69
off_t curOffset
Definition: buffile.c:96
int FileRead(File file, char *buffer, int amount, uint32 wait_event_info)
Definition: fd.c:1894
int curFile
Definition: buffile.c:95
char buffer[BLCKSZ]
Definition: buffile.c:99
off_t FileSeek(File file, off_t offset, int whence)
Definition: fd.c:2100
int File
Definition: fd.h:49
BufferUsage pgBufferUsage
Definition: instrument.c:20

◆ BufFileOpenShared()

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

Definition at line 278 of file buffile.c.

References CHECK_FOR_INTERRUPTS, BufFile::curFile, BufFile::curOffset, CurrentResourceOwner, BufFile::dirty, ereport, errcode_for_file_access(), errmsg(), ERROR, BufFile::files, BufFile::fileset, BufFile::isInterXact, MAXPGPATH, BufFile::name, BufFile::nbytes, BufFile::numFiles, BufFile::offsets, palloc(), palloc0(), BufFile::pos, pstrdup(), BufFile::readOnly, repalloc(), BufFile::resowner, SharedFileSetOpen(), and SharedSegmentName().

Referenced by sts_parallel_scan_next().

279 {
280  BufFile *file = (BufFile *) palloc(sizeof(BufFile));
281  char segment_name[MAXPGPATH];
282  Size capacity = 16;
283  File *files = palloc(sizeof(File) * capacity);
284  int nfiles = 0;
285 
286  file = (BufFile *) palloc(sizeof(BufFile));
287  files = palloc(sizeof(File) * capacity);
288 
289  /*
290  * We don't know how many segments there are, so we'll probe the
291  * filesystem to find out.
292  */
293  for (;;)
294  {
295  /* See if we need to expand our file segment array. */
296  if (nfiles + 1 > capacity)
297  {
298  capacity *= 2;
299  files = repalloc(files, sizeof(File) * capacity);
300  }
301  /* Try to load a segment. */
302  SharedSegmentName(segment_name, name, nfiles);
303  files[nfiles] = SharedFileSetOpen(fileset, segment_name);
304  if (files[nfiles] <= 0)
305  break;
306  ++nfiles;
307 
309  }
310 
311  /*
312  * If we didn't find any files at all, then no BufFile exists with this
313  * name.
314  */
315  if (nfiles == 0)
316  ereport(ERROR,
318  errmsg("could not open BufFile \"%s\"", name)));
319 
320  file->numFiles = nfiles;
321  file->files = files;
322  file->offsets = (off_t *) palloc0(sizeof(off_t) * nfiles);
323  file->isInterXact = false;
324  file->dirty = false;
325  file->resowner = CurrentResourceOwner; /* Unused, can't extend */
326  file->curFile = 0;
327  file->curOffset = 0L;
328  file->pos = 0;
329  file->nbytes = 0;
330  file->readOnly = true; /* Can't write to files opened this way */
331  file->fileset = fileset;
332  file->name = pstrdup(name);
333 
334  return file;
335 }
SharedFileSet * fileset
Definition: buffile.c:81
off_t * offsets
Definition: buffile.c:70
int nbytes
Definition: buffile.c:98
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
char * pstrdup(const char *in)
Definition: mcxt.c:1063
bool readOnly
Definition: buffile.c:79
int numFiles
Definition: buffile.c:67
ResourceOwner resowner
Definition: buffile.c:89
#define ERROR
Definition: elog.h:43
static void SharedSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:200
#define MAXPGPATH
bool isInterXact
Definition: buffile.c:77
int errcode_for_file_access(void)
Definition: elog.c:598
File * files
Definition: buffile.c:69
#define ereport(elevel, rest)
Definition: elog.h:122
off_t curOffset
Definition: buffile.c:96
const char * name
Definition: buffile.c:82
void * palloc0(Size size)
Definition: mcxt.c:864
int curFile
Definition: buffile.c:95
size_t Size
Definition: c.h:414
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:949
const char * name
Definition: encode.c:521
bool dirty
Definition: buffile.c:78
void * palloc(Size size)
Definition: mcxt.c:835
int errmsg(const char *fmt,...)
Definition: elog.c:797
File SharedFileSetOpen(SharedFileSet *fileset, const char *name)
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
int File
Definition: fd.h:49
int pos
Definition: buffile.c:97

◆ BufFileRead()

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

Definition at line 554 of file buffile.c.

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

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

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

◆ BufFileSeek()

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

Definition at line 676 of file buffile.c.

References BufFileFlush(), BufFile::curFile, BufFile::curOffset, elog, ERROR, MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, and BufFile::pos.

Referenced by BufFileSeekBlock(), ExecHashJoinNewBatch(), tuplestore_copy_read_pointer(), tuplestore_gettuple(), tuplestore_puttuple_common(), tuplestore_rescan(), and tuplestore_select_read_pointer().

677 {
678  int newFile;
679  off_t newOffset;
680 
681  switch (whence)
682  {
683  case SEEK_SET:
684  if (fileno < 0)
685  return EOF;
686  newFile = fileno;
687  newOffset = offset;
688  break;
689  case SEEK_CUR:
690 
691  /*
692  * Relative seek considers only the signed offset, ignoring
693  * fileno. Note that large offsets (> 1 gig) risk overflow in this
694  * add, unless we have 64-bit off_t.
695  */
696  newFile = file->curFile;
697  newOffset = (file->curOffset + file->pos) + offset;
698  break;
699 #ifdef NOT_USED
700  case SEEK_END:
701  /* could be implemented, not needed currently */
702  break;
703 #endif
704  default:
705  elog(ERROR, "invalid whence: %d", whence);
706  return EOF;
707  }
708  while (newOffset < 0)
709  {
710  if (--newFile < 0)
711  return EOF;
712  newOffset += MAX_PHYSICAL_FILESIZE;
713  }
714  if (newFile == file->curFile &&
715  newOffset >= file->curOffset &&
716  newOffset <= file->curOffset + file->nbytes)
717  {
718  /*
719  * Seek is to a point within existing buffer; we can just adjust
720  * pos-within-buffer, without flushing buffer. Note this is OK
721  * whether reading or writing, but buffer remains dirty if we were
722  * writing.
723  */
724  file->pos = (int) (newOffset - file->curOffset);
725  return 0;
726  }
727  /* Otherwise, must reposition buffer, so flush any dirty data */
728  if (BufFileFlush(file) != 0)
729  return EOF;
730 
731  /*
732  * At this point and no sooner, check for seek past last segment. The
733  * above flush could have created a new segment, so checking sooner would
734  * not work (at least not with this code).
735  */
736 
737  /* convert seek to "start of next seg" to "end of last seg" */
738  if (newFile == file->numFiles && newOffset == 0)
739  {
740  newFile--;
741  newOffset = MAX_PHYSICAL_FILESIZE;
742  }
743  while (newOffset > MAX_PHYSICAL_FILESIZE)
744  {
745  if (++newFile >= file->numFiles)
746  return EOF;
747  newOffset -= MAX_PHYSICAL_FILESIZE;
748  }
749  if (newFile >= file->numFiles)
750  return EOF;
751  /* Seek is OK! */
752  file->curFile = newFile;
753  file->curOffset = newOffset;
754  file->pos = 0;
755  file->nbytes = 0;
756  return 0;
757 }
int nbytes
Definition: buffile.c:98
static int BufFileFlush(BufFile *file)
Definition: buffile.c:653
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:57
int numFiles
Definition: buffile.c:67
#define ERROR
Definition: elog.h:43
off_t curOffset
Definition: buffile.c:96
int curFile
Definition: buffile.c:95
#define elog
Definition: elog.h:219
int pos
Definition: buffile.c:97

◆ BufFileSeekBlock()

int BufFileSeekBlock ( BufFile file,
long  blknum 
)

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

779 {
780  return BufFileSeek(file,
781  (int) (blknum / BUFFILE_SEG_SIZE),
782  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
783  SEEK_SET);
784 }
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:676
#define BUFFILE_SEG_SIZE
Definition: buffile.c:58

◆ BufFileTell()

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

Definition at line 760 of file buffile.c.

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

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

761 {
762  *fileno = file->curFile;
763  *offset = file->curOffset + file->pos;
764 }
off_t curOffset
Definition: buffile.c:96
int curFile
Definition: buffile.c:95
int pos
Definition: buffile.c:97

◆ BufFileWrite()

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

Definition at line 601 of file buffile.c.

References Assert, BufFile::buffer, BufFileDumpBuffer(), BufFile::curOffset, BufFile::dirty, BufFile::nbytes, BufFile::pos, and BufFile::readOnly.

Referenced by ExecHashJoinSaveTuple(), ltsWriteBlock(), sts_flush_chunk(), WriteTempFileBlock(), and writetup_heap().

602 {
603  size_t nwritten = 0;
604  size_t nthistime;
605 
606  Assert(!file->readOnly);
607 
608  while (size > 0)
609  {
610  if (file->pos >= BLCKSZ)
611  {
612  /* Buffer full, dump it out */
613  if (file->dirty)
614  {
615  BufFileDumpBuffer(file);
616  if (file->dirty)
617  break; /* I/O error */
618  }
619  else
620  {
621  /* Hmm, went directly from reading to writing? */
622  file->curOffset += file->pos;
623  file->pos = 0;
624  file->nbytes = 0;
625  }
626  }
627 
628  nthistime = BLCKSZ - file->pos;
629  if (nthistime > size)
630  nthistime = size;
631  Assert(nthistime > 0);
632 
633  memcpy(file->buffer + file->pos, ptr, nthistime);
634 
635  file->dirty = true;
636  file->pos += nthistime;
637  if (file->nbytes < file->pos)
638  file->nbytes = file->pos;
639  ptr = (void *) ((char *) ptr + nthistime);
640  size -= nthistime;
641  nwritten += nthistime;
642  }
643 
644  return nwritten;
645 }
int nbytes
Definition: buffile.c:98
bool readOnly
Definition: buffile.c:79
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:469
off_t curOffset
Definition: buffile.c:96
#define Assert(condition)
Definition: c.h:680
bool dirty
Definition: buffile.c:78
char buffer[BLCKSZ]
Definition: buffile.c:99
int pos
Definition: buffile.c:97

◆ extendBufFile()

static void extendBufFile ( BufFile file)
static

Definition at line 142 of file buffile.c.

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

Referenced by BufFileDumpBuffer().

143 {
144  File pfile;
145  ResourceOwner oldowner;
146 
147  /* Be sure to associate the file with the BufFile's resource owner */
148  oldowner = CurrentResourceOwner;
150 
151  if (file->fileset == NULL)
152  pfile = OpenTemporaryFile(file->isInterXact);
153  else
154  pfile = MakeNewSharedSegment(file, file->numFiles);
155 
156  Assert(pfile >= 0);
157 
158  CurrentResourceOwner = oldowner;
159 
160  file->files = (File *) repalloc(file->files,
161  (file->numFiles + 1) * sizeof(File));
162  file->offsets = (off_t *) repalloc(file->offsets,
163  (file->numFiles + 1) * sizeof(off_t));
164  file->files[file->numFiles] = pfile;
165  file->offsets[file->numFiles] = 0L;
166  file->numFiles++;
167 }
SharedFileSet * fileset
Definition: buffile.c:81
off_t * offsets
Definition: buffile.c:70
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
int numFiles
Definition: buffile.c:67
ResourceOwner resowner
Definition: buffile.c:89
bool isInterXact
Definition: buffile.c:77
static File MakeNewSharedSegment(BufFile *file, int segment)
Definition: buffile.c:209
File * files
Definition: buffile.c:69
#define Assert(condition)
Definition: c.h:680
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1500
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:949
int File
Definition: fd.h:49

◆ makeBufFile()

static BufFile * makeBufFile ( File  firstfile)
static

Definition at line 115 of file buffile.c.

References BufFile::curFile, BufFile::curOffset, CurrentResourceOwner, BufFile::dirty, BufFile::files, BufFile::fileset, BufFile::isInterXact, BufFile::name, BufFile::nbytes, BufFile::numFiles, BufFile::offsets, palloc(), BufFile::pos, BufFile::readOnly, and BufFile::resowner.

Referenced by BufFileCreateTemp().

116 {
117  BufFile *file = (BufFile *) palloc(sizeof(BufFile));
118 
119  file->numFiles = 1;
120  file->files = (File *) palloc(sizeof(File));
121  file->files[0] = firstfile;
122  file->offsets = (off_t *) palloc(sizeof(off_t));
123  file->offsets[0] = 0L;
124  file->isInterXact = false;
125  file->dirty = false;
127  file->curFile = 0;
128  file->curOffset = 0L;
129  file->pos = 0;
130  file->nbytes = 0;
131  file->readOnly = false;
132  file->fileset = NULL;
133  file->name = NULL;
134 
135  return file;
136 }
SharedFileSet * fileset
Definition: buffile.c:81
off_t * offsets
Definition: buffile.c:70
int nbytes
Definition: buffile.c:98
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
bool readOnly
Definition: buffile.c:79
int numFiles
Definition: buffile.c:67
ResourceOwner resowner
Definition: buffile.c:89
bool isInterXact
Definition: buffile.c:77
File * files
Definition: buffile.c:69
off_t curOffset
Definition: buffile.c:96
const char * name
Definition: buffile.c:82
int curFile
Definition: buffile.c:95
bool dirty
Definition: buffile.c:78
void * palloc(Size size)
Definition: mcxt.c:835
int File
Definition: fd.h:49
int pos
Definition: buffile.c:97

◆ MakeNewSharedSegment()

static File MakeNewSharedSegment ( BufFile file,
int  segment 
)
static

Definition at line 209 of file buffile.c.

References Assert, BufFile::fileset, MAXPGPATH, BufFile::name, SharedFileSetCreate(), SharedFileSetDelete(), and SharedSegmentName().

Referenced by BufFileCreateShared(), and extendBufFile().

210 {
211  char name[MAXPGPATH];
212  File file;
213 
214  /*
215  * It is possible that there are files left over from before a crash
216  * restart with the same name. In order for BufFileOpenShared()
217  * not to get confused about how many segments there are, we'll unlink
218  * the next segment number if it already exists.
219  */
220  SharedSegmentName(name, buffile->name, segment + 1);
221  SharedFileSetDelete(buffile->fileset, name, true);
222 
223  /* Create the new segment. */
224  SharedSegmentName(name, buffile->name, segment);
225  file = SharedFileSetCreate(buffile->fileset, name);
226 
227  /* SharedFileSetCreate would've errored out */
228  Assert(file > 0);
229 
230  return file;
231 }
bool SharedFileSetDelete(SharedFileSet *fileset, const char *name, bool error_on_failure)
static void SharedSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:200
#define MAXPGPATH
#define Assert(condition)
Definition: c.h:680
const char * name
Definition: encode.c:521
File SharedFileSetCreate(SharedFileSet *fileset, const char *name)
int File
Definition: fd.h:49

◆ SharedSegmentName()

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

Definition at line 200 of file buffile.c.

References MAXPGPATH, and snprintf().

Referenced by BufFileDeleteShared(), BufFileOpenShared(), and MakeNewSharedSegment().

201 {
202  snprintf(name, MAXPGPATH, "%s.%d", buffile_name, segment);
203 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define MAXPGPATH
const char * name
Definition: encode.c:521