PostgreSQL Source Code git master
Loading...
Searching...
No Matches
buffile.c File Reference
#include "postgres.h"
#include "commands/tablespace.h"
#include "executor/instrument.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/buffile.h"
#include "storage/bufmgr.h"
#include "storage/fd.h"
#include "utils/resowner.h"
#include "utils/wait_event.h"
Include dependency graph for buffile.c:

Go to the source code of this file.

Data Structures

struct  BufFile
 

Macros

#define MAX_PHYSICAL_FILESIZE   0x40000000
 
#define BUFFILE_SEG_SIZE   (MAX_PHYSICAL_FILESIZE / BLCKSZ)
 

Functions

static BufFilemakeBufFileCommon (int nfiles)
 
static BufFilemakeBufFile (File firstfile)
 
static void extendBufFile (BufFile *file)
 
static void BufFileLoadBuffer (BufFile *file)
 
static void BufFileDumpBuffer (BufFile *file)
 
static void BufFileFlush (BufFile *file)
 
static File MakeNewFileSetSegment (BufFile *buffile, int segment)
 
BufFileBufFileCreateTemp (bool interXact)
 
static void FileSetSegmentName (char *name, const char *buffile_name, int segment)
 
BufFileBufFileCreateFileSet (FileSet *fileset, const char *name)
 
BufFileBufFileOpenFileSet (FileSet *fileset, const char *name, int mode, bool missing_ok)
 
void BufFileDeleteFileSet (FileSet *fileset, const char *name, bool missing_ok)
 
void BufFileExportFileSet (BufFile *file)
 
void BufFileClose (BufFile *file)
 
static size_t BufFileReadCommon (BufFile *file, void *ptr, size_t size, bool exact, bool eofOK)
 
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)
 
void BufFileTruncateFileSet (BufFile *file, int fileno, pgoff_t offset)
 

Macro Definition Documentation

◆ BUFFILE_SEG_SIZE

#define BUFFILE_SEG_SIZE   (MAX_PHYSICAL_FILESIZE / BLCKSZ)

Definition at line 64 of file buffile.c.

◆ MAX_PHYSICAL_FILESIZE

#define MAX_PHYSICAL_FILESIZE   0x40000000

Definition at line 63 of file buffile.c.

Function Documentation

◆ BufFileAppend()

int64 BufFileAppend ( BufFile target,
BufFile source 
)

Definition at line 906 of file buffile.c.

907{
909 int newNumFiles = target->numFiles + source->numFiles;
910 int i;
911
912 Assert(source->readOnly);
913 Assert(!source->dirty);
914
915 if (target->resowner != source->resowner)
916 elog(ERROR, "could not append BufFile with non-matching resource owner");
917
918 target->files = (File *)
919 repalloc(target->files, sizeof(File) * newNumFiles);
920 for (i = target->numFiles; i < newNumFiles; i++)
921 target->files[i] = source->files[i - target->numFiles];
922 target->numFiles = newNumFiles;
923
924 return startBlock;
925}
#define BUFFILE_SEG_SIZE
Definition buffile.c:64
#define Assert(condition)
Definition c.h:1002
int64_t int64
Definition c.h:680
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
int File
Definition fd.h:51
int i
Definition isn.c:77
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1635
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)

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:725
void FileClose(File file)
Definition fd.c:1966
void pfree(void *pointer)
Definition mcxt.c:1619

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

Referenced by decode_concurrent_changes(), ensure_last_message(), ExecHashJoinNewBatch(), ExecHashTableDestroy(), export_initial_snapshot(), get_initial_snapshot(), gistFreeBuildBuffers(), LogicalTapeSetClose(), process_concurrent_changes(), 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 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:89
char * pstrdup(const char *in)
Definition mcxt.c:1910
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 decode_concurrent_changes(), export_initial_snapshot(), LogicalTapeSetCreate(), stream_open_file(), sts_puttuple(), and subxact_info_write().

◆ BufFileCreateTemp()

BufFile * BufFileCreateTemp ( bool  interXact)

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 
)

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:125
#define MAXPGPATH

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

Referenced by stream_cleanup_files(), and subxact_info_write().

◆ BufFileDumpBuffer()

static void BufFileDumpBuffer ( BufFile file)
static

Definition at line 498 of file buffile.c.

