PostgreSQL Source Code  git master
logtape.c File Reference
#include "postgres.h"
#include "storage/buffile.h"
#include "utils/builtins.h"
#include "utils/logtape.h"
#include "utils/memdebug.h"
#include "utils/memutils.h"
Include dependency graph for logtape.c:

Go to the source code of this file.

Data Structures

struct  TapeBlockTrailer
 
struct  LogicalTape
 
struct  LogicalTapeSet
 

Macros

#define TapeBlockPayloadSize   (BLCKSZ - sizeof(TapeBlockTrailer))
 
#define TapeBlockGetTrailer(buf)   ((TapeBlockTrailer *) ((char *) buf + TapeBlockPayloadSize))
 
#define TapeBlockIsLast(buf)   (TapeBlockGetTrailer(buf)->next < 0)
 
#define TapeBlockGetNBytes(buf)
 
#define TapeBlockSetNBytes(buf, nbytes)   (TapeBlockGetTrailer(buf)->next = -(nbytes))
 

Typedefs

typedef struct TapeBlockTrailer TapeBlockTrailer
 
typedef struct LogicalTape LogicalTape
 

Functions

static void ltsWriteBlock (LogicalTapeSet *lts, long blocknum, void *buffer)
 
static void ltsReadBlock (LogicalTapeSet *lts, long blocknum, void *buffer)
 
static long ltsGetFreeBlock (LogicalTapeSet *lts)
 
static void ltsReleaseBlock (LogicalTapeSet *lts, long blocknum)
 
static void ltsConcatWorkerTapes (LogicalTapeSet *lts, TapeShare *shared, SharedFileSet *fileset)
 
static void ltsInitReadBuffer (LogicalTapeSet *lts, LogicalTape *lt)
 
static bool ltsReadFillBuffer (LogicalTapeSet *lts, LogicalTape *lt)
 
static void swap_nodes (long *heap, unsigned long a, unsigned long b)
 
static unsigned long left_offset (unsigned long i)
 
static unsigned long right_offset (unsigned i)
 
static unsigned long parent_offset (unsigned long i)
 
LogicalTapeSetLogicalTapeSetCreate (int ntapes, TapeShare *shared, SharedFileSet *fileset, int worker)
 
void LogicalTapeSetClose (LogicalTapeSet *lts)
 
void LogicalTapeSetForgetFreeSpace (LogicalTapeSet *lts)
 
void LogicalTapeWrite (LogicalTapeSet *lts, int tapenum, void *ptr, size_t size)
 
void LogicalTapeRewindForRead (LogicalTapeSet *lts, int tapenum, size_t buffer_size)
 
void LogicalTapeRewindForWrite (LogicalTapeSet *lts, int tapenum)
 
size_t LogicalTapeRead (LogicalTapeSet *lts, int tapenum, void *ptr, size_t size)
 
void LogicalTapeFreeze (LogicalTapeSet *lts, int tapenum, TapeShare *share)
 
size_t LogicalTapeBackspace (LogicalTapeSet *lts, int tapenum, size_t size)
 
void LogicalTapeSeek (LogicalTapeSet *lts, int tapenum, long blocknum, int offset)
 
void LogicalTapeTell (LogicalTapeSet *lts, int tapenum, long *blocknum, int *offset)
 
long LogicalTapeSetBlocks (LogicalTapeSet *lts)
 

Macro Definition Documentation

◆ TapeBlockGetNBytes

#define TapeBlockGetNBytes (   buf)
Value:
#define TapeBlockIsLast(buf)
Definition: logtape.c:106
#define TapeBlockPayloadSize
Definition: logtape.c:102
static char * buf
Definition: pg_test_fsync.c:67
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:103

Definition at line 107 of file logtape.c.

Referenced by LogicalTapeFreeze(), and ltsReadFillBuffer().

◆ TapeBlockGetTrailer

#define TapeBlockGetTrailer (   buf)    ((TapeBlockTrailer *) ((char *) buf + TapeBlockPayloadSize))

◆ TapeBlockIsLast

#define TapeBlockIsLast (   buf)    (TapeBlockGetTrailer(buf)->next < 0)

Definition at line 106 of file logtape.c.

Referenced by LogicalTapeFreeze(), and ltsReadFillBuffer().

◆ TapeBlockPayloadSize

#define TapeBlockPayloadSize   (BLCKSZ - sizeof(TapeBlockTrailer))

Definition at line 102 of file logtape.c.

Referenced by LogicalTapeBackspace(), LogicalTapeSeek(), and LogicalTapeWrite().

◆ TapeBlockSetNBytes

#define TapeBlockSetNBytes (   buf,
  nbytes 
)    (TapeBlockGetTrailer(buf)->next = -(nbytes))

Definition at line 110 of file logtape.c.

Referenced by LogicalTapeFreeze(), and LogicalTapeRewindForRead().

Typedef Documentation

◆ LogicalTape

typedef struct LogicalTape LogicalTape

◆ TapeBlockTrailer

Function Documentation

◆ left_offset()

static unsigned long left_offset ( unsigned long  i)
inlinestatic

Definition at line 326 of file logtape.c.

Referenced by ltsGetFreeBlock().

327 {
328  return 2 * i + 1;
329 }
int i

◆ LogicalTapeBackspace()

size_t LogicalTapeBackspace ( LogicalTapeSet lts,
int  tapenum,
size_t  size 
)

Definition at line 1021 of file logtape.c.

References Assert, LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, elog, ERROR, LogicalTape::firstBlockNumber, LogicalTape::frozen, ltsInitReadBuffer(), ltsReadBlock(), LogicalTape::nbytes, LogicalTape::nextBlockNumber, LogicalTape::pos, TapeBlockTrailer::prev, TapeBlockGetTrailer, TapeBlockPayloadSize, and LogicalTapeSet::tapes.

Referenced by tuplesort_gettuple_common().

