PostgreSQL Source Code  git master
logtape.c File Reference
#include "postgres.h"
#include <fcntl.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))
 
#define TAPE_WRITE_PREALLOC_MIN   8
 
#define TAPE_WRITE_PREALLOC_MAX   128
 

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 ltsGetBlock (LogicalTapeSet *lts, LogicalTape *lt)
 
static long ltsGetFreeBlock (LogicalTapeSet *lts)
 
static long ltsGetPreallocBlock (LogicalTapeSet *lts, LogicalTape *lt)
 
static void ltsReleaseBlock (LogicalTapeSet *lts, long blocknum)
 
static void ltsConcatWorkerTapes (LogicalTapeSet *lts, TapeShare *shared, SharedFileSet *fileset)
 
static void ltsInitTape (LogicalTape *lt)
 
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, bool preallocate, 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)
 
void LogicalTapeSetExtend (LogicalTapeSet *lts, int nAdditional)
 
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

◆ TAPE_WRITE_PREALLOC_MAX

#define TAPE_WRITE_PREALLOC_MAX   128

Definition at line 126 of file logtape.c.

Referenced by ltsGetPreallocBlock().

◆ TAPE_WRITE_PREALLOC_MIN

#define TAPE_WRITE_PREALLOC_MIN   8

Definition at line 125 of file logtape.c.

Referenced by ltsGetPreallocBlock().

◆ TapeBlockGetNBytes

#define TapeBlockGetNBytes (   buf)
Value:
#define TapeBlockIsLast(buf)
Definition: logtape.c:108
#define TapeBlockPayloadSize
Definition: logtape.c:104
static char * buf
Definition: pg_test_fsync.c:68
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:105

Definition at line 109 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 108 of file logtape.c.

Referenced by LogicalTapeFreeze(), and ltsReadFillBuffer().

◆ TapeBlockPayloadSize

#define TapeBlockPayloadSize   (BLCKSZ - sizeof(TapeBlockTrailer))

Definition at line 104 of file logtape.c.

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

◆ TapeBlockSetNBytes

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

Definition at line 112 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 356 of file logtape.c.

Referenced by ltsGetFreeBlock().

357 {
358  return 2 * i + 1;
359 }
int i

◆ LogicalTapeBackspace()

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

Definition at line 1137 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().

1138 {
1139  LogicalTape *lt;
1140  size_t seekpos = 0;
1141 
1142  Assert(tapenum >= 0 && tapenum < lts->nTapes);
1143  lt = &lts->tapes[tapenum];
1144  Assert(lt->frozen);
1145  Assert(lt->buffer_size == BLCKSZ);
1146 
1147  if (lt->buffer == NULL)
1148  ltsInitReadBuffer(lts, lt);
1149 
1150  /*
1151  * Easy case for seek within current block.
1152  */
1153  if (size <= (size_t) lt->pos)
1154  {
1155  lt->pos -= (int) size;
1156  return size;
1157  }
1158 
1159  /*
1160  * Not-so-easy case, have to walk back the chain of blocks. This
1161  * implementation would be pretty inefficient for long seeks, but we
1162  * really aren't doing that (a seek over one tuple is typical).
1163  */
1164  seekpos = (size_t) lt->pos; /* part within this block */
1165  while (size > seekpos)
1166  {
1167  long prev = TapeBlockGetTrailer(lt->buffer)->prev;
1168 
1169  if (prev == -1L)
1170  {
1171  /* Tried to back up beyond the beginning of tape. */
1172  if (lt->curBlockNumber != lt->firstBlockNumber)
1173  elog(ERROR, "unexpected end of tape");
1174  lt->pos = 0;
1175  return seekpos;
1176  }
1177 
1178  ltsReadBlock(lts, prev, (void *) lt->buffer);
1179 
1180  if (TapeBlockGetTrailer(lt->buffer)->next != lt->curBlockNumber)
1181  elog(ERROR, "broken tape, next of block %ld is %ld, expected %ld",
1182  prev,
1183  TapeBlockGetTrailer(lt->buffer)->next,
1184  lt->curBlockNumber);
1185 
1187  lt->curBlockNumber = prev;
1188  lt->nextBlockNumber = TapeBlockGetTrailer(lt->buffer)->next;
1189 
1190  seekpos += TapeBlockPayloadSize;
1191  }
1192 
1193  /*
1194  * 'seekpos' can now be greater than 'size', because it points to the
1195  * beginning the target block. The difference is the position within the
1196  * page.
1197  */
1198  lt->pos = seekpos - size;
1199  return size;
1200 }
#define TapeBlockPayloadSize
Definition: logtape.c:104
bool frozen
Definition: logtape.c:141
long nextBlockNumber
Definition: logtape.c:157
#define ERROR
Definition: elog.h:46
int nbytes
Definition: logtape.c:167
long firstBlockNumber
Definition: logtape.c:155
static void ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:652
long curBlockNumber
Definition: logtape.c:156
#define Assert(condition)
Definition: c.h:804
LogicalTape * tapes
Definition: logtape.c:219
char * buffer
Definition: logtape.c:163
#define elog(elevel,...)
Definition: elog.h:232
int buffer_size
Definition: logtape.c:164
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:105
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:284

◆ LogicalTapeFreeze()

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

Definition at line 1034 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().

