PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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

Definition at line 33 of file buffile.h.

Function Documentation

◆ BufFileAppend()

int64 BufFileAppend ( BufFile target,
BufFile source 
)
extern

Definition at line 902 of file buffile.c.

903{
905 int newNumFiles = target->numFiles + source->numFiles;
906 int i;
907
908 Assert(source->readOnly);
909 Assert(!source->dirty);
910
911 if (target->resowner != source->resowner)
912 elog(ERROR, "could not append BufFile with non-matching resource owner");
913
914 target->files = (File *)
915 repalloc(target->files, sizeof(File) * newNumFiles);
916 for (i = target->numFiles; i < newNumFiles; i++)
917 target->files[i] = source->files[i - target->numFiles];
918 target->numFiles = newNumFiles;
919
920 return startBlock;
921}
#define BUFFILE_SEG_SIZE
Definition buffile.c:64
#define Assert(condition)
Definition c.h:945
int64_t int64
Definition c.h:615
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
int File
Definition fd.h:51
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
static int fb(int x)
int numFiles
Definition buffile.c:73
File * files
Definition buffile.c:75
ResourceOwner resowner
Definition buffile.c:89

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

Referenced by LogicalTapeImport().

◆ BufFileClose()

void BufFileClose ( BufFile file)
extern

Definition at line 413 of file buffile.c.

414{
415 int i;
416
417 /* flush any unwritten data */
418 BufFileFlush(file);
419 /* close and delete the underlying file(s) */
420 for (i = 0; i < file->numFiles; i++)
421 FileClose(file->files[i]);
422 /* release the buffer space */
423 pfree(file->files);
424 pfree(file);
425}
static void BufFileFlush(BufFile *file)
Definition buffile.c:721
void FileClose(File file)
Definition fd.c:1966
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 
)
extern

Definition at line 268 of file buffile.c.

269{
270 BufFile *file;
271
272 file = makeBufFileCommon(1);
273 file->fileset = fileset;
274 file->name = pstrdup(name);
275 file->files = palloc_object(File);
276 file->files[0] = MakeNewFileSetSegment(file, 0);
277 file->readOnly = false;
278
279 return file;
280}
static BufFile * makeBufFileCommon(int nfiles)
Definition buffile.c:119
static File MakeNewFileSetSegment(BufFile *buffile, int segment)
Definition buffile.c:232
#define palloc_object(type)
Definition fe_memutils.h:74
char * pstrdup(const char *in)
Definition mcxt.c:1781
FileSet * fileset
Definition buffile.c:81
bool readOnly
Definition buffile.c:79
const char * name
Definition buffile.c:82
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)
extern

Definition at line 194 of file buffile.c.

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

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 
)
extern

Definition at line 365 of file buffile.c.

366{
368 int segment = 0;
369 bool found = false;
370
371 /*
372 * We don't know how many segments the file has. We'll keep deleting
373 * until we run out. If we don't manage to find even an initial segment,
374 * raise an error.
375 */
376 for (;;)
377 {
379 if (!FileSetDelete(fileset, segment_name, true))
380 break;
381 found = true;
382 ++segment;
383
385 }
386
387 if (!found && !missing_ok)
388 elog(ERROR, "could not delete unknown BufFile \"%s\"", name);
389}
static void FileSetSegmentName(char *name, const char *buffile_name, int segment)
Definition buffile.c:223
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, fb(), FileSetDelete(), FileSetSegmentName(), MAXPGPATH, and name.

Referenced by stream_cleanup_files(), and subxact_info_write().

◆ BufFileExportFileSet()

void BufFileExportFileSet ( BufFile file)
extern

Definition at line 395 of file buffile.c.

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

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

Referenced by LogicalTapeFreeze().

◆ BufFileOpenFileSet()

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

Definition at line 292 of file buffile.c.

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

References CHECK_FOR_INTERRUPTS, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), 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 
)
extern

Definition at line 646 of file buffile.c.

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

References BufFileReadCommon().

◆ BufFileReadExact()

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

◆ BufFileReadMaybeEOF()

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

Definition at line 665 of file buffile.c.

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

References BufFileReadCommon(), and fb().

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

◆ BufFileSeek()

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

Definition at line 741 of file buffile.c.

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

References BufFileFlush(), BufFile::curFile, BufFile::curOffset, elog, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), 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 
)
extern

Definition at line 851 of file buffile.c.

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

References BUFFILE_SEG_SIZE, BufFileSeek(), and fb().

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

◆ BufFileSize()

int64 BufFileSize ( BufFile file)
extern

Definition at line 866 of file buffile.c.

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

References ereport, errcode_for_file_access(), errmsg, ERROR, fb(), 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 
)
extern

◆ BufFileTruncateFileSet()

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

Definition at line 928 of file buffile.c.

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

References BufFile::curFile, BufFile::curOffset, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), 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 
)
extern

Definition at line 677 of file buffile.c.

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

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