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

Macro Definition Documentation

◆ BUFFILE_SEG_SIZE

#define BUFFILE_SEG_SIZE   (MAX_PHYSICAL_FILESIZE / BLCKSZ)

Definition at line 59 of file buffile.c.

Referenced by BufFileAppend(), and BufFileSeekBlock().

◆ MAX_PHYSICAL_FILESIZE

#define MAX_PHYSICAL_FILESIZE   0x40000000

Definition at line 58 of file buffile.c.

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

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:1824
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:1323
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:1575
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

◆ BufFileDumpBuffer()

static void BufFileDumpBuffer ( BufFile file)
static

Definition at line 459 of file buffile.c.

References Assert, BufFile::buffer, BufFile::curFile, BufFile::curOffset, PGAlignedBlock::data, BufFile::dirty, ereport, errcode_for_file_access(), errmsg(), ERROR, extendBufFile(), FilePathName(), BufFile::files, FileWrite(), MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, pgBufferUsage, BufFile::pos, BufferUsage::temp_blks_written, WAIT_EVENT_BUFFILE_WRITE, and wpos.

Referenced by BufFileFlush(), and BufFileWrite().

460 {
461  int wpos = 0;
462  int bytestowrite;
463  File thisfile;
464 
465  /*
466  * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
467  * crosses a component-file boundary; so we need a loop.
468  */
469  while (wpos < file->nbytes)
470  {
471  off_t availbytes;
472 
473  /*
474  * Advance to next component file if necessary and possible.
475  */
476  if (file->curOffset >= MAX_PHYSICAL_FILESIZE)
477  {
478  while (file->curFile + 1 >= file->numFiles)
479  extendBufFile(file);
480  file->curFile++;
481  file->curOffset = 0L;
482  }
483 
484  /*
485  * Determine how much we need to write into this file.
486  */
487  bytestowrite = file->nbytes - wpos;
488  availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
489 
490  if ((off_t) bytestowrite > availbytes)
491  bytestowrite = (int) availbytes;
492 
493  thisfile = file->files[file->curFile];
494  bytestowrite = FileWrite(thisfile,
495  file->buffer.data + wpos,
496  bytestowrite,
497  file->curOffset,
499  if (bytestowrite <= 0)
500  ereport(ERROR,
502  errmsg("could not write to file \"%s\" : %m",
503  FilePathName(thisfile))));
504  file->curOffset += bytestowrite;
505  wpos += bytestowrite;
506 
508  }
509  file->dirty = false;
510 
511  /*
512  * At this point, curOffset has been advanced to the end of the buffer,
513  * ie, its original value + nbytes. We need to make it point to the
514  * logical file position, ie, original value + pos, in case that is less
515  * (as could happen due to a small backwards seek in a dirty buffer!)
516  */
517  file->curOffset -= (file->nbytes - file->pos);
518  if (file->curOffset < 0) /* handle possible segment crossing */
519  {
520  file->curFile--;
521  Assert(file->curFile >= 0);
523  }
524 
525  /*
526  * Now we can set the buffer empty without changing the logical position
527  */
528  file->pos = 0;
529  file->nbytes = 0;
530 }
int nbytes
Definition: buffile.c:93
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:58
PGAlignedBlock buffer
Definition: buffile.c:94
long temp_blks_written
Definition: instrument.h:30
static void extendBufFile(BufFile *file)
Definition: buffile.c:147
int numFiles
Definition: buffile.c:68
char * FilePathName(File file)
Definition: fd.c:2198
char data[BLCKSZ]
Definition: c.h:1104
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:633
File * files
Definition: buffile.c:70
#define wpos(wep)
Definition: tsrank.c:26
off_t curOffset
Definition: buffile.c:91
int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2027
int curFile
Definition: buffile.c:90
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
bool dirty
Definition: buffile.c:73
int errmsg(const char *fmt,...)
Definition: elog.c:824
int File
Definition: fd.h:49
BufferUsage pgBufferUsage
Definition: instrument.c:20
int pos
Definition: buffile.c:92

◆ 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

◆ BufFileFlush()

static void BufFileFlush ( BufFile file)
static

Definition at line 626 of file buffile.c.

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

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

627 {
628  if (file->dirty)
629  BufFileDumpBuffer(file);
630 
631  Assert(!file->dirty);
632 }
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:459
#define Assert(condition)
Definition: c.h:738
bool dirty
Definition: buffile.c:73

◆ BufFileLoadBuffer()

static void BufFileLoadBuffer ( BufFile file)
static

Definition at line 413 of file buffile.c.

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

Referenced by BufFileRead().

414 {
415  File thisfile;
416 
417  /*
418  * Advance to next component file if necessary and possible.
419  */
420  if (file->curOffset >= MAX_PHYSICAL_FILESIZE &&
421  file->curFile + 1 < file->numFiles)
422  {
423  file->curFile++;
424  file->curOffset = 0L;
425  }
426 
427  /*
428  * Read whatever we can get, up to a full bufferload.
429  */
430  thisfile = file->files[file->curFile];
431  file->nbytes = FileRead(thisfile,
432  file->buffer.data,
433  sizeof(file->buffer),
434  file->curOffset,
436  if (file->nbytes < 0)
437  {
438  file->nbytes = 0;
439  ereport(ERROR,
441  errmsg("could not read file \"%s\": %m",
442  FilePathName(thisfile))));
443  }
444 
445  /* we choose not to advance curOffset here */
446 
447  if (file->nbytes > 0)
449 }
int nbytes
Definition: buffile.c:93
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:58
PGAlignedBlock buffer
Definition: buffile.c:94
int numFiles
Definition: buffile.c:68
char * FilePathName(File file)
Definition: fd.c:2198
char data[BLCKSZ]
Definition: c.h:1104
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:633
long temp_blks_read
Definition: instrument.h:29
File * files
Definition: buffile.c:70
off_t curOffset
Definition: buffile.c:91
int curFile
Definition: buffile.c:90
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:1971
int File
Definition: fd.h:49
BufferUsage pgBufferUsage
Definition: instrument.c:20