1035 {
1036  LogicalTape *lt;
1037 
1038  Assert(tapenum >= 0 && tapenum < lts->nTapes);
1039  lt = &lts->tapes[tapenum];
1040  Assert(lt->writing);
1041  Assert(lt->offsetBlockNumber == 0L);
1042 
1043  /*
1044  * Completion of a write phase. Flush last partial data block, and rewind
1045  * for nondestructive read.
1046  */
1047  if (lt->dirty)
1048  {
1049  /*
1050  * As long as we've filled the buffer at least once, its contents are
1051  * entirely defined from valgrind's point of view, even though
1052  * contents beyond the current end point may be stale. But it's
1053  * possible - at least in the case of a parallel sort - to sort such
1054  * small amount of data that we do not fill the buffer even once. Tell
1055  * valgrind that its contents are defined, so it doesn't bleat.
1056  */
1058  lt->buffer_size - lt->nbytes);
1059 
1060  TapeBlockSetNBytes(lt->buffer, lt->nbytes);
1061  ltsWriteBlock(lts, lt->curBlockNumber, (void *) lt->buffer);
1062  lt->writing = false;
1063  }
1064  lt->writing = false;
1065  lt->frozen = true;
1066 
1067  /*
1068  * The seek and backspace functions assume a single block read buffer.
1069  * That's OK with current usage. A larger buffer is helpful to make the
1070  * read pattern of the backing file look more sequential to the OS, when
1071  * we're reading from multiple tapes. But at the end of a sort, when a
1072  * tape is frozen, we only read from a single tape anyway.
1073  */
1074  if (!lt->buffer || lt->buffer_size != BLCKSZ)
1075  {
1076  if (lt->buffer)
1077  pfree(lt->buffer);
1078  lt->buffer = palloc(BLCKSZ);
1079  lt->buffer_size = BLCKSZ;
1080  }
1081 
1082  /* Read the first block, or reset if tape is empty */
1083  lt->curBlockNumber = lt->firstBlockNumber;
1084  lt->pos = 0;
1085  lt->nbytes = 0;
1086 
1087  if (lt->firstBlockNumber == -1L)
1088  lt->nextBlockNumber = -1L;
1089  ltsReadBlock(lts, lt->curBlockNumber, (void *) lt->buffer);
1090  if (TapeBlockIsLast(lt->buffer))
1091  lt->nextBlockNumber = -1L;
1092  else
1093  lt->nextBlockNumber = TapeBlockGetTrailer(lt->buffer)->next;
1094  lt->nbytes = TapeBlockGetNBytes(lt->buffer);
1095 
1096  /* Handle extra steps when caller is to share its tapeset */
1097  if (share)
1098  {
1099  BufFileExportShared(lts->pfile);
1100  share->firstblocknumber = lt->firstBlockNumber;
1101  }
1102 }
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
#define TapeBlockIsLast(buf)
Definition: logtape.c:108
long offsetBlockNumber
Definition: logtape.c:158
BufFile * pfile
Definition: logtape.c:187
long firstblocknumber
Definition: logtape.h:50
bool frozen
Definition: logtape.c:141
long nextBlockNumber
Definition: logtape.c:157
bool writing
Definition: logtape.c:140
bool dirty
Definition: logtape.c:142
void pfree(void *pointer)
Definition: mcxt.c:1169
int nbytes
Definition: logtape.c:167
long firstBlockNumber
Definition: logtape.c:155
#define TapeBlockSetNBytes(buf, nbytes)
Definition: logtape.c:112
long curBlockNumber
Definition: logtape.c:156
#define Assert(condition)
Definition: c.h:804
void BufFileExportShared(BufFile *file)
Definition: buffile.c:377
LogicalTape * tapes
Definition: logtape.c:219
#define TapeBlockGetNBytes(buf)
Definition: logtape.c:109
char * buffer
Definition: logtape.c:163
void * palloc(Size size)
Definition: mcxt.c:1062
int buffer_size
Definition: logtape.c:164
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:240
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:105
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:284

◆ LogicalTapeRead()

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

Definition at line 977 of file logtape.c.

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

Referenced by getlen(), and hashagg_batch_read().

979 {
980  LogicalTape *lt;
981  size_t nread = 0;
982  size_t nthistime;
983 
984  Assert(tapenum >= 0 && tapenum < lts->nTapes);
985  lt = &lts->tapes[tapenum];
986  Assert(!lt->writing);
987 
988  if (lt->buffer == NULL)
989  ltsInitReadBuffer(lts, lt);
990 
991  while (size > 0)
992  {
993  if (lt->pos >= lt->nbytes)
994  {
995  /* Try to load more data into buffer. */
996  if (!ltsReadFillBuffer(lts, lt))
997  break; /* EOF */
998  }
999 
1000  nthistime = lt->nbytes - lt->pos;
1001  if (nthistime > size)
1002  nthistime = size;
1003  Assert(nthistime > 0);
1004 
1005  memcpy(ptr, lt->buffer + lt->pos, nthistime);
1006 
1007  lt->pos += nthistime;
1008  ptr = (void *) ((char *) ptr + nthistime);
1009  size -= nthistime;
1010  nread += nthistime;
1011  }
1012 
1013  return nread;
1014 }
static bool ltsReadFillBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:307
bool writing
Definition: logtape.c:140
int nbytes
Definition: logtape.c:167
static void ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:652
#define Assert(condition)
Definition: c.h:804
LogicalTape * tapes
Definition: logtape.c:219
char * buffer
Definition: logtape.c:163

◆ LogicalTapeRewindForRead()

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

Definition at line 863 of file logtape.c.

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

Referenced by hashagg_spill_finish(), mergeruns(), and tuplesort_rescan().

