PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
buffile.c File Reference
#include "postgres.h"
#include "executor/instrument.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 BufFilemakeBufFile (File firstfile)
 
static void extendBufFile (BufFile *file)
 
static void BufFileLoadBuffer (BufFile *file)
 
static void BufFileDumpBuffer (BufFile *file)
 
static int BufFileFlush (BufFile *file)
 
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)
 

Macro Definition Documentation

#define BUFFILE_SEG_SIZE   (MAX_PHYSICAL_FILESIZE / BLCKSZ)

Definition at line 52 of file buffile.c.

Referenced by BufFileSeekBlock().

#define MAX_PHYSICAL_FILESIZE   0x40000000

Definition at line 51 of file buffile.c.

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

Function Documentation

void BufFileClose ( BufFile file)

Definition at line 203 of file buffile.c.

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

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

204 {
205  int i;
206 
207  /* flush any unwritten data */
208  BufFileFlush(file);
209  /* close the underlying file(s) (with delete if it's a temp file) */
210  for (i = 0; i < file->numFiles; i++)
211  FileClose(file->files[i]);
212  /* release the buffer space */
213  pfree(file->files);
214  pfree(file->offsets);
215  pfree(file);
216 }
off_t * offsets
Definition: buffile.c:64
static int BufFileFlush(BufFile *file)
Definition: buffile.c:462
int numFiles
Definition: buffile.c:61
void pfree(void *pointer)
Definition: mcxt.c:950
File * files
Definition: buffile.c:63
void FileClose(File file)
Definition: fd.c:1456
int i
BufFile* BufFileCreateTemp ( bool  interXact)

Definition at line 167 of file buffile.c.

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

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

168 {
169  BufFile *file;
170  File pfile;
171 
172  pfile = OpenTemporaryFile(interXact);
173  Assert(pfile >= 0);
174 
175  file = makeBufFile(pfile);
176  file->isTemp = true;
177  file->isInterXact = interXact;
178 
179  return file;
180 }
bool isTemp
Definition: buffile.c:71
bool isInterXact
Definition: buffile.c:72
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:105
#define Assert(condition)
Definition: c.h:675
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1334
int File
Definition: fd.h:51
static void BufFileDumpBuffer ( BufFile file)
static

Definition at line 278 of file buffile.c.

References Assert, BufFile::buffer, BufFile::curFile, BufFile::curOffset, BufFile::dirty, extendBufFile(), BufFile::files, FileSeek(), FileWrite(), BufFile::isTemp, MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, BufFile::offsets, pgBufferUsage, BufFile::pos, BufferUsage::temp_blks_written, WAIT_EVENT_BUFFILE_WRITE, and wpos.

Referenced by BufFileFlush(), and BufFileWrite().

