PostgreSQL Source Code  git master
buffile.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * buffile.c
4  * Management of large buffered temporary files.
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  * src/backend/storage/file/buffile.c
11  *
12  * NOTES:
13  *
14  * BufFiles provide a very incomplete emulation of stdio atop virtual Files
15  * (as managed by fd.c). Currently, we only support the buffered-I/O
16  * aspect of stdio: a read or write of the low-level File occurs only
17  * when the buffer is filled or emptied. This is an even bigger win
18  * for virtual Files than for ordinary kernel files, since reducing the
19  * frequency with which a virtual File is touched reduces "thrashing"
20  * of opening/closing file descriptors.
21  *
22  * Note that BufFile structs are allocated with palloc(), and therefore
23  * will go away automatically at query/transaction end. Since the underlying
24  * virtual Files are made with OpenTemporaryFile, all resources for
25  * the file are certain to be cleaned up even if processing is aborted
26  * by ereport(ERROR). The data structures required are made in the
27  * palloc context that was current when the BufFile was created, and
28  * any external resources such as temp files are owned by the ResourceOwner
29  * that was current at that time.
30  *
31  * BufFile also supports temporary files that exceed the OS file size limit
32  * (by opening multiple fd.c temporary files). This is an essential feature
33  * for sorts and hashjoins on large amounts of data.
34  *
35  * BufFile supports temporary files that can be made read-only and shared with
36  * other backends, as infrastructure for parallel execution. Such files need
37  * to be created as a member of a SharedFileSet that all participants are
38  * attached to.
39  *-------------------------------------------------------------------------
40  */
41 
42 #include "postgres.h"
43 
44 #include "executor/instrument.h"
45 #include "miscadmin.h"
46 #include "pgstat.h"
47 #include "storage/fd.h"
48 #include "storage/buffile.h"
49 #include "storage/buf_internals.h"
50 #include "utils/resowner.h"
51 
52 /*
53  * We break BufFiles into gigabyte-sized segments, regardless of RELSEG_SIZE.
54  * The reason is that we'd like large BufFiles to be spread across multiple
55  * tablespaces when available.
56  */
57 #define MAX_PHYSICAL_FILESIZE 0x40000000
58 #define BUFFILE_SEG_SIZE (MAX_PHYSICAL_FILESIZE / BLCKSZ)
59 
60 /*
61  * This data structure represents a buffered file that consists of one or
62  * more physical files (each accessed through a virtual file descriptor
63  * managed by fd.c).
64  */
65 struct BufFile
66 {
67  int numFiles; /* number of physical files in set */
68  /* all files except the last have length exactly MAX_PHYSICAL_FILESIZE */
69  File *files; /* palloc'd array with numFiles entries */
70  off_t *offsets; /* palloc'd array with numFiles entries */
71 
72  /*
73  * offsets[i] is the current seek position of files[i]. We use this to
74  * avoid making redundant FileSeek calls.
75  */
76 
77  bool isInterXact; /* keep open over transactions? */
78  bool dirty; /* does buffer need to be written? */
79  bool readOnly; /* has the file been set to read only? */
80 
81  SharedFileSet *fileset; /* space for segment files if shared */
82  const char *name; /* name of this BufFile if shared */
83 
84  /*
85  * resowner is the ResourceOwner to use for underlying temp files. (We
86  * don't need to remember the memory context we're using explicitly,
87  * because after creation we only repalloc our arrays larger.)
88  */
90 
91  /*
92  * "current pos" is position of start of buffer within the logical file.
93  * Position as seen by user of BufFile is (curFile, curOffset + pos).
94  */
95  int curFile; /* file index (0..n) part of current pos */
96  off_t curOffset; /* offset part of current pos */
97  int pos; /* next read/write position in buffer */
98  int nbytes; /* total # of valid bytes in buffer */
99  char buffer[BLCKSZ];
100 };
101 
102 static BufFile *makeBufFile(File firstfile);
103 static void extendBufFile(BufFile *file);
104 static void BufFileLoadBuffer(BufFile *file);
105 static void BufFileDumpBuffer(BufFile *file);
106 static int BufFileFlush(BufFile *file);
107 static File MakeNewSharedSegment(BufFile *file, int segment);
108 
109 
110 /*
111  * Create a BufFile given the first underlying physical file.
112  * NOTE: caller must set isInterXact if appropriate.
113  */
114 static BufFile *
115 makeBufFile(File firstfile)
116 {
117  BufFile *file = (BufFile *) palloc(sizeof(BufFile));
118 
119  file->numFiles = 1;
120  file->files = (File *) palloc(sizeof(File));
121  file->files[0] = firstfile;
122  file->offsets = (off_t *) palloc(sizeof(off_t));
123  file->offsets[0] = 0L;
124  file->isInterXact = false;
125  file->dirty = false;
127  file->curFile = 0;
128  file->curOffset = 0L;
129  file->pos = 0;
130  file->nbytes = 0;
131  file->readOnly = false;
132  file->fileset = NULL;
133  file->name = NULL;
134 
135  return file;
136 }
137 
138 /*
139  * Add another component temp file.
140  */
141 static void
143 {
144  File pfile;
145  ResourceOwner oldowner;
146 
147  /* Be sure to associate the file with the BufFile's resource owner */
148  oldowner = CurrentResourceOwner;
150 
151  if (file->fileset == NULL)
152  pfile = OpenTemporaryFile(file->isInterXact);
153  else
154  pfile = MakeNewSharedSegment(file, file->numFiles);
155 
156  Assert(pfile >= 0);
157 
158  CurrentResourceOwner = oldowner;
159 
160  file->files = (File *) repalloc(file->files,
161  (file->numFiles + 1) * sizeof(File));
162  file->offsets = (off_t *) repalloc(file->offsets,
163  (file->numFiles + 1) * sizeof(off_t));
164  file->files[file->numFiles] = pfile;
165  file->offsets[file->numFiles] = 0L;
166  file->numFiles++;
167 }
168 
169 /*
170  * Create a BufFile for a new temporary file (which will expand to become
171  * multiple temporary files if more than MAX_PHYSICAL_FILESIZE bytes are
172  * written to it).
173  *
174  * If interXact is true, the temp file will not be automatically deleted
175  * at end of transaction.
176  *
177  * Note: if interXact is true, the caller had better be calling us in a
178  * memory context, and with a resource owner, that will survive across
179  * transaction boundaries.
180  */
181 BufFile *
182 BufFileCreateTemp(bool interXact)
183 {
184  BufFile *file;
185  File pfile;
186 
187  pfile = OpenTemporaryFile(interXact);
188  Assert(pfile >= 0);
189 
190  file = makeBufFile(pfile);
191  file->isInterXact = interXact;
192 
193  return file;
194 }
195 
196 /*
197  * Build the name for a given segment of a given BufFile.
198  */
199 static void
200 SharedSegmentName(char *name, const char *buffile_name, int segment)
201 {
202  snprintf(name, MAXPGPATH, "%s.%d", buffile_name, segment);
203 }
204 
205 /*
206  * Create a new segment file backing a shared BufFile.
207  */
208 static File
209 MakeNewSharedSegment(BufFile *buffile, int segment)
210 {
211  char name[MAXPGPATH];
212  File file;
213 
214  /*
215  * It is possible that there are files left over from before a crash
216  * restart with the same name. In order for BufFileOpenShared()
217  * not to get confused about how many segments there are, we'll unlink
218  * the next segment number if it already exists.
219  */
220  SharedSegmentName(name, buffile->name, segment + 1);
221  SharedFileSetDelete(buffile->fileset, name, true);
222 
223  /* Create the new segment. */
224  SharedSegmentName(name, buffile->name, segment);
225  file = SharedFileSetCreate(buffile->fileset, name);
226 
227  /* SharedFileSetCreate would've errored out */
228  Assert(file > 0);
229 
230  return file;
231 }
232 
233 /*
234  * Create a BufFile that can be discovered and opened read-only by other
235  * backends that are attached to the same SharedFileSet using the same name.
236  *
237  * The naming scheme for shared BufFiles is left up to the calling code. The
238  * name will appear as part of one or more filenames on disk, and might
239  * provide clues to administrators about which subsystem is generating
240  * temporary file data. Since each SharedFileSet object is backed by one or
241  * more uniquely named temporary directory, names don't conflict with
242  * unrelated SharedFileSet objects.
243  */
244 BufFile *
246 {
247  BufFile *file;
248 
249  file = (BufFile *) palloc(sizeof(BufFile));
250  file->fileset = fileset;
251  file->name = pstrdup(name);
252  file->numFiles = 1;
253  file->files = (File *) palloc(sizeof(File));
254  file->files[0] = MakeNewSharedSegment(file, 0);
255  file->offsets = (off_t *) palloc(sizeof(off_t));
256  file->offsets[0] = 0L;
257  file->isInterXact = false;
258  file->dirty = false;
260  file->curFile = 0;
261  file->curOffset = 0L;
262  file->pos = 0;
263  file->nbytes = 0;
264  file->readOnly = false;
265  file->name = pstrdup(name);
266 
267  return file;
268 }
269 
270 /*
271  * Open a file that was previously created in another backend (or this one)
272  * with BufFileCreateShared in the same SharedFileSet using the same name.
273  * The backend that created the file must have called BufFileClose() or
274  * BufFileExport() to make sure that it is ready to be opened by other
275  * backends and render it read-only.
276  */
277 BufFile *
279 {
280  BufFile *file = (BufFile *) palloc(sizeof(BufFile));
281  char segment_name[MAXPGPATH];
282  Size capacity = 16;
283  File *files = palloc(sizeof(File) * capacity);
284  int nfiles = 0;
285 
286  file = (BufFile *) palloc(sizeof(BufFile));
287  files = palloc(sizeof(File) * capacity);
288 
289  /*
290  * We don't know how many segments there are, so we'll probe the
291  * filesystem to find out.
292  */
293  for (;;)
294  {
295  /* See if we need to expand our file segment array. */
296  if (nfiles + 1 > capacity)
297  {
298  capacity *= 2;
299  files = repalloc(files, sizeof(File) * capacity);
300  }
301  /* Try to load a segment. */
302  SharedSegmentName(segment_name, name, nfiles);
303  files[nfiles] = SharedFileSetOpen(fileset, segment_name);
304  if (files[nfiles] <= 0)
305  break;
306  ++nfiles;
307 
309  }
310 
311  /*
312  * If we didn't find any files at all, then no BufFile exists with this
313  * name.
314  */
315  if (nfiles == 0)
316  ereport(ERROR,
318  errmsg("could not open BufFile \"%s\"", name)));
319 
320  file->numFiles = nfiles;
321  file->files = files;
322  file->offsets = (off_t *) palloc0(sizeof(off_t) * nfiles);
323  file->isInterXact = false;
324  file->dirty = false;
325  file->resowner = CurrentResourceOwner; /* Unused, can't extend */
326  file->curFile = 0;
327  file->curOffset = 0L;
328  file->pos = 0;
329  file->nbytes = 0;
330  file->readOnly = true; /* Can't write to files opened this way */
331  file->fileset = fileset;
332  file->name = pstrdup(name);
333 
334  return file;
335 }
336 
337 /*
338  * Delete a BufFile that was created by BufFileCreateShared in the given
339  * SharedFileSet using the given name.
340  *
341  * It is not necessary to delete files explicitly with this function. It is
342  * provided only as a way to delete files proactively, rather than waiting for
343  * the SharedFileSet to be cleaned up.
344  *
345  * Only one backend should attempt to delete a given name, and should know
346  * that it exists and has been exported or closed.
347  */
348 void
350 {
351  char segment_name[MAXPGPATH];
352  int segment = 0;
353  bool found = false;
354 
355  /*
356  * We don't know how many segments the file has. We'll keep deleting
357  * until we run out. If we don't manage to find even an initial segment,
358  * raise an error.
359  */
360  for (;;)
361  {
362  SharedSegmentName(segment_name, name, segment);
363  if (!SharedFileSetDelete(fileset, segment_name, true))
364  break;
365  found = true;
366  ++segment;
367 
369  }
370 
371  if (!found)
372  elog(ERROR, "could not delete unknown shared BufFile \"%s\"", name);
373 }
374 
375 /*
376  * BufFileExportShared --- flush and make read-only, in preparation for sharing.
377  */
378 void
380 {
381  /* Must be a file belonging to a SharedFileSet. */
382  Assert(file->fileset != NULL);
383 
384  /* It's probably a bug if someone calls this twice. */
385  Assert(!file->readOnly);
386 
387  BufFileFlush(file);
388  file->readOnly = true;
389 }
390 
391 /*
392  * Close a BufFile
393  *
394  * Like fclose(), this also implicitly FileCloses the underlying File.
395  */
396 void
398 {
399  int i;
400 
401  /* flush any unwritten data */
402  BufFileFlush(file);
403  /* close and delete the underlying file(s) */
404  for (i = 0; i < file->numFiles; i++)
405  FileClose(file->files[i]);
406  /* release the buffer space */
407  pfree(file->files);
408  pfree(file->offsets);
409  pfree(file);
410 }
411 
412 /*
413  * BufFileLoadBuffer
414  *
415  * Load some data into buffer, if possible, starting from curOffset.
416  * At call, must have dirty = false, pos and nbytes = 0.
417  * On exit, nbytes is number of bytes loaded.
418  */
419 static void
421 {
422  File thisfile;
423 
424  /*
425  * Advance to next component file if necessary and possible.
426  */
427  if (file->curOffset >= MAX_PHYSICAL_FILESIZE &&
428  file->curFile + 1 < file->numFiles)
429  {
430  file->curFile++;
431  file->curOffset = 0L;
432  }
433 
434  /*
435  * May need to reposition physical file.
436  */
437  thisfile = file->files[file->curFile];
438  if (file->curOffset != file->offsets[file->curFile])
439  {
440  if (FileSeek(thisfile, file->curOffset, SEEK_SET) != file->curOffset)
441  return; /* seek failed, read nothing */
442  file->offsets[file->curFile] = file->curOffset;
443  }
444 
445  /*
446  * Read whatever we can get, up to a full bufferload.
447  */
448  file->nbytes = FileRead(thisfile,
449  file->buffer,
450  sizeof(file->buffer),
452  if (file->nbytes < 0)
453  file->nbytes = 0;
454  file->offsets[file->curFile] += file->nbytes;
455  /* we choose not to advance curOffset here */
456 
457  if (file->nbytes > 0)
459 }
460 
461 /*
462  * BufFileDumpBuffer
463  *
464  * Dump buffer contents starting at curOffset.
465  * At call, should have dirty = true, nbytes > 0.
466  * On exit, dirty is cleared if successful write, and curOffset is advanced.
467  */
468 static void
470 {
471  int wpos = 0;
472  int bytestowrite;
473  File thisfile;
474 
475  /*
476  * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
477  * crosses a component-file boundary; so we need a loop.
478  */
479  while (wpos < file->nbytes)
480  {
481  off_t availbytes;
482 
483  /*
484  * Advance to next component file if necessary and possible.
485  */
486  if (file->curOffset >= MAX_PHYSICAL_FILESIZE)
487  {
488  while (file->curFile + 1 >= file->numFiles)
489  extendBufFile(file);
490  file->curFile++;
491  file->curOffset = 0L;
492  }
493 
494  /*
495  * Determine how much we need to write into this file.
496  */
497  bytestowrite = file->nbytes - wpos;
498  availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
499 
500  if ((off_t) bytestowrite > availbytes)
501  bytestowrite = (int) availbytes;
502 
503  /*
504  * May need to reposition physical file.
505  */
506  thisfile = file->files[file->curFile];
507  if (file->curOffset != file->offsets[file->curFile])
508  {
509  if (FileSeek(thisfile, file->curOffset, SEEK_SET) != file->curOffset)
510  return; /* seek failed, give up */
511  file->offsets[file->curFile] = file->curOffset;
512  }
513  bytestowrite = FileWrite(thisfile,
514  file->buffer + wpos,
515  bytestowrite,
517  if (bytestowrite <= 0)
518  return; /* failed to write */
519  file->offsets[file->curFile] += bytestowrite;
520  file->curOffset += bytestowrite;
521  wpos += bytestowrite;
522 
524  }
525  file->dirty = false;
526 
527  /*
528  * At this point, curOffset has been advanced to the end of the buffer,
529  * ie, its original value + nbytes. We need to make it point to the
530  * logical file position, ie, original value + pos, in case that is less
531  * (as could happen due to a small backwards seek in a dirty buffer!)
532  */
533  file->curOffset -= (file->nbytes - file->pos);
534  if (file->curOffset < 0) /* handle possible segment crossing */
535  {
536  file->curFile--;
537  Assert(file->curFile >= 0);
539  }
540 
541  /*
542  * Now we can set the buffer empty without changing the logical position
543  */
544  file->pos = 0;
545  file->nbytes = 0;
546 }
547 
548 /*
549  * BufFileRead
550  *
551  * Like fread() except we assume 1-byte element size.
552  */
553 size_t
554 BufFileRead(BufFile *file, void *ptr, size_t size)
555 {
556  size_t nread = 0;
557  size_t nthistime;
558 
559  if (file->dirty)
560  {
561  if (BufFileFlush(file) != 0)
562  return 0; /* could not flush... */
563  Assert(!file->dirty);
564  }
565 
566  while (size > 0)
567  {
568  if (file->pos >= file->nbytes)
569  {
570  /* Try to load more data into buffer. */
571  file->curOffset += file->pos;
572  file->pos = 0;
573  file->nbytes = 0;
574  BufFileLoadBuffer(file);
575  if (file->nbytes <= 0)
576  break; /* no more data available */
577  }
578 
579  nthistime = file->nbytes - file->pos;
580  if (nthistime > size)
581  nthistime = size;
582  Assert(nthistime > 0);
583 
584  memcpy(ptr, file->buffer + file->pos, nthistime);
585 
586  file->pos += nthistime;
587  ptr = (void *) ((char *) ptr + nthistime);
588  size -= nthistime;
589  nread += nthistime;
590  }
591 
592  return nread;
593 }
594 
595 /*
596  * BufFileWrite
597  *
598  * Like fwrite() except we assume 1-byte element size.
599  */
600 size_t
601 BufFileWrite(BufFile *file, void *ptr, size_t size)
602 {
603  size_t nwritten = 0;
604  size_t nthistime;
605 
606  Assert(!file->readOnly);
607 
608  while (size > 0)
609  {
610  if (file->pos >= BLCKSZ)
611  {
612  /* Buffer full, dump it out */
613  if (file->dirty)
614  {
615  BufFileDumpBuffer(file);
616  if (file->dirty)
617  break; /* I/O error */
618  }
619  else
620  {
621  /* Hmm, went directly from reading to writing? */
622  file->curOffset += file->pos;
623  file->pos = 0;
624  file->nbytes = 0;
625  }
626  }
627 
628  nthistime = BLCKSZ - file->pos;
629  if (nthistime > size)
630  nthistime = size;
631  Assert(nthistime > 0);
632 
633  memcpy(file->buffer + file->pos, ptr, nthistime);
634 
635  file->dirty = true;
636  file->pos += nthistime;
637  if (file->nbytes < file->pos)
638  file->nbytes = file->pos;
639  ptr = (void *) ((char *) ptr + nthistime);
640  size -= nthistime;
641  nwritten += nthistime;
642  }
643 
644  return nwritten;
645 }
646 
647 /*
648  * BufFileFlush
649  *
650  * Like fflush()
651  */
652 static int
654 {
655  if (file->dirty)
656  {
657  BufFileDumpBuffer(file);
658  if (file->dirty)
659  return EOF;
660  }
661 
662  return 0;
663 }
664 
665 /*
666  * BufFileSeek
667  *
668  * Like fseek(), except that target position needs two values in order to
669  * work when logical filesize exceeds maximum value representable by off_t.
670  * We do not support relative seeks across more than that, however.
671  *
672  * Result is 0 if OK, EOF if not. Logical position is not moved if an
673  * impossible seek is attempted.
674  */
675 int
676 BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
677 {
678  int newFile;
679  off_t newOffset;
680 
681  switch (whence)
682  {
683  case SEEK_SET:
684  if (fileno < 0)
685  return EOF;
686  newFile = fileno;
687  newOffset = offset;
688  break;
689  case SEEK_CUR:
690 
691  /*
692  * Relative seek considers only the signed offset, ignoring
693  * fileno. Note that large offsets (> 1 gig) risk overflow in this
694  * add, unless we have 64-bit off_t.
695  */
696  newFile = file->curFile;
697  newOffset = (file->curOffset + file->pos) + offset;
698  break;
699 #ifdef NOT_USED
700  case SEEK_END:
701  /* could be implemented, not needed currently */
702  break;
703 #endif
704  default:
705  elog(ERROR, "invalid whence: %d", whence);
706  return EOF;
707  }
708  while (newOffset < 0)
709  {
710  if (--newFile < 0)
711  return EOF;
712  newOffset += MAX_PHYSICAL_FILESIZE;
713  }
714  if (newFile == file->curFile &&
715  newOffset >= file->curOffset &&
716  newOffset <= file->curOffset + file->nbytes)
717  {
718  /*
719  * Seek is to a point within existing buffer; we can just adjust
720  * pos-within-buffer, without flushing buffer. Note this is OK
721  * whether reading or writing, but buffer remains dirty if we were
722  * writing.
723  */
724  file->pos = (int) (newOffset - file->curOffset);
725  return 0;
726  }
727  /* Otherwise, must reposition buffer, so flush any dirty data */
728  if (BufFileFlush(file) != 0)
729  return EOF;
730 
731  /*
732  * At this point and no sooner, check for seek past last segment. The
733  * above flush could have created a new segment, so checking sooner would
734  * not work (at least not with this code).
735  */
736 
737  /* convert seek to "start of next seg" to "end of last seg" */
738  if (newFile == file->numFiles && newOffset == 0)
739  {
740  newFile--;
741  newOffset = MAX_PHYSICAL_FILESIZE;
742  }
743  while (newOffset > MAX_PHYSICAL_FILESIZE)
744  {
745  if (++newFile >= file->numFiles)
746  return EOF;
747  newOffset -= MAX_PHYSICAL_FILESIZE;
748  }
749  if (newFile >= file->numFiles)
750  return EOF;
751  /* Seek is OK! */
752  file->curFile = newFile;
753  file->curOffset = newOffset;
754  file->pos = 0;
755  file->nbytes = 0;
756  return 0;
757 }
758 
759 void
760 BufFileTell(BufFile *file, int *fileno, off_t *offset)
761 {
762  *fileno = file->curFile;
763  *offset = file->curOffset + file->pos;
764 }
765 
766 /*
767  * BufFileSeekBlock --- block-oriented seek
768  *
769  * Performs absolute seek to the start of the n'th BLCKSZ-sized block of
770  * the file. Note that users of this interface will fail if their files
771  * exceed BLCKSZ * LONG_MAX bytes, but that is quite a lot; we don't work
772  * with tables bigger than that, either...
773  *
774  * Result is 0 if OK, EOF if not. Logical position is not moved if an
775  * impossible seek is attempted.
776  */
777 int
778 BufFileSeekBlock(BufFile *file, long blknum)
779 {
780  return BufFileSeek(file,
781  (int) (blknum / BUFFILE_SEG_SIZE),
782  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
783  SEEK_SET);
784 }
785 
786 #ifdef NOT_USED
787 /*
788  * BufFileTellBlock --- block-oriented tell
789  *
790  * Any fractional part of a block in the current seek position is ignored.
791  */
792 long
793 BufFileTellBlock(BufFile *file)
794 {
795  long blknum;
796 
797  blknum = (file->curOffset + file->pos) / BLCKSZ;
798  blknum += file->curFile * BUFFILE_SEG_SIZE;
799  return blknum;
800 }
801 
802 #endif
SharedFileSet * fileset
Definition: buffile.c:81
off_t * offsets
Definition: buffile.c:70
int nbytes
Definition: buffile.c:98
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:676
bool SharedFileSetDelete(SharedFileSet *fileset, const char *name, bool error_on_failure)
BufFile * BufFileOpenShared(SharedFileSet *fileset, const char *name)
Definition: buffile.c:278
static int BufFileFlush(BufFile *file)
Definition: buffile.c:653
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
char * pstrdup(const char *in)
Definition: mcxt.c:1063
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:57
bool readOnly
Definition: buffile.c:79
#define BUFFILE_SEG_SIZE
Definition: buffile.c:58
void BufFileDeleteShared(SharedFileSet *fileset, const char *name)
Definition: buffile.c:349
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
long temp_blks_written
Definition: instrument.h:30
static void extendBufFile(BufFile *file)
Definition: buffile.c:142
int numFiles
Definition: buffile.c:67
ResourceOwner resowner
Definition: buffile.c:89
void BufFileClose(BufFile *file)
Definition: buffile.c:397
void pfree(void *pointer)
Definition: mcxt.c:936
#define ERROR
Definition: elog.h:43
static void SharedSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:200
BufFile * BufFileCreateTemp(bool interXact)
Definition: buffile.c:182
#define MAXPGPATH
bool isInterXact
Definition: buffile.c:77
BufFile * BufFileCreateShared(SharedFileSet *fileset, const char *name)
Definition: buffile.c:245
int errcode_for_file_access(void)
Definition: elog.c:598
static File MakeNewSharedSegment(BufFile *file, int segment)
Definition: buffile.c:209
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:115
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:469
int FileWrite(File file, char *buffer, int amount, uint32 wait_event_info)
Definition: fd.c:1957
long temp_blks_read
Definition: instrument.h:29
File * files
Definition: buffile.c:69
#define ereport(elevel, rest)
Definition: elog.h:122
#define wpos(wep)
Definition: tsrank.c:27
off_t curOffset
Definition: buffile.c:96
int FileRead(File file, char *buffer, int amount, uint32 wait_event_info)
Definition: fd.c:1893
const char * name
Definition: buffile.c:82
void * palloc0(Size size)
Definition: mcxt.c:864
int BufFileSeekBlock(BufFile *file, long blknum)
Definition: buffile.c:778
int curFile
Definition: buffile.c:95
void BufFileTell(BufFile *file, int *fileno, off_t *offset)
Definition: buffile.c:760
void FileClose(File file)
Definition: fd.c:1748
#define Assert(condition)
Definition: c.h:680
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1499
size_t Size
Definition: c.h:414
void BufFileExportShared(BufFile *file)
Definition: buffile.c:379
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:949
bool dirty
Definition: buffile.c:78
File SharedFileSetCreate(SharedFileSet *fileset, const char *name)
char buffer[BLCKSZ]
Definition: buffile.c:99
void * palloc(Size size)
Definition: mcxt.c:835
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
size_t BufFileRead(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:554
size_t BufFileWrite(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:601
static void BufFileLoadBuffer(BufFile *file)
Definition: buffile.c:420
File SharedFileSetOpen(SharedFileSet *fileset, const char *name)
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219
off_t FileSeek(File file, off_t offset, int whence)
Definition: fd.c:2099
int File
Definition: fd.h:49
BufferUsage pgBufferUsage
Definition: instrument.c:20
int pos
Definition: buffile.c:97