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)
 
int64 BufFileSize (BufFile *file)
 
long BufFileAppend (BufFile *target, BufFile *source)
 
BufFileBufFileCreateShared (SharedFileSet *fileset, const char *name)
 
void BufFileExportShared (BufFile *file)
 
BufFileBufFileOpenShared (SharedFileSet *fileset, const char *name)
 
void BufFileDeleteShared (SharedFileSet *fileset, const char *name)
 

Typedef Documentation

◆ BufFile

typedef struct BufFile BufFile

Definition at line 33 of file buffile.h.

Function Documentation

◆ BufFileAppend()

long BufFileAppend ( BufFile target,
BufFile source 
)

Definition at line 824 of file buffile.c.

References Assert, BUFFILE_SEG_SIZE, BufFile::dirty, elog, ERROR, BufFile::files, BufFile::fileset, i, BufFile::numFiles, BufFile::readOnly, repalloc(), and BufFile::resowner.

Referenced by ltsConcatWorkerTapes().

825 {
826  long startBlock = target->numFiles * BUFFILE_SEG_SIZE;
827  int newNumFiles = target->numFiles + source->numFiles;
828  int i;
829 
830  Assert(target->fileset != NULL);
831  Assert(source->readOnly);
832  Assert(!source->dirty);
833  Assert(source->fileset != NULL);
834 
835  if (target->resowner != source->resowner)
836  elog(ERROR, "could not append BufFile with non-matching resource owner");
837 
838  target->files = (File *)
839  repalloc(target->files, sizeof(File) * newNumFiles);
840  for (i = target->numFiles; i < newNumFiles; i++)
841  target->files[i] = source->files[i - target->numFiles];
842  target->numFiles = newNumFiles;
843 
844  return startBlock;
845 }
SharedFileSet * fileset
Definition: buffile.c:76
bool readOnly
Definition: buffile.c:74
#define BUFFILE_SEG_SIZE
Definition: buffile.c:59
int numFiles
Definition: buffile.c:68
ResourceOwner resowner
Definition: buffile.c:84
#define ERROR
Definition: elog.h:43
File * files
Definition: buffile.c:70
#define Assert(condition)
Definition: c.h:733
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
bool dirty
Definition: buffile.c:73
#define elog(elevel,...)
Definition: elog.h:228
int i
int File
Definition: fd.h:45

◆ BufFileClose()

void BufFileClose ( BufFile file)

Definition at line 391 of file buffile.c.

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

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

392 {
393  int i;
394 
395  /* flush any unwritten data */
396  BufFileFlush(file);
397  /* close and delete the underlying file(s) */
398  for (i = 0; i < file->numFiles; i++)
399  FileClose(file->files[i]);
400  /* release the buffer space */
401  pfree(file->files);
402  pfree(file);
403 }
static int BufFileFlush(BufFile *file)
Definition: buffile.c:627
int numFiles
Definition: buffile.c:68
void pfree(void *pointer)
Definition: mcxt.c:1056
File * files
Definition: buffile.c:70
void FileClose(File file)
Definition: fd.c:1710
int i

◆ BufFileCreateShared()

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

Definition at line 258 of file buffile.c.

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

Referenced by LogicalTapeSetCreate(), and sts_puttuple().

259 {
260  BufFile *file;
261 
262  file = makeBufFileCommon(1);
263  file->fileset = fileset;
264  file->name = pstrdup(name);
265  file->files = (File *) palloc(sizeof(File));
266  file->files[0] = MakeNewSharedSegment(file, 0);
267  file->readOnly = false;
268 
269  return file;
270 }
SharedFileSet * fileset
Definition: buffile.c:76
char * pstrdup(const char *in)
Definition: mcxt.c:1186
bool readOnly
Definition: buffile.c:74
static File MakeNewSharedSegment(BufFile *file, int segment)
Definition: buffile.c:222
File * files
Definition: buffile.c:70
const char * name
Definition: buffile.c:77
const char * name
Definition: encode.c:521
void * palloc(Size size)
Definition: mcxt.c:949
int File
Definition: fd.h:45
static BufFile * makeBufFileCommon(int nfiles)
Definition: buffile.c:109

◆ BufFileCreateTemp()