864 {
865  LogicalTape *lt;
866 
867  Assert(tapenum >= 0 && tapenum < lts->nTapes);
868  lt = &lts->tapes[tapenum];
869 
870  /*
871  * Round and cap buffer_size if needed.
872  */
873  if (lt->frozen)
874  buffer_size = BLCKSZ;
875  else
876  {
877  /* need at least one block */
878  if (buffer_size < BLCKSZ)
879  buffer_size = BLCKSZ;
880 
881  /* palloc() larger than max_size is unlikely to be helpful */
882  if (buffer_size > lt->max_size)
883  buffer_size = lt->max_size;
884 
885  /* round down to BLCKSZ boundary */
886  buffer_size -= buffer_size % BLCKSZ;
887  }
888 
889  if (lt->writing)
890  {
891  /*
892  * Completion of a write phase. Flush last partial data block, and
893  * rewind for normal (destructive) read.
894  */
895  if (lt->dirty)
896  {
897  /*
898  * As long as we've filled the buffer at least once, its contents
899  * are entirely defined from valgrind's point of view, even though
900  * contents beyond the current end point may be stale. But it's
901  * possible - at least in the case of a parallel sort - to sort
902  * such small amount of data that we do not fill the buffer even
903  * once. Tell valgrind that its contents are defined, so it
904  * doesn't bleat.
905  */
907  lt->buffer_size - lt->nbytes);
908 
909  TapeBlockSetNBytes(lt->buffer, lt->nbytes);
910  ltsWriteBlock(lts, lt->curBlockNumber, (void *) lt->buffer);
911  }
912  lt->writing = false;
913  }
914  else
915  {
916  /*
917  * This is only OK if tape is frozen; we rewind for (another) read
918  * pass.
919  */
920  Assert(lt->frozen);
921  }
922 
923  if (lt->buffer)
924  pfree(lt->buffer);
925 
926  /* the buffer is lazily allocated, but set the size here */
927  lt->buffer = NULL;
928  lt->buffer_size = buffer_size;
929 
930  /* free the preallocation list, and return unused block numbers */
931  if (lt->prealloc != NULL)
932  {
933  for (int i = lt->nprealloc; i > 0; i--)
934  ltsReleaseBlock(lts, lt->prealloc[i - 1]);
935  pfree(lt->prealloc);
936  lt->prealloc = NULL;
937  lt->nprealloc = 0;
938  lt->prealloc_size = 0;
939  }
940 }
int max_size
Definition: logtape.c:165
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
long * prealloc
Definition: logtape.c:174
int prealloc_size
Definition: logtape.c:176
static void ltsReleaseBlock(LogicalTapeSet *lts, long blocknum)
Definition: logtape.c:485
bool frozen
Definition: logtape.c:141
bool writing
Definition: logtape.c:140
bool dirty
Definition: logtape.c:142
void pfree(void *pointer)
Definition: mcxt.c:1169
int nbytes
Definition: logtape.c:167
int nprealloc
Definition: logtape.c:175
#define TapeBlockSetNBytes(buf, nbytes)
Definition: logtape.c:112
long curBlockNumber
Definition: logtape.c:156
#define Assert(condition)
Definition: c.h:804
LogicalTape * tapes
Definition: logtape.c:219
char * buffer
Definition: logtape.c:163
int i
int buffer_size
Definition: logtape.c:164
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:240

◆ LogicalTapeRewindForWrite()

void LogicalTapeRewindForWrite ( LogicalTapeSet lts,
int  tapenum 
)

Definition at line 951 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 hashagg_tapeinfo_release(), mergeruns(), and tuplesort_gettuple_common().

952 {
953  LogicalTape *lt;
954 
955  Assert(tapenum >= 0 && tapenum < lts->nTapes);
956  lt = &lts->tapes[tapenum];
957 
958  Assert(!lt->writing && !lt->frozen);
959  lt->writing = true;
960  lt->dirty = false;
961  lt->firstBlockNumber = -1L;
962  lt->curBlockNumber = -1L;
963  lt->pos = 0;
964  lt->nbytes = 0;
965  if (lt->buffer)
966  pfree(lt->buffer);
967  lt->buffer = NULL;
968  lt->buffer_size = 0;
969 }
bool frozen
Definition: logtape.c:141
bool writing
Definition: logtape.c:140
bool dirty
Definition: logtape.c:142
void pfree(void *pointer)
Definition: mcxt.c:1169
int nbytes
Definition: logtape.c:167
long firstBlockNumber
Definition: logtape.c:155
long curBlockNumber
Definition: logtape.c:156
#define Assert(condition)
Definition: c.h:804
LogicalTape * tapes
Definition: logtape.c:219
char * buffer
Definition: logtape.c:163
int buffer_size
Definition: logtape.c:164

◆ LogicalTapeSeek()

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

Definition at line 1211 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().

1213 {
1214  LogicalTape *lt;
1215 
1216  Assert(tapenum >= 0 && tapenum < lts->nTapes);
1217  lt = &lts->tapes[tapenum];
1218  Assert(lt->frozen);
1219  Assert(offset >= 0 && offset <= TapeBlockPayloadSize);
1220  Assert(lt->buffer_size == BLCKSZ);
1221 
1222  if (lt->buffer == NULL)
1223  ltsInitReadBuffer(lts, lt);
1224 
1225  if (blocknum != lt->curBlockNumber)
1226  {
1227  ltsReadBlock(lts, blocknum, (void *) lt->buffer);
1228  lt->curBlockNumber = blocknum;
1230  lt->nextBlockNumber = TapeBlockGetTrailer(lt->buffer)->next;
1231  }
1232 
1233  if (offset > lt->nbytes)
1234  elog(ERROR, "invalid tape seek position");
1235  lt->pos = offset;
1236 }
#define TapeBlockPayloadSize
Definition: logtape.c:104
bool frozen
Definition: logtape.c:141
long nextBlockNumber
Definition: logtape.c:157
#define ERROR
Definition: elog.h:46
int nbytes
Definition: logtape.c:167
static void ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:652
long curBlockNumber
Definition: logtape.c:156
#define Assert(condition)
Definition: c.h:804
LogicalTape * tapes
Definition: logtape.c:219
char * buffer
Definition: logtape.c:163
#define elog(elevel,...)
Definition: elog.h:232
int buffer_size
Definition: logtape.c:164
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:105
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:284

◆ LogicalTapeSetBlocks()

long LogicalTapeSetBlocks ( LogicalTapeSet lts)

Definition at line 1272 of file logtape.c.

References Assert, LogicalTape::buffer, i, LogicalTapeSet::nBlocksWritten, LogicalTapeSet::nHoleBlocks, LogicalTapeSet::nTapes, LogicalTapeSet::tapes, and LogicalTape::writing.

Referenced by hash_agg_update_metrics(), tuplesort_free(), and tuplesort_updatemax().

1273 {
1274 #ifdef USE_ASSERT_CHECKING
1275  for (int i = 0; i < lts->nTapes; i++)
1276  {
1277  LogicalTape *lt = &lts->tapes[i];
1278 
1279  Assert(!lt->writing || lt->buffer == NULL);
1280  }
1281 #endif
1282  return lts->nBlocksWritten - lts->nHoleBlocks;
1283 }
long nBlocksWritten
Definition: logtape.c:200
bool writing
Definition: logtape.c:140
long nHoleBlocks
Definition: logtape.c:201
#define Assert(condition)
Definition: c.h:804
LogicalTape * tapes
Definition: logtape.c:219
char * buffer
Definition: logtape.c:163
int i

