PostgreSQL Source Code git master
buffile.h File Reference
#include "storage/fileset.h"
Include dependency graph for buffile.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct BufFile BufFile
 

Functions

BufFileBufFileCreateTemp (bool interXact)
 
void BufFileClose (BufFile *file)
 
pg_nodiscard size_t BufFileRead (BufFile *file, void *ptr, size_t size)
 
void BufFileReadExact (BufFile *file, void *ptr, size_t size)
 
size_t BufFileReadMaybeEOF (BufFile *file, void *ptr, size_t size, bool eofOK)
 
void BufFileWrite (BufFile *file, const void *ptr, size_t size)
 
int BufFileSeek (BufFile *file, int fileno, pgoff_t offset, int whence)
 
void BufFileTell (BufFile *file, int *fileno, pgoff_t *offset)
 
int BufFileSeekBlock (BufFile *file, int64 blknum)
 
int64 BufFileSize (BufFile *file)
 
int64 BufFileAppend (BufFile *target, BufFile *source)
 
BufFileBufFileCreateFileSet (FileSet *fileset, const char *name)
 
void BufFileExportFileSet (BufFile *file)
 
BufFileBufFileOpenFileSet (FileSet *fileset, const char *name, int mode, bool missing_ok)
 
void BufFileDeleteFileSet (FileSet *fileset, const char *name, bool missing_ok)
 
void BufFileTruncateFileSet (BufFile *file, int fileno, pgoff_t offset)
 

Typedef Documentation

◆ BufFile

typedef struct BufFile BufFile

Definition at line 33 of file buffile.h.

Function Documentation

◆ BufFileAppend()

int64 BufFileAppend ( BufFile target,
BufFile source 
)

Definition at line 901 of file buffile.c.

902{
903 int64 startBlock = (int64) target->numFiles * BUFFILE_SEG_SIZE;
904 int newNumFiles = target->numFiles + source->numFiles;
905 int i;
906
907 Assert(source->readOnly);
908 Assert(!source->dirty);
909
910 if (target->resowner != source->resowner)
911 elog(ERROR, "could not append BufFile with non-matching resource owner");
912
913 target->files = (File *)
914 repalloc(target->files, sizeof(File) * newNumFiles);
915 for (i = target->numFiles; i < newNumFiles; i++)
916 target->files[i] = source->files[i - target->numFiles];
917 target->numFiles = newNumFiles;
918
919 return startBlock;
920}
#define BUFFILE_SEG_SIZE
Definition: buffile.c:63
int64_t int64
Definition: c.h:549
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
int File
Definition: fd.h:51
Assert(PointerIsAligned(start, uint64))
int i
Definition: isn.c:77
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1632
static rewind_source * source
Definition: pg_rewind.c:89
int numFiles
Definition: buffile.c:72
File * files
Definition: buffile.c:74
ResourceOwner resowner
Definition: buffile.c:88

References Assert(), BUFFILE_SEG_SIZE, elog, ERROR, BufFile::files, i, BufFile::numFiles, repalloc(), BufFile::resowner, and source.

Referenced by LogicalTapeImport().

◆ BufFileClose()

void BufFileClose ( BufFile file)

Definition at line 412 of file buffile.c.

413{
414 int i;
415
416 /* flush any unwritten data */
417 BufFileFlush(file);
418 /* close and delete the underlying file(s) */
419 for (i = 0; i < file->numFiles; i++)
420 FileClose(file->files[i]);
421 /* release the buffer space */
422 pfree(file->files);
423 pfree(file);
424}
static void BufFileFlush(BufFile *file)
Definition: buffile.c:720
void FileClose(File file)
Definition: fd.c:1962
void pfree(void *pointer)
Definition: mcxt.c:1616

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

Referenced by ensure_last_message(), ExecHashJoinNewBatch(), ExecHashTableDestroy(), gistFreeBuildBuffers(), LogicalTapeSetClose(), SendBackupManifest(), stream_abort_internal(), stream_close_file(), sts_end_parallel_scan(), sts_end_write(), sts_parallel_scan_next(), subxact_info_read(), subxact_info_write(), tuplestore_clear(), and tuplestore_end().

◆ BufFileCreateFileSet()

BufFile * BufFileCreateFileSet ( FileSet fileset,
const char *  name 
)

Definition at line 267 of file buffile.c.