1022 {
1023  LogicalTape *lt;
1024  size_t seekpos = 0;
1025 
1026  Assert(tapenum >= 0 && tapenum < lts->nTapes);
1027  lt = &lts->tapes[tapenum];
1028  Assert(lt->frozen);
1029  Assert(lt->buffer_size == BLCKSZ);
1030 
1031  if (lt->buffer == NULL)
1032  ltsInitReadBuffer(lts, lt);
1033 
1034  /*
1035  * Easy case for seek within current block.
1036  */
1037  if (size <= (size_t) lt->pos)
1038  {
1039  lt->pos -= (int) size;
1040  return size;
1041  }
1042 
1043  /*
1044  * Not-so-easy case, have to walk back the chain of blocks. This
1045  * implementation would be pretty inefficient for long seeks, but we
1046  * really aren't doing that (a seek over one tuple is typical).
1047  */
1048  seekpos = (size_t) lt->pos; /* part within this block */
1049  while (size > seekpos)
1050  {
1051  long prev = TapeBlockGetTrailer(lt->buffer)->prev;
1052 
1053  if (prev == -1L)
1054  {
1055  /* Tried to back up beyond the beginning of tape. */
1056  if (lt->curBlockNumber != lt->firstBlockNumber)
1057  elog(ERROR, "unexpected end of tape");
1058  lt->pos = 0;
1059  return seekpos;
1060  }
1061 
1062  ltsReadBlock(lts, prev, (void *) lt->buffer);
1063 
1064  if (TapeBlockGetTrailer(lt->buffer)->next != lt->curBlockNumber)
1065  elog(ERROR, "broken tape, next of block %ld is %ld, expected %ld",
1066  prev,
1067  TapeBlockGetTrailer(lt->buffer)->next,
1068  lt->curBlockNumber);
1069 
1071  lt->curBlockNumber = prev;
1072  lt->nextBlockNumber = TapeBlockGetTrailer(lt->buffer)->next;
1073 
1074  seekpos += TapeBlockPayloadSize;
1075  }
1076 
1077  /*
1078  * 'seekpos' can now be greater than 'size', because it points to the
1079  * beginning the target block. The difference is the position within the
1080  * page.
1081  */
1082  lt->pos = seekpos - size;
1083  return size;
1084 }
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
#define TapeBlockPayloadSize
Definition: logtape.c:102
bool frozen
Definition: logtape.c:127
long nextBlockNumber
Definition: logtape.c:143
#define ERROR
Definition: elog.h:43
int nbytes
Definition: logtape.c:153
long firstBlockNumber
Definition: logtape.c:141
static void ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:545
long curBlockNumber
Definition: logtape.c:142
#define Assert(condition)
Definition: c.h:738
char * buffer
Definition: logtape.c:149
#define elog(elevel,...)
Definition: elog.h:228
int buffer_size
Definition: logtape.c:150
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:103
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:261

◆ LogicalTapeFreeze()

void LogicalTapeFreeze ( LogicalTapeSet lts,
int  tapenum,
TapeShare share 
)

Definition at line 937 of file logtape.c.

References Assert, LogicalTape::buffer, LogicalTape::buffer_size, BufFileExportShared(), LogicalTape::curBlockNumber, LogicalTape::dirty, TapeShare::firstblocknumber, LogicalTape::firstBlockNumber, LogicalTape::frozen, ltsReadBlock(), ltsWriteBlock(), LogicalTape::nbytes, LogicalTape::nextBlockNumber, LogicalTape::offsetBlockNumber, palloc(), LogicalTapeSet::pfile, pfree(), LogicalTape::pos, TapeBlockGetNBytes, TapeBlockGetTrailer, TapeBlockIsLast, TapeBlockSetNBytes, LogicalTapeSet::tapes, VALGRIND_MAKE_MEM_DEFINED, and LogicalTape::writing.

Referenced by mergeruns(), and worker_freeze_result_tape().

938 {
939  LogicalTape *lt;
940 
941  Assert(tapenum >= 0 && tapenum < lts->nTapes);
942  lt = &lts->tapes[tapenum];
943  Assert(lt->writing);
944  Assert(lt->offsetBlockNumber == 0L);
945 
946  /*
947  * Completion of a write phase. Flush last partial data block, and rewind
948  * for nondestructive read.
949  */
950  if (lt->dirty)
951  {
952  /*
953  * As long as we've filled the buffer at least once, its contents are
954  * entirely defined from valgrind's point of view, even though
955  * contents beyond the current end point may be stale. But it's
956  * possible - at least in the case of a parallel sort - to sort such
957  * small amount of data that we do not fill the buffer even once. Tell
958  * valgrind that its contents are defined, so it doesn't bleat.
959  */
961  lt->buffer_size - lt->nbytes);
962 
963  TapeBlockSetNBytes(lt->buffer, lt->nbytes);
964  ltsWriteBlock(lts, lt->curBlockNumber, (void *) lt->buffer);
965  lt->writing = false;
966  }
967  lt->writing = false;
968  lt->frozen = true;
969 
970  /*
971  * The seek and backspace functions assume a single block read buffer.
972  * That's OK with current usage. A larger buffer is helpful to make the
973  * read pattern of the backing file look more sequential to the OS, when
974  * we're reading from multiple tapes. But at the end of a sort, when a
975  * tape is frozen, we only read from a single tape anyway.
976  */
977  if (!lt->buffer || lt->buffer_size != BLCKSZ)
978  {
979  if (lt->buffer)
980  pfree(lt->buffer);
981  lt->buffer = palloc(BLCKSZ);
982  lt->buffer_size = BLCKSZ;
983  }
984 
985  /* Read the first block, or reset if tape is empty */
987  lt->pos = 0;
988  lt->nbytes = 0;
989 
990  if (lt->firstBlockNumber == -1L)
991  lt->nextBlockNumber = -1L;
992  ltsReadBlock(lts, lt->curBlockNumber, (void *) lt->buffer);
993  if (TapeBlockIsLast(lt->buffer))
994  lt->nextBlockNumber = -1L;
995  else
996  lt->nextBlockNumber = TapeBlockGetTrailer(lt->buffer)->next;
997  lt->nbytes = TapeBlockGetNBytes(lt->buffer);
998 
999  /* Handle extra steps when caller is to share its tapeset */
1000  if (share)
1001  {
1002  BufFileExportShared(lts->pfile);
1003  share->firstblocknumber = lt->firstBlockNumber;
1004  }
1005 }
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
#define TapeBlockIsLast(buf)
Definition: logtape.c:106
long offsetBlockNumber
Definition: logtape.c:144
BufFile * pfile
Definition: logtape.c:164
long firstblocknumber
Definition: logtape.h:50
bool frozen
Definition: logtape.c:127
long nextBlockNumber
Definition: logtape.c:143
bool writing
Definition: logtape.c:126
bool dirty
Definition: logtape.c:128
void pfree(void *pointer)
Definition: mcxt.c:1056
int nbytes
Definition: logtape.c:153
long firstBlockNumber
Definition: logtape.c:141
#define TapeBlockSetNBytes(buf, nbytes)
Definition: logtape.c:110
long curBlockNumber
Definition: logtape.c:142
#define Assert(condition)
Definition: c.h:738
void BufFileExportShared(BufFile *file)
Definition: buffile.c:373
#define TapeBlockGetNBytes(buf)
Definition: logtape.c:107
char * buffer
Definition: logtape.c:149
void * palloc(Size size)
Definition: mcxt.c:949
int buffer_size
Definition: logtape.c:150
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:213
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:103
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:261