◆ LogicalTapeSetClose()

void LogicalTapeSetClose ( LogicalTapeSet lts)

Definition at line 737 of file logtape.c.

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

Referenced by hashagg_reset_spill_state(), and tuplesort_free().

738 {
739  LogicalTape *lt;
740  int i;
741 
742  BufFileClose(lts->pfile);
743  for (i = 0; i < lts->nTapes; i++)
744  {
745  lt = &lts->tapes[i];
746  if (lt->buffer)
747  pfree(lt->buffer);
748  }
749  pfree(lts->tapes);
750  pfree(lts->freeBlocks);
751  pfree(lts);
752 }
BufFile * pfile
Definition: logtape.c:187
void BufFileClose(BufFile *file)
Definition: buffile.c:395
long * freeBlocks
Definition: logtape.c:212
void pfree(void *pointer)
Definition: mcxt.c:1169
LogicalTape * tapes
Definition: logtape.c:219
char * buffer
Definition: logtape.c:163
int i

◆ LogicalTapeSetCreate()

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

Definition at line 685 of file logtape.c.

References Assert, BufFileCreateShared(), BufFileCreateTemp(), LogicalTapeSet::enable_prealloc, filename, LogicalTapeSet::forgetFreeSpace, LogicalTapeSet::freeBlocks, LogicalTapeSet::freeBlocksLen, i, ltsConcatWorkerTapes(), ltsInitTape(), MAXPGPATH, LogicalTapeSet::nBlocksAllocated, LogicalTapeSet::nBlocksWritten, LogicalTapeSet::nFreeBlocks, LogicalTapeSet::nHoleBlocks, LogicalTapeSet::nTapes, palloc(), LogicalTapeSet::pfile, pg_itoa(), and LogicalTapeSet::tapes.

Referenced by hashagg_tapeinfo_init(), inittapes(), and leader_takeover_tapes().

687 {
688  LogicalTapeSet *lts;
689  int i;
690 
691  /*
692  * Create top-level struct including per-tape LogicalTape structs.
693  */
694  Assert(ntapes > 0);
695  lts = (LogicalTapeSet *) palloc(sizeof(LogicalTapeSet));
696  lts->nBlocksAllocated = 0L;
697  lts->nBlocksWritten = 0L;
698  lts->nHoleBlocks = 0L;
699  lts->forgetFreeSpace = false;
700  lts->freeBlocksLen = 32; /* reasonable initial guess */
701  lts->freeBlocks = (long *) palloc(lts->freeBlocksLen * sizeof(long));
702  lts->nFreeBlocks = 0;
703  lts->enable_prealloc = preallocate;
704  lts->nTapes = ntapes;
705  lts->tapes = (LogicalTape *) palloc(ntapes * sizeof(LogicalTape));
706 
707  for (i = 0; i < ntapes; i++)
708  ltsInitTape(&lts->tapes[i]);
709 
710  /*
711  * Create temp BufFile storage as required.
712  *
713  * Leader concatenates worker tapes, which requires special adjustment to
714  * final tapeset data. Things are simpler for the worker case and the
715  * serial case, though. They are generally very similar -- workers use a
716  * shared fileset, whereas serial sorts use a conventional serial BufFile.
717  */
718  if (shared)
719  ltsConcatWorkerTapes(lts, shared, fileset);
720  else if (fileset)
721  {
722  char filename[MAXPGPATH];
723 
724  pg_itoa(worker, filename);
725  lts->pfile = BufFileCreateShared(fileset, filename);
726  }
727  else
728  lts->pfile = BufFileCreateTemp(false);
729 
730  return lts;
731 }
long nBlocksWritten
Definition: logtape.c:200
BufFile * pfile
Definition: logtape.c:187
long * freeBlocks
Definition: logtape.c:212
BufFile * BufFileCreateTemp(bool interXact)
Definition: buffile.c:188
#define MAXPGPATH
BufFile * BufFileCreateShared(SharedFileSet *fileset, const char *name)
Definition: buffile.c:262
long nHoleBlocks
Definition: logtape.c:201
static void ltsInitTape(LogicalTape *lt)
Definition: logtape.c:626
long nFreeBlocks
Definition: logtape.c:213
static void ltsConcatWorkerTapes(LogicalTapeSet *lts, TapeShare *shared, SharedFileSet *fileset)
Definition: logtape.c:542
#define Assert(condition)
Definition: c.h:804
bool enable_prealloc
Definition: logtape.c:215
LogicalTape * tapes
Definition: logtape.c:219
static char * filename
Definition: pg_dumpall.c:91
void * palloc(Size size)
Definition: mcxt.c:1062
bool forgetFreeSpace
Definition: logtape.c:211
int i
Size freeBlocksLen
Definition: logtape.c:214
long nBlocksAllocated
Definition: logtape.c:199
int pg_itoa(int16 i, char *a)
Definition: numutils.c:338

◆ LogicalTapeSetExtend()

void LogicalTapeSetExtend ( LogicalTapeSet lts,
int  nAdditional 
)

Definition at line 1109 of file logtape.c.

References i, ltsInitTape(), LogicalTapeSet::nTapes, repalloc(), and LogicalTapeSet::tapes.

Referenced by hashagg_tapeinfo_assign().

1110 {
1111  int i;
1112  int nTapesOrig = lts->nTapes;
1113 
1114  lts->nTapes += nAdditional;
1115 
1116  lts->tapes = (LogicalTape *) repalloc(lts->tapes,
1117  lts->nTapes * sizeof(LogicalTape));
1118 
1119  for (i = nTapesOrig; i < lts->nTapes; i++)
1120  ltsInitTape(&lts->tapes[i]);
1121 }
static void ltsInitTape(LogicalTape *lt)
Definition: logtape.c:626
LogicalTape * tapes
Definition: logtape.c:219
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
int i