◆ 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:2146
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:58
int numFiles
Definition: buffile.c:68
char * FilePathName(File file)
Definition: fd.c:2198
#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

◆ extendBufFile()

static void extendBufFile ( BufFile file)
static

Definition at line 147 of file buffile.c.

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

Referenced by BufFileDumpBuffer().

148 {
149  File pfile;
150  ResourceOwner oldowner;
151 
152  /* Be sure to associate the file with the BufFile's resource owner */
153  oldowner = CurrentResourceOwner;
155 
156  if (file->fileset == NULL)
157  pfile = OpenTemporaryFile(file->isInterXact);
158  else
159  pfile = MakeNewSharedSegment(file, file->numFiles);
160 
161  Assert(pfile >= 0);
162 
163  CurrentResourceOwner = oldowner;
164 
165  file->files = (File *) repalloc(file->files,
166  (file->numFiles + 1) * sizeof(File));
167  file->files[file->numFiles] = pfile;
168  file->numFiles++;
169 }
SharedFileSet * fileset
Definition: buffile.c:76
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
int numFiles
Definition: buffile.c:68
ResourceOwner resowner
Definition: buffile.c:84
bool isInterXact
Definition: buffile.c:72
static File MakeNewSharedSegment(BufFile *file, int segment)
Definition: buffile.c:222
File * files
Definition: buffile.c:70
#define Assert(condition)
Definition: c.h:738
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1575
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
int File
Definition: fd.h:49

◆ makeBufFile()

static BufFile * makeBufFile ( File  firstfile)
static

Definition at line 130 of file buffile.c.

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

Referenced by BufFileCreateTemp().

131 {
132  BufFile *file = makeBufFileCommon(1);
133 
134  file->files = (File *) palloc(sizeof(File));
135  file->files[0] = firstfile;
136  file->readOnly = false;
137  file->fileset = NULL;
138  file->name = NULL;
139 
140  return file;
141 }
SharedFileSet * fileset
Definition: buffile.c:76
bool readOnly
Definition: buffile.c:74
File * files
Definition: buffile.c:70
const char * name
Definition: buffile.c:77
void * palloc(Size size)
Definition: mcxt.c:949
int File
Definition: fd.h:49
static BufFile * makeBufFileCommon(int nfiles)
Definition: buffile.c:109

◆ makeBufFileCommon()

static BufFile * makeBufFileCommon ( int  nfiles)
static

Definition at line 109 of file buffile.c.

References BufFile::curFile, BufFile::curOffset, CurrentResourceOwner, BufFile::dirty, BufFile::isInterXact, BufFile::nbytes, BufFile::numFiles, palloc(), BufFile::pos, and BufFile::resowner.

Referenced by BufFileCreateShared(), BufFileOpenShared(), and makeBufFile().

110 {
111  BufFile *file = (BufFile *) palloc(sizeof(BufFile));
112 
113  file->numFiles = nfiles;
114  file->isInterXact = false;
115  file->dirty = false;
117  file->curFile = 0;
118  file->curOffset = 0L;
119  file->pos = 0;
120  file->nbytes = 0;
121 
122  return file;
123 }
int nbytes
Definition: buffile.c:93
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
int numFiles
Definition: buffile.c:68
ResourceOwner resowner
Definition: buffile.c:84
bool isInterXact
Definition: buffile.c:72
off_t curOffset
Definition: buffile.c:91
int curFile
Definition: buffile.c:90
bool dirty
Definition: buffile.c:73
void * palloc(Size size)
Definition: mcxt.c:949
int pos
Definition: buffile.c:92

◆ MakeNewSharedSegment()

static File MakeNewSharedSegment ( BufFile file,
int  segment 
)
static

Definition at line 222 of file buffile.c.

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

Referenced by BufFileCreateShared(), and extendBufFile().

223 {
224  char name[MAXPGPATH];
225  File file;
226 
227  /*
228  * It is possible that there are files left over from before a crash
229  * restart with the same name. In order for BufFileOpenShared() not to
230  * get confused about how many segments there are, we'll unlink the next
231  * segment number if it already exists.
232  */
233  SharedSegmentName(name, buffile->name, segment + 1);
234  SharedFileSetDelete(buffile->fileset, name, true);
235 
236  /* Create the new segment. */
237  SharedSegmentName(name, buffile->name, segment);
238  file = SharedFileSetCreate(buffile->fileset, name);
239 
240  /* SharedFileSetCreate would've errored out */
241  Assert(file > 0);
242 
243  return file;
244 }
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:213
#define MAXPGPATH
#define Assert(condition)
Definition: c.h:738
const char * name
Definition: encode.c:561
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 213 of file buffile.c.

References MAXPGPATH, and snprintf.

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

214 {
215  snprintf(name, MAXPGPATH, "%s.%d", buffile_name, segment);
216 }
#define MAXPGPATH
const char * name
Definition: encode.c:561
#define snprintf
Definition: port.h:193