1 /*-------------------------------------------------------------------------
2  *
3  * buffile.c
4  *	  Management of large buffered temporary files.
5  *
6  * Portions Copyright (c) 1996-2018, 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 	 */
89 	ResourceOwner resowner;
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 	PGAlignedBlock buffer;
100 };
101 
102 static BufFile *makeBufFileCommon(int nfiles);
103 static BufFile *makeBufFile(File firstfile);
104 static void extendBufFile(BufFile *file);
105 static void BufFileLoadBuffer(BufFile *file);
106 static void BufFileDumpBuffer(BufFile *file);
107 static void BufFileFlush(BufFile *file);
108 static File MakeNewSharedSegment(BufFile *file, int segment);
109 
110 /*
111  * Create BufFile and perform the common initialization.
112  */
113 static BufFile *
makeBufFileCommon(int nfiles)114 makeBufFileCommon(int nfiles)
115 {
116 	BufFile    *file = (BufFile *) palloc(sizeof(BufFile));
117 
118 	file->numFiles = nfiles;
119 	file->offsets = (off_t *) palloc0(sizeof(off_t) * nfiles);
120 	file->isInterXact = false;
121 	file->dirty = false;
122 	file->resowner = CurrentResourceOwner;
123 	file->curFile = 0;
124 	file->curOffset = 0L;
125 	file->pos = 0;
126 	file->nbytes = 0;
127 
128 	return file;
129 }
130 
131 /*
132  * Create a BufFile given the first underlying physical file.
133  * NOTE: caller must set isInterXact if appropriate.
134  */
135 static BufFile *
makeBufFile(File firstfile)136 makeBufFile(File firstfile)
137 {
138 	BufFile    *file = makeBufFileCommon(1);
139 
140 	file->files = (File *) palloc(sizeof(File));
141 	file->files[0] = firstfile;
142 	file->readOnly = false;
143 	file->fileset = NULL;
144 	file->name = NULL;
145 
146 	return file;
147 }
148 
149 /*
150  * Add another component temp file.
151  */
152 static void
extendBufFile(BufFile * file)153 extendBufFile(BufFile *file)
154 {
155 	File		pfile;
156 	ResourceOwner oldowner;
157 
158 	/* Be sure to associate the file with the BufFile's resource owner */
159 	oldowner = CurrentResourceOwner;
160 	CurrentResourceOwner = file->resowner;
161 
162 	if (file->fileset == NULL)
163 		pfile = OpenTemporaryFile(file->isInterXact);
164 	else
165 		pfile = MakeNewSharedSegment(file, file->numFiles);
166 
167 	Assert(pfile >= 0);
168 
169 	CurrentResourceOwner = oldowner;
170 
171 	file->files = (File *) repalloc(file->files,
172 									(file->numFiles + 1) * sizeof(File));
173 	file->offsets = (off_t *) repalloc(file->offsets,
174 									   (file->numFiles + 1) * sizeof(off_t));
175 	file->files[file->numFiles] = pfile;
176 	file->offsets[file->numFiles] = 0L;
177 	file->numFiles++;
178 }
179 
180 /*
181  * Create a BufFile for a new temporary file (which will expand to become
182  * multiple temporary files if more than MAX_PHYSICAL_FILESIZE bytes are
183  * written to it).
184  *
185  * If interXact is true, the temp file will not be automatically deleted
186  * at end of transaction.
187  *
188  * Note: if interXact is true, the caller had better be calling us in a
189  * memory context, and with a resource owner, that will survive across
190  * transaction boundaries.
191  */
192 BufFile *
BufFileCreateTemp(bool interXact)193 BufFileCreateTemp(bool interXact)
194 {
195 	BufFile    *file;
196 	File		pfile;
197 
198 	pfile = OpenTemporaryFile(interXact);
199 	Assert(pfile >= 0);
200 
201 	file = makeBufFile(pfile);
202 	file->isInterXact = interXact;
203 
204 	return file;
205 }
206 
207 /*
208  * Build the name for a given segment of a given BufFile.
209  */
210 static void
SharedSegmentName(char * name,const char * buffile_name,int segment)211 SharedSegmentName(char *name, const char *buffile_name, int segment)
212 {
213 	snprintf(name, MAXPGPATH, "%s.%d", buffile_name, segment);
214 }
215 
216 /*
217  * Create a new segment file backing a shared BufFile.
218  */
219 static File
MakeNewSharedSegment(BufFile * buffile,int segment)220 MakeNewSharedSegment(BufFile *buffile, int segment)
221 {
222 	char		name[MAXPGPATH];
223 	File		file;
224 
225 	/*
226 	 * It is possible that there are files left over from before a crash
227 	 * restart with the same name.  In order for BufFileOpenShared() not to
228 	 * get confused about how many segments there are, we'll unlink the next
229 	 * segment number if it already exists.
230 	 */
231 	SharedSegmentName(name, buffile->name, segment + 1);
232 	SharedFileSetDelete(buffile->fileset, name, true);
233 
234 	/* Create the new segment. */
235 	SharedSegmentName(name, buffile->name, segment);
236 	file = SharedFileSetCreate(buffile->fileset, name);
237 
238 	/* SharedFileSetCreate would've errored out */
239 	Assert(file > 0);
240 
241 	return file;
242 }
243 
244 /*
245  * Create a BufFile that can be discovered and opened read-only by other
246  * backends that are attached to the same SharedFileSet using the same name.
247  *
248  * The naming scheme for shared BufFiles is left up to the calling code.  The
249  * name will appear as part of one or more filenames on disk, and might
250  * provide clues to administrators about which subsystem is generating
251  * temporary file data.  Since each SharedFileSet object is backed by one or
252  * more uniquely named temporary directory, names don't conflict with
253  * unrelated SharedFileSet objects.
254  */
255 BufFile *
BufFileCreateShared(SharedFileSet * fileset,const char * name)256 BufFileCreateShared(SharedFileSet *fileset, const char *name)
257 {
258 	BufFile    *file;
259 
260 	file = makeBufFileCommon(1);
261 	file->fileset = fileset;
262 	file->name = pstrdup(name);
263 	file->files = (File *) palloc(sizeof(File));
264 	file->files[0] = MakeNewSharedSegment(file, 0);
265 	file->readOnly = false;
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  * BufFileExportShared() to make sure that it is ready to be opened by other
275  * backends and render it read-only.
276  */
277 BufFile *
BufFileOpenShared(SharedFileSet * fileset,const char * name)278 BufFileOpenShared(SharedFileSet *fileset, const char *name)
279 {
280 	BufFile    *file;
281 	char		segment_name[MAXPGPATH];
282 	Size		capacity = 16;
283 	File	   *files;
284 	int			nfiles = 0;
285 
286 	files = palloc(sizeof(File) * capacity);
287 
288 	/*
289 	 * We don't know how many segments there are, so we'll probe the
290 	 * filesystem to find out.
291 	 */
292 	for (;;)
293 	{
294 		/* See if we need to expand our file segment array. */
295 		if (nfiles + 1 > capacity)
296 		{
297 			capacity *= 2;
298 			files = repalloc(files, sizeof(File) * capacity);
299 		}
300 		/* Try to load a segment. */
301 		SharedSegmentName(segment_name, name, nfiles);
302 		files[nfiles] = SharedFileSetOpen(fileset, segment_name);
303 		if (files[nfiles] <= 0)
304 			break;
305 		++nfiles;
306 
307 		CHECK_FOR_INTERRUPTS();
308 	}
309 
310 	/*
311 	 * If we didn't find any files at all, then no BufFile exists with this
312 	 * name.
313 	 */
314 	if (nfiles == 0)
315 		ereport(ERROR,
316 				(errcode_for_file_access(),
317 				 errmsg("could not open temporary file \"%s\" from BufFile \"%s\": %m",
318 						segment_name, name)));
319 
320 	file = makeBufFileCommon(nfiles);
321 	file->files = files;
322 	file->readOnly = true;		/* Can't write to files opened this way */
323 	file->fileset = fileset;
324 	file->name = pstrdup(name);
325 
326 	return file;
327 }
328 
329 /*
330  * Delete a BufFile that was created by BufFileCreateShared in the given
331  * SharedFileSet using the given name.
332  *
333  * It is not necessary to delete files explicitly with this function.  It is
334  * provided only as a way to delete files proactively, rather than waiting for
335  * the SharedFileSet to be cleaned up.
336  *
337  * Only one backend should attempt to delete a given name, and should know
338  * that it exists and has been exported or closed.
339  */
340 void
BufFileDeleteShared(SharedFileSet * fileset,const char * name)341 BufFileDeleteShared(SharedFileSet *fileset, const char *name)
342 {
343 	char		segment_name[MAXPGPATH];
344 	int			segment = 0;
345 	bool		found = false;
346 
347 	/*
348 	 * We don't know how many segments the file has.  We'll keep deleting
349 	 * until we run out.  If we don't manage to find even an initial segment,
350 	 * raise an error.
351 	 */
352 	for (;;)
353 	{
354 		SharedSegmentName(segment_name, name, segment);
355 		if (!SharedFileSetDelete(fileset, segment_name, true))
356 			break;
357 		found = true;
358 		++segment;
359 
360 		CHECK_FOR_INTERRUPTS();
361 	}
362 
363 	if (!found)
364 		elog(ERROR, "could not delete unknown shared BufFile \"%s\"", name);
365 }
366 
367 /*
368  * BufFileExportShared --- flush and make read-only, in preparation for sharing.
369  */
370 void
BufFileExportShared(BufFile * file)371 BufFileExportShared(BufFile *file)
372 {
373 	/* Must be a file belonging to a SharedFileSet. */
374 	Assert(file->fileset != NULL);
375 
376 	/* It's probably a bug if someone calls this twice. */
377 	Assert(!file->readOnly);
378 
379 	BufFileFlush(file);
380 	file->readOnly = true;
381 }
382 
383 /*
384  * Close a BufFile
385  *
386  * Like fclose(), this also implicitly FileCloses the underlying File.
387  */
388 void
BufFileClose(BufFile * file)389 BufFileClose(BufFile *file)
390 {
391 	int			i;
392 
393 	/* flush any unwritten data */
394 	BufFileFlush(file);
395 	/* close and delete the underlying file(s) */
396 	for (i = 0; i < file->numFiles; i++)
397 		FileClose(file->files[i]);
398 	/* release the buffer space */
399 	pfree(file->files);
400 	pfree(file->offsets);
401 	pfree(file);
402 }
403 
404 /*
405  * BufFileLoadBuffer
406  *
407  * Load some data into buffer, if possible, starting from curOffset.
408  * At call, must have dirty = false, pos and nbytes = 0.
409  * On exit, nbytes is number of bytes loaded.
410  */
411 static void
BufFileLoadBuffer(BufFile * file)412 BufFileLoadBuffer(BufFile *file)
413 {
414 	File		thisfile;
415 
416 	/*
417 	 * Advance to next component file if necessary and possible.
418 	 */
419 	if (file->curOffset >= MAX_PHYSICAL_FILESIZE &&
420 		file->curFile + 1 < file->numFiles)
421 	{
422 		file->curFile++;
423 		file->curOffset = 0L;
424 	}
425 
426 	/*
427 	 * May need to reposition physical file.
428 	 */
429 	thisfile = file->files[file->curFile];
430 	if (file->curOffset != file->offsets[file->curFile])
431 	{
432 		if (FileSeek(thisfile, file->curOffset, SEEK_SET) != file->curOffset)
433 			ereport(ERROR,
434 					(errcode_for_file_access(),
435 					 errmsg("could not seek in file \"%s\": %m",
436 							FilePathName(thisfile))));
437 		file->offsets[file->curFile] = file->curOffset;
438 	}
439 
440 	/*
441 	 * Read whatever we can get, up to a full bufferload.
442 	 */
443 	file->nbytes = FileRead(thisfile,
444 							file->buffer.data,
445 							sizeof(file->buffer),
446 							WAIT_EVENT_BUFFILE_READ);
447 	if (file->nbytes < 0)
448 	{
449 		file->nbytes = 0;
450 		ereport(ERROR,
451 				(errcode_for_file_access(),
452 				 errmsg("could not read file \"%s\": %m",
453 						FilePathName(thisfile))));
454 	}
455 
456 	file->offsets[file->curFile] += file->nbytes;
457 	/* we choose not to advance curOffset here */
458 
459 	if (file->nbytes > 0)
460 		pgBufferUsage.temp_blks_read++;
461 }
462 
463 /*
464  * BufFileDumpBuffer
465  *
466  * Dump buffer contents starting at curOffset.
467  * At call, should have dirty = true, nbytes > 0.
468  * On exit, dirty is cleared if successful write, and curOffset is advanced.
469  */
470 static void
BufFileDumpBuffer(BufFile * file)471 BufFileDumpBuffer(BufFile *file)
472 {
473 	int			wpos = 0;
474 	int			bytestowrite;
475 	File		thisfile;
476 
477 	/*
478 	 * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
479 	 * crosses a component-file boundary; so we need a loop.
480 	 */
481 	while (wpos < file->nbytes)
482 	{
483 		off_t		availbytes;
484 
485 		/*
486 		 * Advance to next component file if necessary and possible.
487 		 */
488 		if (file->curOffset >= MAX_PHYSICAL_FILESIZE)
489 		{
490 			while (file->curFile + 1 >= file->numFiles)
491 				extendBufFile(file);
492 			file->curFile++;
493 			file->curOffset = 0L;
494 		}
495 
496 		/*
497 		 * Determine how much we need to write into this file.
498 		 */
499 		bytestowrite = file->nbytes - wpos;
500 		availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
501 
502 		if ((off_t) bytestowrite > availbytes)
503 			bytestowrite = (int) availbytes;
504 
505 		/*
506 		 * May need to reposition physical file.
507 		 */
508 		thisfile = file->files[file->curFile];
509 		if (file->curOffset != file->offsets[file->curFile])
510 		{
511 			if (FileSeek(thisfile, file->curOffset, SEEK_SET) != file->curOffset)
512 				ereport(ERROR,
513 						(errcode_for_file_access(),
514 						 errmsg("could not seek in file \"%s\": %m",
515 								FilePathName(thisfile))));
516 			file->offsets[file->curFile] = file->curOffset;
517 		}
518 		bytestowrite = FileWrite(thisfile,
519 								 file->buffer.data + wpos,
520 								 bytestowrite,
521 								 WAIT_EVENT_BUFFILE_WRITE);
522 		if (bytestowrite <= 0)
523 			ereport(ERROR,
524 					(errcode_for_file_access(),
525 					 errmsg("could not write to file \"%s\": %m",
526 							FilePathName(thisfile))));
527 
528 		file->offsets[file->curFile] += bytestowrite;
529 		file->curOffset += bytestowrite;
530 		wpos += bytestowrite;
531 
532 		pgBufferUsage.temp_blks_written++;
533 	}
534 	file->dirty = false;
535 
536 	/*
537 	 * At this point, curOffset has been advanced to the end of the buffer,
538 	 * ie, its original value + nbytes.  We need to make it point to the
539 	 * logical file position, ie, original value + pos, in case that is less
540 	 * (as could happen due to a small backwards seek in a dirty buffer!)
541 	 */
542 	file->curOffset -= (file->nbytes - file->pos);
543 	if (file->curOffset < 0)	/* handle possible segment crossing */
544 	{
545 		file->curFile--;
546 		Assert(file->curFile >= 0);
547 		file->curOffset += MAX_PHYSICAL_FILESIZE;
548 	}
549 
550 	/*
551 	 * Now we can set the buffer empty without changing the logical position
552 	 */
553 	file->pos = 0;
554 	file->nbytes = 0;
555 }
556 
557 /*
558  * BufFileRead
559  *
560  * Like fread() except we assume 1-byte element size and report I/O errors via
561  * ereport().
562  */
563 size_t
BufFileRead(BufFile * file,void * ptr,size_t size)564 BufFileRead(BufFile *file, void *ptr, size_t size)
565 {
566 	size_t		nread = 0;
567 	size_t		nthistime;
568 
569 	BufFileFlush(file);
570 
571 	while (size > 0)
572 	{
573 		if (file->pos >= file->nbytes)
574 		{
575 			/* Try to load more data into buffer. */
576 			file->curOffset += file->pos;
577 			file->pos = 0;
578 			file->nbytes = 0;
579 			BufFileLoadBuffer(file);
580 			if (file->nbytes <= 0)
581 				break;			/* no more data available */
582 		}
583 
584 		nthistime = file->nbytes - file->pos;
585 		if (nthistime > size)
586 			nthistime = size;
587 		Assert(nthistime > 0);
588 
589 		memcpy(ptr, file->buffer.data + file->pos, nthistime);
590 
591 		file->pos += nthistime;
592 		ptr = (void *) ((char *) ptr + nthistime);
593 		size -= nthistime;
594 		nread += nthistime;
595 	}
596 
597 	return nread;
598 }
599 
600 /*
601  * BufFileWrite
602  *
603  * Like fwrite() except we assume 1-byte element size and report errors via
604  * ereport().
605  */
606 size_t
BufFileWrite(BufFile * file,void * ptr,size_t size)607 BufFileWrite(BufFile *file, void *ptr, size_t size)
608 {
609 	size_t		nwritten = 0;
610 	size_t		nthistime;
611 
612 	Assert(!file->readOnly);
613 
614 	while (size > 0)
615 	{
616 		if (file->pos >= BLCKSZ)
617 		{
618 			/* Buffer full, dump it out */
619 			if (file->dirty)
620 				BufFileDumpBuffer(file);
621 			else
622 			{
623 				/* Hmm, went directly from reading to writing? */
624 				file->curOffset += file->pos;
625 				file->pos = 0;
626 				file->nbytes = 0;
627 			}
628 		}
629 
630 		nthistime = BLCKSZ - file->pos;
631 		if (nthistime > size)
632 			nthistime = size;
633 		Assert(nthistime > 0);
634 
635 		memcpy(file->buffer.data + file->pos, ptr, nthistime);
636 
637 		file->dirty = true;
638 		file->pos += nthistime;
639 		if (file->nbytes < file->pos)
640 			file->nbytes = file->pos;
641 		ptr = (void *) ((char *) ptr + nthistime);
642 		size -= nthistime;
643 		nwritten += nthistime;
644 	}
645 
646 	return nwritten;
647 }
648 
649 /*
650  * BufFileFlush
651  *
652  * Like fflush(), except that I/O errors are reported with ereport().
653  */
654 static void
BufFileFlush(BufFile * file)655 BufFileFlush(BufFile *file)
656 {
657 	if (file->dirty)
658 		BufFileDumpBuffer(file);
659 
660 	Assert(!file->dirty);
661 }
662 
663 /*
664  * BufFileSeek
665  *
666  * Like fseek(), except that target position needs two values in order to
667  * work when logical filesize exceeds maximum value representable by off_t.
668  * We do not support relative seeks across more than that, however.
669  * I/O errors are reported by ereport().
670  *
671  * Result is 0 if OK, EOF if not.  Logical position is not moved if an
672  * impossible seek is attempted.
673  */
674 int
BufFileSeek(BufFile * file,int fileno,off_t offset,int whence)675 BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
676 {
677 	int			newFile;
678 	off_t		newOffset;
679 
680 	switch (whence)
681 	{
682 		case SEEK_SET:
683 			if (fileno < 0)
684 				return EOF;
685 			newFile = fileno;
686 			newOffset = offset;
687 			break;
688 		case SEEK_CUR:
689 
690 			/*
691 			 * Relative seek considers only the signed offset, ignoring
692 			 * fileno. Note that large offsets (> 1 gig) risk overflow in this
693 			 * add, unless we have 64-bit off_t.
694 			 */
695 			newFile = file->curFile;
696 			newOffset = (file->curOffset + file->pos) + offset;
697 			break;
698 #ifdef NOT_USED
699 		case SEEK_END:
700 			/* could be implemented, not needed currently */
701 			break;
702 #endif
703 		default:
704 			elog(ERROR, "invalid whence: %d", whence);
705 			return EOF;
706 	}
707 	while (newOffset < 0)
708 	{
709 		if (--newFile < 0)
710 			return EOF;
711 		newOffset += MAX_PHYSICAL_FILESIZE;
712 	}
713 	if (newFile == file->curFile &&
714 		newOffset >= file->curOffset &&
715 		newOffset <= file->curOffset + file->nbytes)
716 	{
717 		/*
718 		 * Seek is to a point within existing buffer; we can just adjust
719 		 * pos-within-buffer, without flushing buffer.  Note this is OK
720 		 * whether reading or writing, but buffer remains dirty if we were
721 		 * writing.
722 		 */
723 		file->pos = (int) (newOffset - file->curOffset);
724 		return 0;
725 	}
726 	/* Otherwise, must reposition buffer, so flush any dirty data */
727 	BufFileFlush(file);
728 
729 	/*
730 	 * At this point and no sooner, check for seek past last segment. The
731 	 * above flush could have created a new segment, so checking sooner would
732 	 * not work (at least not with this code).
733 	 */
734 
735 	/* convert seek to "start of next seg" to "end of last seg" */
736 	if (newFile == file->numFiles && newOffset == 0)
737 	{
738 		newFile--;
739 		newOffset = MAX_PHYSICAL_FILESIZE;
740 	}
741 	while (newOffset > MAX_PHYSICAL_FILESIZE)
742 	{
743 		if (++newFile >= file->numFiles)
744 			return EOF;
745 		newOffset -= MAX_PHYSICAL_FILESIZE;
746 	}
747 	if (newFile >= file->numFiles)
748 		return EOF;
749 	/* Seek is OK! */
750 	file->curFile = newFile;
751 	file->curOffset = newOffset;
752 	file->pos = 0;
753 	file->nbytes = 0;
754 	return 0;
755 }
756 
757 void
BufFileTell(BufFile * file,int * fileno,off_t * offset)758 BufFileTell(BufFile *file, int *fileno, off_t *offset)
759 {
760 	*fileno = file->curFile;
761 	*offset = file->curOffset + file->pos;
762 }
763 
764 /*
765  * BufFileSeekBlock --- block-oriented seek
766  *
767  * Performs absolute seek to the start of the n'th BLCKSZ-sized block of
768  * the file.  Note that users of this interface will fail if their files
769  * exceed BLCKSZ * LONG_MAX bytes, but that is quite a lot; we don't work
770  * with tables bigger than that, either...
771  *
772  * Result is 0 if OK, EOF if not.  Logical position is not moved if an
773  * impossible seek is attempted.
774  */
775 int
BufFileSeekBlock(BufFile * file,long blknum)776 BufFileSeekBlock(BufFile *file, long blknum)
777 {
778 	return BufFileSeek(file,
779 					   (int) (blknum / BUFFILE_SEG_SIZE),
780 					   (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
781 					   SEEK_SET);
782 }
783 
784 #ifdef NOT_USED
785 /*
786  * BufFileTellBlock --- block-oriented tell
787  *
788  * Any fractional part of a block in the current seek position is ignored.
789  */
790 long
BufFileTellBlock(BufFile * file)791 BufFileTellBlock(BufFile *file)
792 {
793 	long		blknum;
794 
795 	blknum = (file->curOffset + file->pos) / BLCKSZ;
796 	blknum += file->curFile * BUFFILE_SEG_SIZE;
797 	return blknum;
798 }
799 
800 #endif
801 
802 /*
803  * Return the current shared BufFile size.
804  *
805  * Counts any holes left behind by BufFileAppend as part of the size.
806  * ereport()s on failure.
807  */
808 int64
BufFileSize(BufFile * file)809 BufFileSize(BufFile *file)
810 {
811 	int64		lastFileSize;
812 
813 	Assert(file->fileset != NULL);
814 
815 	/* Get the size of the last physical file by seeking to end. */
816 	lastFileSize = FileSeek(file->files[file->numFiles - 1], 0, SEEK_END);
817 	if (lastFileSize < 0)
818 		ereport(ERROR,
819 				(errcode_for_file_access(),
820 				 errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
821 						FilePathName(file->files[file->numFiles - 1]),
822 						file->name)));
823 	file->offsets[file->numFiles - 1] = lastFileSize;
824 
825 	return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +
826 		lastFileSize;
827 }
828 
829 /*
830  * Append the contents of source file (managed within shared fileset) to
831  * end of target file (managed within same shared fileset).
832  *
833  * Note that operation subsumes ownership of underlying resources from
834  * "source".  Caller should never call BufFileClose against source having
835  * called here first.  Resource owners for source and target must match,
836  * too.
837  *
838  * This operation works by manipulating lists of segment files, so the
839  * file content is always appended at a MAX_PHYSICAL_FILESIZE-aligned
840  * boundary, typically creating empty holes before the boundary.  These
841  * areas do not contain any interesting data, and cannot be read from by
842  * caller.
843  *
844  * Returns the block number within target where the contents of source
845  * begins.  Caller should apply this as an offset when working off block
846  * positions that are in terms of the original BufFile space.
847  */
848 long
BufFileAppend(BufFile * target,BufFile * source)849 BufFileAppend(BufFile *target, BufFile *source)
850 {
851 	long		startBlock = target->numFiles * BUFFILE_SEG_SIZE;
852 	int			newNumFiles = target->numFiles + source->numFiles;
853 	int			i;
854 
855 	Assert(target->fileset != NULL);
856 	Assert(source->readOnly);
857 	Assert(!source->dirty);
858 	Assert(source->fileset != NULL);
859 
860 	if (target->resowner != source->resowner)
861 		elog(ERROR, "could not append BufFile with non-matching resource owner");
862 
863 	target->files = (File *)
864 		repalloc(target->files, sizeof(File) * newNumFiles);
865 	target->offsets = (off_t *)
866 		repalloc(target->offsets, sizeof(off_t) * newNumFiles);
867 	for (i = target->numFiles; i < newNumFiles; i++)
868 	{
869 		target->files[i] = source->files[i - target->numFiles];
870 		target->offsets[i] = source->offsets[i - target->numFiles];
871 	}
872 	target->numFiles = newNumFiles;
873 
874 	return startBlock;
875 }
876