BufFile* BufFileCreateTemp ( bool  interXact)

Definition at line 184 of file buffile.c.

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

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

185 {
186  BufFile *file;
187  File pfile;
188 
189  /*
190  * Ensure that temp tablespaces are set up for OpenTemporaryFile to use.
191  * Possibly the caller will have done this already, but it seems useful to
192  * double-check here. Failure to do this at all would result in the temp
193  * files always getting placed in the default tablespace, which is a
194  * pretty hard-to-detect bug. Callers may prefer to do it earlier if they
195  * want to be sure that any required catalog access is done in some other
196  * resource context.
197  */
199 
200  pfile = OpenTemporaryFile(interXact);
201  Assert(pfile >= 0);
202 
203  file = makeBufFile(pfile);
204  file->isInterXact = interXact;
205 
206  return file;
207 }
void PrepareTempTablespaces(void)
Definition: tablespace.c:1323
bool isInterXact
Definition: buffile.c:72
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:130
#define Assert(condition)
Definition: c.h:733
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1461
int File
Definition: fd.h:45

◆ BufFileDeleteShared()

void BufFileDeleteShared ( SharedFileSet fileset,
const char *  name 
)

Definition at line 343 of file buffile.c.

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

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

◆ BufFileExportShared()

void BufFileExportShared ( BufFile file)

Definition at line 373 of file buffile.c.

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

Referenced by LogicalTapeFreeze().

374 {
375  /* Must be a file belonging to a SharedFileSet. */
376  Assert(file->fileset != NULL);
377 
378  /* It's probably a bug if someone calls this twice. */
379  Assert(!file->readOnly);
380 
381  BufFileFlush(file);
382  file->readOnly = true;
383 }
SharedFileSet * fileset
Definition: buffile.c:76
static int BufFileFlush(BufFile *file)
Definition: buffile.c:627
bool readOnly
Definition: buffile.c:74
#define Assert(condition)
Definition: c.h:733

◆ BufFileOpenShared()

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

Definition at line 280 of file buffile.c.

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

Referenced by ltsConcatWorkerTapes(), and sts_parallel_scan_next().

281 {
282  BufFile *file;
283  char segment_name[MAXPGPATH];
284  Size capacity = 16;
285  File *files;
286  int nfiles = 0;
287 
288  files = palloc(sizeof(File) * capacity);
289 
290  /*
291  * We don't know how many segments there are, so we'll probe the
292  * filesystem to find out.
293  */
294  for (;;)
295  {
296  /* See if we need to expand our file segment array. */
297  if (nfiles + 1 > capacity)
298  {
299  capacity *= 2;
300  files = repalloc(files, sizeof(File) * capacity);
301  }
302  /* Try to load a segment. */
303  SharedSegmentName(segment_name, name, nfiles);
304  files[nfiles] = SharedFileSetOpen(fileset, segment_name);
305  if (files[nfiles] <= 0)
306  break;
307  ++nfiles;
308 
310  }
311 
312  /*
313  * If we didn't find any files at all, then no BufFile exists with this
314  * name.
315  */
316  if (nfiles == 0)
317  ereport(ERROR,
319  errmsg("could not open temporary file \"%s\" from BufFile \"%s\": %m",
320  segment_name, name)));
321 
322  file = makeBufFileCommon(nfiles);
323  file->files = files;
324  file->readOnly = true; /* Can't write to files opened this way */
325  file->fileset = fileset;
326  file->name = pstrdup(name);
327 
328  return file;
329 }
SharedFileSet * fileset
Definition: buffile.c:76
char * pstrdup(const char *in)
Definition: mcxt.c:1186
bool readOnly
Definition: buffile.c:74
static int64 files
Definition: pg_checksums.c:34
#define ERROR
Definition: elog.h:43
static void SharedSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:213
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:631
File * files
Definition: buffile.c:70
#define ereport(elevel, rest)
Definition: elog.h:141
const char * name
Definition: buffile.c:77
size_t Size
Definition: c.h:467
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
const char * name
Definition: encode.c:521
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
File SharedFileSetOpen(SharedFileSet *fileset, const char *name)
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
int File
Definition: fd.h:45
static BufFile * makeBufFileCommon(int nfiles)
Definition: buffile.c:109