◆ LogicalTapeRead()

size_t LogicalTapeRead ( LogicalTapeSet lts,
int  tapenum,
void *  ptr,
size_t  size 
)

Definition at line 880 of file logtape.c.

References Assert, LogicalTape::buffer, ltsInitReadBuffer(), ltsReadFillBuffer(), LogicalTape::nbytes, LogicalTape::pos, LogicalTapeSet::tapes, and LogicalTape::writing.

Referenced by getlen().

882 {
883  LogicalTape *lt;
884  size_t nread = 0;
885  size_t nthistime;
886 
887  Assert(tapenum >= 0 && tapenum < lts->nTapes);
888  lt = &lts->tapes[tapenum];
889  Assert(!lt->writing);
890 
891  if (lt->buffer == NULL)
892  ltsInitReadBuffer(lts, lt);
893 
894  while (size > 0)
895  {
896  if (lt->pos >= lt->nbytes)
897  {
898  /* Try to load more data into buffer. */
899  if (!ltsReadFillBuffer(lts, lt))
900  break; /* EOF */
901  }
902 
903  nthistime = lt->nbytes - lt->pos;
904  if (nthistime > size)
905  nthistime = size;
906  Assert(nthistime > 0);
907 
908  memcpy(ptr, lt->buffer + lt->pos, nthistime);
909 
910  lt->pos += nthistime;
911  ptr = (void *) ((char *) ptr + nthistime);
912  size -= nthistime;
913  nread += nthistime;
914  }
915 
916  return nread;
917 }
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
static bool ltsReadFillBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:277
bool writing
Definition: logtape.c:126
int nbytes
Definition: logtape.c:153
static void ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:545
#define Assert(condition)
Definition: c.h:738
char * buffer
Definition: logtape.c:149

◆ LogicalTapeRewindForRead()

void LogicalTapeRewindForRead ( LogicalTapeSet lts,
int  tapenum,
size_t  buffer_size 
)

Definition at line 776 of file logtape.c.

References Assert, LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, LogicalTape::dirty, LogicalTape::frozen, ltsWriteBlock(), LogicalTape::max_size, LogicalTape::nbytes, pfree(), TapeBlockSetNBytes, LogicalTapeSet::tapes, VALGRIND_MAKE_MEM_DEFINED, and LogicalTape::writing.

Referenced by mergeruns(), and tuplesort_rescan().

777 {
778  LogicalTape *lt;
779 
780  Assert(tapenum >= 0 && tapenum < lts->nTapes);
781  lt = &lts->tapes[tapenum];
782 
783  /*
784  * Round and cap buffer_size if needed.
785  */
786  if (lt->frozen)
787  buffer_size = BLCKSZ;
788  else
789  {
790  /* need at least one block */
791  if (buffer_size < BLCKSZ)
792  buffer_size = BLCKSZ;
793 
794  /* palloc() larger than max_size is unlikely to be helpful */
795  if (buffer_size > lt->max_size)
796  buffer_size = lt->max_size;
797 
798  /* round down to BLCKSZ boundary */
799  buffer_size -= buffer_size % BLCKSZ;
800  }
801 
802  if (lt->writing)
803  {
804  /*
805  * Completion of a write phase. Flush last partial data block, and
806  * rewind for normal (destructive) read.
807  */
808  if (lt->dirty)
809  {
810  /*
811  * As long as we've filled the buffer at least once, its contents
812  * are entirely defined from valgrind's point of view, even though
813  * contents beyond the current end point may be stale. But it's
814  * possible - at least in the case of a parallel sort - to sort
815  * such small amount of data that we do not fill the buffer even
816  * once. Tell valgrind that its contents are defined, so it
817  * doesn't bleat.
818  */
820  lt->buffer_size - lt->nbytes);
821 
822  TapeBlockSetNBytes(lt->buffer, lt->nbytes);
823  ltsWriteBlock(lts, lt->curBlockNumber, (void *) lt->buffer);
824  }
825  lt->writing = false;
826  }
827  else
828  {
829  /*
830  * This is only OK if tape is frozen; we rewind for (another) read
831  * pass.
832  */
833  Assert(lt->frozen);
834  }
835 
836  /* Allocate a read buffer (unless the tape is empty) */
837  if (lt->buffer)
838  pfree(lt->buffer);
839 
840  /* the buffer is lazily allocated, but set the size here */
841  lt->buffer = NULL;
842  lt->buffer_size = buffer_size;
843 }
int max_size
Definition: logtape.c:151
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
bool frozen
Definition: logtape.c:127
bool writing
Definition: logtape.c:126
bool dirty
Definition: logtape.c:128
void pfree(void *pointer)
Definition: mcxt.c:1056
int nbytes
Definition: logtape.c:153
#define TapeBlockSetNBytes(buf, nbytes)
Definition: logtape.c:110
long curBlockNumber
Definition: logtape.c:142
#define Assert(condition)
Definition: c.h:738
char * buffer
Definition: logtape.c:149
int buffer_size
Definition: logtape.c:150
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:213

◆ LogicalTapeRewindForWrite()

void LogicalTapeRewindForWrite ( LogicalTapeSet lts,
int  tapenum 
)

Definition at line 854 of file logtape.c.

References Assert, LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, LogicalTape::dirty, LogicalTape::firstBlockNumber, LogicalTape::frozen, LogicalTape::nbytes, pfree(), LogicalTape::pos, LogicalTapeSet::tapes, and LogicalTape::writing.

Referenced by mergeruns(), and tuplesort_gettuple_common().

855 {
856  LogicalTape *lt;
857 
858  Assert(tapenum >= 0 && tapenum < lts->nTapes);
859  lt = &lts->tapes[tapenum];
860 
861  Assert(!lt->writing && !lt->frozen);
862  lt->writing = true;
863  lt->dirty = false;
864  lt->firstBlockNumber = -1L;
865  lt->curBlockNumber = -1L;
866  lt->pos = 0;
867  lt->nbytes = 0;
868  if (lt->buffer)
869  pfree(lt->buffer);
870  lt->buffer = NULL;
871  lt->buffer_size = 0;
872 }
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
bool frozen
Definition: logtape.c:127
bool writing
Definition: logtape.c:126
bool dirty
Definition: logtape.c:128
void pfree(void *pointer)
Definition: mcxt.c:1056
int nbytes
Definition: logtape.c:153
long firstBlockNumber
Definition: logtape.c:141
long curBlockNumber
Definition: logtape.c:142
#define Assert(condition)
Definition: c.h:738
char * buffer
Definition: logtape.c:149
int buffer_size
Definition: logtape.c:150

