PostgreSQL Source Code git master
Loading...
Searching...
No Matches
blkreftable.c File Reference
#include "postgres.h"
#include "common/blkreftable.h"
#include "common/hashfn.h"
#include "port/pg_crc32c.h"
#include "lib/simplehash.h"
Include dependency graph for blkreftable.c:

Go to the source code of this file.

Data Structures

struct  BlockRefTableKey
 
struct  BlockRefTableEntry
 
struct  BlockRefTable
 
struct  BlockRefTableSerializedEntry
 
struct  BlockRefTableBuffer
 
struct  BlockRefTableReader
 
struct  BlockRefTableWriter
 

Macros

#define BLOCKS_PER_CHUNK   (1 << 16)
 
#define BLOCKS_PER_ENTRY   (BITS_PER_BYTE * sizeof(uint16))
 
#define MAX_ENTRIES_PER_CHUNK   (BLOCKS_PER_CHUNK / BLOCKS_PER_ENTRY)
 
#define INITIAL_ENTRIES_PER_CHUNK   16
 
#define SH_PREFIX   blockreftable
 
#define SH_ELEMENT_TYPE   BlockRefTableEntry
 
#define SH_KEY_TYPE   BlockRefTableKey
 
#define SH_KEY   key
 
#define SH_HASH_KEY(tb, key)    hash_bytes((const unsigned char *) &key, sizeof(BlockRefTableKey))
 
#define SH_EQUAL(tb, a, b)   (memcmp(&a, &b, sizeof(BlockRefTableKey)) == 0)
 
#define SH_SCOPE   static inline
 
#define SH_DEFINE
 
#define SH_DECLARE
 
#define BUFSIZE   65536
 

Typedefs

typedef struct BlockRefTableKey BlockRefTableKey
 
typedef uint16BlockRefTableChunk
 
typedef struct BlockRefTableSerializedEntry BlockRefTableSerializedEntry
 
typedef struct BlockRefTableBuffer BlockRefTableBuffer
 

Functions

static int BlockRefTableComparator (const void *a, const void *b)
 
static void BlockRefTableFlush (BlockRefTableBuffer *buffer)
 
static void BlockRefTableRead (BlockRefTableReader *reader, void *data, int length)
 
static void BlockRefTableWrite (BlockRefTableBuffer *buffer, void *data, int length)
 
static void BlockRefTableFileTerminate (BlockRefTableBuffer *buffer)
 
BlockRefTableCreateEmptyBlockRefTable (void)
 
void BlockRefTableSetLimitBlock (BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber limit_block)
 
void BlockRefTableMarkBlockModified (BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum)
 
BlockRefTableEntryBlockRefTableGetEntry (BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber *limit_block)
 
int BlockRefTableEntryGetBlocks (BlockRefTableEntry *entry, BlockNumber start_blkno, BlockNumber stop_blkno, BlockNumber *blocks, int nblocks)
 
void WriteBlockRefTable (BlockRefTable *brtab, io_callback_fn write_callback, void *write_callback_arg)
 
BlockRefTableReaderCreateBlockRefTableReader (io_callback_fn read_callback, void *read_callback_arg, char *error_filename, report_error_fn error_callback, void *error_callback_arg)
 
bool BlockRefTableReaderNextRelation (BlockRefTableReader *reader, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *limit_block)
 
unsigned BlockRefTableReaderGetBlocks (BlockRefTableReader *reader, BlockNumber *blocks, int nblocks)
 
void DestroyBlockRefTableReader (BlockRefTableReader *reader)
 
BlockRefTableWriterCreateBlockRefTableWriter (io_callback_fn write_callback, void *write_callback_arg)
 
void BlockRefTableWriteEntry (BlockRefTableWriter *writer, BlockRefTableEntry *entry)
 
void DestroyBlockRefTableWriter (BlockRefTableWriter *writer)
 
BlockRefTableEntryCreateBlockRefTableEntry (RelFileLocator rlocator, ForkNumber forknum)
 
void BlockRefTableEntrySetLimitBlock (BlockRefTableEntry *entry, BlockNumber limit_block)
 
void BlockRefTableEntryMarkBlockModified (BlockRefTableEntry *entry, ForkNumber forknum, BlockNumber blknum)
 
void BlockRefTableFreeEntry (BlockRefTableEntry *entry)
 

Macro Definition Documentation

◆ BLOCKS_PER_CHUNK

#define BLOCKS_PER_CHUNK   (1 << 16)

Definition at line 78 of file blkreftable.c.

◆ BLOCKS_PER_ENTRY

#define BLOCKS_PER_ENTRY   (BITS_PER_BYTE * sizeof(uint16))

Definition at line 79 of file blkreftable.c.

◆ BUFSIZE

#define BUFSIZE   65536

Definition at line 166 of file blkreftable.c.

◆ INITIAL_ENTRIES_PER_CHUNK

#define INITIAL_ENTRIES_PER_CHUNK   16

Definition at line 81 of file blkreftable.c.

◆ MAX_ENTRIES_PER_CHUNK

#define MAX_ENTRIES_PER_CHUNK   (BLOCKS_PER_CHUNK / BLOCKS_PER_ENTRY)

Definition at line 80 of file blkreftable.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 134 of file blkreftable.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 133 of file blkreftable.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   BlockRefTableEntry

Definition at line 123 of file blkreftable.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
  b 
)    (memcmp(&a, &b, sizeof(BlockRefTableKey)) == 0)

Definition at line 128 of file blkreftable.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)     hash_bytes((const unsigned char *) &key, sizeof(BlockRefTableKey))

Definition at line 126 of file blkreftable.c.