268{
269 BufFile *file;
270
271 file = makeBufFileCommon(1);
272 file->fileset = fileset;
273 file->name = pstrdup(name);
274 file->files = palloc_object(File);
275 file->files[0] = MakeNewFileSetSegment(file, 0);
276 file->readOnly = false;
277
278 return file;
279}
static BufFile * makeBufFileCommon(int nfiles)
Definition: buffile.c:118
static File MakeNewFileSetSegment(BufFile *buffile, int segment)
Definition: buffile.c:231
#define palloc_object(type)
Definition: fe_memutils.h:74
char * pstrdup(const char *in)
Definition: mcxt.c:1781
FileSet * fileset
Definition: buffile.c:80
bool readOnly
Definition: buffile.c:78
const char * name
Definition: buffile.c:81
const char * name

References BufFile::files, BufFile::fileset, makeBufFileCommon(), MakeNewFileSetSegment(), BufFile::name, name, palloc_object, pstrdup(), and BufFile::readOnly.

Referenced by LogicalTapeSetCreate(), stream_open_file(), sts_puttuple(), and subxact_info_write().

◆ BufFileCreateTemp()

BufFile * BufFileCreateTemp ( bool  interXact)

Definition at line 193 of file buffile.c.

194{
195 BufFile *file;
196 File pfile;
197
198 /*
199 * Ensure that temp tablespaces are set up for OpenTemporaryFile to use.
200 * Possibly the caller will have done this already, but it seems useful to
201 * double-check here. Failure to do this at all would result in the temp
202 * files always getting placed in the default tablespace, which is a
203 * pretty hard-to-detect bug. Callers may prefer to do it earlier if they
204 * want to be sure that any required catalog access is done in some other
205 * resource context.
206 */
208
209 pfile = OpenTemporaryFile(interXact);
210 Assert(pfile >= 0);
211
212 file = makeBufFile(pfile);
213 file->isInterXact = interXact;
214
215 return file;
216}
void PrepareTempTablespaces(void)
Definition: tablespace.c:1331
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:139
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1708
bool isInterXact
Definition: buffile.c:76

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

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

◆ BufFileDeleteFileSet()

void BufFileDeleteFileSet ( FileSet fileset,
const char *  name,
bool  missing_ok 
)

Definition at line 364 of file buffile.c.

365{
366 char segment_name[MAXPGPATH];
367 int segment = 0;
368 bool found = false;
369
370 /*
371 * We don't know how many segments the file has. We'll keep deleting
372 * until we run out. If we don't manage to find even an initial segment,
373 * raise an error.
374 */
375 for (;;)
376 {
377 FileSetSegmentName(segment_name, name, segment);
378 if (!FileSetDelete(fileset, segment_name, true))
379 break;
380 found = true;
381 ++segment;
382
384 }
385
386 if (!found && !missing_ok)
387 elog(ERROR, "could not delete unknown BufFile \"%s\"", name);
388}
static void FileSetSegmentName(char *name, const char *buffile_name, int segment)
Definition: buffile.c:222
bool FileSetDelete(FileSet *fileset, const char *name, bool error_on_failure)
Definition: fileset.c:137
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
#define MAXPGPATH

References CHECK_FOR_INTERRUPTS, elog, ERROR, FileSetDelete(), FileSetSegmentName(), MAXPGPATH, and name.

Referenced by stream_cleanup_files(), and subxact_info_write().

◆ BufFileExportFileSet()

void BufFileExportFileSet ( BufFile file)

Definition at line 394 of file buffile.c.

395{
396 /* Must be a file belonging to a FileSet. */
397 Assert(file->fileset != NULL);
398
399 /* It's probably a bug if someone calls this twice. */
400 Assert(!file->readOnly);
401
402 BufFileFlush(file);
403 file->readOnly = true;
404}

References Assert(), BufFileFlush(), BufFile::fileset, and BufFile::readOnly.

Referenced by LogicalTapeFreeze().

◆ BufFileOpenFileSet()

BufFile * BufFileOpenFileSet ( FileSet fileset,
const char *  name,
int  mode,
bool  missing_ok 
)

Definition at line 291 of file buffile.c.