499{
500 int64 wpos = 0;
502
503 /*
504 * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
505 * crosses a component-file boundary; so we need a loop.
506 */
507 while (wpos < file->nbytes)
508 {
512 size_t bytestowrite;
513 ssize_t rc;
514
515 /*
516 * Advance to next component file if necessary and possible.
517 */
518 if (file->curOffset >= MAX_PHYSICAL_FILESIZE)
519 {
520 while (file->curFile + 1 >= file->numFiles)
521 extendBufFile(file);
522 file->curFile++;
523 file->curOffset = 0;
524 }
525
526 /*
527 * Determine how much we need to write into this file.
528 */
529 bytestowrite = file->nbytes - wpos;
531
534
535 thisfile = file->files[file->curFile];
536
537 if (track_io_timing)
539 else
541
542 rc = FileWrite(thisfile,
543 file->buffer.data + wpos,
545 file->curOffset,
547 if (rc <= 0)
550 errmsg("could not write to file \"%s\": %m",
552
553 if (track_io_timing)
554 {
557 }
558
559 file->curOffset += rc;
560 wpos += rc;
561
563 }
564 file->dirty = false;
565
566 /*
567 * At this point, curOffset has been advanced to the end of the buffer,
568 * ie, its original value + nbytes. We need to make it point to the
569 * logical file position, ie, original value + pos, in case that is less
570 * (as could happen due to a small backwards seek in a dirty buffer!)
571 */
572 file->curOffset -= (file->nbytes - file->pos);
573 if (file->curOffset < 0) /* handle possible segment crossing */
574 {
575 file->curFile--;
576 Assert(file->curFile >= 0);
578 }
579
580 /*
581 * Now we can set the buffer empty without changing the logical position
582 */
583 file->pos = 0;
584 file->nbytes = 0;
585}
static void extendBufFile(BufFile *file)
Definition buffile.c:157
#define MAX_PHYSICAL_FILESIZE
Definition buffile.c:63
bool track_io_timing
Definition bufmgr.c:192
int errcode_for_file_access(void)
Definition elog.c:898
#define ereport(elevel,...)
Definition elog.h:152
char * FilePathName(File file)
Definition fd.c:2500
static ssize_t FileWrite(File file, const void *buffer, size_t amount, pgoff_t offset, uint32 wait_event_info)
Definition fd.h:237
#define INSTR_TIME_SET_CURRENT(t)
Definition instr_time.h:434
#define INSTR_TIME_SET_ZERO(t)
Definition instr_time.h:429
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition instr_time.h:447
BufferUsage pgBufferUsage
Definition instrument.c:25
static char * errmsg
PGAlignedBlock buffer
Definition buffile.c:104
int64 nbytes
Definition buffile.c:98
bool dirty
Definition buffile.c:78
pgoff_t curOffset
Definition buffile.c:96
int curFile
Definition buffile.c:95
int64 pos
Definition buffile.c:97
instr_time temp_blk_write_time
Definition instrument.h:41
int64 temp_blks_written
Definition instrument.h:35
char data[BLCKSZ]
Definition c.h:1244
#define wpos(wep)
Definition tsrank.c:27

References Assert, BufFile::buffer, BufFile::curFile, BufFile::curOffset, PGAlignedBlock::data, BufFile::dirty, ereport, errcode_for_file_access(), errmsg, ERROR, extendBufFile(), fb(), FilePathName(), BufFile::files, FileWrite(), INSTR_TIME_ACCUM_DIFF, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, pgBufferUsage, BufFile::pos, BufferUsage::temp_blk_write_time, BufferUsage::temp_blks_written, track_io_timing, and wpos.

Referenced by BufFileFlush(), and BufFileWrite().

◆ BufFileExportFileSet()

void BufFileExportFileSet ( BufFile file)

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().

◆ BufFileFlush()

static void BufFileFlush ( BufFile file)
static

Definition at line 725 of file buffile.c.

726{
727 if (file->dirty)
728 BufFileDumpBuffer(file);
729
730 Assert(!file->dirty);
731}
static void BufFileDumpBuffer(BufFile *file)
Definition buffile.c:498

References Assert, BufFileDumpBuffer(), and BufFile::dirty.

Referenced by BufFileClose(), BufFileExportFileSet(), BufFileReadCommon(), and BufFileSeek().

◆ BufFileLoadBuffer()

static void BufFileLoadBuffer ( BufFile file)
static

Definition at line 435 of file buffile.c.

436{
440 ssize_t rc;
441
442 /*
443 * Advance to next component file if necessary and possible.
444 */
445 if (file->curOffset >= MAX_PHYSICAL_FILESIZE &&
446 file->curFile + 1 < file->numFiles)
447 {
448 file->curFile++;
449 file->curOffset = 0;
450 }
451
452 thisfile = file->files[file->curFile];
453
454 if (track_io_timing)
456 else
458
459 /*
460 * Read whatever we can get, up to a full bufferload.
461 */
462 rc = FileRead(thisfile,
463 file->buffer.data,
464 sizeof(file->buffer.data),
465 file->curOffset,
467 if (rc < 0)
468 {
469 file->nbytes = 0;
472 errmsg("could not read file \"%s\": %m",
474 }
475
476 file->nbytes = rc;
477
478 if (track_io_timing)
479 {
482 }
483
484 /* we choose not to advance curOffset here */
485
486 if (file->nbytes > 0)
488}
static ssize_t FileRead(File file, void *buffer, size_t amount, pgoff_t offset, uint32 wait_event_info)
Definition fd.h:225
instr_time temp_blk_read_time
Definition instrument.h:40
int64 temp_blks_read
Definition instrument.h:34

References BufFile::buffer, BufFile::curFile, BufFile::curOffset, PGAlignedBlock::data, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), FilePathName(), FileRead(), BufFile::files, INSTR_TIME_ACCUM_DIFF, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, pgBufferUsage, BufferUsage::temp_blk_read_time, BufferUsage::temp_blks_read, and track_io_timing.

Referenced by BufFileReadCommon().

◆ BufFileOpenFileSet()

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

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:748
#define repalloc_array(pointer, type, count)
Definition fe_memutils.h:94
#define palloc_array(type, count)
Definition fe_memutils.h:91
File FileSetOpen(FileSet *fileset, const char *name, int mode)
Definition fileset.c:120
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(), get_initial_snapshot(), LogicalTapeImport(), process_concurrent_changes(), stream_abort_internal(), stream_open_file(), sts_parallel_scan_next(), subxact_info_read(), and subxact_info_write().

◆ BufFileRead()

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

Definition at line 650 of file buffile.c.

651{
652 return BufFileReadCommon(file, ptr, size, false, false);
653}
static size_t BufFileReadCommon(BufFile *file, void *ptr, size_t size, bool exact, bool eofOK)
Definition buffile.c:598

References BufFileReadCommon().

◆ BufFileReadCommon()

static size_t BufFileReadCommon ( BufFile file,
void ptr,
size_t  size,
bool  exact,
bool  eofOK 
)
static

Definition at line 598 of file buffile.c.

599{
600 size_t start_size = size;
601 size_t nread = 0;
602 size_t nthistime;
603
604 BufFileFlush(file);
605
606 while (size > 0)
607 {
608 if (file->pos >= file->nbytes)
609 {
610 /* Try to load more data into buffer. */
611 file->curOffset += file->pos;
612 file->pos = 0;
613 file->nbytes = 0;
614 BufFileLoadBuffer(file);
615 if (file->nbytes <= 0)
616 break; /* no more data available */
617 }
618
619 nthistime = file->nbytes - file->pos;
620 if (nthistime > size)
621 nthistime = size;
622 Assert(nthistime > 0);
623
624 memcpy(ptr, file->buffer.data + file->pos, nthistime);
625
626 file->pos += nthistime;
627 ptr = (char *) ptr + nthistime;
628 size -= nthistime;
629 nread += nthistime;
630 }
631
632 if (exact &&
633 (nread != start_size && !(nread == 0 && eofOK)))
636 file->name ?
637 errmsg("could not read from file set \"%s\": read only %zu of %zu bytes",
638 file->name, nread, start_size) :
639 errmsg("could not read from temporary file: read only %zu of %zu bytes",
640 nread, start_size));
641
642 return nread;
643}
static void BufFileLoadBuffer(BufFile *file)
Definition buffile.c:435
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))