◆ LogicalTapeSeek()

void LogicalTapeSeek ( LogicalTapeSet lts,
int  tapenum,
long  blocknum,
int  offset 
)

Definition at line 1095 of file logtape.c.

References Assert, LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, elog, ERROR, LogicalTape::frozen, ltsInitReadBuffer(), ltsReadBlock(), LogicalTape::nbytes, LogicalTape::nextBlockNumber, LogicalTape::pos, TapeBlockGetTrailer, TapeBlockPayloadSize, and LogicalTapeSet::tapes.

Referenced by tuplesort_restorepos().

1097 {
1098  LogicalTape *lt;
1099 
1100  Assert(tapenum >= 0 && tapenum < lts->nTapes);
1101  lt = &lts->tapes[tapenum];
1102  Assert(lt->frozen);
1103  Assert(offset >= 0 && offset <= TapeBlockPayloadSize);
1104  Assert(lt->buffer_size == BLCKSZ);
1105 
1106  if (lt->buffer == NULL)
1107  ltsInitReadBuffer(lts, lt);
1108 
1109  if (blocknum != lt->curBlockNumber)
1110  {
1111  ltsReadBlock(lts, blocknum, (void *) lt->buffer);
1112  lt->curBlockNumber = blocknum;
1114  lt->nextBlockNumber = TapeBlockGetTrailer(lt->buffer)->next;
1115  }
1116 
1117  if (offset > lt->nbytes)
1118  elog(ERROR, "invalid tape seek position");
1119  lt->pos = offset;
1120 }
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
#define TapeBlockPayloadSize
Definition: logtape.c:102
bool frozen
Definition: logtape.c:127
long nextBlockNumber
Definition: logtape.c:143
#define ERROR
Definition: elog.h:43
int nbytes
Definition: logtape.c:153
static void ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:545
long curBlockNumber
Definition: logtape.c:142
#define Assert(condition)
Definition: c.h:738
char * buffer
Definition: logtape.c:149
#define elog(elevel,...)
Definition: elog.h:228
int buffer_size
Definition: logtape.c:150
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:103
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:261

◆ LogicalTapeSetBlocks()

long LogicalTapeSetBlocks ( LogicalTapeSet lts)

Definition at line 1153 of file logtape.c.

References LogicalTapeSet::nBlocksAllocated, and LogicalTapeSet::nHoleBlocks.

Referenced by tuplesort_end(), and tuplesort_get_stats().

1154 {
1155  return lts->nBlocksAllocated - lts->nHoleBlocks;
1156 }
long nHoleBlocks
Definition: logtape.c:178
long nBlocksAllocated
Definition: logtape.c:176

◆ LogicalTapeSetClose()

void LogicalTapeSetClose ( LogicalTapeSet lts)

Definition at line 651 of file logtape.c.

References LogicalTape::buffer, BufFileClose(), LogicalTapeSet::freeBlocks, i, LogicalTapeSet::nTapes, LogicalTapeSet::pfile, pfree(), and LogicalTapeSet::tapes.

Referenced by tuplesort_end().

652 {
653  LogicalTape *lt;
654  int i;
655 
656  BufFileClose(lts->pfile);
657  for (i = 0; i < lts->nTapes; i++)
658  {
659  lt = &lts->tapes[i];
660  if (lt->buffer)
661  pfree(lt->buffer);
662  }
663  pfree(lts->freeBlocks);
664  pfree(lts);
665 }
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
BufFile * pfile
Definition: logtape.c:164
void BufFileClose(BufFile *file)
Definition: buffile.c:391
long * freeBlocks
Definition: logtape.c:189
void pfree(void *pointer)
Definition: mcxt.c:1056
char * buffer
Definition: logtape.c:149
int i

◆ LogicalTapeSetCreate()

LogicalTapeSet* LogicalTapeSetCreate ( int  ntapes,
TapeShare shared,
SharedFileSet fileset,
int  worker 
)

Definition at line 578 of file logtape.c.

References Assert, LogicalTape::buffer, LogicalTape::buffer_size, BufFileCreateShared(), BufFileCreateTemp(), LogicalTape::curBlockNumber, LogicalTape::dirty, filename, LogicalTape::firstBlockNumber, LogicalTapeSet::forgetFreeSpace, LogicalTapeSet::freeBlocks, LogicalTapeSet::freeBlocksLen, LogicalTape::frozen, i, ltsConcatWorkerTapes(), LogicalTape::max_size, MaxAllocSize, MAXPGPATH, LogicalTapeSet::nBlocksAllocated, LogicalTapeSet::nBlocksWritten, LogicalTape::nbytes, LogicalTape::nextBlockNumber, LogicalTapeSet::nFreeBlocks, LogicalTapeSet::nHoleBlocks, LogicalTapeSet::nTapes, LogicalTape::offsetBlockNumber, offsetof, palloc(), LogicalTapeSet::pfile, pg_itoa(), LogicalTape::pos, LogicalTapeSet::tapes, and LogicalTape::writing.

Referenced by inittapes(), and leader_takeover_tapes().

