PostgreSQL Source Code  git master
buffile.h File Reference
Include dependency graph for buffile.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct BufFile BufFile
 

Functions

BufFileBufFileCreateTemp (bool interXact)
 
void BufFileClose (BufFile *file)
 
size_t BufFileRead (BufFile *file, void *ptr, size_t size)
 
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)
 
BufFileBufFileCreateShared (SharedFileSet *fileset, const char *name)
 
void BufFileExportShared (BufFile *file)
 
BufFileBufFileOpenShared (SharedFileSet *fileset, const char *name)
 
void BufFileDeleteShared (SharedFileSet *fileset, const char *name)
 

Typedef Documentation

◆ BufFile

Definition at line 33 of file buffile.h.

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

◆ 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

◆ 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