293{
294 BufFile *file;
295 char segment_name[MAXPGPATH];
296 Size capacity = 16;
297 File *files;
298 int nfiles = 0;
299
300 files = palloc_array(File, capacity);
301
302 /*
303 * We don't know how many segments there are, so we'll probe the
304 * filesystem to find out.
305 */
306 for (;;)
307 {
308 /* See if we need to expand our file segment array. */
309 if (nfiles + 1 > capacity)
310 {
311 capacity *= 2;
312 files = repalloc_array(files, File, capacity);
313 }
314 /* Try to load a segment. */
315 FileSetSegmentName(segment_name, name, nfiles);
316 files[nfiles] = FileSetOpen(fileset, segment_name, mode);
317 if (files[nfiles] <= 0)
318 break;
319 ++nfiles;
320
322 }
323
324 /*
325 * If we didn't find any files at all, then no BufFile exists with this
326 * name.
327 */
328 if (nfiles == 0)
329 {
330 /* free the memory */
331 pfree(files);
332
333 if (missing_ok)
334 return NULL;
335
338 errmsg("could not open temporary file \"%s\" from BufFile \"%s\": %m",
339 segment_name, name)));
340 }
341
342 file = makeBufFileCommon(nfiles);
343 file->files = files;
344 file->readOnly = (mode == O_RDONLY);
345 file->fileset = fileset;
346 file->name = pstrdup(name);
347
348 return file;
349}
size_t Size
Definition: c.h:625
int errcode_for_file_access(void)
Definition: elog.c:886
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ereport(elevel,...)
Definition: elog.h:150
#define repalloc_array(pointer, type, count)
Definition: fe_memutils.h:78
#define palloc_array(type, count)
Definition: fe_memutils.h:76
File FileSetOpen(FileSet *fileset, const char *name, int mode)
Definition: fileset.c:120
static PgChecksumMode mode
Definition: pg_checksums.c:56

References CHECK_FOR_INTERRUPTS, ereport, errcode_for_file_access(), errmsg(), ERROR, BufFile::files, BufFile::fileset, FileSetOpen(), FileSetSegmentName(), makeBufFileCommon(), MAXPGPATH, mode, BufFile::name, name, palloc_array, pfree(), pstrdup(), BufFile::readOnly, and repalloc_array.

Referenced by apply_spooled_messages(), ensure_last_message(), LogicalTapeImport(), stream_abort_internal(), stream_open_file(), sts_parallel_scan_next(), subxact_info_read(), and subxact_info_write().

◆ BufFileRead()

pg_nodiscard size_t BufFileRead ( BufFile file,
void *  ptr,
size_t  size 
)

Definition at line 645 of file buffile.c.

646{
647 return BufFileReadCommon(file, ptr, size, false, false);
648}
static size_t BufFileReadCommon(BufFile *file, void *ptr, size_t size, bool exact, bool eofOK)
Definition: buffile.c:593

References BufFileReadCommon().

◆ BufFileReadExact()

void BufFileReadExact ( BufFile file,
void *  ptr,
size_t  size 
)

◆ BufFileReadMaybeEOF()

size_t BufFileReadMaybeEOF ( BufFile file,
void *  ptr,
size_t  size,
bool  eofOK 
)

Definition at line 664 of file buffile.c.

665{
666 return BufFileReadCommon(file, ptr, size, true, eofOK);
667}

References BufFileReadCommon().

Referenced by apply_spooled_messages(), ExecHashJoinGetSavedTuple(), and getlen().

◆ BufFileSeek()

int BufFileSeek ( BufFile file,
int  fileno,
pgoff_t  offset,
int  whence 
)

Definition at line 740 of file buffile.c.