580 {
581  LogicalTapeSet *lts;
582  LogicalTape *lt;
583  int i;
584 
585  /*
586  * Create top-level struct including per-tape LogicalTape structs.
587  */
588  Assert(ntapes > 0);
589  lts = (LogicalTapeSet *) palloc(offsetof(LogicalTapeSet, tapes) +
590  ntapes * sizeof(LogicalTape));
591  lts->nBlocksAllocated = 0L;
592  lts->nBlocksWritten = 0L;
593  lts->nHoleBlocks = 0L;
594  lts->forgetFreeSpace = false;
595  lts->freeBlocksLen = 32; /* reasonable initial guess */
596  lts->freeBlocks = (long *) palloc(lts->freeBlocksLen * sizeof(long));
597  lts->nFreeBlocks = 0;
598  lts->nTapes = ntapes;
599 
600  /*
601  * Initialize per-tape structs. Note we allocate the I/O buffer and the
602  * first block for a tape only when it is first actually written to. This
603  * avoids wasting memory space when tuplesort.c overestimates the number
604  * of tapes needed.
605  */
606  for (i = 0; i < ntapes; i++)
607  {
608  lt = &lts->tapes[i];
609  lt->writing = true;
610  lt->frozen = false;
611  lt->dirty = false;
612  lt->firstBlockNumber = -1L;
613  lt->curBlockNumber = -1L;
614  lt->nextBlockNumber = -1L;
615  lt->offsetBlockNumber = 0L;
616  lt->buffer = NULL;
617  lt->buffer_size = 0;
618  /* palloc() larger than MaxAllocSize would fail */
619  lt->max_size = MaxAllocSize;
620  lt->pos = 0;
621  lt->nbytes = 0;
622  }
623 
624  /*
625  * Create temp BufFile storage as required.
626  *
627  * Leader concatenates worker tapes, which requires special adjustment to
628  * final tapeset data. Things are simpler for the worker case and the
629  * serial case, though. They are generally very similar -- workers use a
630  * shared fileset, whereas serial sorts use a conventional serial BufFile.
631  */
632  if (shared)
633  ltsConcatWorkerTapes(lts, shared, fileset);
634  else if (fileset)
635  {
636  char filename[MAXPGPATH];
637 
638  pg_itoa(worker, filename);
639  lts->pfile = BufFileCreateShared(fileset, filename);
640  }
641  else
642  lts->pfile = BufFileCreateTemp(false);
643 
644  return lts;
645 }
int max_size
Definition: logtape.c:151
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
long offsetBlockNumber
Definition: logtape.c:144
long nBlocksWritten
Definition: logtape.c:177
BufFile * pfile
Definition: logtape.c:164
bool frozen
Definition: logtape.c:127
long nextBlockNumber
Definition: logtape.c:143
bool writing
Definition: logtape.c:126
long * freeBlocks
Definition: logtape.c:189
bool dirty
Definition: logtape.c:128
BufFile * BufFileCreateTemp(bool interXact)
Definition: buffile.c:184
#define MAXPGPATH
BufFile * BufFileCreateShared(SharedFileSet *fileset, const char *name)
Definition: buffile.c:258
long nHoleBlocks
Definition: logtape.c:178
int nbytes
Definition: logtape.c:153
void pg_itoa(int16 i, char *a)
Definition: numutils.c:335
#define MaxAllocSize
Definition: memutils.h:40
long firstBlockNumber
Definition: logtape.c:141
long nFreeBlocks
Definition: logtape.c:190
static void ltsConcatWorkerTapes(LogicalTapeSet *lts, TapeShare *shared, SharedFileSet *fileset)
Definition: logtape.c:459
long curBlockNumber
Definition: logtape.c:142
#define Assert(condition)
Definition: c.h:738
char * buffer
Definition: logtape.c:149
static char * filename
Definition: pg_dumpall.c:90
void * palloc(Size size)
Definition: mcxt.c:949
bool forgetFreeSpace
Definition: logtape.c:188
int i
Size freeBlocksLen
Definition: logtape.c:191
struct LogicalTape LogicalTape
long nBlocksAllocated
Definition: logtape.c:176
int buffer_size
Definition: logtape.c:150
#define offsetof(type, field)
Definition: c.h:661

◆ LogicalTapeSetForgetFreeSpace()

void LogicalTapeSetForgetFreeSpace ( LogicalTapeSet lts)

Definition at line 677 of file logtape.c.

References LogicalTapeSet::forgetFreeSpace.

Referenced by mergeruns().

678 {
679  lts->forgetFreeSpace = true;
680 }
bool forgetFreeSpace
Definition: logtape.c:188

◆ LogicalTapeTell()

void LogicalTapeTell ( LogicalTapeSet lts,
int  tapenum,
long *  blocknum,
int *  offset 
)

Definition at line 1129 of file logtape.c.

References Assert, LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, ltsInitReadBuffer(), LogicalTape::offsetBlockNumber, LogicalTape::pos, and LogicalTapeSet::tapes.

Referenced by tuplesort_markpos().

1131 {
1132  LogicalTape *lt;
1133 
1134  Assert(tapenum >= 0 && tapenum < lts->nTapes);
1135  lt = &lts->tapes[tapenum];
1136 
1137  if (lt->buffer == NULL)
1138  ltsInitReadBuffer(lts, lt);
1139 
1140  Assert(lt->offsetBlockNumber == 0L);
1141 
1142  /* With a larger buffer, 'pos' wouldn't be the same as offset within page */
1143  Assert(lt->buffer_size == BLCKSZ);
1144 
1145  *blocknum = lt->curBlockNumber;
1146  *offset = lt->pos;
1147 }
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
long offsetBlockNumber
Definition: logtape.c:144
static void ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:545
long curBlockNumber
Definition: logtape.c:142
#define Assert(condition)
Definition: c.h:738
char * buffer
Definition: logtape.c:149
int buffer_size
Definition: logtape.c:150

◆ LogicalTapeWrite()

void LogicalTapeWrite ( LogicalTapeSet lts,
int  tapenum,
void *  ptr,
size_t  size 
)

Definition at line 688 of file logtape.c.

References Assert, LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, LogicalTape::dirty, elog, ERROR, LogicalTape::firstBlockNumber, ltsGetFreeBlock(), ltsWriteBlock(), LogicalTape::nbytes, LogicalTape::offsetBlockNumber, palloc(), LogicalTape::pos, TapeBlockGetTrailer, TapeBlockPayloadSize, LogicalTapeSet::tapes, and LogicalTape::writing.

Referenced by markrunend(), writetup_cluster(), writetup_datum(), writetup_heap(), and writetup_index().