References Assert, BufFile::buffer, BufFileFlush(), BufFileLoadBuffer(), BufFile::curOffset, PGAlignedBlock::data, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), memcpy(), BufFile::name, BufFile::nbytes, and BufFile::pos.

Referenced by BufFileRead(), BufFileReadExact(), and BufFileReadMaybeEOF().

◆ BufFileReadExact()

◆ BufFileReadMaybeEOF()

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

Definition at line 669 of file buffile.c.

670{
671 return BufFileReadCommon(file, ptr, size, true, eofOK);
672}

References BufFileReadCommon(), and fb().

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

◆ BufFileSeek()

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

Definition at line 745 of file buffile.c.

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

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 
)

Definition at line 855 of file buffile.c.

856{
857 return BufFileSeek(file,
858 (int) (blknum / BUFFILE_SEG_SIZE),
860 SEEK_SET);
861}
int BufFileSeek(BufFile *file, int fileno, pgoff_t offset, int whence)
Definition buffile.c:745

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

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

◆ BufFileSize()

int64 BufFileSize ( BufFile file)

Definition at line 870 of file buffile.c.

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

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 
)

◆ BufFileTruncateFileSet()

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

Definition at line 932 of file buffile.c.

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

Definition at line 681 of file buffile.c.

682{
683 size_t nthistime;
684
685 Assert(!file->readOnly);
686
687 while (size > 0)
688 {
689 if (file->pos >= BLCKSZ)
690 {
691 /* Buffer full, dump it out */
692 if (file->dirty)
693 BufFileDumpBuffer(file);
694 else
695 {
696 /* Hmm, went directly from reading to writing? */
697 file->curOffset += file->pos;
698 file->pos = 0;
699 file->nbytes = 0;
700 }
701 }
702
703 nthistime = BLCKSZ - file->pos;
704 if (nthistime > size)
705 nthistime = size;
706 Assert(nthistime > 0);
707
708 memcpy(file->buffer.data + file->pos, ptr, nthistime);
709
710 file->dirty = true;
711 file->pos += nthistime;
712 if (file->nbytes < file->pos)
713 file->nbytes = file->pos;
714 ptr = (const char *) ptr + nthistime;
715 size -= nthistime;
716 }
717}

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

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