◆ LogicalTapeSetForgetFreeSpace()

void LogicalTapeSetForgetFreeSpace ( LogicalTapeSet lts)

Definition at line 764 of file logtape.c.

References LogicalTapeSet::forgetFreeSpace.

Referenced by mergeruns().

765 {
766  lts->forgetFreeSpace = true;
767 }
bool forgetFreeSpace
Definition: logtape.c:211

◆ LogicalTapeTell()

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

Definition at line 1245 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().

1247 {
1248  LogicalTape *lt;
1249 
1250  Assert(tapenum >= 0 && tapenum < lts->nTapes);
1251  lt = &lts->tapes[tapenum];
1252 
1253  if (lt->buffer == NULL)
1254  ltsInitReadBuffer(lts, lt);
1255 
1256  Assert(lt->offsetBlockNumber == 0L);
1257 
1258  /* With a larger buffer, 'pos' wouldn't be the same as offset within page */
1259  Assert(lt->buffer_size == BLCKSZ);
1260 
1261  *blocknum = lt->curBlockNumber;
1262  *offset = lt->pos;
1263 }
long offsetBlockNumber
Definition: logtape.c:158
static void ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:652
long curBlockNumber
Definition: logtape.c:156
#define Assert(condition)
Definition: c.h:804
LogicalTape * tapes
Definition: logtape.c:219
char * buffer
Definition: logtape.c:163
int buffer_size
Definition: logtape.c:164

◆ LogicalTapeWrite()

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

Definition at line 775 of file logtape.c.

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

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