690 {
691  LogicalTape *lt;
692  size_t nthistime;
693 
694  Assert(tapenum >= 0 && tapenum < lts->nTapes);
695  lt = &lts->tapes[tapenum];
696  Assert(lt->writing);
697  Assert(lt->offsetBlockNumber == 0L);
698 
699  /* Allocate data buffer and first block on first write */
700  if (lt->buffer == NULL)
701  {
702  lt->buffer = (char *) palloc(BLCKSZ);
703  lt->buffer_size = BLCKSZ;
704  }
705  if (lt->curBlockNumber == -1)
706  {
707  Assert(lt->firstBlockNumber == -1);
708  Assert(lt->pos == 0);
709 
710  lt->curBlockNumber = ltsGetFreeBlock(lts);
712 
713  TapeBlockGetTrailer(lt->buffer)->prev = -1L;
714  }
715 
716  Assert(lt->buffer_size == BLCKSZ);
717  while (size > 0)
718  {
719  if (lt->pos >= TapeBlockPayloadSize)
720  {
721  /* Buffer full, dump it out */
722  long nextBlockNumber;
723 
724  if (!lt->dirty)
725  {
726  /* Hmm, went directly from reading to writing? */
727  elog(ERROR, "invalid logtape state: should be dirty");
728  }
729 
730  /*
731  * First allocate the next block, so that we can store it in the
732  * 'next' pointer of this block.
733  */
734  nextBlockNumber = ltsGetFreeBlock(lts);
735 
736  /* set the next-pointer and dump the current block. */
737  TapeBlockGetTrailer(lt->buffer)->next = nextBlockNumber;
738  ltsWriteBlock(lts, lt->curBlockNumber, (void *) lt->buffer);
739 
740  /* initialize the prev-pointer of the next block */
741  TapeBlockGetTrailer(lt->buffer)->prev = lt->curBlockNumber;
742  lt->curBlockNumber = nextBlockNumber;
743  lt->pos = 0;
744  lt->nbytes = 0;
745  }
746 
747  nthistime = TapeBlockPayloadSize - lt->pos;
748  if (nthistime > size)
749  nthistime = size;
750  Assert(nthistime > 0);
751 
752  memcpy(lt->buffer + lt->pos, ptr, nthistime);
753 
754  lt->dirty = true;
755  lt->pos += nthistime;
756  if (lt->nbytes < lt->pos)
757  lt->nbytes = lt->pos;
758  ptr = (void *) ((char *) ptr + nthistime);
759  size -= nthistime;
760  }
761 }
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
#define TapeBlockPayloadSize
Definition: logtape.c:102
long offsetBlockNumber
Definition: logtape.c:144
static long ltsGetFreeBlock(LogicalTapeSet *lts)
Definition: logtape.c:348
bool writing
Definition: logtape.c:126
bool dirty
Definition: logtape.c:128
#define ERROR
Definition: elog.h:43
int nbytes
Definition: logtape.c:153
long firstBlockNumber
Definition: logtape.c:141
long curBlockNumber
Definition: logtape.c:142
#define Assert(condition)
Definition: c.h:738
char * buffer
Definition: logtape.c:149
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
int buffer_size
Definition: logtape.c:150
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:213
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:103

◆ ltsConcatWorkerTapes()

static void ltsConcatWorkerTapes ( LogicalTapeSet lts,
TapeShare shared,
SharedFileSet fileset 
)
static

Definition at line 459 of file logtape.c.

References Assert, BufFileAppend(), BufFileOpenShared(), BufFileSize(), filename, TapeShare::firstblocknumber, LogicalTape::firstBlockNumber, i, LogicalTape::max_size, MaxAllocSize, MAXPGPATH, Min, LogicalTapeSet::nBlocksAllocated, LogicalTapeSet::nBlocksWritten, LogicalTapeSet::nHoleBlocks, LogicalTapeSet::nTapes, LogicalTape::offsetBlockNumber, LogicalTapeSet::pfile, pg_itoa(), and LogicalTapeSet::tapes.

Referenced by LogicalTapeSetCreate().

461 {
462  LogicalTape *lt = NULL;
463  long tapeblocks = 0L;
464  long nphysicalblocks = 0L;
465  int i;
466 
467  /* Should have at least one worker tape, plus leader's tape */
468  Assert(lts->nTapes >= 2);
469 
470  /*
471  * Build concatenated view of all BufFiles, remembering the block number
472  * where each source file begins. No changes are needed for leader/last
473  * tape.
474  */
475  for (i = 0; i < lts->nTapes - 1; i++)
476  {
477  char filename[MAXPGPATH];
478  BufFile *file;
479  int64 filesize;
480 
481  lt = &lts->tapes[i];
482 
483  pg_itoa(i, filename);
484  file = BufFileOpenShared(fileset, filename);
485  filesize = BufFileSize(file);
486 
487  /*
488  * Stash first BufFile, and concatenate subsequent BufFiles to that.
489  * Store block offset into each tape as we go.
490  */
491  lt->firstBlockNumber = shared[i].firstblocknumber;
492  if (i == 0)
493  {
494  lts->pfile = file;
495  lt->offsetBlockNumber = 0L;
496  }
497  else
498  {
499  lt->offsetBlockNumber = BufFileAppend(lts->pfile, file);
500  }
501  /* Don't allocate more for read buffer than could possibly help */
502  lt->max_size = Min(MaxAllocSize, filesize);
503  tapeblocks = filesize / BLCKSZ;
504  nphysicalblocks += tapeblocks;
505  }
506 
507  /*
508  * Set # of allocated blocks, as well as # blocks written. Use extent of
509  * new BufFile space (from 0 to end of last worker's tape space) for this.
510  * Allocated/written blocks should include space used by holes left
511  * between concatenated BufFiles.
512  */
513  lts->nBlocksAllocated = lt->offsetBlockNumber + tapeblocks;
514  lts->nBlocksWritten = lts->nBlocksAllocated;
515 
516  /*
517  * Compute number of hole blocks so that we can later work backwards, and
518  * instrument number of physical blocks. We don't simply use physical
519  * blocks directly for instrumentation because this would break if we ever
520  * subsequently wrote to the leader tape.
521  *
522  * Working backwards like this keeps our options open. If shared BufFiles
523  * ever support being written to post-export, logtape.c can automatically
524  * take advantage of that. We'd then support writing to the leader tape
525  * while recycling space from worker tapes, because the leader tape has a
526  * zero offset (write routines won't need to have extra logic to apply an
527  * offset).
528  *
529  * The only thing that currently prevents writing to the leader tape from
530  * working is the fact that BufFiles opened using BufFileOpenShared() are
531  * read-only by definition, but that could be changed if it seemed
532  * worthwhile. For now, writing to the leader tape will raise a "Bad file
533  * descriptor" error, so tuplesort must avoid writing to the leader tape
534  * altogether.
535  */
536  lts->nHoleBlocks = lts->nBlocksAllocated - nphysicalblocks;
537 }
int max_size
Definition: logtape.c:151
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]
Definition: logtape.c:195
long offsetBlockNumber
Definition: logtape.c:144
long nBlocksWritten
Definition: logtape.c:177
BufFile * BufFileOpenShared(SharedFileSet *fileset, const char *name)
Definition: buffile.c:280
int64 BufFileSize(BufFile *file)
Definition: buffile.c:785
#define Min(x, y)
Definition: c.h:920
BufFile * pfile
Definition: logtape.c:164
long firstblocknumber
Definition: logtape.h:50
#define MAXPGPATH
long nHoleBlocks
Definition: logtape.c:178
void pg_itoa(int16 i, char *a)
Definition: numutils.c:335
#define MaxAllocSize
Definition: memutils.h:40
long firstBlockNumber
Definition: logtape.c:141
#define Assert(condition)
Definition: c.h:738
static char * filename
Definition: pg_dumpall.c:90
int i
long BufFileAppend(BufFile *target, BufFile *source)
Definition: buffile.c:824
long nBlocksAllocated
Definition: logtape.c:176