◆ extendBufFile()

static void extendBufFile ( BufFile file)
static

Definition at line 157 of file buffile.c.

158{
159 File pfile;
160 ResourceOwner oldowner;
161
162 /* Be sure to associate the file with the BufFile's resource owner */
163 oldowner = CurrentResourceOwner;
165
166 if (file->fileset == NULL)
167 pfile = OpenTemporaryFile(file->isInterXact);
168 else
169 pfile = MakeNewFileSetSegment(file, file->numFiles);
170
171 Assert(pfile >= 0);
172
173 CurrentResourceOwner = oldowner;
174
175 file->files = (File *) repalloc(file->files,
176 (file->numFiles + 1) * sizeof(File));
177 file->files[file->numFiles] = pfile;
178 file->numFiles++;
179}
ResourceOwner CurrentResourceOwner
Definition resowner.c:173

References Assert, CurrentResourceOwner, fb(), BufFile::files, BufFile::fileset, BufFile::isInterXact, MakeNewFileSetSegment(), BufFile::numFiles, OpenTemporaryFile(), repalloc(), and BufFile::resowner.

Referenced by BufFileDumpBuffer().

◆ FileSetSegmentName()

static void FileSetSegmentName ( char name,
const char buffile_name,
int  segment 
)
static

Definition at line 223 of file buffile.c.

224{
225 snprintf(name, MAXPGPATH, "%s.%d", buffile_name, segment);
226}
#define snprintf
Definition port.h:261

References fb(), MAXPGPATH, name, and snprintf.

Referenced by BufFileDeleteFileSet(), BufFileOpenFileSet(), BufFileTruncateFileSet(), and MakeNewFileSetSegment().

◆ makeBufFile()

static BufFile * makeBufFile ( File  firstfile)
static

Definition at line 140 of file buffile.c.

141{
142 BufFile *file = makeBufFileCommon(1);
143
144 file->files = palloc_object(File);
145 file->files[0] = firstfile;
146 file->readOnly = false;
147 file->fileset = NULL;
148 file->name = NULL;
149
150 return file;
151}

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

Referenced by BufFileCreateTemp().

◆ makeBufFileCommon()

static BufFile * makeBufFileCommon ( int  nfiles)
static

Definition at line 119 of file buffile.c.

120{
122
123 file->numFiles = nfiles;
124 file->isInterXact = false;
125 file->dirty = false;
127 file->curFile = 0;
128 file->curOffset = 0;
129 file->pos = 0;
130 file->nbytes = 0;
131
132 return file;
133}

References BufFile::curFile, BufFile::curOffset, CurrentResourceOwner, BufFile::dirty, fb(), BufFile::isInterXact, BufFile::nbytes, BufFile::numFiles, palloc_object, BufFile::pos, and BufFile::resowner.

Referenced by BufFileCreateFileSet(), BufFileOpenFileSet(), and makeBufFile().

◆ MakeNewFileSetSegment()

static File MakeNewFileSetSegment ( BufFile buffile,
int  segment 
)
static

Definition at line 232 of file buffile.c.

233{
234 char name[MAXPGPATH];
235 File file;
236
237 /*
238 * It is possible that there are files left over from before a crash
239 * restart with the same name. In order for BufFileOpenFileSet() not to
240 * get confused about how many segments there are, we'll unlink the next
241 * segment number if it already exists.
242 */
243 FileSetSegmentName(name, buffile->name, segment + 1);
244 FileSetDelete(buffile->fileset, name, true);
245
246 /* Create the new segment. */
247 FileSetSegmentName(name, buffile->name, segment);
248 file = FileSetCreate(buffile->fileset, name);
249
250 /* FileSetCreate would've errored out */
251 Assert(file > 0);
252
253 return file;
254}
File FileSetCreate(FileSet *fileset, const char *name)
Definition fileset.c:92

References Assert, BufFile::fileset, FileSetCreate(), FileSetDelete(), FileSetSegmentName(), MAXPGPATH, BufFile::name, and name.

Referenced by BufFileCreateFileSet(), and extendBufFile().