741{
742 int newFile;
743 pgoff_t newOffset;
744
745 switch (whence)
746 {
747 case SEEK_SET:
748 if (fileno < 0)
749 return EOF;
750 newFile = fileno;
751 newOffset = offset;
752 break;
753 case SEEK_CUR:
754
755 /*
756 * Relative seek considers only the signed offset, ignoring
757 * fileno.
758 */
759 newFile = file->curFile;
760 newOffset = (file->curOffset + file->pos) + offset;
761 break;
762 case SEEK_END:
763
764 /*
765 * The file size of the last file gives us the end offset of that
766 * file.
767 */
768 newFile = file->numFiles - 1;
769 newOffset = FileSize(file->files[file->numFiles - 1]);
770 if (newOffset < 0)
773 errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
774 FilePathName(file->files[file->numFiles - 1]),
775 file->name)));
776 break;
777 default:
778 elog(ERROR, "invalid whence: %d", whence);
779 return EOF;
780 }
781 while (newOffset < 0)
782 {
783 if (--newFile < 0)
784 return EOF;
785 newOffset += MAX_PHYSICAL_FILESIZE;
786 }
787 if (newFile == file->curFile &&
788 newOffset >= file->curOffset &&
789 newOffset <= file->curOffset + file->nbytes)
790 {
791 /*
792 * Seek is to a point within existing buffer; we can just adjust
793 * pos-within-buffer, without flushing buffer. Note this is OK
794 * whether reading or writing, but buffer remains dirty if we were
795 * writing.
796 */
797 file->pos = (int64) (newOffset - file->curOffset);
798 return 0;
799 }
800 /* Otherwise, must reposition buffer, so flush any dirty data */
801 BufFileFlush(file);
802
803 /*
804 * At this point and no sooner, check for seek past last segment. The
805 * above flush could have created a new segment, so checking sooner would
806 * not work (at least not with this code).
807 */
808
809 /* convert seek to "start of next seg" to "end of last seg" */
810 if (newFile == file->numFiles && newOffset == 0)
811 {
812 newFile--;
813 newOffset = MAX_PHYSICAL_FILESIZE;
814 }
815 while (newOffset > MAX_PHYSICAL_FILESIZE)
816 {
817 if (++newFile >= file->numFiles)
818 return EOF;
819 newOffset -= MAX_PHYSICAL_FILESIZE;
820 }
821 if (newFile >= file->numFiles)
822 return EOF;
823 /* Seek is OK! */
824 file->curFile = newFile;
825 file->curOffset = newOffset;
826 file->pos = 0;
827 file->nbytes = 0;
828 return 0;
829}
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:62
char * FilePathName(File file)
Definition: fd.c:2496
pgoff_t FileSize(File file)
Definition: fd.c:2444
off_t pgoff_t
Definition: port.h:421
int64 nbytes
Definition: buffile.c:97
pgoff_t curOffset
Definition: buffile.c:95
int curFile
Definition: buffile.c:94
int64 pos
Definition: buffile.c:96

References BufFileFlush(), BufFile::curFile, BufFile::curOffset, elog, ereport, errcode_for_file_access(), errmsg(), ERROR, FilePathName(), BufFile::files, FileSize(), MAX_PHYSICAL_FILESIZE, BufFile::name, BufFile::nbytes, BufFile::numFiles, and BufFile::pos.

Referenced by BufFileSeekBlock(), ensure_last_message(), ExecHashJoinNewBatch(), SendBackupManifest(), stream_open_file(), tuplestore_copy_read_pointer(), tuplestore_gettuple(), tuplestore_puttuple_common(), tuplestore_rescan(), and tuplestore_select_read_pointer().

◆ BufFileSeekBlock()

int BufFileSeekBlock ( BufFile file,
int64  blknum 
)

Definition at line 850 of file buffile.c.

851{
852 return BufFileSeek(file,
853 (int) (blknum / BUFFILE_SEG_SIZE),
854 (pgoff_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
855 SEEK_SET);
856}
int BufFileSeek(BufFile *file, int fileno, pgoff_t offset, int whence)
Definition: buffile.c:740

References BUFFILE_SEG_SIZE, and BufFileSeek().

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

◆ BufFileSize()

int64 BufFileSize ( BufFile file)

Definition at line 865 of file buffile.c.

866{
867 int64 lastFileSize;
868
869 /* Get the size of the last physical file. */
870 lastFileSize = FileSize(file->files[file->numFiles - 1]);
871 if (lastFileSize < 0)
874 errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
875 FilePathName(file->files[file->numFiles - 1]),
876 file->name)));
877
878 return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +
879 lastFileSize;
880}

References ereport, errcode_for_file_access(), errmsg(), ERROR, FilePathName(), BufFile::files, FileSize(), MAX_PHYSICAL_FILESIZE, BufFile::name, and BufFile::numFiles.

Referenced by LogicalTapeImport(), and tuplestore_updatemax().

◆ BufFileTell()

void BufFileTell ( BufFile file,
int *  fileno,
pgoff_t offset 
)

◆ BufFileTruncateFileSet()

void BufFileTruncateFileSet ( BufFile file,
int  fileno,
pgoff_t  offset 
)

Definition at line 927 of file buffile.c.