◆ ltsGetFreeBlock()

static long ltsGetFreeBlock ( LogicalTapeSet lts)
static

Definition at line 348 of file logtape.c.

References LogicalTapeSet::freeBlocks, left_offset(), LogicalTapeSet::nBlocksAllocated, LogicalTapeSet::nFreeBlocks, right_offset(), and swap_nodes().

Referenced by LogicalTapeWrite().

349 {
350  long *heap = lts->freeBlocks;
351  long blocknum;
352  int heapsize;
353  unsigned long pos;
354 
355  /* freelist empty; allocate a new block */
356  if (lts->nFreeBlocks == 0)
357  return lts->nBlocksAllocated++;
358 
359  if (lts->nFreeBlocks == 1)
360  {
361  lts->nFreeBlocks--;
362  return lts->freeBlocks[0];
363  }
364 
365  /* take top of minheap */
366  blocknum = heap[0];
367 
368  /* replace with end of minheap array */
369  heap[0] = heap[--lts->nFreeBlocks];
370 
371  /* sift down */
372  pos = 0;
373  heapsize = lts->nFreeBlocks;
374  while (true)
375  {
376  unsigned long left = left_offset(pos);
377  unsigned long right = right_offset(pos);
378  unsigned long min_child;
379 
380  if (left < heapsize && right < heapsize)
381  min_child = (heap[left] < heap[right]) ? left : right;
382  else if (left < heapsize)
383  min_child = left;
384  else if (right < heapsize)
385  min_child = right;
386  else
387  break;
388 
389  if (heap[min_child] >= heap[pos])
390  break;
391 
392  swap_nodes(heap, min_child, pos);
393  pos = min_child;
394  }
395 
396  return blocknum;
397 }
static void swap_nodes(long *heap, unsigned long a, unsigned long b)
Definition: logtape.c:316
long * freeBlocks
Definition: logtape.c:189
static unsigned long right_offset(unsigned i)
Definition: logtape.c:332
static unsigned long left_offset(unsigned long i)
Definition: logtape.c:326
long nFreeBlocks
Definition: logtape.c:190
long nBlocksAllocated
Definition: logtape.c:176

◆ ltsInitReadBuffer()

static void ltsInitReadBuffer ( LogicalTapeSet lts,
LogicalTape lt 
)
static

Definition at line 545 of file logtape.c.

References Assert, LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::firstBlockNumber, ltsReadFillBuffer(), LogicalTape::nbytes, LogicalTape::nextBlockNumber, palloc(), and LogicalTape::pos.

Referenced by LogicalTapeBackspace(), LogicalTapeRead(), LogicalTapeSeek(), and LogicalTapeTell().

546 {
547  Assert(lt->buffer_size > 0);
548  lt->buffer = palloc(lt->buffer_size);
549 
550  /* Read the first block, or reset if tape is empty */
552  lt->pos = 0;
553  lt->nbytes = 0;
554  ltsReadFillBuffer(lts, lt);
555 }
static bool ltsReadFillBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:277
long nextBlockNumber
Definition: logtape.c:143
int nbytes
Definition: logtape.c:153
long firstBlockNumber
Definition: logtape.c:141
#define Assert(condition)
Definition: c.h:738
char * buffer
Definition: logtape.c:149
void * palloc(Size size)
Definition: mcxt.c:949
int buffer_size
Definition: logtape.c:150

◆ ltsReadBlock()

static void ltsReadBlock ( LogicalTapeSet lts,
long  blocknum,
void *  buffer 
)
static

Definition at line 261 of file logtape.c.

References BufFileRead(), BufFileSeekBlock(), ereport, errcode_for_file_access(), errmsg(), ERROR, and LogicalTapeSet::pfile.

Referenced by LogicalTapeBackspace(), LogicalTapeFreeze(), LogicalTapeSeek(), and ltsReadFillBuffer().