◆ BufFileRead()

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

Definition at line 528 of file buffile.c.

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

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

529 {
530  size_t nread = 0;
531  size_t nthistime;
532 
533  if (file->dirty)
534  {
535  if (BufFileFlush(file) != 0)
536  return 0; /* could not flush... */
537  Assert(!file->dirty);
538  }
539 
540  while (size > 0)
541  {
542  if (file->pos >= file->nbytes)
543  {
544  /* Try to load more data into buffer. */
545  file->curOffset += file->pos;
546  file->pos = 0;
547  file->nbytes = 0;
548  BufFileLoadBuffer(file);
549  if (file->nbytes <= 0)
550  break; /* no more data available */
551  }
552 
553  nthistime = file->nbytes - file->pos;
554  if (nthistime > size)
555  nthistime = size;
556  Assert(nthistime > 0);
557 
558  memcpy(ptr, file->buffer.data + file->pos, nthistime);
559 
560  file->pos += nthistime;
561  ptr = (void *) ((char *) ptr + nthistime);
562  size -= nthistime;
563  nread += nthistime;
564  }
565 
566  return nread;
567 }
int nbytes
Definition: buffile.c:93
static int BufFileFlush(BufFile *file)
Definition: buffile.c:627
PGAlignedBlock buffer
Definition: buffile.c:94
char data[BLCKSZ]
Definition: c.h:1085
off_t curOffset
Definition: buffile.c:91
#define Assert(condition)
Definition: c.h:733
bool dirty
Definition: buffile.c:73
static void BufFileLoadBuffer(BufFile *file)
Definition: buffile.c:413
int pos
Definition: buffile.c:92

◆ BufFileSeek()

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

Definition at line 650 of file buffile.c.

References BufFileFlush(), BufFile::curFile, BufFile::curOffset, elog, 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().

651 {
652  int newFile;
653  off_t newOffset;
654 
655  switch (whence)
656  {
657  case SEEK_SET:
658  if (fileno < 0)
659  return EOF;
660  newFile = fileno;
661  newOffset = offset;
662  break;
663  case SEEK_CUR:
664 
665  /*
666  * Relative seek considers only the signed offset, ignoring
667  * fileno. Note that large offsets (> 1 GB) risk overflow in this
668  * add, unless we have 64-bit off_t.
669  */
670  newFile = file->curFile;
671  newOffset = (file->curOffset + file->pos) + offset;
672  break;
673 #ifdef NOT_USED
674  case SEEK_END:
675  /* could be implemented, not needed currently */
676  break;
677 #endif
678  default:
679  elog(ERROR, "invalid whence: %d", whence);
680  return EOF;
681  }
682  while (newOffset < 0)
683  {
684  if (--newFile < 0)
685  return EOF;
686  newOffset += MAX_PHYSICAL_FILESIZE;
687  }
688  if (newFile == file->curFile &&
689  newOffset >= file->curOffset &&
690  newOffset <= file->curOffset + file->nbytes)
691  {
692  /*
693  * Seek is to a point within existing buffer; we can just adjust
694  * pos-within-buffer, without flushing buffer. Note this is OK
695  * whether reading or writing, but buffer remains dirty if we were
696  * writing.
697  */
698  file->pos = (int) (newOffset - file->curOffset);
699  return 0;
700  }
701  /* Otherwise, must reposition buffer, so flush any dirty data */
702  if (BufFileFlush(file) != 0)
703  return EOF;
704 
705  /*
706  * At this point and no sooner, check for seek past last segment. The
707  * above flush could have created a new segment, so checking sooner would
708  * not work (at least not with this code).
709  */
710 
711  /* convert seek to "start of next seg" to "end of last seg" */
712  if (newFile == file->numFiles && newOffset == 0)
713  {
714  newFile--;
715  newOffset = MAX_PHYSICAL_FILESIZE;
716  }
717  while (newOffset > MAX_PHYSICAL_FILESIZE)
718  {
719  if (++newFile >= file->numFiles)
720  return EOF;
721  newOffset -= MAX_PHYSICAL_FILESIZE;
722  }
723  if (newFile >= file->numFiles)
724  return EOF;
725  /* Seek is OK! */
726  file->curFile = newFile;
727  file->curOffset = newOffset;
728  file->pos = 0;
729  file->nbytes = 0;
730  return 0;
731 }
int nbytes
Definition: buffile.c:93
static int BufFileFlush(BufFile *file)
Definition: buffile.c:627
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:58
int numFiles
Definition: buffile.c:68
#define ERROR
Definition: elog.h:43
off_t curOffset
Definition: buffile.c:91
int curFile
Definition: buffile.c:90
#define elog(elevel,...)
Definition: elog.h:228
int pos
Definition: buffile.c:92