928{
929 int numFiles = file->numFiles;
930 int newFile = fileno;
931 pgoff_t newOffset = file->curOffset;
932 char segment_name[MAXPGPATH];
933 int i;
934
935 /*
936 * Loop over all the files up to the given fileno and remove the files
937 * that are greater than the fileno and truncate the given file up to the
938 * offset. Note that we also remove the given fileno if the offset is 0
939 * provided it is not the first file in which we truncate it.
940 */
941 for (i = file->numFiles - 1; i >= fileno; i--)
942 {
943 if ((i != fileno || offset == 0) && i != 0)
944 {
945 FileSetSegmentName(segment_name, file->name, i);
946 FileClose(file->files[i]);
947 if (!FileSetDelete(file->fileset, segment_name, true))
950 errmsg("could not delete fileset \"%s\": %m",
951 segment_name)));
952 numFiles--;
953 newOffset = MAX_PHYSICAL_FILESIZE;
954
955 /*
956 * This is required to indicate that we have deleted the given
957 * fileno.
958 */
959 if (i == fileno)
960 newFile--;
961 }
962 else
963 {
964 if (FileTruncate(file->files[i], offset,
965 WAIT_EVENT_BUFFILE_TRUNCATE) < 0)
968 errmsg("could not truncate file \"%s\": %m",
969 FilePathName(file->files[i]))));
970 newOffset = offset;
971 }
972 }
973
974 file->numFiles = numFiles;
975
976 /*
977 * If the truncate point is within existing buffer then we can just adjust
978 * pos within buffer.
979 */
980 if (newFile == file->curFile &&
981 newOffset >= file->curOffset &&
982 newOffset <= file->curOffset + file->nbytes)
983 {
984 /* No need to reset the current pos if the new pos is greater. */
985 if (newOffset <= file->curOffset + file->pos)
986 file->pos = (int64) newOffset - file->curOffset;
987
988 /* Adjust the nbytes for the current buffer. */
989 file->nbytes = (int64) newOffset - file->curOffset;
990 }
991 else if (newFile == file->curFile &&
992 newOffset < file->curOffset)
993 {
994 /*
995 * The truncate point is within the existing file but prior to the
996 * current position, so we can forget the current buffer and reset the
997 * current position.
998 */
999 file->curOffset = newOffset;
1000 file->pos = 0;
1001 file->nbytes = 0;
1002 }
1003 else if (newFile < file->curFile)
1004 {
1005 /*
1006 * The truncate point is prior to the current file, so need to reset
1007 * the current position accordingly.
1008 */
1009 file->curFile = newFile;
1010 file->curOffset = newOffset;
1011 file->pos = 0;
1012 file->nbytes = 0;
1013 }
1014 /* Nothing to do, if the truncate point is beyond current file. */
1015}
int FileTruncate(File file, pgoff_t offset, uint32 wait_event_info)
Definition: fd.c:2461

References BufFile::curFile, BufFile::curOffset, ereport, errcode_for_file_access(), errmsg(), ERROR, FileClose(), FilePathName(), BufFile::files, BufFile::fileset, FileSetDelete(), FileSetSegmentName(), FileTruncate(), i, MAX_PHYSICAL_FILESIZE, MAXPGPATH, BufFile::name, BufFile::nbytes, BufFile::numFiles, and BufFile::pos.

Referenced by stream_abort_internal().

◆ BufFileWrite()

void BufFileWrite ( BufFile file,
const void *  ptr,
size_t  size 
)

Definition at line 676 of file buffile.c.

677{
678 size_t nthistime;
679
680 Assert(!file->readOnly);
681
682 while (size > 0)
683 {
684 if (file->pos >= BLCKSZ)
685 {
686 /* Buffer full, dump it out */
687 if (file->dirty)
688 BufFileDumpBuffer(file);
689 else
690 {
691 /* Hmm, went directly from reading to writing? */
692 file->curOffset += file->pos;
693 file->pos = 0;
694 file->nbytes = 0;
695 }
696 }
697
698 nthistime = BLCKSZ - file->pos;
699 if (nthistime > size)
700 nthistime = size;
701 Assert(nthistime > 0);
702
703 memcpy(file->buffer.data + file->pos, ptr, nthistime);
704
705 file->dirty = true;
706 file->pos += nthistime;
707 if (file->nbytes < file->pos)
708 file->nbytes = file->pos;
709 ptr = (const char *) ptr + nthistime;
710 size -= nthistime;
711 }
712}
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:494
PGAlignedBlock buffer
Definition: buffile.c:103
bool dirty
Definition: buffile.c:77
char data[BLCKSZ]
Definition: c.h:1116

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

Referenced by AppendStringToManifest(), ExecHashJoinSaveTuple(), ltsWriteBlock(), stream_write_change(), sts_flush_chunk(), subxact_info_write(), WriteTempFileBlock(), and writetup_heap().