262 {
263  if (BufFileSeekBlock(lts->pfile, blocknum) != 0 ||
264  BufFileRead(lts->pfile, buffer, BLCKSZ) != BLCKSZ)
265  ereport(ERROR,
267  errmsg("could not read block %ld of temporary file: %m",
268  blocknum)));
269 }
BufFile * pfile
Definition: logtape.c:164
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
int BufFileSeekBlock(BufFile *file, long blknum)
Definition: buffile.c:752
int errmsg(const char *fmt,...)
Definition: elog.c:822
size_t BufFileRead(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:528

◆ ltsReadFillBuffer()

static bool ltsReadFillBuffer ( LogicalTapeSet lts,
LogicalTape lt 
)
static

Definition at line 277 of file logtape.c.

References LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, LogicalTape::frozen, ltsReadBlock(), ltsReleaseBlock(), LogicalTape::nbytes, LogicalTape::nextBlockNumber, LogicalTape::offsetBlockNumber, LogicalTape::pos, TapeBlockGetNBytes, TapeBlockGetTrailer, and TapeBlockIsLast.

Referenced by LogicalTapeRead(), and ltsInitReadBuffer().

278 {
279  lt->pos = 0;
280  lt->nbytes = 0;
281 
282  do
283  {
284  char *thisbuf = lt->buffer + lt->nbytes;
285  long datablocknum = lt->nextBlockNumber;
286 
287  /* Fetch next block number */
288  if (datablocknum == -1L)
289  break; /* EOF */
290  /* Apply worker offset, needed for leader tapesets */
291  datablocknum += lt->offsetBlockNumber;
292 
293  /* Read the block */
294  ltsReadBlock(lts, datablocknum, (void *) thisbuf);
295  if (!lt->frozen)
296  ltsReleaseBlock(lts, datablocknum);
298 
299  lt->nbytes += TapeBlockGetNBytes(thisbuf);
300  if (TapeBlockIsLast(thisbuf))
301  {
302  lt->nextBlockNumber = -1L;
303  /* EOF */
304  break;
305  }
306  else
307  lt->nextBlockNumber = TapeBlockGetTrailer(thisbuf)->next;
308 
309  /* Advance to next block, if we have buffer space left */
310  } while (lt->buffer_size - lt->nbytes > BLCKSZ);
311 
312  return (lt->nbytes > 0);
313 }
#define TapeBlockIsLast(buf)
Definition: logtape.c:106
long offsetBlockNumber
Definition: logtape.c:144
static void ltsReleaseBlock(LogicalTapeSet *lts, long blocknum)
Definition: logtape.c:403
bool frozen
Definition: logtape.c:127
long nextBlockNumber
Definition: logtape.c:143
int nbytes
Definition: logtape.c:153
long curBlockNumber
Definition: logtape.c:142
#define TapeBlockGetNBytes(buf)
Definition: logtape.c:107
char * buffer
Definition: logtape.c:149
int buffer_size
Definition: logtape.c:150
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:103
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:261

◆ ltsReleaseBlock()

static void ltsReleaseBlock ( LogicalTapeSet lts,
long  blocknum 
)
static

Definition at line 403 of file logtape.c.

References LogicalTapeSet::forgetFreeSpace, LogicalTapeSet::freeBlocks, LogicalTapeSet::freeBlocksLen, MaxAllocSize, LogicalTapeSet::nFreeBlocks, parent_offset(), repalloc(), and swap_nodes().

Referenced by ltsReadFillBuffer().

404 {
405  long *heap;
406  unsigned long pos;
407 
408  /*
409  * Do nothing if we're no longer interested in remembering free space.
410  */
411  if (lts->forgetFreeSpace)
412  return;
413 
414  /*
415  * Enlarge freeBlocks array if full.
416  */
417  if (lts->nFreeBlocks >= lts->freeBlocksLen)
418  {
419  /*
420  * If the freelist becomes very large, just return and leak this free
421  * block.
422  */
423  if (lts->freeBlocksLen * 2 > MaxAllocSize)
424  return;
425 
426  lts->freeBlocksLen *= 2;
427  lts->freeBlocks = (long *) repalloc(lts->freeBlocks,
428  lts->freeBlocksLen * sizeof(long));
429  }
430 
431  heap = lts->freeBlocks;
432  pos = lts->nFreeBlocks;
433 
434  /* place entry at end of minheap array */
435  heap[pos] = blocknum;
436  lts->nFreeBlocks++;
437 
438  /* sift up */
439  while (pos != 0)
440  {
441  unsigned long parent = parent_offset(pos);
442  if (heap[parent] < heap[pos])
443  break;
444 
445  swap_nodes(heap, parent, pos);
446  pos = parent;
447  }
448 }
static void swap_nodes(long *heap, unsigned long a, unsigned long b)
Definition: logtape.c:316
long * freeBlocks
Definition: logtape.c:189
#define MaxAllocSize
Definition: memutils.h:40
long nFreeBlocks
Definition: logtape.c:190
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
bool forgetFreeSpace
Definition: logtape.c:188
Size freeBlocksLen
Definition: logtape.c:191
static unsigned long parent_offset(unsigned long i)
Definition: logtape.c:338

◆ ltsWriteBlock()

static void ltsWriteBlock ( LogicalTapeSet lts,
long  blocknum,
void *  buffer 
)
static

Definition at line 213 of file logtape.c.

References BufFileSeekBlock(), BufFileWrite(), PGAlignedBlock::data, ereport, errcode_for_file_access(), errmsg(), ERROR, MemSet, LogicalTapeSet::nBlocksWritten, and LogicalTapeSet::pfile.

Referenced by LogicalTapeFreeze(), LogicalTapeRewindForRead(), and LogicalTapeWrite().

214 {
215  /*
216  * BufFile does not support "holes", so if we're about to write a block
217  * that's past the current end of file, fill the space between the current
218  * end of file and the target block with zeros.
219  *
220  * This should happen rarely, otherwise you are not writing very
221  * sequentially. In current use, this only happens when the sort ends
222  * writing a run, and switches to another tape. The last block of the
223  * previous tape isn't flushed to disk until the end of the sort, so you
224  * get one-block hole, where the last block of the previous tape will
225  * later go.
226  *
227  * Note that BufFile concatenation can leave "holes" in BufFile between
228  * worker-owned block ranges. These are tracked for reporting purposes
229  * only. We never read from nor write to these hole blocks, and so they
230  * are not considered here.
231  */
232  while (blocknum > lts->nBlocksWritten)
233  {
234  PGAlignedBlock zerobuf;
235 
236  MemSet(zerobuf.data, 0, sizeof(zerobuf));
237 
238  ltsWriteBlock(lts, lts->nBlocksWritten, zerobuf.data);
239  }
240 
241  /* Write the requested block */
242  if (BufFileSeekBlock(lts->pfile, blocknum) != 0 ||
243  BufFileWrite(lts->pfile, buffer, BLCKSZ) != BLCKSZ)
244  ereport(ERROR,
246  errmsg("could not write block %ld of temporary file: %m",
247  blocknum)));
248 
249  /* Update nBlocksWritten, if we extended the file */
250  if (blocknum == lts->nBlocksWritten)
251  lts->nBlocksWritten++;
252 }
long nBlocksWritten
Definition: logtape.c:177
BufFile * pfile
Definition: logtape.c:164
#define MemSet(start, val, len)
Definition: c.h:971
char data[BLCKSZ]
Definition: c.h:1100
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:631
#define ereport(elevel, rest)
Definition: elog.h:141
int BufFileSeekBlock(BufFile *file, long blknum)
Definition: buffile.c:752
int errmsg(const char *fmt,...)
Definition: elog.c:822
size_t BufFileWrite(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:575
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:213

◆ parent_offset()

static unsigned long parent_offset ( unsigned long  i)
inlinestatic

Definition at line 338 of file logtape.c.

Referenced by ltsReleaseBlock().

339 {
340  return (i - 1) / 2;
341 }
int i

◆ right_offset()

static unsigned long right_offset ( unsigned  i)
inlinestatic

Definition at line 332 of file logtape.c.

Referenced by ltsGetFreeBlock().

333 {
334  return 2 * i + 2;
335 }
int i

◆ swap_nodes()

static void swap_nodes ( long *  heap,
unsigned long  a,
unsigned long  b 
)
inlinestatic

Definition at line 316 of file logtape.c.

References swap.

Referenced by ltsGetFreeBlock(), and ltsReleaseBlock().

317 {
318  unsigned long swap;
319 
320  swap = heap[a];
321  heap[a] = heap[b];
322  heap[b] = swap;
323 }
#define swap(a, b)
Definition: qsort.c:94