◆ BufFileSeekBlock()

int BufFileSeekBlock ( BufFile file,
long  blknum 
)

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

753 {
754  return BufFileSeek(file,
755  (int) (blknum / BUFFILE_SEG_SIZE),
756  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
757  SEEK_SET);
758 }
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:650
#define BUFFILE_SEG_SIZE
Definition: buffile.c:59

◆ BufFileSize()

int64 BufFileSize ( BufFile file)

Definition at line 785 of file buffile.c.

References Assert, ereport, errcode_for_file_access(), errmsg(), ERROR, FilePathName(), BufFile::files, BufFile::fileset, FileSize(), MAX_PHYSICAL_FILESIZE, BufFile::name, and BufFile::numFiles.

Referenced by ltsConcatWorkerTapes().

786 {
787  int64 lastFileSize;
788 
789  Assert(file->fileset != NULL);
790 
791  /* Get the size of the last physical file. */
792  lastFileSize = FileSize(file->files[file->numFiles - 1]);
793  if (lastFileSize < 0)
794  ereport(ERROR,
796  errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
797  FilePathName(file->files[file->numFiles - 1]),
798  file->name)));
799 
800  return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +
801  lastFileSize;
802 }
SharedFileSet * fileset
Definition: buffile.c:76
off_t FileSize(File file)
Definition: fd.c:2032
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:58
int numFiles
Definition: buffile.c:68
char * FilePathName(File file)
Definition: fd.c:2084
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:631
File * files
Definition: buffile.c:70
#define ereport(elevel, rest)
Definition: elog.h:141
const char * name
Definition: buffile.c:77
#define Assert(condition)
Definition: c.h:733
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ BufFileTell()

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

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

735 {
736  *fileno = file->curFile;
737  *offset = file->curOffset + file->pos;
738 }
off_t curOffset
Definition: buffile.c:91
int curFile
Definition: buffile.c:90
int pos
Definition: buffile.c:92

◆ BufFileWrite()

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

Definition at line 575 of file buffile.c.

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

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

576 {
577  size_t nwritten = 0;
578  size_t nthistime;
579 
580  Assert(!file->readOnly);
581 
582  while (size > 0)
583  {
584  if (file->pos >= BLCKSZ)
585  {
586  /* Buffer full, dump it out */
587  if (file->dirty)
588  {
589  BufFileDumpBuffer(file);
590  if (file->dirty)
591  break; /* I/O error */
592  }
593  else
594  {
595  /* Hmm, went directly from reading to writing? */
596  file->curOffset += file->pos;
597  file->pos = 0;
598  file->nbytes = 0;
599  }
600  }
601 
602  nthistime = BLCKSZ - file->pos;
603  if (nthistime > size)
604  nthistime = size;
605  Assert(nthistime > 0);
606 
607  memcpy(file->buffer.data + file->pos, ptr, nthistime);
608 
609  file->dirty = true;
610  file->pos += nthistime;
611  if (file->nbytes < file->pos)
612  file->nbytes = file->pos;
613  ptr = (void *) ((char *) ptr + nthistime);
614  size -= nthistime;
615  nwritten += nthistime;
616  }
617 
618  return nwritten;
619 }
int nbytes
Definition: buffile.c:93
bool readOnly
Definition: buffile.c:74
PGAlignedBlock buffer
Definition: buffile.c:94
char data[BLCKSZ]
Definition: c.h:1085
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:452
off_t curOffset
Definition: buffile.c:91
#define Assert(condition)
Definition: c.h:733
bool dirty
Definition: buffile.c:73
int pos
Definition: buffile.c:92