Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b1e5c9f

Browse files
committed
Change logtape/tuplestore code to use int64 for block numbers
The code previously relied on "long" as type to track block numbers, which would be 4 bytes in all Windows builds or any 32-bit builds. This limited the code to be able to handle up to 16TB of data with the default block size of 8kB, like during a CLUSTER. This code now relies on a more portable int64, which should be more than enough for at least the next 20 years to come. This issue has been reported back in 2017, but nothing was done about it back then, so here we go now. Reported-by: Peter Geoghegan Reviewed-by: Heikki Linnakangas Discussion: https://postgr.es/m/CAH2-WznCscXnWmnj=STC0aSa7QG+BRedDnZsP=Jo_R9GUZvUrg@mail.gmail.com
1 parent c99c7a4 commit b1e5c9f

File tree

5 files changed

+80
-80
lines changed

5 files changed

+80
-80
lines changed

src/backend/storage/file/buffile.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -841,14 +841,14 @@ BufFileTell(BufFile *file, int *fileno, off_t *offset)
841841
*
842842
* Performs absolute seek to the start of the n'th BLCKSZ-sized block of
843843
* the file. Note that users of this interface will fail if their files
844-
* exceed BLCKSZ * LONG_MAX bytes, but that is quite a lot; we don't work
845-
* with tables bigger than that, either...
844+
* exceed BLCKSZ * PG_INT64_MAX bytes, but that is quite a lot; we don't
845+
* work with tables bigger than that, either...
846846
*
847847
* Result is 0 if OK, EOF if not. Logical position is not moved if an
848848
* impossible seek is attempted.
849849
*/
850850
int
851-
BufFileSeekBlock(BufFile *file, long blknum)
851+
BufFileSeekBlock(BufFile *file, int64 blknum)
852852
{
853853
return BufFileSeek(file,
854854
(int) (blknum / BUFFILE_SEG_SIZE),
@@ -901,10 +901,10 @@ BufFileSize(BufFile *file)
901901
* begins. Caller should apply this as an offset when working off block
902902
* positions that are in terms of the original BufFile space.
903903
*/
904-
long
904+
int64
905905
BufFileAppend(BufFile *target, BufFile *source)
906906
{
907-
long startBlock = target->numFiles * BUFFILE_SEG_SIZE;
907+
int64 startBlock = target->numFiles * BUFFILE_SEG_SIZE;
908908
int newNumFiles = target->numFiles + source->numFiles;
909909
int i;
910910

src/backend/utils/sort/logtape.c

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@
9494
*/
9595
typedef struct TapeBlockTrailer
9696
{
97-
long prev; /* previous block on this tape, or -1 on first
97+
int64 prev; /* previous block on this tape, or -1 on first
9898
* block */
99-
long next; /* next block on this tape, or # of valid
99+
int64 next; /* next block on this tape, or # of valid
100100
* bytes on last block (if < 0) */
101101
} TapeBlockTrailer;
102102

@@ -153,10 +153,10 @@ struct LogicalTape
153153
* When concatenation of worker tape BufFiles is performed, an offset to
154154
* the first block in the unified BufFile space is applied during reads.
155155
*/
156-
long firstBlockNumber;
157-
long curBlockNumber;
158-
long nextBlockNumber;
159-
long offsetBlockNumber;
156+
int64 firstBlockNumber;
157+
int64 curBlockNumber;
158+
int64 nextBlockNumber;
159+
int64 offsetBlockNumber;
160160

161161
/*
162162
* Buffer for current data block(s).
@@ -172,7 +172,7 @@ struct LogicalTape
172172
* order; blocks are consumed from the end of the array (lowest block
173173
* numbers first).
174174
*/
175-
long *prealloc;
175+
int64 *prealloc;
176176
int nprealloc; /* number of elements in list */
177177
int prealloc_size; /* number of elements list can hold */
178178
};
@@ -200,9 +200,9 @@ struct LogicalTapeSet
200200
* blocks that are in unused holes between worker spaces following BufFile
201201
* concatenation.
202202
*/
203-
long nBlocksAllocated; /* # of blocks allocated */
204-
long nBlocksWritten; /* # of blocks used in underlying file */
205-
long nHoleBlocks; /* # of "hole" blocks left */
203+
int64 nBlocksAllocated; /* # of blocks allocated */
204+
int64 nBlocksWritten; /* # of blocks used in underlying file */
205+
int64 nHoleBlocks; /* # of "hole" blocks left */
206206

207207
/*
208208
* We store the numbers of recycled-and-available blocks in freeBlocks[].
@@ -213,19 +213,19 @@ struct LogicalTapeSet
213213
* LogicalTapeSetForgetFreeSpace().
214214
*/
215215
bool forgetFreeSpace; /* are we remembering free blocks? */
216-
long *freeBlocks; /* resizable array holding minheap */
217-
long nFreeBlocks; /* # of currently free blocks */
216+
int64 *freeBlocks; /* resizable array holding minheap */
217+
int64 nFreeBlocks; /* # of currently free blocks */
218218
Size freeBlocksLen; /* current allocated length of freeBlocks[] */
219219
bool enable_prealloc; /* preallocate write blocks? */
220220
};
221221

222222
static LogicalTape *ltsCreateTape(LogicalTapeSet *lts);
223-
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, const void *buffer);
224-
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer);
225-
static long ltsGetBlock(LogicalTapeSet *lts, LogicalTape *lt);
226-
static long ltsGetFreeBlock(LogicalTapeSet *lts);
227-
static long ltsGetPreallocBlock(LogicalTapeSet *lts, LogicalTape *lt);
228-
static void ltsReleaseBlock(LogicalTapeSet *lts, long blocknum);
223+
static void ltsWriteBlock(LogicalTapeSet *lts, int64 blocknum, const void *buffer);
224+
static void ltsReadBlock(LogicalTapeSet *lts, int64 blocknum, void *buffer);
225+
static int64 ltsGetBlock(LogicalTapeSet *lts, LogicalTape *lt);
226+
static int64 ltsGetFreeBlock(LogicalTapeSet *lts);
227+
static int64 ltsGetPreallocBlock(LogicalTapeSet *lts, LogicalTape *lt);
228+
static void ltsReleaseBlock(LogicalTapeSet *lts, int64 blocknum);
229229
static void ltsInitReadBuffer(LogicalTape *lt);
230230