145{
147#ifndef FRONTEND
148 MemoryContext mcxt;
149#endif
150};
151
152/*
153 * On-disk serialization format for block reference table entries.
154 */
155typedef struct BlockRefTableSerializedEntry
156{
162
163/*
164 * Buffer size, so that we avoid doing many small I/Os.
165 */
166#define BUFSIZE 65536
167
168/*
169 * Ad-hoc buffer for file I/O.
170 */
171typedef struct BlockRefTableBuffer
172{
174 void *io_callback_arg;
175 char data[BUFSIZE];
176 int used;
177 int cursor;
180
181/*
182 * State for keeping track of progress while incrementally reading a block
183 * table reference file from disk.
184 *
185 * total_chunks means the number of chunks for the RelFileLocator/ForkNumber
186 * combination that is currently being read, and consumed_chunks is the number
187 * of those that have been read. (We always read all the information for
188 * a single chunk at one time, so we don't need to be able to represent the
189 * state where a chunk has been partially read.)
190 *
191 * chunk_size is the array of chunk sizes. The length is given by total_chunks.
192 *
193 * chunk_data holds the current chunk.
194 *
195 * chunk_position helps us figure out how much progress we've made in returning
196 * the block numbers for the current chunk to the caller. If the chunk is a
197 * bitmap, it's the number of bits we've scanned; otherwise, it's the number
198 * of chunk entries we've scanned.
199 */
201{
203 char *error_filename;
205 void *error_callback_arg;
211};
212
213/*
214 * State for keeping track of progress while incrementally writing a block
215 * reference table file to disk.
216 */
218{
220};
221
222/* Function prototypes. */
223static int BlockRefTableComparator(const void *a, const void *b);
224static void BlockRefTableFlush(BlockRefTableBuffer *buffer);
225static void BlockRefTableRead(BlockRefTableReader *reader, void *data,
226 int length);
227static void BlockRefTableWrite(BlockRefTableBuffer *buffer, void *data,
228 int length);
230
231/*
232 * Create an empty block reference table.
233 */
236{
238
239 /*
240 * Even completely empty database has a few hundred relation forks, so it
241 * seems best to size the hash on the assumption that we're going to have
242 * at least a few thousand entries.
243 */
244#ifdef FRONTEND
245 brtab->hash = blockreftable_create(4096, NULL);
246#else
247 brtab->mcxt = CurrentMemoryContext;
248 brtab->hash = blockreftable_create(brtab->mcxt, 4096, NULL);
249#endif
250
251 return brtab;
252}
253
254/*
255 * Set the "limit block" for a relation fork and forget any modified blocks
256 * with equal or higher block numbers.
257 *
258 * The "limit block" is the shortest known length of the relation within the
259 * range of WAL records covered by this block reference table.
260 */
261void
263 const RelFileLocator *rlocator,
264 ForkNumber forknum,
265 BlockNumber limit_block)
266{
268 BlockRefTableKey key = {0}; /* make sure any padding is zero */
269 bool found;
270
271 memcpy(&key.rlocator, rlocator, sizeof(RelFileLocator));
272 key.forknum = forknum;
273 brtentry = blockreftable_insert(brtab->hash, key, &found);
274
275 if (!found)
276 {
277 /*
278 * We have no existing data about this relation fork, so just record
279 * the limit_block value supplied by the caller, and make sure other
280 * parts of the entry are properly initialized.
281 */
282 brtentry->limit_block = limit_block;
283 brtentry->nchunks = 0;
284 brtentry->chunk_size = NULL;
285 brtentry->chunk_usage = NULL;
286 brtentry->chunk_data = NULL;
287 return;
288 }
289
291}
292
293/*
294 * Mark a block in a given relation fork as known to have been modified.
295 */
296void
298 const RelFileLocator *rlocator,
299 ForkNumber forknum,
301{
303 BlockRefTableKey key = {0}; /* make sure any padding is zero */
304 bool found;
305#ifndef FRONTEND
306 MemoryContext oldcontext = MemoryContextSwitchTo(brtab->mcxt);
307#endif
308
309 memcpy(&key.rlocator, rlocator, sizeof(RelFileLocator));
310 key.forknum = forknum;
311 brtentry = blockreftable_insert(brtab->hash, key, &found);
312
313 if (!found)
314 {
315 /*
316 * We want to set the initial limit block value to something higher
317 * than any legal block number. InvalidBlockNumber fits the bill.
318 */
319 brtentry->limit_block = InvalidBlockNumber;
320 brtentry->nchunks = 0;
321 brtentry->chunk_size = NULL;
322 brtentry->chunk_usage = NULL;
323 brtentry->chunk_data = NULL;
324 }
325
327
328#ifndef FRONTEND
329 MemoryContextSwitchTo(oldcontext);
330#endif
331}
332
333/*
334 * Get an entry from a block reference table.
335 *
336 * If the entry does not exist, this function returns NULL. Otherwise, it
337 * returns the entry and sets *limit_block to the value from the entry.
338 */
341 ForkNumber forknum, BlockNumber *limit_block)
342{
343 BlockRefTableKey key = {0}; /* make sure any padding is zero */
344 BlockRefTableEntry *entry;
345
346 Assert(limit_block != NULL);
347
348 memcpy(&key.rlocator, rlocator, sizeof(RelFileLocator));
349 key.forknum = forknum;
350 entry = blockreftable_lookup(brtab->hash, key);
351
352 if (entry != NULL)
353 *limit_block = entry->limit_block;
354
355 return entry;
356}
357
358/*
359 * Get block numbers from a table entry.
360 *
361 * 'blocks' must point to enough space to hold at least 'nblocks' block
362 * numbers, and any block numbers we manage to get will be written there.
363 * The return value is the number of block numbers actually written.
364 *
365 * We do not return block numbers unless they are greater than or equal to
366 * start_blkno and strictly less than stop_blkno.
367 */
368int
372 BlockNumber *blocks,
373 int nblocks)
374{
378 int nresults = 0;
379
380 Assert(entry != NULL);
381
382 /*
383 * Figure out which chunks could potentially contain blocks of interest.
384 *
385 * We need to be careful about overflow here, because stop_blkno could be
386 * InvalidBlockNumber or something very close to it.
387 */
390 if ((stop_blkno % BLOCKS_PER_CHUNK) != 0)
391 ++stop_chunkno;
392 if (stop_chunkno > entry->nchunks)
393 stop_chunkno = entry->nchunks;
394
395 /*
396 * Loop over chunks.
397 */
399 {
400 uint16 chunk_usage = entry->chunk_usage[chunkno];
401 BlockRefTableChunk chunk_data = entry->chunk_data[chunkno];
402 unsigned start_offset = 0;
403 unsigned stop_offset = BLOCKS_PER_CHUNK;
404
405 /*
406 * If the start and/or stop block number falls within this chunk, the
407 * whole chunk may not be of interest. Figure out which portion we
408 * care about, if it's not the whole thing.
409 */
410 if (chunkno == start_chunkno)
412 if (chunkno == stop_chunkno - 1)
413 {
417 }
418
419 /*
420 * Handling differs depending on whether this is an array of offsets
421 * or a bitmap.
422 */
423 if (chunk_usage == MAX_ENTRIES_PER_CHUNK)
424 {
425 unsigned i;
426
427 /* It's a bitmap, so test every relevant bit. */
428 for (i = start_offset; i < stop_offset; ++i)
429 {
430 uint16 w = chunk_data[i / BLOCKS_PER_ENTRY];
431
432 if ((w & (1 << (i % BLOCKS_PER_ENTRY))) != 0)
433 {
435
436 blocks[nresults++] = blkno;
437
438 /* Early exit if we run out of output space. */
439 if (nresults == nblocks)
440 return nresults;
441 }
442 }
443 }
444 else
445 {
446 unsigned i;
447
448 /* It's an array of offsets, so check each one. */
449 for (i = 0; i < chunk_usage; ++i)
450 {
451 uint16 offset = chunk_data[i];
452
453 if (offset >= start_offset && offset < stop_offset)
454 {
455 BlockNumber blkno = chunkno * BLOCKS_PER_CHUNK + offset;
456
457 blocks[nresults++] = blkno;
458
459 /* Early exit if we run out of output space. */
460 if (nresults == nblocks)
461 return nresults;
462 }
463 }
464 }
465 }
466
467 return nresults;
468}
469
470/*
471 * Serialize a block reference table to a file.
472 */
473void
476 void *write_callback_arg)
477{
479 BlockRefTableBuffer buffer;
481
482 /* Prepare buffer. */
483 memset(&buffer, 0, sizeof(BlockRefTableBuffer));
486 INIT_CRC32C(buffer.crc);
487
488 /* Write magic number. */
489 BlockRefTableWrite(&buffer, &magic, sizeof(uint32));
490
491 /* Write the entries, assuming there are some. */
492 if (brtab->hash->members > 0)
493 {
494 unsigned i = 0;
497
498 /* Extract entries into serializable format and sort them. */
499 sdata =
502 while ((brtentry = blockreftable_iterate(brtab->hash, &it)) != NULL)
503 {
505
506 sentry->rlocator = brtentry->key.rlocator;
507 sentry->forknum = brtentry->key.forknum;
508 sentry->limit_block = brtentry->limit_block;
509 sentry->nchunks = brtentry->nchunks;
510
511 /* trim trailing zero entries */
512 while (sentry->nchunks > 0 &&
513 brtentry->chunk_usage[sentry->nchunks - 1] == 0)
514 sentry->nchunks--;
515 }
516 Assert(i == brtab->hash->members);
519
520 /* Loop over entries in sorted order and serialize each one. */
521 for (i = 0; i < brtab->hash->members; ++i)
522 {
524 BlockRefTableKey key = {0}; /* make sure any padding is zero */
525 unsigned j;
526
527 /* Write the serialized entry itself. */
528 BlockRefTableWrite(&buffer, sentry,
530
531 /* Look up the original entry so we can access the chunks. */
532 memcpy(&key.rlocator, &sentry->rlocator, sizeof(RelFileLocator));
533 key.forknum = sentry->forknum;
534 brtentry = blockreftable_lookup(brtab->hash, key);
535 Assert(brtentry != NULL);
536
537 /* Write the untruncated portion of the chunk length array. */
538 if (sentry->nchunks != 0)
539 BlockRefTableWrite(&buffer, brtentry->chunk_usage,
540 sentry->nchunks * sizeof(uint16));
541
542 /* Write the contents of each chunk. */
543 for (j = 0; j < brtentry->nchunks; ++j)
544 {
545 if (brtentry->chunk_usage[j] == 0)
546 continue;
547 BlockRefTableWrite(&buffer, brtentry->chunk_data[j],
548 brtentry->chunk_usage[j] * sizeof(uint16));
549 }
550 }
551 }
552
553 /* Write out appropriate terminator and CRC and flush buffer. */
555}
556
557/*
558 * Prepare to incrementally read a block reference table file.
559 *
560 * 'read_callback' is a function that can be called to read data from the
561 * underlying file (or other data source) into our internal buffer.
562 *
563 * 'read_callback_arg' is an opaque argument to be passed to read_callback.
564 *
565 * 'error_filename' is the filename that should be included in error messages
566 * if the file is found to be malformed. The value is not copied, so the
567 * caller should ensure that it remains valid until done with this
568 * BlockRefTableReader.
569 *
570 * 'error_callback' is a function to be called if the file is found to be
571 * malformed. This is not used for I/O errors, which must be handled internally
572 * by read_callback.
573 *
574 * 'error_callback_arg' is an opaque argument to be passed to error_callback.
575 */
578 void *read_callback_arg,
579 char *error_filename,
580 report_error_fn error_callback,
581 void *error_callback_arg)
582{
583 BlockRefTableReader *reader;
584 uint32 magic;
585
586 /* Initialize data structure. */
590 reader->error_filename = error_filename;
591 reader->error_callback = error_callback;
592 reader->error_callback_arg = error_callback_arg;
593 INIT_CRC32C(reader->buffer.crc);
594
595 /* Verify magic number. */
596 BlockRefTableRead(reader, &magic, sizeof(uint32));
597 if (magic != BLOCKREFTABLE_MAGIC)
598 error_callback(error_callback_arg,
599 "file \"%s\" has wrong magic number: expected %u, found %u",
600 error_filename,
601 BLOCKREFTABLE_MAGIC, magic);
602
603 return reader;
604}
605
606/*
607 * Read next relation fork covered by this block reference table file.
608 *
609 * After calling this function, you must call BlockRefTableReaderGetBlocks
610 * until it returns 0 before calling it again.
611 */
612bool
614 RelFileLocator *rlocator,
615 ForkNumber *forknum,
616 BlockNumber *limit_block)
617{
620
621 /*
622 * Sanity check: caller must read all blocks from all chunks before moving
623 * on to the next relation.
624 */
625 Assert(reader->total_chunks == reader->consumed_chunks);
626
627 /* Read serialized entry. */
628 BlockRefTableRead(reader, &sentry,
630
631 /*
632 * If we just read the sentinel entry indicating that we've reached the
633 * end, read and check the CRC.
634 */
635 if (memcmp(&sentry, &zentry, sizeof(BlockRefTableSerializedEntry)) == 0)
636 {
639
640 /*
641 * We want to know the CRC of the file excluding the 4-byte CRC
642 * itself, so copy the current value of the CRC accumulator before
643 * reading those bytes, and use the copy to finalize the calculation.
644 */
645 expected_crc = reader->buffer.crc;
647
648 /* Now we can read the actual value. */
649 BlockRefTableRead(reader, &actual_crc, sizeof(pg_crc32c));
650
651 /* Throw an error if there is a mismatch. */
653 reader->error_callback(reader->error_callback_arg,
654 "file \"%s\" has wrong checksum: expected %08X, found %08X",
656
657 return false;
658 }
659
660 /*
661 * Sanity-check the nchunks value. In the backend, palloc_array would
662 * enforce this anyway (with a more generic error message); but in
663 * frontend it would not, potentially allowing BlockRefTableRead's length
664 * parameter to overflow.
665 */
666 if (sentry.nchunks > MaxAllocSize / sizeof(uint16))
667 {
668 reader->error_callback(reader->error_callback_arg,
669 "file \"%s\" has oversized chunk size array",
670 reader->error_filename);
671 return false;
672 }
673
674 /* Read chunk size array. */
675 if (reader->chunk_size != NULL)
676 pfree(reader->chunk_size);
677 reader->chunk_size = palloc_array(uint16, sentry.nchunks);
678 BlockRefTableRead(reader, reader->chunk_size,
679 sentry.nchunks * sizeof(uint16));
680
681 /* Set up for chunk scan. */
682 reader->total_chunks = sentry.nchunks;
683 reader->consumed_chunks = 0;
684
685 /* Return data to caller. */
686 memcpy(rlocator, &sentry.rlocator, sizeof(RelFileLocator));
687 *forknum = sentry.forknum;
688 *limit_block = sentry.limit_block;
689 return true;
690}
691
692/*
693 * Get modified blocks associated with the relation fork returned by
694 * the most recent call to BlockRefTableReaderNextRelation.
695 *
696 * On return, block numbers will be written into the 'blocks' array, whose
697 * length should be passed via 'nblocks'. The return value is the number of
698 * entries actually written into the 'blocks' array, which may be less than
699 * 'nblocks' if we run out of modified blocks in the relation fork before
700 * we run out of room in the array.
701 */
702unsigned
704 BlockNumber *blocks,
705 int nblocks)
706{
707 unsigned blocks_found = 0;
708
709 /* Must provide space for at least one block number to be returned. */
710 Assert(nblocks > 0);
711
712 /* Loop collecting blocks to return to caller. */
713 for (;;)
714 {
716
717 /*
718 * If we've read at least one chunk, maybe it contains some block
719 * numbers that could satisfy caller's request.
720 */
721 if (reader->consumed_chunks > 0)
722 {
723 uint32 chunkno = reader->consumed_chunks - 1;
724 uint16 chunk_size = reader->chunk_size[chunkno];
725
726 if (chunk_size == MAX_ENTRIES_PER_CHUNK)
727 {
728 /* Bitmap format, so search for bits that are set. */
729 while (reader->chunk_position < BLOCKS_PER_CHUNK &&
730 blocks_found < nblocks)
731 {
733 uint16 w;
734
736 if ((w & (1u << (chunkoffset % BLOCKS_PER_ENTRY))) != 0)
737 blocks[blocks_found++] =
739 ++reader->chunk_position;
740 }
741 }
742 else
743 {
744 /* Not in bitmap format, so each entry is a 2-byte offset. */
745 while (reader->chunk_position < chunk_size &&
746 blocks_found < nblocks)
747 {
749 + reader->chunk_data[reader->chunk_position];
750 ++reader->chunk_position;
751 }
752 }
753 }
754
755 /* We found enough blocks, so we're done. */
756 if (blocks_found >= nblocks)
757 break;
758
759 /*
760 * We didn't find enough blocks, so we must need the next chunk. If
761 * there are none left, though, then we're done anyway.
762 */
763 if (reader->consumed_chunks == reader->total_chunks)
764 break;
765
766 /*
767 * Read data for next chunk and reset scan position to beginning of
768 * chunk. Note that the next chunk might be empty, in which case we
769 * consume the chunk without actually consuming any bytes from the
770 * underlying file.
771 */
772 next_chunk_size = reader->chunk_size[reader->consumed_chunks];
773 if (next_chunk_size > 0)
774 BlockRefTableRead(reader, reader->chunk_data,
775 next_chunk_size * sizeof(uint16));
776 ++reader->consumed_chunks;
777 reader->chunk_position = 0;
778 }
779
780 return blocks_found;
781}
782
783/*
784 * Release memory used while reading a block reference table from a file.
785 */
786void
788{
789 if (reader->chunk_size != NULL)
790 {
791 pfree(reader->chunk_size);
792 reader->chunk_size = NULL;
793 }
794 pfree(reader);
795}
796
797/*
798 * Prepare to write a block reference table file incrementally.
799 *
800 * Caller must be able to supply BlockRefTableEntry objects sorted in the
801 * appropriate order.
802 */
805 void *write_callback_arg)
806{
809
810 /* Prepare buffer and CRC check and save callbacks. */
812 writer->buffer.io_callback = write_callback;
813 writer->buffer.io_callback_arg = write_callback_arg;
814 INIT_CRC32C(writer->buffer.crc);
815
816 /* Write magic number. */
817 BlockRefTableWrite(&writer->buffer, &magic, sizeof(uint32));
818
819 return writer;
820}
821
822/*
823 * Append one entry to a block reference table file.
824 *
825 * Note that entries must be written in the proper order, that is, sorted by
826 * tablespace, then database, then relfilenumber, then fork number. Caller
827 * is responsible for supplying data in the correct order. If that seems hard,
828 * use an in-memory BlockRefTable instead.
829 */
830void
832{
834 unsigned j;
835
836 /* Convert to serialized entry format. */
837 sentry.rlocator = entry->key.rlocator;
838 sentry.forknum = entry->key.forknum;
839 sentry.limit_block = entry->limit_block;
840 sentry.nchunks = entry->nchunks;
841
842 /* Trim trailing zero entries. */
843 while (sentry.nchunks > 0 && entry->chunk_usage[sentry.nchunks - 1] == 0)
844 sentry.nchunks--;
845
846 /* Write the serialized entry itself. */
849
850 /* Write the untruncated portion of the chunk length array. */
851 if (sentry.nchunks != 0)
852 BlockRefTableWrite(&writer->buffer, entry->chunk_usage,
853 sentry.nchunks * sizeof(uint16));
854
855 /* Write the contents of each chunk. */
856 for (j = 0; j < entry->nchunks; ++j)
857 {
858 if (entry->chunk_usage[j] == 0)
859 continue;
860 BlockRefTableWrite(&writer->buffer, entry->chunk_data[j],
861 entry->chunk_usage[j] * sizeof(uint16));
862 }
863}
864
865/*
866 * Finalize an incremental write of a block reference table file.
867 */
868void
870{
872 pfree(writer);
873}
874
875/*
876 * Allocate a standalone BlockRefTableEntry.
877 *
878 * When we're manipulating a full in-memory BlockRefTable, the entries are
879 * part of the hash table and are allocated by simplehash. This routine is
880 * used by callers that want to write out a BlockRefTable to a file without
881 * needing to store the whole thing in memory at once.
882 *
883 * Entries allocated by this function can be manipulated using the functions
884 * BlockRefTableEntrySetLimitBlock and BlockRefTableEntryMarkBlockModified
885 * and then written using BlockRefTableWriteEntry and freed using
886 * BlockRefTableFreeEntry.
887 */
890{
892
893 memcpy(&entry->key.rlocator, &rlocator, sizeof(RelFileLocator));
894 entry->key.forknum = forknum;
896
897 return entry;
898}
899
900/*
901 * Update a BlockRefTableEntry with a new value for the "limit block" and
902 * forget any equal-or-higher-numbered modified blocks.
903 *
904 * The "limit block" is the shortest known length of the relation within the
905 * range of WAL records covered by this block reference table.
906 */
907void
909 BlockNumber limit_block)
910{
911 unsigned chunkno;
912 unsigned limit_chunkno;
913 unsigned limit_chunkoffset;
915
916 /* If we already have an equal or lower limit block, do nothing. */
917 if (limit_block >= entry->limit_block)
918 return;
919
920 /* Record the new limit block value. */
921 entry->limit_block = limit_block;
922
923 /*
924 * Figure out which chunk would store the state of the new limit block,
925 * and which offset within that chunk.
926 */
927 limit_chunkno = limit_block / BLOCKS_PER_CHUNK;
928 limit_chunkoffset = limit_block % BLOCKS_PER_CHUNK;
929
930 /*
931 * If the number of chunks is not large enough for any blocks with equal
932 * or higher block numbers to exist, then there is nothing further to do.
933 */
934 if (limit_chunkno >= entry->nchunks)
935 return;
936
937 /* Discard entire contents of any higher-numbered chunks. */
938 for (chunkno = limit_chunkno + 1; chunkno < entry->nchunks; ++chunkno)
939 entry->chunk_usage[chunkno] = 0;
940
941 /*
942 * Next, we need to discard any offsets within the chunk that would
943 * contain the limit_block. We must handle this differently depending on
944 * whether the chunk that would contain limit_block is a bitmap or an
945 * array of offsets.
946 */
949 {
950 unsigned chunkoffset;
951
952 /* It's a bitmap. Unset bits. */
954 ++chunkoffset)
956 ~(1 << (chunkoffset % BLOCKS_PER_ENTRY));
957 }
958 else
959 {
960 unsigned i,
961 j = 0;
962
963 /* It's an offset array. Filter out large offsets. */
964 for (i = 0; i < entry->chunk_usage[limit_chunkno]; ++i)
965 {
966 Assert(j <= i);
969 }
970 Assert(j <= entry->chunk_usage[limit_chunkno]);
971 entry->chunk_usage[limit_chunkno] = j;
972 }
973}
974
975/*
976 * Mark a block in a given BlockRefTableEntry as known to have been modified.
977 */
978void
980 ForkNumber forknum,
982{
983 unsigned chunkno;
984 unsigned chunkoffset;
985 unsigned i;
986
987 /*
988 * Which chunk should store the state of this block? And what is the
989 * offset of this block relative to the start of that chunk?
990 */
993
994 /*
995 * If 'nchunks' isn't big enough for us to be able to represent the state
996 * of this block, we need to enlarge our arrays.
997 */
998 if (chunkno >= entry->nchunks)
999 {
1000 unsigned max_chunks;
1001 unsigned extra_chunks;
1002
1003 /*
1004 * New array size is a power of 2, at least 16, big enough so that
1005 * chunkno will be a valid array index.
1006 */
1007 max_chunks = Max(16, entry->nchunks);
1008 while (max_chunks < chunkno + 1)
1009 max_chunks *= 2;
1010 extra_chunks = max_chunks - entry->nchunks;
1011
1012 if (entry->nchunks == 0)
1013 {
1017 }
1018 else
1019 {
1020 entry->chunk_size = repalloc(entry->chunk_size,
1021 sizeof(uint16) * max_chunks);
1022 memset(&entry->chunk_size[entry->nchunks], 0,
1023 extra_chunks * sizeof(uint16));
1024 entry->chunk_usage = repalloc(entry->chunk_usage,
1025 sizeof(uint16) * max_chunks);
1026 memset(&entry->chunk_usage[entry->nchunks], 0,
1027 extra_chunks * sizeof(uint16));
1028 entry->chunk_data = repalloc(entry->chunk_data,
1029 sizeof(BlockRefTableChunk) * max_chunks);
1030 memset(&entry->chunk_data[entry->nchunks], 0,
1032 }
1033 entry->nchunks = max_chunks;
1034 }
1035
1036 /*
1037 * If the chunk that covers this block number doesn't exist yet, create it
1038 * as an array and add the appropriate offset to it. We make it pretty
1039 * small initially, because there might only be 1 or a few block
1040 * references in this chunk and we don't want to use up too much memory.
1041 */
1042 if (entry->chunk_size[chunkno] == 0)
1043 {
1044 entry->chunk_data[chunkno] =
1047 entry->chunk_data[chunkno][0] = chunkoffset;
1048 entry->chunk_usage[chunkno] = 1;
1049 return;
1050 }
1051
1052 /*
1053 * If the number of entries in this chunk is already maximum, it must be a
1054 * bitmap. Just set the appropriate bit.
1055 */
1057 {
1058 BlockRefTableChunk chunk = entry->chunk_data[chunkno];
1059
1060 chunk[chunkoffset / BLOCKS_PER_ENTRY] |=
1062 return;
1063 }
1064
1065 /*
1066 * There is an existing chunk and it's in array format. Let's find out
1067 * whether it already has an entry for this block. If so, we do not need
1068 * to do anything.
1069 */
1070 for (i = 0; i < entry->chunk_usage[chunkno]; ++i)
1071 {
1072 if (entry->chunk_data[chunkno][i] == chunkoffset)
1073 return;
1074 }
1075
1076 /*
1077 * If the number of entries currently used is one less than the maximum,
1078 * it's time to convert to bitmap format.
1079 */
1080 if (entry->chunk_usage[chunkno] == MAX_ENTRIES_PER_CHUNK - 1)
1081 {
1083 unsigned j;
1084
1085 /* Allocate a new chunk. */
1087
1088 /* Set the bit for each existing entry. */
1089 for (j = 0; j < entry->chunk_usage[chunkno]; ++j)
1090 {
1091 unsigned coff = entry->chunk_data[chunkno][j];
1092
1094 1 << (coff % BLOCKS_PER_ENTRY);
1095 }
1096
1097 /* Set the bit for the new entry. */
1100
1101 /* Swap the new chunk into place and update metadata. */
1102 pfree(entry->chunk_data[chunkno]);
1103 entry->chunk_data[chunkno] = newchunk;
1106 return;
1107 }
1108
1109 /*
1110 * OK, we currently have an array, and we don't need to convert to a
1111 * bitmap, but we do need to add a new element. If there's not enough
1112 * room, we'll have to expand the array.
1113 */
1114 if (entry->chunk_usage[chunkno] == entry->chunk_size[chunkno])
1115 {
1116 unsigned newsize = entry->chunk_size[chunkno] * 2;
1117
1119 entry->chunk_data[chunkno] = repalloc(entry->chunk_data[chunkno],
1120 newsize * sizeof(uint16));
1121 entry->chunk_size[chunkno] = newsize;
1122 }
1123
1124 /* Now we can add the new entry. */
1125 entry->chunk_data[chunkno][entry->chunk_usage[chunkno]] =
1127 entry->chunk_usage[chunkno]++;
1128}
1129
1130/*
1131 * Release memory for a BlockRefTableEntry that was created by
1132 * CreateBlockRefTableEntry.
1133 */
1134void
1136{
1137 if (entry->chunk_size != NULL)
1138 {
1139 pfree(entry->chunk_size);
1140 entry->chunk_size = NULL;
1141 }
1142
1143 if (entry->chunk_usage != NULL)
1144 {
1145 pfree(entry->chunk_usage);
1146 entry->chunk_usage = NULL;
1147 }
1148
1149 if (entry->chunk_data != NULL)
1150 {
1151 pfree(entry->chunk_data);
1152 entry->chunk_data = NULL;
1153 }
1154
1155 pfree(entry);
1156}
1157
1158/*
1159 * Comparator for BlockRefTableSerializedEntry objects.
1160 *
1161 * We make the tablespace OID the first column of the sort key to match
1162 * the on-disk tree structure.
1163 */
1164static int
1165BlockRefTableComparator(const void *a, const void *b)
1166{
1169
1170 if (sa->rlocator.spcOid > sb->rlocator.spcOid)
1171 return 1;
1172 if (sa->rlocator.spcOid < sb->rlocator.spcOid)
1173 return -1;
1174
1175 if (sa->rlocator.dbOid > sb->rlocator.dbOid)
1176 return 1;
1177 if (sa->rlocator.dbOid < sb->rlocator.dbOid)
1178 return -1;
1179
1180 if (sa->rlocator.relNumber > sb->rlocator.relNumber)
1181 return 1;
1182 if (sa->rlocator.relNumber < sb->rlocator.relNumber)
1183 return -1;
1184
1185 if (sa->forknum > sb->forknum)
1186 return 1;
1187 if (sa->forknum < sb->forknum)
1188 return -1;
1189
1190 return 0;
1191}
1192
1193/*
1194 * Flush any buffered data out of a BlockRefTableBuffer.
1195 */
1196static void
1198{
1199 buffer->io_callback(buffer->io_callback_arg, buffer->data, buffer->used);
1200 buffer->used = 0;
1201}
1202
1203/*
1204 * Read data from a BlockRefTableBuffer, and update the running CRC
1205 * calculation for the returned data (but not any data that we may have
1206 * buffered but not yet actually returned).
1207 */
1208static void
1209BlockRefTableRead(BlockRefTableReader *reader, void *data, int length)
1210{
1211 BlockRefTableBuffer *buffer = &reader->buffer;
1212
1213 /* Loop until read is fully satisfied. */
1214 while (length > 0)
1215 {
1216 if (buffer->cursor < buffer->used)
1217 {
1218 /*
1219 * If any buffered data is available, use that to satisfy as much
1220 * of the request as possible.
1221 */
1222 int bytes_to_copy = Min(length, buffer->used - buffer->cursor);
1223
1224 memcpy(data, &buffer->data[buffer->cursor], bytes_to_copy);
1225 COMP_CRC32C(buffer->crc, &buffer->data[buffer->cursor],
1227 buffer->cursor += bytes_to_copy;
1228 data = ((char *) data) + bytes_to_copy;
1229 length -= bytes_to_copy;
1230 }
1231 else if (length >= BUFSIZE)
1232 {
1233 /*
1234 * If the request length is long, read directly into caller's
1235 * buffer.
1236 */
1237 int bytes_read;
1238
1239 bytes_read = buffer->io_callback(buffer->io_callback_arg,
1240 data, length);
1241 COMP_CRC32C(buffer->crc, data, bytes_read);
1242 data = ((char *) data) + bytes_read;
1243 length -= bytes_read;
1244
1245 /* If we didn't get anything, that's bad. */
1246 if (bytes_read == 0)
1247 reader->error_callback(reader->error_callback_arg,
1248 "file \"%s\" ends unexpectedly",
1249 reader->error_filename);
1250 }
1251 else
1252 {
1253 /*
1254 * Refill our buffer.
1255 */
1256 buffer->used = buffer->io_callback(buffer->io_callback_arg,
1257 buffer->data, BUFSIZE);
1258 buffer->cursor = 0;
1259
1260 /* If we didn't get anything, that's bad. */
1261 if (buffer->used == 0)
1262 reader->error_callback(reader->error_callback_arg,
1263 "file \"%s\" ends unexpectedly",
1264 reader->error_filename);
1265 }
1266 }
1267}
1268
1269/*
1270 * Supply data to a BlockRefTableBuffer for write to the underlying File,
1271 * and update the running CRC calculation for that data.
1272 */
1273static void
1274BlockRefTableWrite(BlockRefTableBuffer *buffer, void *data, int length)
1275{
1276 /* Update running CRC calculation. */
1277 COMP_CRC32C(buffer->crc, data, length);
1278
1279 /* If the new data can't fit into the buffer, flush the buffer. */
1280 if (buffer->used + length > BUFSIZE)
1281 {
1282 buffer->io_callback(buffer->io_callback_arg, buffer->data,
1283 buffer->used);
1284 buffer->used = 0;
1285 }
1286
1287 /* If the new data would fill the buffer, or more, write it directly. */
1288 if (length >= BUFSIZE)
1289 {
1290 buffer->io_callback(buffer->io_callback_arg, data, length);
1291 return;
1292 }
1293
1294 /* Otherwise, copy the new data into the buffer. */
1295 memcpy(&buffer->data[buffer->used], data, length);
1296 buffer->used += length;
1297 Assert(buffer->used <= BUFSIZE);
1298}
1299
1300/*
1301 * Generate the sentinel and CRC required at the end of a block reference
1302 * table file and flush them out of our internal buffer.
1303 */
1304static void
1306{
1308 pg_crc32c crc;
1309
1310 /* Write a sentinel indicating that there are no more entries. */
1311 BlockRefTableWrite(buffer, &zentry,
1313
1314 /*
1315 * Writing the checksum will perturb the ongoing checksum calculation, so
1316 * copy the state first and finalize the computation using the copy.
1317 */
1318 crc = buffer->crc;
1319 FIN_CRC32C(crc);
1320 BlockRefTableWrite(buffer, &crc, sizeof(pg_crc32c));
1321
1322 /* Flush any leftover data out of our buffer. */
1323 BlockRefTableFlush(buffer);
1324}
void BlockRefTableFreeEntry(BlockRefTableEntry *entry)
BlockRefTableEntry * BlockRefTableGetEntry(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber *limit_block)
bool BlockRefTableReaderNextRelation(BlockRefTableReader *reader, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *limit_block)
int BlockRefTableEntryGetBlocks(BlockRefTableEntry *entry, BlockNumber start_blkno, BlockNumber stop_blkno, BlockNumber *blocks, int nblocks)
void BlockRefTableMarkBlockModified(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum)
BlockRefTableWriter * CreateBlockRefTableWriter(io_callback_fn write_callback, void *write_callback_arg)
#define BLOCKS_PER_CHUNK
Definition blkreftable.c:78
void BlockRefTableWriteEntry(BlockRefTableWriter *writer, BlockRefTableEntry *entry)
static void BlockRefTableRead(BlockRefTableReader *reader, void *data, int length)
void BlockRefTableEntryMarkBlockModified(BlockRefTableEntry *entry, ForkNumber forknum, BlockNumber blknum)
BlockRefTableReader * CreateBlockRefTableReader(io_callback_fn read_callback, void *read_callback_arg, char *error_filename, report_error_fn error_callback, void *error_callback_arg)
unsigned BlockRefTableReaderGetBlocks(BlockRefTableReader *reader, BlockNumber *blocks, int nblocks)
#define BLOCKS_PER_ENTRY
Definition blkreftable.c:79
void BlockRefTableSetLimitBlock(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber limit_block)
void BlockRefTableEntrySetLimitBlock(BlockRefTableEntry *entry, BlockNumber limit_block)
BlockRefTable * CreateEmptyBlockRefTable(void)
BlockRefTableEntry * CreateBlockRefTableEntry(RelFileLocator rlocator, ForkNumber forknum)
void WriteBlockRefTable(BlockRefTable *brtab, io_callback_fn write_callback, void *write_callback_arg)
static void BlockRefTableFileTerminate(BlockRefTableBuffer *buffer)
void DestroyBlockRefTableReader(BlockRefTableReader *reader)
#define MAX_ENTRIES_PER_CHUNK
Definition blkreftable.c:80
void DestroyBlockRefTableWriter(BlockRefTableWriter *writer)
#define INITIAL_ENTRIES_PER_CHUNK
Definition blkreftable.c:81
#define BUFSIZE
uint16 * BlockRefTableChunk
Definition blkreftable.c:82
static void BlockRefTableWrite(BlockRefTableBuffer *buffer, void *data, int length)
static void BlockRefTableFlush(BlockRefTableBuffer *buffer)
static int BlockRefTableComparator(const void *a, const void *b)
void(* report_error_fn)(void *callback_arg, char *msg,...) pg_attribute_printf(2
Definition blkreftable.h:47
int(* io_callback_fn)(void *callback_arg, void *data, int length)
Definition blkreftable.h:46
#define BLOCKREFTABLE_MAGIC
Definition blkreftable.h:30
uint32 BlockNumber
Definition block.h:31
#define InvalidBlockNumber
Definition block.h:33
#define Min(x, y)
Definition c.h:1091
#define Max(x, y)
Definition c.h:1085
#define Assert(condition)
Definition c.h:943
uint16_t uint16
Definition c.h:623
uint32_t uint32
Definition c.h:624
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define palloc_object(type)
Definition fe_memutils.h:89
#define MaxAllocSize
Definition fe_memutils.h:22
#define palloc_array(type, count)
Definition fe_memutils.h:91
#define palloc0_array(type, count)
Definition fe_memutils.h:92
#define palloc0_object(type)
Definition fe_memutils.h:90
int b
Definition isn.c:74
int a
Definition isn.c:73
int j
Definition isn.c:78
int i
Definition isn.c:77
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1635
void pfree(void *pointer)
Definition mcxt.c:1619
void * palloc0(Size size)
Definition mcxt.c:1420
MemoryContext CurrentMemoryContext
Definition mcxt.c:161
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
uint32 pg_crc32c
Definition pg_crc32c.h:38
#define COMP_CRC32C(crc, data, len)
Definition pg_crc32c.h:177
#define EQ_CRC32C(c1, c2)
Definition pg_crc32c.h:42
#define INIT_CRC32C(crc)
Definition pg_crc32c.h:41
#define FIN_CRC32C(crc)
Definition pg_crc32c.h:182
const void * data
return crc
#define qsort(a, b, c, d)
Definition port.h:496
static int fb(int x)
static unsigned hash(unsigned *uv, int n)
Definition rege_dfa.c:724
ForkNumber
Definition relpath.h:56
io_callback_fn io_callback
char data[BUFSIZE]
BlockRefTableKey key
BlockNumber limit_block
BlockRefTableChunk * chunk_data
RelFileLocator rlocator
Definition blkreftable.c:49
ForkNumber forknum
Definition blkreftable.c:50
BlockRefTableBuffer buffer
uint16 chunk_data[MAX_ENTRIES_PER_CHUNK]
report_error_fn error_callback
BlockRefTableBuffer buffer
blockreftable_hash * hash
MemoryContext mcxt

◆ SH_KEY

#define SH_KEY   key

Definition at line 125 of file blkreftable.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   BlockRefTableKey

Definition at line 124 of file blkreftable.c.

◆ SH_PREFIX

#define SH_PREFIX   blockreftable

Definition at line 122 of file blkreftable.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 129 of file blkreftable.c.

Typedef Documentation

◆ BlockRefTableBuffer

◆ BlockRefTableChunk

Definition at line 82 of file blkreftable.c.

◆ BlockRefTableKey

◆ BlockRefTableSerializedEntry

Function Documentation

◆ BlockRefTableComparator()

static int BlockRefTableComparator ( const void a,
const void b 
)
static

Definition at line 1165 of file blkreftable.c.

1166{
1169
1170 if (sa->rlocator.spcOid > sb->rlocator.spcOid)
1171 return 1;
1172 if (sa->rlocator.spcOid < sb->rlocator.spcOid)
1173 return -1;
1174
1175 if (sa->rlocator.dbOid > sb->rlocator.dbOid)
1176 return 1;
1177 if (sa->rlocator.dbOid < sb->rlocator.dbOid)
1178 return -1;
1179
1180 if (sa->rlocator.relNumber > sb->rlocator.relNumber)
1181 return 1;
1182 if (sa->rlocator.relNumber < sb->rlocator.relNumber)
1183 return -1;
1184
1185 if (sa->forknum > sb->forknum)
1186 return 1;
1187 if (sa->forknum < sb->forknum)
1188 return -1;
1189
1190 return 0;
1191}

References a, b, and fb().

Referenced by WriteBlockRefTable().

◆ BlockRefTableEntryGetBlocks()

int BlockRefTableEntryGetBlocks ( BlockRefTableEntry entry,
BlockNumber  start_blkno,
BlockNumber  stop_blkno,
BlockNumber blocks,
int  nblocks 
)

Definition at line 369 of file blkreftable.c.

374{
378 int nresults = 0;
379
380 Assert(entry != NULL);
381
382 /*
383 * Figure out which chunks could potentially contain blocks of interest.
384 *
385 * We need to be careful about overflow here, because stop_blkno could be
386 * InvalidBlockNumber or something very close to it.
387 */
390 if ((stop_blkno % BLOCKS_PER_CHUNK) != 0)
391 ++stop_chunkno;
392 if (stop_chunkno > entry->nchunks)
393 stop_chunkno = entry->nchunks;
394
395 /*
396 * Loop over chunks.
397 */
399 {
400 uint16 chunk_usage = entry->chunk_usage[chunkno];
401 BlockRefTableChunk chunk_data = entry->chunk_data[chunkno];
402 unsigned start_offset = 0;
403 unsigned stop_offset = BLOCKS_PER_CHUNK;
404
405 /*
406 * If the start and/or stop block number falls within this chunk, the
407 * whole chunk may not be of interest. Figure out which portion we
408 * care about, if it's not the whole thing.
409 */
410 if (chunkno == start_chunkno)
412 if (chunkno == stop_chunkno - 1)
413 {
417 }
418
419 /*
420 * Handling differs depending on whether this is an array of offsets
421 * or a bitmap.
422 */
423 if (chunk_usage == MAX_ENTRIES_PER_CHUNK)
424 {
425 unsigned i;
426
427 /* It's a bitmap, so test every relevant bit. */
428 for (i = start_offset; i < stop_offset; ++i)
429 {
430 uint16 w = chunk_data[i / BLOCKS_PER_ENTRY];
431
432 if ((w & (1 << (i % BLOCKS_PER_ENTRY))) != 0)
433 {
435
436 blocks[nresults++] = blkno;
437
438 /* Early exit if we run out of output space. */
439 if (nresults == nblocks)
440 return nresults;
441 }
442 }
443 }
444 else
445 {
446 unsigned i;
447
448 /* It's an array of offsets, so check each one. */
449 for (i = 0; i < chunk_usage; ++i)
450 {
451 uint16 offset = chunk_data[i];
452
453 if (offset >= start_offset && offset < stop_offset)
454 {
455 BlockNumber blkno = chunkno * BLOCKS_PER_CHUNK + offset;
456
457 blocks[nresults++] = blkno;
458
459 /* Early exit if we run out of output space. */
460 if (nresults == nblocks)
461 return nresults;
462 }
463 }
464 }
465 }
466
467 return nresults;
468}

References Assert, BLOCKS_PER_CHUNK, BLOCKS_PER_ENTRY, BlockRefTableEntry::chunk_data, BlockRefTableEntry::chunk_usage, fb(), i, MAX_ENTRIES_PER_CHUNK, and BlockRefTableEntry::nchunks.

Referenced by GetFileBackupMethod().

◆ BlockRefTableEntryMarkBlockModified()

void BlockRefTableEntryMarkBlockModified ( BlockRefTableEntry entry,
ForkNumber  forknum,
BlockNumber  blknum 
)

Definition at line 979 of file blkreftable.c.

982{
983 unsigned chunkno;
984 unsigned chunkoffset;
985 unsigned i;
986
987 /*
988 * Which chunk should store the state of this block? And what is the
989 * offset of this block relative to the start of that chunk?
990 */
993
994 /*
995 * If 'nchunks' isn't big enough for us to be able to represent the state
996 * of this block, we need to enlarge our arrays.
997 */
998 if (chunkno >= entry->nchunks)
999 {
1000 unsigned max_chunks;
1001 unsigned extra_chunks;
1002
1003 /*
1004 * New array size is a power of 2, at least 16, big enough so that
1005 * chunkno will be a valid array index.
1006 */
1007 max_chunks = Max(16, entry->nchunks);
1008 while (max_chunks < chunkno + 1)
1009 max_chunks *= 2;
1010 extra_chunks = max_chunks - entry->nchunks;
1011
1012 if (entry->nchunks == 0)
1013 {
1017 }
1018 else
1019 {
1020 entry->chunk_size = repalloc(entry->chunk_size,
1021 sizeof(uint16) * max_chunks);
1022 memset(&entry->chunk_size[entry->nchunks], 0,
1023 extra_chunks * sizeof(uint16));
1024 entry->chunk_usage = repalloc(entry->chunk_usage,
1025 sizeof(uint16) * max_chunks);
1026 memset(&entry->chunk_usage[entry->nchunks], 0,
1027 extra_chunks * sizeof(uint16));
1028 entry->chunk_data = repalloc(entry->chunk_data,
1029 sizeof(BlockRefTableChunk) * max_chunks);
1030 memset(&entry->chunk_data[entry->nchunks], 0,
1032 }
1033 entry->nchunks = max_chunks;
1034 }
1035
1036 /*
1037 * If the chunk that covers this block number doesn't exist yet, create it
1038 * as an array and add the appropriate offset to it. We make it pretty
1039 * small initially, because there might only be 1 or a few block
1040 * references in this chunk and we don't want to use up too much memory.
1041 */
1042 if (entry->chunk_size[chunkno] == 0)
1043 {
1044 entry->chunk_data[chunkno] =
1047 entry->chunk_data[chunkno][0] = chunkoffset;
1048 entry->chunk_usage[chunkno] = 1;
1049 return;
1050 }
1051
1052 /*
1053 * If the number of entries in this chunk is already maximum, it must be a
1054 * bitmap. Just set the appropriate bit.
1055 */
1057 {
1058 BlockRefTableChunk chunk = entry->chunk_data[chunkno];
1059
1060 chunk[chunkoffset / BLOCKS_PER_ENTRY] |=
1062 return;
1063 }
1064
1065 /*
1066 * There is an existing chunk and it's in array format. Let's find out
1067 * whether it already has an entry for this block. If so, we do not need
1068 * to do anything.
1069 */
1070 for (i = 0; i < entry->chunk_usage[chunkno]; ++i)
1071 {
1072 if (entry->chunk_data[chunkno][i] == chunkoffset)
1073 return;
1074 }
1075
1076 /*
1077 * If the number of entries currently used is one less than the maximum,
1078 * it's time to convert to bitmap format.
1079 */
1080 if (entry->chunk_usage[chunkno] == MAX_ENTRIES_PER_CHUNK - 1)
1081 {
1083 unsigned j;
1084
1085 /* Allocate a new chunk. */
1087
1088 /* Set the bit for each existing entry. */
1089 for (j = 0; j < entry->chunk_usage[chunkno]; ++j)
1090 {
1091 unsigned coff = entry->chunk_data[chunkno][j];
1092
1094 1 << (coff % BLOCKS_PER_ENTRY);
1095 }
1096
1097 /* Set the bit for the new entry. */
1100
1101 /* Swap the new chunk into place and update metadata. */
1102 pfree(entry->chunk_data[chunkno]);
1103 entry->chunk_data[chunkno] = newchunk;
1106 return;
1107 }
1108
1109 /*
1110 * OK, we currently have an array, and we don't need to convert to a
1111 * bitmap, but we do need to add a new element. If there's not enough
1112 * room, we'll have to expand the array.
1113 */
1114 if (entry->chunk_usage[chunkno] == entry->chunk_size[chunkno])
1115 {
1116 unsigned newsize = entry->chunk_size[chunkno] * 2;
1117
1119 entry->chunk_data[chunkno] = repalloc(entry->chunk_data[chunkno],
1120 newsize * sizeof(uint16));
1121 entry->chunk_size[chunkno] = newsize;
1122 }
1123
1124 /* Now we can add the new entry. */
1125 entry->chunk_data[chunkno][entry->chunk_usage[chunkno]] =
1127 entry->chunk_usage[chunkno]++;
1128}

References Assert, BLOCKS_PER_CHUNK, BLOCKS_PER_ENTRY, BlockRefTableEntry::chunk_data, BlockRefTableEntry::chunk_size, BlockRefTableEntry::chunk_usage, fb(), i, INITIAL_ENTRIES_PER_CHUNK, j, Max, MAX_ENTRIES_PER_CHUNK, BlockRefTableEntry::nchunks, palloc0(), palloc0_array, palloc_array, pfree(), and repalloc().

Referenced by BlockRefTableMarkBlockModified().

◆ BlockRefTableEntrySetLimitBlock()

void BlockRefTableEntrySetLimitBlock ( BlockRefTableEntry entry,
BlockNumber  limit_block 
)

Definition at line 908 of file blkreftable.c.

910{
911 unsigned chunkno;
912 unsigned limit_chunkno;
913 unsigned limit_chunkoffset;
915
916 /* If we already have an equal or lower limit block, do nothing. */
917 if (limit_block >= entry->limit_block)
918 return;
919
920 /* Record the new limit block value. */
921 entry->limit_block = limit_block;
922
923 /*
924 * Figure out which chunk would store the state of the new limit block,
925 * and which offset within that chunk.
926 */
927 limit_chunkno = limit_block / BLOCKS_PER_CHUNK;
928 limit_chunkoffset = limit_block % BLOCKS_PER_CHUNK;
929
930 /*
931 * If the number of chunks is not large enough for any blocks with equal
932 * or higher block numbers to exist, then there is nothing further to do.
933 */
934 if (limit_chunkno >= entry->nchunks)
935 return;
936
937 /* Discard entire contents of any higher-numbered chunks. */
938 for (chunkno = limit_chunkno + 1; chunkno < entry->nchunks; ++chunkno)
939 entry->chunk_usage[chunkno] = 0;
940
941 /*
942 * Next, we need to discard any offsets within the chunk that would
943 * contain the limit_block. We must handle this differently depending on
944 * whether the chunk that would contain limit_block is a bitmap or an
945 * array of offsets.
946 */
949 {
950 unsigned chunkoffset;
951
952 /* It's a bitmap. Unset bits. */
954 ++chunkoffset)
956 ~(1 << (chunkoffset % BLOCKS_PER_ENTRY));
957 }
958 else
959 {
960 unsigned i,
961 j = 0;
962
963 /* It's an offset array. Filter out large offsets. */
964 for (i = 0; i < entry->chunk_usage[limit_chunkno]; ++i)
965 {
966 Assert(j <= i);
969 }
970 Assert(j <= entry->chunk_usage[limit_chunkno]);
971 entry->chunk_usage[limit_chunkno] = j;
972 }
973}

References Assert, BLOCKS_PER_CHUNK, BLOCKS_PER_ENTRY, BlockRefTableEntry::chunk_data, BlockRefTableEntry::chunk_usage, fb(), i, j, BlockRefTableEntry::limit_block, MAX_ENTRIES_PER_CHUNK, and BlockRefTableEntry::nchunks.

Referenced by BlockRefTableSetLimitBlock().

◆ BlockRefTableFileTerminate()

static void BlockRefTableFileTerminate ( BlockRefTableBuffer buffer)
static

Definition at line 1305 of file blkreftable.c.

1306{
1308 pg_crc32c crc;
1309
1310 /* Write a sentinel indicating that there are no more entries. */
1311 BlockRefTableWrite(buffer, &zentry,
1313
1314 /*
1315 * Writing the checksum will perturb the ongoing checksum calculation, so
1316 * copy the state first and finalize the computation using the copy.
1317 */
1318 crc = buffer->crc;
1319 FIN_CRC32C(crc);
1320 BlockRefTableWrite(buffer, &crc, sizeof(pg_crc32c));
1321
1322 /* Flush any leftover data out of our buffer. */
1323 BlockRefTableFlush(buffer);
1324}

References BlockRefTableFlush(), BlockRefTableWrite(), BlockRefTableBuffer::crc, crc, fb(), and FIN_CRC32C.

Referenced by DestroyBlockRefTableWriter(), and WriteBlockRefTable().

◆ BlockRefTableFlush()

static void BlockRefTableFlush ( BlockRefTableBuffer buffer)
static

Definition at line 1197 of file blkreftable.c.

1198{
1199 buffer->io_callback(buffer->io_callback_arg, buffer->data, buffer->used);
1200 buffer->used = 0;
1201}

References BlockRefTableBuffer::data, BlockRefTableBuffer::io_callback, BlockRefTableBuffer::io_callback_arg, and BlockRefTableBuffer::used.

Referenced by BlockRefTableFileTerminate().

◆ BlockRefTableFreeEntry()

void BlockRefTableFreeEntry ( BlockRefTableEntry entry)

Definition at line 1135 of file blkreftable.c.

1136{
1137 if (entry->chunk_size != NULL)
1138 {
1139 pfree(entry->chunk_size);
1140 entry->chunk_size = NULL;
1141 }
1142
1143 if (entry->chunk_usage != NULL)
1144 {
1145 pfree(entry->chunk_usage);
1146 entry->chunk_usage = NULL;
1147 }
1148
1149 if (entry->chunk_data != NULL)
1150 {
1151 pfree(entry->chunk_data);
1152 entry->chunk_data = NULL;
1153 }
1154
1155 pfree(entry);
1156}

References BlockRefTableEntry::chunk_data, BlockRefTableEntry::chunk_size, BlockRefTableEntry::chunk_usage, fb(), and pfree().

◆ BlockRefTableGetEntry()

BlockRefTableEntry * BlockRefTableGetEntry ( BlockRefTable brtab,
const RelFileLocator rlocator,
ForkNumber  forknum,
BlockNumber limit_block 
)

Definition at line 340 of file blkreftable.c.

342{
343 BlockRefTableKey key = {0}; /* make sure any padding is zero */
344 BlockRefTableEntry *entry;
345
346 Assert(limit_block != NULL);
347
348 memcpy(&key.rlocator, rlocator, sizeof(RelFileLocator));
349 key.forknum = forknum;
350 entry = blockreftable_lookup(brtab->hash, key);
351
352 if (entry != NULL)
353 *limit_block = entry->limit_block;
354
355 return entry;
356}

References Assert, fb(), BlockRefTable::hash, BlockRefTableEntry::limit_block, and memcpy().

Referenced by GetFileBackupMethod().

◆ BlockRefTableMarkBlockModified()

void BlockRefTableMarkBlockModified ( BlockRefTable brtab,
const RelFileLocator rlocator,
ForkNumber  forknum,
BlockNumber  blknum 
)

Definition at line 297 of file blkreftable.c.

301{
303 BlockRefTableKey key = {0}; /* make sure any padding is zero */
304 bool found;
305#ifndef FRONTEND
306 MemoryContext oldcontext = MemoryContextSwitchTo(brtab->mcxt);
307#endif
308
309 memcpy(&key.rlocator, rlocator, sizeof(RelFileLocator));
310 key.forknum = forknum;
311 brtentry = blockreftable_insert(brtab->hash, key, &found);
312
313 if (!found)
314 {
315 /*
316 * We want to set the initial limit block value to something higher
317 * than any legal block number. InvalidBlockNumber fits the bill.
318 */
319 brtentry->limit_block = InvalidBlockNumber;
320 brtentry->nchunks = 0;
321 brtentry->chunk_size = NULL;
322 brtentry->chunk_usage = NULL;
323 brtentry->chunk_data = NULL;
324 }
325
327
328#ifndef FRONTEND
329 MemoryContextSwitchTo(oldcontext);
330#endif
331}

References BlockRefTableEntryMarkBlockModified(), fb(), BlockRefTable::hash, InvalidBlockNumber, BlockRefTable::mcxt, memcpy(), and MemoryContextSwitchTo().

Referenced by PrepareForIncrementalBackup(), and SummarizeWAL().

◆ BlockRefTableRead()

static void BlockRefTableRead ( BlockRefTableReader reader,
void data,
int  length 
)
static

Definition at line 1209 of file blkreftable.c.

1210{
1211 BlockRefTableBuffer *buffer = &reader->buffer;
1212
1213 /* Loop until read is fully satisfied. */
1214 while (length > 0)
1215 {
1216 if (buffer->cursor < buffer->used)
1217 {
1218 /*
1219 * If any buffered data is available, use that to satisfy as much
1220 * of the request as possible.
1221 */
1222 int bytes_to_copy = Min(length, buffer->used - buffer->cursor);
1223
1224 memcpy(data, &buffer->data[buffer->cursor], bytes_to_copy);
1225 COMP_CRC32C(buffer->crc, &buffer->data[buffer->cursor],
1227 buffer->cursor += bytes_to_copy;
1228 data = ((char *) data) + bytes_to_copy;
1229 length -= bytes_to_copy;
1230 }
1231 else if (length >= BUFSIZE)
1232 {
1233 /*
1234 * If the request length is long, read directly into caller's
1235 * buffer.
1236 */
1237 int bytes_read;
1238
1239 bytes_read = buffer->io_callback(buffer->io_callback_arg,
1240 data, length);
1241 COMP_CRC32C(buffer->crc, data, bytes_read);
1242 data = ((char *) data) + bytes_read;
1243 length -= bytes_read;
1244
1245 /* If we didn't get anything, that's bad. */
1246 if (bytes_read == 0)
1247 reader->error_callback(reader->error_callback_arg,
1248 "file \"%s\" ends unexpectedly",
1249 reader->error_filename);
1250 }
1251 else
1252 {
1253 /*
1254 * Refill our buffer.
1255 */
1256 buffer->used = buffer->io_callback(buffer->io_callback_arg,
1257 buffer->data, BUFSIZE);
1258 buffer->cursor = 0;
1259
1260 /* If we didn't get anything, that's bad. */
1261 if (buffer->used == 0)
1262 reader->error_callback(reader->error_callback_arg,
1263 "file \"%s\" ends unexpectedly",
1264 reader->error_filename);
1265 }
1266 }
1267}

References BlockRefTableReader::buffer, BUFSIZE, COMP_CRC32C, BlockRefTableBuffer::crc, BlockRefTableBuffer::cursor, BlockRefTableBuffer::data, data, BlockRefTableReader::error_callback, BlockRefTableReader::error_callback_arg, BlockRefTableReader::error_filename, fb(), BlockRefTableBuffer::io_callback, BlockRefTableBuffer::io_callback_arg, memcpy(), Min, and BlockRefTableBuffer::used.

Referenced by BlockRefTableReaderGetBlocks(), BlockRefTableReaderNextRelation(), and CreateBlockRefTableReader().

◆ BlockRefTableReaderGetBlocks()

unsigned BlockRefTableReaderGetBlocks ( BlockRefTableReader reader,
BlockNumber blocks,
int  nblocks 
)

Definition at line 703 of file blkreftable.c.

706{
707 unsigned blocks_found = 0;
708
709 /* Must provide space for at least one block number to be returned. */
710 Assert(nblocks > 0);
711
712 /* Loop collecting blocks to return to caller. */
713 for (;;)
714 {
716
717 /*
718 * If we've read at least one chunk, maybe it contains some block
719 * numbers that could satisfy caller's request.
720 */
721 if (reader->consumed_chunks > 0)
722 {
723 uint32 chunkno = reader->consumed_chunks - 1;
724 uint16 chunk_size = reader->chunk_size[chunkno];
725
726 if (chunk_size == MAX_ENTRIES_PER_CHUNK)
727 {
728 /* Bitmap format, so search for bits that are set. */
729 while (reader->chunk_position < BLOCKS_PER_CHUNK &&
730 blocks_found < nblocks)
731 {
733 uint16 w;
734
736 if ((w & (1u << (chunkoffset % BLOCKS_PER_ENTRY))) != 0)
737 blocks[blocks_found++] =
739 ++reader->chunk_position;
740 }
741 }
742 else
743 {
744 /* Not in bitmap format, so each entry is a 2-byte offset. */
745 while (reader->chunk_position < chunk_size &&
746 blocks_found < nblocks)
747 {
749 + reader->chunk_data[reader->chunk_position];
750 ++reader->chunk_position;
751 }
752 }
753 }
754
755 /* We found enough blocks, so we're done. */
756 if (blocks_found >= nblocks)
757 break;
758
759 /*
760 * We didn't find enough blocks, so we must need the next chunk. If
761 * there are none left, though, then we're done anyway.
762 */
763 if (reader->consumed_chunks == reader->total_chunks)
764 break;
765
766 /*
767 * Read data for next chunk and reset scan position to beginning of
768 * chunk. Note that the next chunk might be empty, in which case we
769 * consume the chunk without actually consuming any bytes from the
770 * underlying file.
771 */
772 next_chunk_size = reader->chunk_size[reader->consumed_chunks];
773 if (next_chunk_size > 0)
774 BlockRefTableRead(reader, reader->chunk_data,
775 next_chunk_size * sizeof(uint16));
776 ++reader->consumed_chunks;
777 reader->chunk_position = 0;
778 }
779
780 return blocks_found;
781}

References Assert, BlockRefTableRead(), BLOCKS_PER_CHUNK, BLOCKS_PER_ENTRY, BlockRefTableReader::chunk_data, BlockRefTableReader::chunk_position, BlockRefTableReader::chunk_size, BlockRefTableReader::consumed_chunks, fb(), MAX_ENTRIES_PER_CHUNK, and BlockRefTableReader::total_chunks.

Referenced by dump_one_relation(), pg_wal_summary_contents(), and PrepareForIncrementalBackup().

◆ BlockRefTableReaderNextRelation()

bool BlockRefTableReaderNextRelation ( BlockRefTableReader reader,
RelFileLocator rlocator,
ForkNumber forknum,
BlockNumber limit_block 
)

Definition at line 613 of file blkreftable.c.

617{
620
621 /*
622 * Sanity check: caller must read all blocks from all chunks before moving
623 * on to the next relation.
624 */
625 Assert(reader->total_chunks == reader->consumed_chunks);
626
627 /* Read serialized entry. */
628 BlockRefTableRead(reader, &sentry,
630
631 /*
632 * If we just read the sentinel entry indicating that we've reached the
633 * end, read and check the CRC.
634 */
635 if (memcmp(&sentry, &zentry, sizeof(BlockRefTableSerializedEntry)) == 0)
636 {
639
640 /*
641 * We want to know the CRC of the file excluding the 4-byte CRC
642 * itself, so copy the current value of the CRC accumulator before
643 * reading those bytes, and use the copy to finalize the calculation.
644 */
645 expected_crc = reader->buffer.crc;
647
648 /* Now we can read the actual value. */
649 BlockRefTableRead(reader, &actual_crc, sizeof(pg_crc32c));
650
651 /* Throw an error if there is a mismatch. */
653 reader->error_callback(reader->error_callback_arg,
654 "file \"%s\" has wrong checksum: expected %08X, found %08X",
656
657 return false;
658 }
659
660 /*
661 * Sanity-check the nchunks value. In the backend, palloc_array would
662 * enforce this anyway (with a more generic error message); but in
663 * frontend it would not, potentially allowing BlockRefTableRead's length
664 * parameter to overflow.
665 */
666 if (sentry.nchunks > MaxAllocSize / sizeof(uint16))
667 {
668 reader->error_callback(reader->error_callback_arg,
669 "file \"%s\" has oversized chunk size array",
670 reader->error_filename);
671 return false;
672 }
673
674 /* Read chunk size array. */
675 if (reader->chunk_size != NULL)
676 pfree(reader->chunk_size);
677 reader->chunk_size = palloc_array(uint16, sentry.nchunks);
678 BlockRefTableRead(reader, reader->chunk_size,
679 sentry.nchunks * sizeof(uint16));
680
681 /* Set up for chunk scan. */
682 reader->total_chunks = sentry.nchunks;
683 reader->consumed_chunks = 0;
684
685 /* Return data to caller. */
686 memcpy(rlocator, &sentry.rlocator, sizeof(RelFileLocator));
687 *forknum = sentry.forknum;
688 *limit_block = sentry.limit_block;
689 return true;
690}

References Assert, BlockRefTableRead(), BlockRefTableReader::buffer, BlockRefTableReader::chunk_size, BlockRefTableReader::consumed_chunks, BlockRefTableBuffer::crc, EQ_CRC32C, BlockRefTableReader::error_callback, BlockRefTableReader::error_callback_arg, BlockRefTableReader::error_filename, fb(), FIN_CRC32C, MaxAllocSize, memcpy(), palloc_array, pfree(), and BlockRefTableReader::total_chunks.

Referenced by main(), pg_wal_summary_contents(), and PrepareForIncrementalBackup().

◆ BlockRefTableSetLimitBlock()

void BlockRefTableSetLimitBlock ( BlockRefTable brtab,
const RelFileLocator rlocator,
ForkNumber  forknum,
BlockNumber  limit_block 
)

Definition at line 262 of file blkreftable.c.

266{
268 BlockRefTableKey key = {0}; /* make sure any padding is zero */
269 bool found;
270
271 memcpy(&key.rlocator, rlocator, sizeof(RelFileLocator));
272 key.forknum = forknum;
273 brtentry = blockreftable_insert(brtab->hash, key, &found);
274
275 if (!found)
276 {
277 /*
278 * We have no existing data about this relation fork, so just record
279 * the limit_block value supplied by the caller, and make sure other
280 * parts of the entry are properly initialized.
281 */
282 brtentry->limit_block = limit_block;
283 brtentry->nchunks = 0;
284 brtentry->chunk_size = NULL;
285 brtentry->chunk_usage = NULL;
286 brtentry->chunk_data = NULL;
287 return;
288 }
289
291}

References BlockRefTableEntrySetLimitBlock(), fb(), BlockRefTable::hash, and memcpy().

Referenced by PrepareForIncrementalBackup(), SummarizeDbaseRecord(), SummarizeSmgrRecord(), and SummarizeXactRecord().

◆ BlockRefTableWrite()

static void BlockRefTableWrite ( BlockRefTableBuffer buffer,
void data,
int  length 
)
static

Definition at line 1274 of file blkreftable.c.

1275{
1276 /* Update running CRC calculation. */
1277 COMP_CRC32C(buffer->crc, data, length);
1278
1279 /* If the new data can't fit into the buffer, flush the buffer. */
1280 if (buffer->used + length > BUFSIZE)
1281 {
1282 buffer->io_callback(buffer->io_callback_arg, buffer->data,
1283 buffer->used);
1284 buffer->used = 0;
1285 }
1286
1287 /* If the new data would fill the buffer, or more, write it directly. */
1288 if (length >= BUFSIZE)
1289 {
1290 buffer->io_callback(buffer->io_callback_arg, data, length);
1291 return;
1292 }
1293
1294 /* Otherwise, copy the new data into the buffer. */
1295 memcpy(&buffer->data[buffer->used], data, length);
1296 buffer->used += length;
1297 Assert(buffer->used <= BUFSIZE);
1298}

References Assert, BUFSIZE, COMP_CRC32C, BlockRefTableBuffer::crc, BlockRefTableBuffer::data, data, BlockRefTableBuffer::io_callback, BlockRefTableBuffer::io_callback_arg, memcpy(), and BlockRefTableBuffer::used.

Referenced by BlockRefTableFileTerminate(), BlockRefTableWriteEntry(), CreateBlockRefTableWriter(), and WriteBlockRefTable().

◆ BlockRefTableWriteEntry()

void BlockRefTableWriteEntry ( BlockRefTableWriter writer,
BlockRefTableEntry entry 
)

Definition at line 831 of file blkreftable.c.

832{
834 unsigned j;
835
836 /* Convert to serialized entry format. */
837 sentry.rlocator = entry->key.rlocator;
838 sentry.forknum = entry->key.forknum;
839 sentry.limit_block = entry->limit_block;
840 sentry.nchunks = entry->nchunks;
841
842 /* Trim trailing zero entries. */
843 while (sentry.nchunks > 0 && entry->chunk_usage[sentry.nchunks - 1] == 0)
844 sentry.nchunks--;
845
846 /* Write the serialized entry itself. */
849
850 /* Write the untruncated portion of the chunk length array. */
851 if (sentry.nchunks != 0)
852 BlockRefTableWrite(&writer->buffer, entry->chunk_usage,
853 sentry.nchunks * sizeof(uint16));
854
855 /* Write the contents of each chunk. */
856 for (j = 0; j < entry->nchunks; ++j)
857 {
858 if (entry->chunk_usage[j] == 0)
859 continue;
860 BlockRefTableWrite(&writer->buffer, entry->chunk_data[j],
861 entry->chunk_usage[j] * sizeof(uint16));
862 }
863}

References BlockRefTableWrite(), BlockRefTableEntry::chunk_data, BlockRefTableEntry::chunk_usage, fb(), BlockRefTableKey::forknum, j, BlockRefTableEntry::key, BlockRefTableEntry::limit_block, BlockRefTableEntry::nchunks, BlockRefTableKey::rlocator, and BlockRefTableSerializedEntry::rlocator.

◆ CreateBlockRefTableEntry()

BlockRefTableEntry * CreateBlockRefTableEntry ( RelFileLocator  rlocator,
ForkNumber  forknum 
)

Definition at line 889 of file blkreftable.c.

890{
892
893 memcpy(&entry->key.rlocator, &rlocator, sizeof(RelFileLocator));
894 entry->key.forknum = forknum;
896
897 return entry;
898}

References BlockRefTableKey::forknum, InvalidBlockNumber, BlockRefTableEntry::key, BlockRefTableEntry::limit_block, memcpy(), palloc0_object, and BlockRefTableKey::rlocator.

◆ CreateBlockRefTableReader()

BlockRefTableReader * CreateBlockRefTableReader ( io_callback_fn  read_callback,
void read_callback_arg,
char error_filename,
report_error_fn  error_callback,
void error_callback_arg 
)

Definition at line 577 of file blkreftable.c.

582{
583 BlockRefTableReader *reader;
584 uint32 magic;
585
586 /* Initialize data structure. */
590 reader->error_filename = error_filename;
591 reader->error_callback = error_callback;
592 reader->error_callback_arg = error_callback_arg;
593 INIT_CRC32C(reader->buffer.crc);
594
595 /* Verify magic number. */
596 BlockRefTableRead(reader, &magic, sizeof(uint32));
597 if (magic != BLOCKREFTABLE_MAGIC)
598 error_callback(error_callback_arg,
599 "file \"%s\" has wrong magic number: expected %u, found %u",
600 error_filename,
601 BLOCKREFTABLE_MAGIC, magic);
602
603 return reader;
604}

References BLOCKREFTABLE_MAGIC, BlockRefTableRead(), BlockRefTableReader::buffer, BlockRefTableBuffer::crc, BlockRefTableReader::error_callback, BlockRefTableReader::error_callback_arg, BlockRefTableReader::error_filename, fb(), INIT_CRC32C, BlockRefTableBuffer::io_callback, BlockRefTableBuffer::io_callback_arg, and palloc0_object.

Referenced by main(), pg_wal_summary_contents(), and PrepareForIncrementalBackup().

◆ CreateBlockRefTableWriter()

BlockRefTableWriter * CreateBlockRefTableWriter ( io_callback_fn  write_callback,
void write_callback_arg 
)

Definition at line 804 of file blkreftable.c.

806{
809
810 /* Prepare buffer and CRC check and save callbacks. */
812 writer->buffer.io_callback = write_callback;
813 writer->buffer.io_callback_arg = write_callback_arg;
814 INIT_CRC32C(writer->buffer.crc);
815
816 /* Write magic number. */
817 BlockRefTableWrite(&writer->buffer, &magic, sizeof(uint32));
818
819 return writer;
820}

References BLOCKREFTABLE_MAGIC, BlockRefTableWrite(), fb(), INIT_CRC32C, and palloc0_object.

◆ CreateEmptyBlockRefTable()

BlockRefTable * CreateEmptyBlockRefTable ( void  )

Definition at line 235 of file blkreftable.c.

236{
238
239 /*
240 * Even completely empty database has a few hundred relation forks, so it
241 * seems best to size the hash on the assumption that we're going to have
242 * at least a few thousand entries.
243 */
244#ifdef FRONTEND
245 brtab->hash = blockreftable_create(4096, NULL);
246#else
247 brtab->mcxt = CurrentMemoryContext;
248 brtab->hash = blockreftable_create(brtab->mcxt, 4096, NULL);
249#endif
250
251 return brtab;
252}

References CurrentMemoryContext, fb(), BlockRefTable::hash, BlockRefTable::mcxt, and palloc_object.

◆ DestroyBlockRefTableReader()

void DestroyBlockRefTableReader ( BlockRefTableReader reader)

Definition at line 787 of file blkreftable.c.

788{
789 if (reader->chunk_size != NULL)
790 {
791 pfree(reader->chunk_size);
792 reader->chunk_size = NULL;
793 }
794 pfree(reader);
795}

References BlockRefTableReader::chunk_size, fb(), and pfree().

Referenced by main(), pg_wal_summary_contents(), and PrepareForIncrementalBackup().

◆ DestroyBlockRefTableWriter()

void DestroyBlockRefTableWriter ( BlockRefTableWriter writer)

Definition at line 869 of file blkreftable.c.

870{
872 pfree(writer);
873}

References BlockRefTableFileTerminate(), fb(), and pfree().

◆ WriteBlockRefTable()

void WriteBlockRefTable ( BlockRefTable brtab,
io_callback_fn  write_callback,
void write_callback_arg 
)

Definition at line 474 of file blkreftable.c.

477{
479 BlockRefTableBuffer buffer;
481
482 /* Prepare buffer. */
483 memset(&buffer, 0, sizeof(BlockRefTableBuffer));
486 INIT_CRC32C(buffer.crc);
487
488 /* Write magic number. */
489 BlockRefTableWrite(&buffer, &magic, sizeof(uint32));
490
491 /* Write the entries, assuming there are some. */
492 if (brtab->hash->members > 0)
493 {
494 unsigned i = 0;
497
498 /* Extract entries into serializable format and sort them. */
499 sdata =
502 while ((brtentry = blockreftable_iterate(brtab->hash, &it)) != NULL)
503 {
505
506 sentry->rlocator = brtentry->key.rlocator;
507 sentry->forknum = brtentry->key.forknum;
508 sentry->limit_block = brtentry->limit_block;
509 sentry->nchunks = brtentry->nchunks;
510
511 /* trim trailing zero entries */
512 while (sentry->nchunks > 0 &&
513 brtentry->chunk_usage[sentry->nchunks - 1] == 0)
514 sentry->nchunks--;
515 }
516 Assert(i == brtab->hash->members);
519
520 /* Loop over entries in sorted order and serialize each one. */
521 for (i = 0; i < brtab->hash->members; ++i)
522 {
524 BlockRefTableKey key = {0}; /* make sure any padding is zero */
525 unsigned j;
526
527 /* Write the serialized entry itself. */
528 BlockRefTableWrite(&buffer, sentry,
530
531 /* Look up the original entry so we can access the chunks. */
532 memcpy(&key.rlocator, &sentry->rlocator, sizeof(RelFileLocator));
533 key.forknum = sentry->forknum;
534 brtentry = blockreftable_lookup(brtab->hash, key);
535 Assert(brtentry != NULL);
536
537 /* Write the untruncated portion of the chunk length array. */
538 if (sentry->nchunks != 0)
539 BlockRefTableWrite(&buffer, brtentry->chunk_usage,
540 sentry->nchunks * sizeof(uint16));
541
542 /* Write the contents of each chunk. */
543 for (j = 0; j < brtentry->nchunks; ++j)
544 {
545 if (brtentry->chunk_usage[j] == 0)
546 continue;
547 BlockRefTableWrite(&buffer, brtentry->chunk_data[j],
548 brtentry->chunk_usage[j] * sizeof(uint16));
549 }
550 }
551 }
552
553 /* Write out appropriate terminator and CRC and flush buffer. */
555}

References Assert, BLOCKREFTABLE_MAGIC, BlockRefTableComparator(), BlockRefTableFileTerminate(), BlockRefTableWrite(), BlockRefTableBuffer::crc, fb(), BlockRefTable::hash, i, INIT_CRC32C, BlockRefTableBuffer::io_callback, BlockRefTableBuffer::io_callback_arg, j, memcpy(), palloc_array, qsort, and BlockRefTableSerializedEntry::rlocator.

Referenced by SummarizeWAL().