777 {
778  LogicalTape *lt;
779  size_t nthistime;
780 
781  Assert(tapenum >= 0 && tapenum < lts->nTapes);
782  lt = &lts->tapes[tapenum];
783  Assert(lt->writing);
784  Assert(lt->offsetBlockNumber == 0L);
785 
786  /* Allocate data buffer and first block on first write */
787  if (lt->buffer == NULL)
788  {
789  lt->buffer = (char *) palloc(BLCKSZ);
790  lt->buffer_size = BLCKSZ;
791  }
792  if (lt->curBlockNumber == -1)
793  {
794  Assert(lt->firstBlockNumber == -1);
795  Assert(lt->pos == 0);
796 
797  lt->curBlockNumber = ltsGetBlock(lts, lt);
799 
800  TapeBlockGetTrailer(lt->buffer)->prev = -1L;
801  }
802 
803  Assert(lt->buffer_size == BLCKSZ);
804  while (size > 0)
805  {
806  if (lt->pos >= (int) TapeBlockPayloadSize)
807  {
808  /* Buffer full, dump it out */
809  long nextBlockNumber;
810 
811  if (!lt->dirty)
812  {
813  /* Hmm, went directly from reading to writing? */
814  elog(ERROR, "invalid logtape state: should be dirty");
815  }
816 
817  /*
818  * First allocate the next block, so that we can store it in the
819  * 'next' pointer of this block.
820  */
821  nextBlockNumber = ltsGetBlock(lts, lt);
822 
823  /* set the next-pointer and dump the current block. */
824  TapeBlockGetTrailer(lt->buffer)->next = nextBlockNumber;
825  ltsWriteBlock(lts, lt->curBlockNumber, (void *) lt->buffer);
826 
827  /* initialize the prev-pointer of the next block */
828  TapeBlockGetTrailer(lt->buffer)->prev = lt->curBlockNumber;
829  lt->curBlockNumber = nextBlockNumber;
830  lt->pos = 0;
831  lt->nbytes = 0;
832  }
833 
834  nthistime = TapeBlockPayloadSize - lt->pos;
835  if (nthistime > size)
836  nthistime = size;
837  Assert(nthistime > 0);
838 
839  memcpy(lt->buffer + lt->pos, ptr, nthistime);
840 
841  lt->dirty = true;
842  lt->pos += nthistime;
843  if (lt->nbytes < lt->pos)
844  lt->nbytes = lt->pos;
845  ptr = (void *) ((char *) ptr + nthistime);
846  size -= nthistime;
847  }
848 }
#define TapeBlockPayloadSize
Definition: logtape.c:104
long offsetBlockNumber
Definition: logtape.c:158
bool writing
Definition: logtape.c:140
bool dirty
Definition: logtape.c:142
#define ERROR
Definition: elog.h:46
int nbytes
Definition: logtape.c:167
long firstBlockNumber
Definition: logtape.c:155
long curBlockNumber
Definition: logtape.c:156
#define Assert(condition)
Definition: c.h:804
static long ltsGetBlock(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:377
LogicalTape * tapes
Definition: logtape.c:219
char * buffer
Definition: logtape.c:163
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int buffer_size
Definition: logtape.c:164
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:240
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:105

◆ ltsConcatWorkerTapes()

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

Definition at line 542 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().

544 {
545  LogicalTape *lt = NULL;
546  long tapeblocks = 0L;
547  long nphysicalblocks = 0L;
548  int i;
549 
550  /* Should have at least one worker tape, plus leader's tape */
551  Assert(lts->nTapes >= 2);
552 
553  /*
554  * Build concatenated view of all BufFiles, remembering the block number
555  * where each source file begins. No changes are needed for leader/last
556  * tape.
557  */
558  for (i = 0; i < lts->nTapes - 1; i++)
559  {
560  char filename[MAXPGPATH];
561  BufFile *file;
562  int64 filesize;
563 
564  lt = &lts->tapes[i];
565 
566  pg_itoa(i, filename);
567  file = BufFileOpenShared(fileset, filename, O_RDONLY);
568  filesize = BufFileSize(file);
569 
570  /*
571  * Stash first BufFile, and concatenate subsequent BufFiles to that.
572  * Store block offset into each tape as we go.
573  */
574  lt->firstBlockNumber = shared[i].firstblocknumber;
575  if (i == 0)
576  {
577  lts->pfile = file;
578  lt->offsetBlockNumber = 0L;
579  }
580  else
581  {
582  lt->offsetBlockNumber = BufFileAppend(lts->pfile, file);
583  }
584  /* Don't allocate more for read buffer than could possibly help */
585  lt->max_size = Min(MaxAllocSize, filesize);
586  tapeblocks = filesize / BLCKSZ;
587  nphysicalblocks += tapeblocks;
588  }
589 
590  /*
591  * Set # of allocated blocks, as well as # blocks written. Use extent of
592  * new BufFile space (from 0 to end of last worker's tape space) for this.
593  * Allocated/written blocks should include space used by holes left
594  * between concatenated BufFiles.
595  */
596  lts->nBlocksAllocated = lt->offsetBlockNumber + tapeblocks;
597  lts->nBlocksWritten = lts->nBlocksAllocated;
598 
599  /*
600  * Compute number of hole blocks so that we can later work backwards, and
601  * instrument number of physical blocks. We don't simply use physical
602  * blocks directly for instrumentation because this would break if we ever
603  * subsequently wrote to the leader tape.
604  *
605  * Working backwards like this keeps our options open. If shared BufFiles
606  * ever support being written to post-export, logtape.c can automatically
607  * take advantage of that. We'd then support writing to the leader tape
608  * while recycling space from worker tapes, because the leader tape has a
609  * zero offset (write routines won't need to have extra logic to apply an
610  * offset).
611  *
612  * The only thing that currently prevents writing to the leader tape from
613  * working is the fact that BufFiles opened using BufFileOpenShared() are
614  * read-only by definition, but that could be changed if it seemed
615  * worthwhile. For now, writing to the leader tape will raise a "Bad file
616  * descriptor" error, so tuplesort must avoid writing to the leader tape
617  * altogether.
618  */
619  lts->nHoleBlocks = lts->nBlocksAllocated - nphysicalblocks;
620 }
int max_size
Definition: logtape.c:165
long offsetBlockNumber
Definition: logtape.c:158
long nBlocksWritten
Definition: logtape.c:200
int64 BufFileSize(BufFile *file)
Definition: buffile.c:794
#define Min(x, y)
Definition: c.h:986
BufFile * pfile
Definition: logtape.c:187
long firstblocknumber
Definition: logtape.h:50
#define MAXPGPATH
long nHoleBlocks
Definition: logtape.c:201
BufFile * BufFileOpenShared(SharedFileSet *fileset, const char *name, int mode)
Definition: buffile.c:284
#define MaxAllocSize
Definition: memutils.h:40
long firstBlockNumber
Definition: logtape.c:155
#define Assert(condition)
Definition: c.h:804
LogicalTape * tapes
Definition: logtape.c:219
static char * filename
Definition: pg_dumpall.c:91
int i
long BufFileAppend(BufFile *target, BufFile *source)
Definition: buffile.c:833
long nBlocksAllocated
Definition: logtape.c:199
int pg_itoa(int16 i, char *a)
Definition: numutils.c:338

◆ ltsGetBlock()

static long ltsGetBlock ( LogicalTapeSet lts,
LogicalTape lt 
)
static

Definition at line 377 of file logtape.c.

References LogicalTapeSet::enable_prealloc, ltsGetFreeBlock(), and ltsGetPreallocBlock().

Referenced by LogicalTapeWrite().

378 {
379  if (lts->enable_prealloc)
380  return ltsGetPreallocBlock(lts, lt);
381  else
382  return ltsGetFreeBlock(lts);
383 }
static long ltsGetFreeBlock(LogicalTapeSet *lts)
Definition: logtape.c:390
static long ltsGetPreallocBlock(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:447
bool enable_prealloc
Definition: logtape.c:215

◆ ltsGetFreeBlock()

static long ltsGetFreeBlock ( LogicalTapeSet lts)
static

Definition at line 390 of file logtape.c.

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

Referenced by ltsGetBlock(), and ltsGetPreallocBlock().

391 {
392  long *heap = lts->freeBlocks;
393  long blocknum;
394  int heapsize;
395  unsigned long pos;
396 
397  /* freelist empty; allocate a new block */
398  if (lts->nFreeBlocks == 0)
399  return lts->nBlocksAllocated++;
400 
401  if (lts->nFreeBlocks == 1)
402  {
403  lts->nFreeBlocks--;
404  return lts->freeBlocks[0];
405  }
406 
407  /* take top of minheap */
408  blocknum = heap[0];
409 
410  /* replace with end of minheap array */
411  heap[0] = heap[--lts->nFreeBlocks];
412 
413  /* sift down */
414  pos = 0;
415  heapsize = lts->nFreeBlocks;
416  while (true)
417  {
418  unsigned long left = left_offset(pos);
419  unsigned long right = right_offset(pos);
420  unsigned long min_child;
421 
422  if (left < heapsize && right < heapsize)
423  min_child = (heap[left] < heap[right]) ? left : right;
424  else if (left < heapsize)
425  min_child = left;
426  else if (right < heapsize)
427  min_child = right;
428  else
429  break;
430 
431  if (heap[min_child] >= heap[pos])
432  break;
433 
434  swap_nodes(heap, min_child, pos);
435  pos = min_child;
436  }
437 
438  return blocknum;
439 }
static void swap_nodes(long *heap, unsigned long a, unsigned long b)
Definition: logtape.c:346
long * freeBlocks
Definition: logtape.c:212
static unsigned long right_offset(unsigned i)
Definition: logtape.c:362
static unsigned long left_offset(unsigned long i)
Definition: logtape.c:356
long nFreeBlocks
Definition: logtape.c:213
long nBlocksAllocated
Definition: logtape.c:199

◆ ltsGetPreallocBlock()

static long ltsGetPreallocBlock ( LogicalTapeSet lts,
LogicalTape lt 
)
static

Definition at line 447 of file logtape.c.

References Assert, i, ltsGetFreeBlock(), LogicalTape::nprealloc, palloc(), LogicalTape::prealloc, LogicalTape::prealloc_size, repalloc(), TAPE_WRITE_PREALLOC_MAX, and TAPE_WRITE_PREALLOC_MIN.

Referenced by ltsGetBlock().

448 {
449  /* sorted in descending order, so return the last element */
450  if (lt->nprealloc > 0)
451  return lt->prealloc[--lt->nprealloc];
452 
453  if (lt->prealloc == NULL)
454  {
456  lt->prealloc = (long *) palloc(sizeof(long) * lt->prealloc_size);
457  }
458  else if (lt->prealloc_size < TAPE_WRITE_PREALLOC_MAX)
459  {
460  /* when the preallocation list runs out, double the size */
461  lt->prealloc_size *= 2;
464  lt->prealloc = (long *) repalloc(lt->prealloc,
465  sizeof(long) * lt->prealloc_size);
466  }
467 
468  /* refill preallocation list */
469  lt->nprealloc = lt->prealloc_size;
470  for (int i = lt->nprealloc; i > 0; i--)
471  {
472  lt->prealloc[i - 1] = ltsGetFreeBlock(lts);
473 
474  /* verify descending order */
475  Assert(i == lt->nprealloc || lt->prealloc[i - 1] > lt->prealloc[i]);
476  }
477 
478  return lt->prealloc[--lt->nprealloc];
479 }
long * prealloc
Definition: logtape.c:174
static long ltsGetFreeBlock(LogicalTapeSet *lts)
Definition: logtape.c:390
int prealloc_size
Definition: logtape.c:176
int nprealloc
Definition: logtape.c:175
#define TAPE_WRITE_PREALLOC_MAX
Definition: logtape.c:126
#define TAPE_WRITE_PREALLOC_MIN
Definition: logtape.c:125
#define Assert(condition)
Definition: c.h:804
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
void * palloc(Size size)
Definition: mcxt.c:1062
int i

◆ ltsInitReadBuffer()

static void ltsInitReadBuffer ( LogicalTapeSet lts,
LogicalTape lt 
)
static

Definition at line 652 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().

653 {
654  Assert(lt->buffer_size > 0);
655  lt->buffer = palloc(lt->buffer_size);
656 
657  /* Read the first block, or reset if tape is empty */
659  lt->pos = 0;
660  lt->nbytes = 0;
661  ltsReadFillBuffer(lts, lt);
662 }
static bool ltsReadFillBuffer(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:307
long nextBlockNumber
Definition: logtape.c:157
int nbytes
Definition: logtape.c:167
long firstBlockNumber
Definition: logtape.c:155
#define Assert(condition)
Definition: c.h:804
char * buffer
Definition: logtape.c:163
void * palloc(Size size)
Definition: mcxt.c:1062
int buffer_size
Definition: logtape.c:164

◆ ltsInitTape()

static void ltsInitTape ( LogicalTape lt)
static

Definition at line 626 of file logtape.c.

References LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, LogicalTape::dirty, LogicalTape::firstBlockNumber, LogicalTape::frozen, LogicalTape::max_size, MaxAllocSize, LogicalTape::nbytes, LogicalTape::nextBlockNumber, LogicalTape::nprealloc, LogicalTape::offsetBlockNumber, LogicalTape::pos, LogicalTape::prealloc, LogicalTape::prealloc_size, and LogicalTape::writing.

Referenced by LogicalTapeSetCreate(), and LogicalTapeSetExtend().

627 {
628  lt->writing = true;
629  lt->frozen = false;
630  lt->dirty = false;
631  lt->firstBlockNumber = -1L;
632  lt->curBlockNumber = -1L;
633  lt->nextBlockNumber = -1L;
634  lt->offsetBlockNumber = 0L;
635  lt->buffer = NULL;
636  lt->buffer_size = 0;
637  /* palloc() larger than MaxAllocSize would fail */
638  lt->max_size = MaxAllocSize;
639  lt->pos = 0;
640  lt->nbytes = 0;
641  lt->prealloc = NULL;
642  lt->nprealloc = 0;
643  lt->prealloc_size = 0;
644 }
int max_size
Definition: logtape.c:165
long offsetBlockNumber
Definition: logtape.c:158
long * prealloc
Definition: logtape.c:174
int prealloc_size
Definition: logtape.c:176
bool frozen
Definition: logtape.c:141
long nextBlockNumber
Definition: logtape.c:157
bool writing
Definition: logtape.c:140
bool dirty
Definition: logtape.c:142
int nbytes
Definition: logtape.c:167
int nprealloc
Definition: logtape.c:175
#define MaxAllocSize
Definition: memutils.h:40
long firstBlockNumber
Definition: logtape.c:155
long curBlockNumber
Definition: logtape.c:156
char * buffer
Definition: logtape.c:163
int buffer_size
Definition: logtape.c:164

◆ ltsReadBlock()

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

Definition at line 284 of file logtape.c.

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

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

285 {
286  size_t nread;
287 
288  if (BufFileSeekBlock(lts->pfile, blocknum) != 0)
289  ereport(ERROR,
291  errmsg("could not seek to block %ld of temporary file",
292  blocknum)));
293  nread = BufFileRead(lts->pfile, buffer, BLCKSZ);
294  if (nread != BLCKSZ)
295  ereport(ERROR,
297  errmsg("could not read block %ld of temporary file: read only %zu of %zu bytes",
298  blocknum, nread, (size_t) BLCKSZ)));
299 }
BufFile * pfile
Definition: logtape.c:187
#define ERROR
Definition: elog.h:46
int errcode_for_file_access(void)
Definition: elog.c:721
int BufFileSeekBlock(BufFile *file, long blknum)
Definition: buffile.c:761
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
size_t BufFileRead(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:543

◆ ltsReadFillBuffer()

static bool ltsReadFillBuffer ( LogicalTapeSet lts,
LogicalTape lt 
)
static

Definition at line 307 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().

308 {
309  lt->pos = 0;
310  lt->nbytes = 0;
311 
312  do
313  {
314  char *thisbuf = lt->buffer + lt->nbytes;
315  long datablocknum = lt->nextBlockNumber;
316 
317  /* Fetch next block number */
318  if (datablocknum == -1L)
319  break; /* EOF */
320  /* Apply worker offset, needed for leader tapesets */
321  datablocknum += lt->offsetBlockNumber;
322 
323  /* Read the block */
324  ltsReadBlock(lts, datablocknum, (void *) thisbuf);
325  if (!lt->frozen)
326  ltsReleaseBlock(lts, datablocknum);
328 
329  lt->nbytes += TapeBlockGetNBytes(thisbuf);
330  if (TapeBlockIsLast(thisbuf))
331  {
332  lt->nextBlockNumber = -1L;
333  /* EOF */
334  break;
335  }
336  else
337  lt->nextBlockNumber = TapeBlockGetTrailer(thisbuf)->next;
338 
339  /* Advance to next block, if we have buffer space left */
340  } while (lt->buffer_size - lt->nbytes > BLCKSZ);
341 
342  return (lt->nbytes > 0);
343 }
#define TapeBlockIsLast(buf)
Definition: logtape.c:108
long offsetBlockNumber
Definition: logtape.c:158
static void ltsReleaseBlock(LogicalTapeSet *lts, long blocknum)
Definition: logtape.c:485
bool frozen
Definition: logtape.c:141
long nextBlockNumber
Definition: logtape.c:157
int nbytes
Definition: logtape.c:167
long curBlockNumber
Definition: logtape.c:156
#define TapeBlockGetNBytes(buf)
Definition: logtape.c:109
char * buffer
Definition: logtape.c:163
int buffer_size
Definition: logtape.c:164
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:105
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:284

◆ ltsReleaseBlock()

static void ltsReleaseBlock ( LogicalTapeSet lts,
long  blocknum 
)
static

Definition at line 485 of file logtape.c.

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

Referenced by LogicalTapeRewindForRead(), and ltsReadFillBuffer().

486 {
487  long *heap;
488  unsigned long pos;
489 
490  /*
491  * Do nothing if we're no longer interested in remembering free space.
492  */
493  if (lts->forgetFreeSpace)
494  return;
495 
496  /*
497  * Enlarge freeBlocks array if full.
498  */
499  if (lts->nFreeBlocks >= lts->freeBlocksLen)
500  {
501  /*
502  * If the freelist becomes very large, just return and leak this free
503  * block.
504  */
505  if (lts->freeBlocksLen * 2 * sizeof(long) > MaxAllocSize)
506  return;
507 
508  lts->freeBlocksLen *= 2;
509  lts->freeBlocks = (long *) repalloc(lts->freeBlocks,
510  lts->freeBlocksLen * sizeof(long));
511  }
512 
513  heap = lts->freeBlocks;
514  pos = lts->nFreeBlocks;
515 
516  /* place entry at end of minheap array */
517  heap[pos] = blocknum;
518  lts->nFreeBlocks++;
519 
520  /* sift up */
521  while (pos != 0)
522  {
523  unsigned long parent = parent_offset(pos);
524 
525  if (heap[parent] < heap[pos])
526  break;
527 
528  swap_nodes(heap, parent, pos);
529  pos = parent;
530  }
531 }
static void swap_nodes(long *heap, unsigned long a, unsigned long b)
Definition: logtape.c:346
long * freeBlocks
Definition: logtape.c:212
#define MaxAllocSize
Definition: memutils.h:40
long nFreeBlocks
Definition: logtape.c:213
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
bool forgetFreeSpace
Definition: logtape.c:211
Size freeBlocksLen
Definition: logtape.c:214
static unsigned long parent_offset(unsigned long i)
Definition: logtape.c:368

◆ ltsWriteBlock()

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

Definition at line 240 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().

241 {
242  /*
243  * BufFile does not support "holes", so if we're about to write a block
244  * that's past the current end of file, fill the space between the current
245  * end of file and the target block with zeros.
246  *
247  * This can happen either when tapes preallocate blocks; or for the last
248  * block of a tape which might not have been flushed.
249  *
250  * Note that BufFile concatenation can leave "holes" in BufFile between
251  * worker-owned block ranges. These are tracked for reporting purposes
252  * only. We never read from nor write to these hole blocks, and so they
253  * are not considered here.
254  */
255  while (blocknum > lts->nBlocksWritten)
256  {
257  PGAlignedBlock zerobuf;
258 
259  MemSet(zerobuf.data, 0, sizeof(zerobuf));
260 
261  ltsWriteBlock(lts, lts->nBlocksWritten, zerobuf.data);
262  }
263 
264  /* Write the requested block */
265  if (BufFileSeekBlock(lts->pfile, blocknum) != 0)
266  ereport(ERROR,
268  errmsg("could not seek to block %ld of temporary file",
269  blocknum)));
270  BufFileWrite(lts->pfile, buffer, BLCKSZ);
271 
272  /* Update nBlocksWritten, if we extended the file */
273  if (blocknum == lts->nBlocksWritten)
274  lts->nBlocksWritten++;
275 }
long nBlocksWritten
Definition: logtape.c:200
BufFile * pfile
Definition: logtape.c:187
#define MemSet(start, val, len)
Definition: c.h:1008
char data[BLCKSZ]
Definition: c.h:1141
#define ERROR
Definition: elog.h:46
int errcode_for_file_access(void)
Definition: elog.c:721
int BufFileSeekBlock(BufFile *file, long blknum)
Definition: buffile.c:761
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
void BufFileWrite(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:586
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:240

◆ parent_offset()

static unsigned long parent_offset ( unsigned long  i)
inlinestatic

Definition at line 368 of file logtape.c.

Referenced by ltsReleaseBlock().

369 {
370  return (i - 1) / 2;
371 }
int i

◆ right_offset()

static unsigned long right_offset ( unsigned  i)
inlinestatic

Definition at line 362 of file logtape.c.

Referenced by ltsGetFreeBlock().

363 {
364  return 2 * i + 2;
365 }
int i

◆ swap_nodes()

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

Definition at line 346 of file logtape.c.

Referenced by ltsGetFreeBlock(), and ltsReleaseBlock().

347 {
348  unsigned long swap;
349 
350  swap = heap[a];
351  heap[a] = heap[b];
352  heap[b] = swap;
353 }