231231

@@ -235,7 +235,7 @@ static void ltsInitReadBuffer(LogicalTape *lt);
235235
* No need for an error return convention; we ereport() on any error.
236236
*/
237237
static void
238-
ltsWriteBlock(LogicalTapeSet *lts, long blocknum, const void *buffer)
238+
ltsWriteBlock(LogicalTapeSet *lts, int64 blocknum, const void *buffer)
239239
{
240240
/*
241241
* BufFile does not support "holes", so if we're about to write a block
@@ -263,8 +263,8 @@ ltsWriteBlock(LogicalTapeSet *lts, long blocknum, const void *buffer)
263263
if (BufFileSeekBlock(lts->pfile, blocknum) != 0)
264264
ereport(ERROR,
265265
(errcode_for_file_access(),
266-
errmsg("could not seek to block %ld of temporary file",
267-
blocknum)));
266+
errmsg("could not seek to block %lld of temporary file",
267+
(long long) blocknum)));
268268
BufFileWrite(lts->pfile, buffer, BLCKSZ);
269269

270270
/* Update nBlocksWritten, if we extended the file */
@@ -279,13 +279,13 @@ ltsWriteBlock(LogicalTapeSet *lts, long blocknum, const void *buffer)
279279
* module should never attempt to read a block it doesn't know is there.
280280
*/
281281
static void
282-
ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
282+
ltsReadBlock(LogicalTapeSet *lts, int64 blocknum, void *buffer)
283283
{
284284
if (BufFileSeekBlock(lts->pfile, blocknum) != 0)
285285
ereport(ERROR,
286286
(errcode_for_file_access(),
287-
errmsg("could not seek to block %ld of temporary file",
288-
blocknum)));
287+
errmsg("could not seek to block %lld of temporary file",
288+
(long long) blocknum)));
289289
BufFileReadExact(lts->pfile, buffer, BLCKSZ);
290290
}
291291

@@ -303,7 +303,7 @@ ltsReadFillBuffer(LogicalTape *lt)
303303
do
304304
{
305305
char *thisbuf = lt->buffer + lt->nbytes;
306-
long datablocknum = lt->nextBlockNumber;
306+
int64 datablocknum = lt->nextBlockNumber;
307307

308308
/* Fetch next block number */
309309
if (datablocknum == -1L)
@@ -333,28 +333,28 @@ ltsReadFillBuffer(LogicalTape *lt)
333333
return (lt->nbytes > 0);
334334
}
335335

336-
static inline unsigned long
337-
left_offset(unsigned long i)
336+
static inline uint64
337+
left_offset(uint64 i)
338338
{
339339
return 2 * i + 1;
340340
}
341341

342-
static inline unsigned long
343-
right_offset(unsigned long i)
342+
static inline uint64
343+
right_offset(uint64 i)
344344
{
345345
return 2 * i + 2;
346346
}
347347

348-
static inline unsigned long
349-
parent_offset(unsigned long i)
348+
static inline uint64
349+
parent_offset(uint64 i)
350350
{
351351
return (i - 1) / 2;
352352
}
353353