279 {
280  int wpos = 0;
281  int bytestowrite;
282  File thisfile;
283 
284  /*
285  * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
286  * crosses a component-file boundary; so we need a loop.
287  */
288  while (wpos < file->nbytes)
289  {
290  /*
291  * Advance to next component file if necessary and possible.
292  */
293  if (file->curOffset >= MAX_PHYSICAL_FILESIZE && file->isTemp)
294  {
295  while (file->curFile + 1 >= file->numFiles)
296  extendBufFile(file);
297  file->curFile++;
298  file->curOffset = 0L;
299  }
300 
301  /*
302  * Enforce per-file size limit only for temp files, else just try to
303  * write as much as asked...
304  */
305  bytestowrite = file->nbytes - wpos;
306  if (file->isTemp)
307  {
308  off_t availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
309 
310  if ((off_t) bytestowrite > availbytes)
311  bytestowrite = (int) availbytes;
312  }
313 
314  /*
315  * May need to reposition physical file.
316  */
317  thisfile = file->files[file->curFile];
318  if (file->curOffset != file->offsets[file->curFile])
319  {
320  if (FileSeek(thisfile, file->curOffset, SEEK_SET) != file->curOffset)
321  return; /* seek failed, give up */
322  file->offsets[file->curFile] = file->curOffset;
323  }
324  bytestowrite = FileWrite(thisfile,
325  file->buffer + wpos,
326  bytestowrite,
328  if (bytestowrite <= 0)
329  return; /* failed to write */
330  file->offsets[file->curFile] += bytestowrite;
331  file->curOffset += bytestowrite;
332  wpos += bytestowrite;
333 
335  }
336  file->dirty = false;
337 
338  /*
339  * At this point, curOffset has been advanced to the end of the buffer,
340  * ie, its original value + nbytes. We need to make it point to the
341  * logical file position, ie, original value + pos, in case that is less
342  * (as could happen due to a small backwards seek in a dirty buffer!)
343  */
344  file->curOffset -= (file->nbytes - file->pos);
345  if (file->curOffset < 0) /* handle possible segment crossing */
346  {
347  file->curFile--;
348  Assert(file->curFile >= 0);
350  }
351 
352  /*
353  * Now we can set the buffer empty without changing the logical position
354  */
355  file->pos = 0;
356  file->nbytes = 0;
357 }
off_t * offsets
Definition: buffile.c:64
int nbytes
Definition: buffile.c:89
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:51
bool isTemp
Definition: buffile.c:71
long temp_blks_written
Definition: instrument.h:30
static void extendBufFile(BufFile *file)
Definition: buffile.c:130
int numFiles
Definition: buffile.c:61
int FileWrite(File file, char *buffer, int amount, uint32 wait_event_info)
Definition: fd.c:1672
File * files
Definition: buffile.c:63
#define wpos(wep)
Definition: tsrank.c:27
off_t curOffset
Definition: buffile.c:87
int curFile
Definition: buffile.c:86
#define Assert(condition)
Definition: c.h:675
bool dirty
Definition: buffile.c:73
char buffer[BLCKSZ]
Definition: buffile.c:90
off_t FileSeek(File file, off_t offset, int whence)
Definition: fd.c:1814
int File
Definition: fd.h:51
BufferUsage pgBufferUsage
Definition: instrument.c:20
int pos
Definition: buffile.c:88
static int BufFileFlush ( BufFile file)
static

Definition at line 462 of file buffile.c.

References BufFileDumpBuffer(), and BufFile::dirty.

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

463 {
464  if (file->dirty)
465  {
466  BufFileDumpBuffer(file);
467  if (file->dirty)
468  return EOF;
469  }
470 
471  return 0;
472 }
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:278
bool dirty
Definition: buffile.c:73
static void BufFileLoadBuffer ( BufFile file)
static

Definition at line 226 of file buffile.c.

References BufFile::buffer, BufFile::curFile, BufFile::curOffset, FileRead(), BufFile::files, FileSeek(), MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, BufFile::offsets, pgBufferUsage, BufferUsage::temp_blks_read, and WAIT_EVENT_BUFFILE_READ.

Referenced by BufFileRead().

227 {
228  File thisfile;
229 
230  /*
231  * Advance to next component file if necessary and possible.
232  *
233  * This path can only be taken if there is more than one component, so it
234  * won't interfere with reading a non-temp file that is over
235  * MAX_PHYSICAL_FILESIZE.
236  */
237  if (file->curOffset >= MAX_PHYSICAL_FILESIZE &&
238  file->curFile + 1 < file->numFiles)
239  {
240  file->curFile++;
241  file->curOffset = 0L;
242  }
243 
244  /*
245  * May need to reposition physical file.
246  */
247  thisfile = file->files[file->curFile];
248  if (file->curOffset != file->offsets[file->curFile])
249  {
250  if (FileSeek(thisfile, file->curOffset, SEEK_SET) != file->curOffset)
251  return; /* seek failed, read nothing */
252  file->offsets[file->curFile] = file->curOffset;
253  }
254 
255  /*
256  * Read whatever we can get, up to a full bufferload.
257  */
258  file->nbytes = FileRead(thisfile,
259  file->buffer,
260  sizeof(file->buffer),
262  if (file->nbytes < 0)
263  file->nbytes = 0;
264  file->offsets[file->curFile] += file->nbytes;
265  /* we choose not to advance curOffset here */
266 
268 }
off_t * offsets
Definition: buffile.c:64
int nbytes
Definition: buffile.c:89
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:51
int numFiles
Definition: buffile.c:61
long temp_blks_read
Definition: instrument.h:29
File * files
Definition: buffile.c:63
off_t curOffset
Definition: buffile.c:87
int FileRead(File file, char *buffer, int amount, uint32 wait_event_info)
Definition: fd.c:1608
int curFile
Definition: buffile.c:86
char buffer[BLCKSZ]
Definition: buffile.c:90
off_t FileSeek(File file, off_t offset, int whence)
Definition: fd.c:1814
int File
Definition: fd.h:51
BufferUsage pgBufferUsage
Definition: instrument.c:20
size_t BufFileRead ( BufFile file,
void *  ptr,
size_t  size 
)

Definition at line 365 of file buffile.c.

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

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

366 {
367  size_t nread = 0;
368  size_t nthistime;
369 
370  if (file->dirty)
371  {
372  if (BufFileFlush(file) != 0)
373  return 0; /* could not flush... */
374  Assert(!file->dirty);
375  }
376 
377  while (size > 0)
378  {
379  if (file->pos >= file->nbytes)
380  {
381  /* Try to load more data into buffer. */
382  file->curOffset += file->pos;
383  file->pos = 0;
384  file->nbytes = 0;
385  BufFileLoadBuffer(file);
386  if (file->nbytes <= 0)
387  break; /* no more data available */
388  }
389 
390  nthistime = file->nbytes - file->pos;
391  if (nthistime > size)
392  nthistime = size;
393  Assert(nthistime > 0);
394 
395  memcpy(ptr, file->buffer + file->pos, nthistime);
396 
397  file->pos += nthistime;
398  ptr = (void *) ((char *) ptr + nthistime);
399  size -= nthistime;
400  nread += nthistime;
401  }
402 
403  return nread;
404 }
int nbytes
Definition: buffile.c:89
static int BufFileFlush(BufFile *file)
Definition: buffile.c:462
off_t curOffset
Definition: buffile.c:87
#define Assert(condition)
Definition: c.h:675
bool dirty
Definition: buffile.c:73
char buffer[BLCKSZ]
Definition: buffile.c:90
static void BufFileLoadBuffer(BufFile *file)
Definition: buffile.c:226
int pos
Definition: buffile.c:88
int BufFileSeek ( BufFile file,
int  fileno,
off_t  offset,
int  whence 
)

Definition at line 485 of file buffile.c.

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

486 {
487  int newFile;
488  off_t newOffset;
489 
490  switch (whence)
491  {
492  case SEEK_SET:
493  if (fileno < 0)
494  return EOF;
495  newFile = fileno;
496  newOffset = offset;
497  break;
498  case SEEK_CUR:
499 
500  /*
501  * Relative seek considers only the signed offset, ignoring
502  * fileno. Note that large offsets (> 1 gig) risk overflow in this
503  * add, unless we have 64-bit off_t.
504  */
505  newFile = file->curFile;
506  newOffset = (file->curOffset + file->pos) + offset;
507  break;
508 #ifdef NOT_USED
509  case SEEK_END:
510  /* could be implemented, not needed currently */
511  break;
512 #endif
513  default:
514  elog(ERROR, "invalid whence: %d", whence);
515  return EOF;
516  }
517  while (newOffset < 0)
518  {
519  if (--newFile < 0)
520  return EOF;
521  newOffset += MAX_PHYSICAL_FILESIZE;
522  }
523  if (newFile == file->curFile &&
524  newOffset >= file->curOffset &&
525  newOffset <= file->curOffset + file->nbytes)
526  {
527  /*
528  * Seek is to a point within existing buffer; we can just adjust
529  * pos-within-buffer, without flushing buffer. Note this is OK
530  * whether reading or writing, but buffer remains dirty if we were
531  * writing.
532  */
533  file->pos = (int) (newOffset - file->curOffset);
534  return 0;
535  }
536  /* Otherwise, must reposition buffer, so flush any dirty data */
537  if (BufFileFlush(file) != 0)
538  return EOF;
539 
540  /*
541  * At this point and no sooner, check for seek past last segment. The
542  * above flush could have created a new segment, so checking sooner would
543  * not work (at least not with this code).
544  */
545  if (file->isTemp)
546  {
547  /* convert seek to "start of next seg" to "end of last seg" */
548  if (newFile == file->numFiles && newOffset == 0)
549  {
550  newFile--;
551  newOffset = MAX_PHYSICAL_FILESIZE;
552  }
553  while (newOffset > MAX_PHYSICAL_FILESIZE)
554  {
555  if (++newFile >= file->numFiles)
556  return EOF;
557  newOffset -= MAX_PHYSICAL_FILESIZE;
558  }
559  }
560  if (newFile >= file->numFiles)
561  return EOF;
562  /* Seek is OK! */
563  file->curFile = newFile;
564  file->curOffset = newOffset;
565  file->pos = 0;
566  file->nbytes = 0;
567  return 0;
568 }
int nbytes
Definition: buffile.c:89
static int BufFileFlush(BufFile *file)
Definition: buffile.c:462
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:51
bool isTemp
Definition: buffile.c:71
int numFiles
Definition: buffile.c:61
#define ERROR
Definition: elog.h:43
off_t curOffset
Definition: buffile.c:87
int curFile
Definition: buffile.c:86
#define elog
Definition: elog.h:219
int pos
Definition: buffile.c:88
int BufFileSeekBlock ( BufFile file,
long  blknum 
)

Definition at line 589 of file buffile.c.

References BUFFILE_SEG_SIZE, and BufFileSeek().

Referenced by ltsReadBlock(), ltsWriteBlock(), ReadTempFileBlock(), and WriteTempFileBlock().

590 {
591  return BufFileSeek(file,
592  (int) (blknum / BUFFILE_SEG_SIZE),
593  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
594  SEEK_SET);
595 }
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:485
#define BUFFILE_SEG_SIZE
Definition: buffile.c:52
void BufFileTell ( BufFile file,
int *  fileno,
off_t *  offset 
)

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

572 {
573  *fileno = file->curFile;
574  *offset = file->curOffset + file->pos;
575 }
off_t curOffset
Definition: buffile.c:87
int curFile
Definition: buffile.c:86
int pos
Definition: buffile.c:88
size_t BufFileWrite ( BufFile file,
void *  ptr,
size_t  size 
)

Definition at line 412 of file buffile.c.

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

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

413 {
414  size_t nwritten = 0;
415  size_t nthistime;
416 
417  while (size > 0)
418  {
419  if (file->pos >= BLCKSZ)
420  {
421  /* Buffer full, dump it out */
422  if (file->dirty)
423  {
424  BufFileDumpBuffer(file);
425  if (file->dirty)
426  break; /* I/O error */
427  }
428  else
429  {
430  /* Hmm, went directly from reading to writing? */
431  file->curOffset += file->pos;
432  file->pos = 0;
433  file->nbytes = 0;
434  }
435  }
436 
437  nthistime = BLCKSZ - file->pos;
438  if (nthistime > size)
439  nthistime = size;
440  Assert(nthistime > 0);
441 
442  memcpy(file->buffer + file->pos, ptr, nthistime);
443 
444  file->dirty = true;
445  file->pos += nthistime;
446  if (file->nbytes < file->pos)
447  file->nbytes = file->pos;
448  ptr = (void *) ((char *) ptr + nthistime);
449  size -= nthistime;
450  nwritten += nthistime;
451  }
452 
453  return nwritten;
454 }
int nbytes
Definition: buffile.c:89
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:278
off_t curOffset
Definition: buffile.c:87
#define Assert(condition)
Definition: c.h:675
bool dirty
Definition: buffile.c:73
char buffer[BLCKSZ]
Definition: buffile.c:90
int pos
Definition: buffile.c:88
static void extendBufFile ( BufFile file)
static

Definition at line 130 of file buffile.c.

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

Referenced by BufFileDumpBuffer().

131 {
132  File pfile;
133  ResourceOwner oldowner;
134 
135  /* Be sure to associate the file with the BufFile's resource owner */
136  oldowner = CurrentResourceOwner;
138 
139  Assert(file->isTemp);
140  pfile = OpenTemporaryFile(file->isInterXact);
141  Assert(pfile >= 0);
142 
143  CurrentResourceOwner = oldowner;
144 
145  file->files = (File *) repalloc(file->files,
146  (file->numFiles + 1) * sizeof(File));
147  file->offsets = (off_t *) repalloc(file->offsets,
148  (file->numFiles + 1) * sizeof(off_t));
149  file->files[file->numFiles] = pfile;
150  file->offsets[file->numFiles] = 0L;
151  file->numFiles++;
152 }
off_t * offsets
Definition: buffile.c:64
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
bool isTemp
Definition: buffile.c:71
int numFiles
Definition: buffile.c:61
ResourceOwner resowner
Definition: buffile.c:80
bool isInterXact
Definition: buffile.c:72
File * files
Definition: buffile.c:63
#define Assert(condition)
Definition: c.h:675
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1334
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
int File
Definition: fd.h:51
static BufFile * makeBufFile ( File  firstfile)
static

Definition at line 105 of file buffile.c.

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

Referenced by BufFileCreateTemp().

106 {
107  BufFile *file = (BufFile *) palloc(sizeof(BufFile));
108 
109  file->numFiles = 1;
110  file->files = (File *) palloc(sizeof(File));
111  file->files[0] = firstfile;
112  file->offsets = (off_t *) palloc(sizeof(off_t));
113  file->offsets[0] = 0L;
114  file->isTemp = false;
115  file->isInterXact = false;
116  file->dirty = false;
118  file->curFile = 0;
119  file->curOffset = 0L;
120  file->pos = 0;
121  file->nbytes = 0;
122 
123  return file;
124 }
off_t * offsets
Definition: buffile.c:64
int nbytes
Definition: buffile.c:89
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
bool isTemp
Definition: buffile.c:71
int numFiles
Definition: buffile.c:61
ResourceOwner resowner
Definition: buffile.c:80
bool isInterXact
Definition: buffile.c:72
File * files
Definition: buffile.c:63
off_t curOffset
Definition: buffile.c:87
int curFile
Definition: buffile.c:86
bool dirty
Definition: buffile.c:73
void * palloc(Size size)
Definition: mcxt.c:849
int File
Definition: fd.h:51
int pos
Definition: buffile.c:88