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

820 {
821  long startBlock = target->numFiles * BUFFILE_SEG_SIZE;
822  int newNumFiles = target->numFiles + source->numFiles;
823  int i;
824 
825  Assert(target->fileset != NULL);
826  Assert(source->readOnly);
827  Assert(!source->dirty);
828  Assert(source->fileset != NULL);
829 
830  if (target->resowner != source->resowner)
831  elog(ERROR, "could not append BufFile with non-matching resource owner");
832 
833  target->files = (File *)
834  repalloc(target->files, sizeof(File) * newNumFiles);
835  for (i = target->numFiles; i < newNumFiles; i++)
836  target->files[i] = source->files[i - target->numFiles];
837  target->numFiles = newNumFiles;
838 
839  return startBlock;
840 }
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:738
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
bool dirty
Definition: buffile.c:73
#define elog(elevel,...)
Definition: elog.h:214
int i
int File
Definition: fd.h:49

◆ 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(), SendBackupManifest(), 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 void BufFileFlush(BufFile *file)
Definition: buffile.c:626
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:1826
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:561
void * palloc(Size size)
Definition: mcxt.c:949
int File
Definition: fd.h:49
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(), InitializeBackupManifest(), 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:1326
bool isInterXact
Definition: buffile.c:72
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:130
#define Assert(condition)
Definition: c.h:738
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1577
int File
Definition: fd.h:49

◆ 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:561
#define elog(elevel,...)
Definition: elog.h:214
#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 void BufFileFlush(BufFile *file)
Definition: buffile.c:626
bool readOnly
Definition: buffile.c:74
#define Assert(condition)
Definition: c.h:738

◆ 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:633
File * files
Definition: buffile.c:70
const char * name
Definition: buffile.c:77
#define ereport(elevel,...)
Definition: elog.h:144
size_t Size
Definition: c.h:466
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
const char * name
Definition: encode.c:561
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
File SharedFileSetOpen(SharedFileSet *fileset, const char *name)
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
int File
Definition: fd.h:49
static BufFile * makeBufFileCommon(int nfiles)
Definition: buffile.c:109

◆ BufFileRead()

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

Definition at line 539 of file buffile.c.

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

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

540 {
541  size_t nread = 0;
542  size_t nthistime;
543 
544  BufFileFlush(file);
545 
546  while (size > 0)
547  {
548  if (file->pos >= file->nbytes)
549  {
550  /* Try to load more data into buffer. */
551  file->curOffset += file->pos;
552  file->pos = 0;
553  file->nbytes = 0;
554  BufFileLoadBuffer(file);
555  if (file->nbytes <= 0)
556  break; /* no more data available */
557  }
558 
559  nthistime = file->nbytes - file->pos;
560  if (nthistime > size)
561  nthistime = size;
562  Assert(nthistime > 0);
563 
564  memcpy(ptr, file->buffer.data + file->pos, nthistime);
565 
566  file->pos += nthistime;
567  ptr = (void *) ((char *) ptr + nthistime);
568  size -= nthistime;
569  nread += nthistime;
570  }
571 
572  return nread;
573 }
static void BufFileFlush(BufFile *file)
Definition: buffile.c:626
int nbytes
Definition: buffile.c:93
PGAlignedBlock buffer
Definition: buffile.c:94
char data[BLCKSZ]
Definition: c.h:1104
off_t curOffset
Definition: buffile.c:91
#define Assert(condition)
Definition: c.h:738
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 646 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(), SendBackupManifest(), tuplestore_copy_read_pointer(), tuplestore_gettuple(), tuplestore_puttuple_common(), tuplestore_rescan(), and tuplestore_select_read_pointer().

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

◆ BufFileSeekBlock()

int BufFileSeekBlock ( BufFile file,
long  blknum 
)

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

748 {
749  return BufFileSeek(file,
750  (int) (blknum / BUFFILE_SEG_SIZE),
751  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
752  SEEK_SET);
753 }
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:646
#define BUFFILE_SEG_SIZE
Definition: buffile.c:59

◆ BufFileSize()

int64 BufFileSize ( BufFile file)

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

781 {
782  int64 lastFileSize;
783 
784  Assert(file->fileset != NULL);
785 
786  /* Get the size of the last physical file. */
787  lastFileSize = FileSize(file->files[file->numFiles - 1]);
788  if (lastFileSize < 0)
789  ereport(ERROR,
791  errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
792  FilePathName(file->files[file->numFiles - 1]),
793  file->name)));
794 
795  return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +
796  lastFileSize;
797 }
SharedFileSet * fileset
Definition: buffile.c:76
off_t FileSize(File file)
Definition: fd.c:2148
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:58
int numFiles
Definition: buffile.c:68
char * FilePathName(File file)
Definition: fd.c:2200
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:633
File * files
Definition: buffile.c:70
const char * name
Definition: buffile.c:77
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ BufFileTell()

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

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

730 {
731  *fileno = file->curFile;
732  *offset = file->curOffset + file->pos;
733 }
off_t curOffset
Definition: buffile.c:91
int curFile
Definition: buffile.c:90
int pos
Definition: buffile.c:92

◆ BufFileWrite()

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

Definition at line 582 of file buffile.c.

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

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

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