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/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 BufFilemakeBufFileCommon (int nfiles)
 
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)
 
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 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:732
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044
bool dirty
Definition: buffile.c:73
#define elog(elevel,...)
Definition: elog.h:226
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:1031
File * files
Definition: buffile.c:70
void FileClose(File file)
Definition: fd.c:1713
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:1161
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:924
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:1324
bool isInterXact
Definition: buffile.c:72
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:130
#define Assert(condition)
Definition: c.h:732
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1464
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:226
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ BufFileDumpBuffer()

static void BufFileDumpBuffer ( BufFile file)
static

Definition at line 452 of file buffile.c.

References Assert, BufFile::buffer, BufFile::curFile, BufFile::curOffset, PGAlignedBlock::data, BufFile::dirty, extendBufFile(), 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().

453 {
454  int wpos = 0;
455  int bytestowrite;
456  File thisfile;
457 
458  /*
459  * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
460  * crosses a component-file boundary; so we need a loop.
461  */
462  while (wpos < file->nbytes)
463  {
464  off_t availbytes;
465 
466  /*
467  * Advance to next component file if necessary and possible.
468  */
469  if (file->curOffset >= MAX_PHYSICAL_FILESIZE)
470  {
471  while (file->curFile + 1 >= file->numFiles)
472  extendBufFile(file);
473  file->curFile++;
474  file->curOffset = 0L;
475  }
476 
477  /*
478  * Determine how much we need to write into this file.
479  */
480  bytestowrite = file->nbytes - wpos;
481  availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
482 
483  if ((off_t) bytestowrite > availbytes)
484  bytestowrite = (int) availbytes;
485 
486  thisfile = file->files[file->curFile];
487  bytestowrite = FileWrite(thisfile,
488  file->buffer.data + wpos,
489  bytestowrite,
490  file->curOffset,
492  if (bytestowrite <= 0)
493  return; /* failed to write */
494  file->curOffset += bytestowrite;
495  wpos += bytestowrite;
496 
498  }
499  file->dirty = false;
500 
501  /*
502  * At this point, curOffset has been advanced to the end of the buffer,
503  * ie, its original value + nbytes. We need to make it point to the
504  * logical file position, ie, original value + pos, in case that is less
505  * (as could happen due to a small backwards seek in a dirty buffer!)
506  */
507  file->curOffset -= (file->nbytes - file->pos);
508  if (file->curOffset < 0) /* handle possible segment crossing */
509  {
510  file->curFile--;
511  Assert(file->curFile >= 0);
513  }
514 
515  /*
516  * Now we can set the buffer empty without changing the logical position
517  */
518  file->pos = 0;
519  file->nbytes = 0;
520 }
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 data[BLCKSZ]
Definition: c.h:1060
File * files
Definition: buffile.c:70
#define wpos(wep)
Definition: tsrank.c:27
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:1916
int curFile
Definition: buffile.c:90
#define Assert(condition)
Definition: c.h:732
bool dirty
Definition: buffile.c:73
int File
Definition: fd.h:45
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 int BufFileFlush(BufFile *file)
Definition: buffile.c:627
bool readOnly
Definition: buffile.c:74
#define Assert(condition)
Definition: c.h:732

◆ BufFileFlush()

static int BufFileFlush ( BufFile file)
static

Definition at line 627 of file buffile.c.

References BufFileDumpBuffer(), and BufFile::dirty.

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

628 {
629  if (file->dirty)
630  {
631  BufFileDumpBuffer(file);
632  if (file->dirty)
633  return EOF;
634  }
635 
636  return 0;
637 }
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:452
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, 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  file->nbytes = 0;
438  /* we choose not to advance curOffset here */
439 
440  if (file->nbytes > 0)
442 }
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 data[BLCKSZ]
Definition: c.h:1060
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
int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:1860
int File
Definition: fd.h:45
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:1161
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:593
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:466
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044
const char * name
Definition: encode.c:521
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
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:1060
off_t curOffset
Definition: buffile.c:91
#define Assert(condition)
Definition: c.h:732
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:226
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:2035
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:58
int numFiles
Definition: buffile.c:68
char * FilePathName(File file)
Definition: fd.c:2087
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:593
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:732
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ 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:1060
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:452
off_t curOffset
Definition: buffile.c:91
#define Assert(condition)
Definition: c.h:732
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:732
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1464
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044
int File
Definition: fd.h:45

◆ 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:924
int File
Definition: fd.h:45
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:924
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:732
const char * name
Definition: encode.c:521
File SharedFileSetCreate(SharedFileSet *fileset, const char *name)
int File
Definition: fd.h:45

◆ 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:521
#define snprintf
Definition: port.h:192