354354
/*
355355
* Get the next block for writing.
356356
*/
357-
static long
357+
static int64
358358
ltsGetBlock(LogicalTapeSet *lts, LogicalTape *lt)
359359
{
360360
if (lts->enable_prealloc)
@@ -367,14 +367,14 @@ ltsGetBlock(LogicalTapeSet *lts, LogicalTape *lt)
367367
* Select the lowest currently unused block from the tape set's global free
368368
* list min heap.
369369
*/
370-
static long
370+
static int64
371371
ltsGetFreeBlock(LogicalTapeSet *lts)
372372
{
373-
long *heap = lts->freeBlocks;
374-
long blocknum;
375-
long heapsize;
376-
long holeval;
377-
unsigned long holepos;
373+
int64 *heap = lts->freeBlocks;
374+
int64 blocknum;
375+
int64 heapsize;
376+
int64 holeval;
377+
uint64 holepos;
378378

379379
/* freelist empty; allocate a new block */
380380
if (lts->nFreeBlocks == 0)
@@ -398,9 +398,9 @@ ltsGetFreeBlock(LogicalTapeSet *lts)
398398
heapsize = lts->nFreeBlocks;
399399
while (true)
400400
{
401-
unsigned long left = left_offset(holepos);
402-
unsigned long right = right_offset(holepos);
403-
unsigned long min_child;
401+
uint64 left = left_offset(holepos);
402+
uint64 right = right_offset(holepos);
403+
uint64 min_child;
404404

405405
if (left < heapsize && right < heapsize)
406406
min_child = (heap[left] < heap[right]) ? left : right;
@@ -427,7 +427,7 @@ ltsGetFreeBlock(LogicalTapeSet *lts)
427427
* Refill the preallocation list with blocks from the tape set's free list if
428428
* necessary.
429429
*/
430-
static long
430+
static int64
431431
ltsGetPreallocBlock(LogicalTapeSet *lts, LogicalTape *lt)
432432
{
433433
/* sorted in descending order, so return the last element */
@@ -437,16 +437,16 @@ ltsGetPreallocBlock(LogicalTapeSet *lts, LogicalTape *lt)
437437
if (lt->prealloc == NULL)
438438
{
439439
lt->prealloc_size = TAPE_WRITE_PREALLOC_MIN;
440-
lt->prealloc = (long *) palloc(sizeof(long) * lt->prealloc_size);
440+
lt->prealloc = (int64 *) palloc(sizeof(int64) * lt->prealloc_size);
441441
}
442442
else if (lt->prealloc_size < TAPE_WRITE_PREALLOC_MAX)
443443
{
444444
/* when the preallocation list runs out, double the size */
445445
lt->prealloc_size *= 2;
446446
if (lt->prealloc_size > TAPE_WRITE_PREALLOC_MAX)
447447
lt->prealloc_size = TAPE_WRITE_PREALLOC_MAX;
448-
lt->prealloc = (long *) repalloc(lt->prealloc,
449-
sizeof(long) * lt->prealloc_size);
448+
lt->prealloc = (int64 *) repalloc(lt->prealloc,
449+
sizeof(int64) * lt->prealloc_size);
450450
}
451451

452452
/* refill preallocation list */
@@ -466,10 +466,10 @@ ltsGetPreallocBlock(LogicalTapeSet *lts, LogicalTape *lt)
466466
* Return a block# to the freelist.
467467
*/
468468
static void
469-
ltsReleaseBlock(LogicalTapeSet *lts, long blocknum)
469+
ltsReleaseBlock(LogicalTapeSet *lts, int64 blocknum)
470470
{
471-
long *heap;
472-
unsigned long holepos;
471+
int64 *heap;
472+
uint64 holepos;
473473

474474
/*
475475
* Do nothing if we're no longer interested in remembering free space.
@@ -486,12 +486,12 @@ ltsReleaseBlock(LogicalTapeSet *lts, long blocknum)
486486
* If the freelist becomes very large, just return and leak this free
487487
* block.
488488
*/
489-
if (lts->freeBlocksLen * 2 * sizeof(long) > MaxAllocSize)
489+
if (lts->freeBlocksLen * 2 * sizeof(int64) > MaxAllocSize)
490490
return;
491491

492492
lts->freeBlocksLen *= 2;
493-
lts->freeBlocks = (long *) repalloc(lts->freeBlocks,
494-
lts->freeBlocksLen * sizeof(long));
493+
lts->freeBlocks = (int64 *) repalloc(lts->freeBlocks,
494+
lts->freeBlocksLen * sizeof(int64));
495495
}
496496

497497
/* create a "hole" at end of minheap array */
@@ -502,7 +502,7 @@ ltsReleaseBlock(LogicalTapeSet *lts, long blocknum)
502502
/* sift up to insert blocknum */
503503
while (holepos != 0)
504504
{
505-
unsigned long parent = parent_offset(holepos);
505+
uint64 parent = parent_offset(holepos);
506506

507507
if (heap[parent] < blocknum)
508508
break;
@@ -566,7 +566,7 @@ LogicalTapeSetCreate(bool preallocate, SharedFileSet *fileset, int worker)
566566
lts->nHoleBlocks = 0L;
567567
lts->forgetFreeSpace = false;
568568
lts->freeBlocksLen = 32; /* reasonable initial guess */
569-
lts->freeBlocks = (long *) palloc(lts->freeBlocksLen * sizeof(long));
569+
lts->freeBlocks = (int64 *) palloc(lts->freeBlocksLen * sizeof(int64));
570570
lts->nFreeBlocks = 0;
571571
lts->enable_prealloc = preallocate;
572572

@@ -609,7 +609,7 @@ LogicalTape *
609609
LogicalTapeImport(LogicalTapeSet *lts, int worker, TapeShare *shared)
610610
{
611611
LogicalTape *lt;
612-
long tapeblocks;
612+
int64 tapeblocks;
613613
char filename[MAXPGPATH];
614614
BufFile *file;
615615
int64 filesize;
@@ -789,7 +789,7 @@ LogicalTapeWrite(LogicalTape *lt, const void *ptr, size_t size)
789789
if (lt->pos >= (int) TapeBlockPayloadSize)
790790
{
791791
/* Buffer full, dump it out */
792-
long nextBlockNumber;
792+
int64 nextBlockNumber;
793793

794794
if (!lt->dirty)
795795
{
@@ -1086,7 +1086,7 @@ LogicalTapeBackspace(LogicalTape *lt, size_t size)
10861086
seekpos = (size_t) lt->pos; /* part within this block */
10871087
while (size > seekpos)
10881088
{
1089-
long prev = TapeBlockGetTrailer(lt->buffer)->prev;
1089+
int64 prev = TapeBlockGetTrailer(lt->buffer)->prev;
10901090

10911091
if (prev == -1L)
10921092
{
@@ -1100,10 +1100,10 @@ LogicalTapeBackspace(LogicalTape *lt, size_t size)
11001100
ltsReadBlock(lt->tapeSet, prev, lt->buffer);
11011101

11021102
if (TapeBlockGetTrailer(lt->buffer)->next != lt->curBlockNumber)
1103-
elog(ERROR, "broken tape, next of block %ld is %ld, expected %ld",
1104-
prev,
1105-
TapeBlockGetTrailer(lt->buffer)->next,
1106-
lt->curBlockNumber);
1103+
elog(ERROR, "broken tape, next of block %lld is %lld, expected %lld",
1104+
(long long) prev,
1105+
(long long) (TapeBlockGetTrailer(lt->buffer)->next),
1106+
(long long) lt->curBlockNumber);
11071107

11081108
lt->nbytes = TapeBlockPayloadSize;
11091109
lt->curBlockNumber = prev;
@@ -1130,7 +1130,7 @@ LogicalTapeBackspace(LogicalTape *lt, size_t size)
11301130
* LogicalTapeTell().
11311131
*/
11321132
void
1133-
LogicalTapeSeek(LogicalTape *lt, long blocknum, int offset)
1133+
LogicalTapeSeek(LogicalTape *lt, int64 blocknum, int offset)
11341134
{
11351135
Assert(lt->frozen);
11361136
Assert(offset >= 0 && offset <= TapeBlockPayloadSize);
@@ -1159,7 +1159,7 @@ LogicalTapeSeek(LogicalTape *lt, long blocknum, int offset)
11591159
* the position for a seek after freezing. Not clear if anyone needs that.
11601160
*/
11611161
void
1162-
LogicalTapeTell(LogicalTape *lt, long *blocknum, int *offset)
1162+
LogicalTapeTell(LogicalTape *lt, int64 *blocknum, int *offset)
11631163
{
11641164
if (lt->buffer == NULL)
11651165
ltsInitReadBuffer(lt);
@@ -1179,7 +1179,7 @@ LogicalTapeTell(LogicalTape *lt, long *blocknum, int *offset)
11791179
* This should not be called while there are open write buffers; otherwise it
11801180
* may not account for buffered data.
11811181
*/
1182-
long
1182+
int64
11831183
LogicalTapeSetBlocks(LogicalTapeSet *lts)
11841184
{
11851185
return lts->nBlocksWritten - lts->nHoleBlocks;

0 commit comments

Comments
 (0)