PostgreSQL Source Code git master
Loading...
Searching...
No Matches
blkreftable.h File Reference
Include dependency graph for blkreftable.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define BLOCKREFTABLE_MAGIC   0x652b137b
 

Typedefs

typedef struct BlockRefTable BlockRefTable
 
typedef struct BlockRefTableEntry BlockRefTableEntry
 
typedef struct BlockRefTableReader BlockRefTableReader
 
typedef struct BlockRefTableWriter BlockRefTableWriter
 
typedef size_t(* io_callback_fn) (void *callback_arg, void *data, size_t length)
 
typedef void(* report_error_fn) (void *callback_arg, char *msg,...) pg_attribute_printf(2
 
typedef void(*) BlockRefTable CreateEmptyBlockRefTable) (void)
 

Functions

void BlockRefTableSetLimitBlock (BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber limit_block)
 
void BlockRefTableMarkBlockModified (BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum)
 
void WriteBlockRefTable (BlockRefTable *brtab, io_callback_fn write_callback, void *write_callback_arg)
 
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)
 
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

◆ BLOCKREFTABLE_MAGIC

#define BLOCKREFTABLE_MAGIC   0x652b137b

Definition at line 30 of file blkreftable.h.

Typedef Documentation

◆ BlockRefTable

Definition at line 32 of file blkreftable.h.

◆ BlockRefTableEntry

Definition at line 33 of file blkreftable.h.

◆ BlockRefTableReader

Definition at line 34 of file blkreftable.h.

◆ BlockRefTableWriter

Definition at line 35 of file blkreftable.h.

◆ CreateEmptyBlockRefTable

typedef void(*) BlockRefTable CreateEmptyBlockRefTable) (void)
extern

◆ io_callback_fn

typedef size_t(* io_callback_fn) (void *callback_arg, void *data, size_t length)

Definition at line 46 of file blkreftable.h.

◆ report_error_fn

typedef void(* report_error_fn) (void *callback_arg, char *msg,...) pg_attribute_printf(2

Definition at line 47 of file blkreftable.h.

Function Documentation

◆ BlockRefTableEntryGetBlocks()

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

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}
#define BLOCKS_PER_CHUNK
Definition blkreftable.c:78
#define BLOCKS_PER_ENTRY
Definition blkreftable.c:79
#define MAX_ENTRIES_PER_CHUNK
Definition blkreftable.c:80
uint16 * BlockRefTableChunk
Definition blkreftable.c:82
uint32 BlockNumber
Definition block.h:31
#define Assert(condition)
Definition c.h:1002
uint16_t uint16
Definition c.h:682
uint32_t uint32
Definition c.h:683
int i
Definition isn.c:77
static int fb(int x)
BlockRefTableChunk * chunk_data

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 
)
extern

Definition at line 1001 of file blkreftable.c.

1004{
1005 unsigned chunkno;
1006 unsigned chunkoffset;
1007 unsigned i;
1008
1009 /*
1010 * Which chunk should store the state of this block? And what is the
1011 * offset of this block relative to the start of that chunk?
1012 */
1015
1016 /*
1017 * If 'nchunks' isn't big enough for us to be able to represent the state
1018 * of this block, we need to enlarge our arrays.
1019 */
1020 if (chunkno >= entry->nchunks)
1021 {
1022 unsigned max_chunks;
1023 unsigned extra_chunks;
1024
1025 /*
1026 * New array size is a power of 2, at least 16, big enough so that
1027 * chunkno will be a valid array index.
1028 */
1029 max_chunks = Max(16, entry->nchunks);
1030 while (max_chunks < chunkno + 1)
1031 max_chunks *= 2;
1032 extra_chunks = max_chunks - entry->nchunks;
1033
1034 if (entry->nchunks == 0)
1035 {
1039 }
1040 else
1041 {
1042 entry->chunk_size = repalloc(entry->chunk_size,
1043 sizeof(uint16) * max_chunks);
1044 memset(&entry->chunk_size[entry->nchunks], 0,
1045 extra_chunks * sizeof(uint16));
1046 entry->chunk_usage = repalloc(entry->chunk_usage,
1047 sizeof(uint16) * max_chunks);
1048 memset(&entry->chunk_usage[entry->nchunks], 0,
1049 extra_chunks * sizeof(uint16));
1050 entry->chunk_data = repalloc(entry->chunk_data,
1051 sizeof(BlockRefTableChunk) * max_chunks);
1052 memset(&entry->chunk_data[entry->nchunks], 0,
1054 }
1055 entry->nchunks = max_chunks;
1056 }
1057
1058 /*
1059 * If the chunk that covers this block number doesn't exist yet, create it
1060 * as an array and add the appropriate offset to it. We make it pretty
1061 * small initially, because there might only be 1 or a few block
1062 * references in this chunk and we don't want to use up too much memory.
1063 */
1064 if (entry->chunk_size[chunkno] == 0)
1065 {
1066 entry->chunk_data[chunkno] =
1069 entry->chunk_data[chunkno][0] = chunkoffset;
1070 entry->chunk_usage[chunkno] = 1;
1071 return;
1072 }
1073
1074 /*
1075 * If the number of entries in this chunk is already maximum, it must be a
1076 * bitmap. Just set the appropriate bit.
1077 */
1079 {
1080 BlockRefTableChunk chunk = entry->chunk_data[chunkno];
1081
1082 chunk[chunkoffset / BLOCKS_PER_ENTRY] |=
1084 return;
1085 }
1086
1087 /*
1088 * There is an existing chunk and it's in array format. Let's find out
1089 * whether it already has an entry for this block. If so, we do not need
1090 * to do anything.
1091 */
1092 for (i = 0; i < entry->chunk_usage[chunkno]; ++i)
1093 {
1094 if (entry->chunk_data[chunkno][i] == chunkoffset)
1095 return;
1096 }
1097
1098 /*
1099 * If the number of entries currently used is one less than the maximum,
1100 * it's time to convert to bitmap format.
1101 */
1102 if (entry->chunk_usage[chunkno] == MAX_ENTRIES_PER_CHUNK - 1)
1103 {
1105 unsigned j;
1106
1107 /* Allocate a new chunk. */
1109
1110 /* Set the bit for each existing entry. */
1111 for (j = 0; j < entry->chunk_usage[chunkno]; ++j)
1112 {
1113 unsigned coff = entry->chunk_data[chunkno][j];
1114
1116 1 << (coff % BLOCKS_PER_ENTRY);
1117 }
1118
1119 /* Set the bit for the new entry. */
1122
1123 /* Swap the new chunk into place and update metadata. */
1124 pfree(entry->chunk_data[chunkno]);
1125 entry->chunk_data[chunkno] = newchunk;
1128 return;
1129 }
1130
1131 /*
1132 * OK, we currently have an array, and we don't need to convert to a
1133 * bitmap, but we do need to add a new element. If there's not enough
1134 * room, we'll have to expand the array.
1135 */
1136 if (entry->chunk_usage[chunkno] == entry->chunk_size[chunkno])
1137 {
1138 unsigned newsize = entry->chunk_size[chunkno] * 2;
1139
1141 entry->chunk_data[chunkno] = repalloc(entry->chunk_data[chunkno],
1142 newsize * sizeof(uint16));
1143 entry->chunk_size[chunkno] = newsize;
1144 }
1145
1146 /* Now we can add the new entry. */
1147 entry->chunk_data[chunkno][entry->chunk_usage[chunkno]] =
1149 entry->chunk_usage[chunkno]++;
1150}
#define INITIAL_ENTRIES_PER_CHUNK
Definition blkreftable.c:81
#define Max(x, y)
Definition c.h:1125
#define palloc_array(type, count)
Definition fe_memutils.h:91
#define palloc0_array(type, count)
Definition fe_memutils.h:92
int j
Definition isn.c:78
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

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 
)
extern

Definition at line 930 of file blkreftable.c.

932{
933 unsigned chunkno;
934 unsigned limit_chunkno;
935 unsigned limit_chunkoffset;
937
938 /* If we already have an equal or lower limit block, do nothing. */
939 if (limit_block >= entry->limit_block)
940 return;
941
942 /* Record the new limit block value. */
943 entry->limit_block = limit_block;
944
945 /*
946 * Figure out which chunk would store the state of the new limit block,
947 * and which offset within that chunk.
948 */
949 limit_chunkno = limit_block / BLOCKS_PER_CHUNK;
950 limit_chunkoffset = limit_block % BLOCKS_PER_CHUNK;
951
952 /*
953 * If the number of chunks is not large enough for any blocks with equal
954 * or higher block numbers to exist, then there is nothing further to do.
955 */
956 if (limit_chunkno >= entry->nchunks)
957 return;
958
959 /* Discard entire contents of any higher-numbered chunks. */
960 for (chunkno = limit_chunkno + 1; chunkno < entry->nchunks; ++chunkno)
961 entry->chunk_usage[chunkno] = 0;
962
963 /*
964 * Next, we need to discard any offsets within the chunk that would
965 * contain the limit_block. We must handle this differently depending on
966 * whether the chunk that would contain limit_block is a bitmap or an
967 * array of offsets.
968 */
971 {
972 unsigned chunkoffset;
973
974 /* It's a bitmap. Unset bits. */
976 ++chunkoffset)
978 ~(1 << (chunkoffset % BLOCKS_PER_ENTRY));
979 }
980 else
981 {
982 unsigned i,
983 j = 0;
984
985 /* It's an offset array. Filter out large offsets. */
986 for (i = 0; i < entry->chunk_usage[limit_chunkno]; ++i)
987 {
988 Assert(j <= i);
991 }
992 Assert(j <= entry->chunk_usage[limit_chunkno]);
993 entry->chunk_usage[limit_chunkno] = j;
994 }
995}
BlockNumber limit_block

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

◆ BlockRefTableFreeEntry()

void BlockRefTableFreeEntry ( BlockRefTableEntry entry)
extern

Definition at line 1157 of file blkreftable.c.

1158{
1159 if (entry->chunk_size != NULL)
1160 {
1161 pfree(entry->chunk_size);
1162 entry->chunk_size = NULL;
1163 }
1164
1165 if (entry->chunk_usage != NULL)
1166 {
1167 pfree(entry->chunk_usage);
1168 entry->chunk_usage = NULL;
1169 }
1170
1171 if (entry->chunk_data != NULL)
1172 {
1173 pfree(entry->chunk_data);
1174 entry->chunk_data = NULL;
1175 }
1176
1177 pfree(entry);
1178}

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 
)
extern

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}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
blockreftable_hash * hash

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 
)
extern

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}
void BlockRefTableEntryMarkBlockModified(BlockRefTableEntry *entry, ForkNumber forknum, BlockNumber blknum)
#define InvalidBlockNumber
Definition block.h:33
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
MemoryContext mcxt

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

Referenced by PrepareForIncrementalBackup(), and SummarizeWAL().

◆ BlockRefTableReaderGetBlocks()

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

Definition at line 725 of file blkreftable.c.

728{
729 unsigned blocks_found = 0;
730
731 /* Must provide space for at least one block number to be returned. */
732 Assert(nblocks > 0);
733
734 /* Loop collecting blocks to return to caller. */
735 for (;;)
736 {
738
739 /*
740 * If we've read at least one chunk, maybe it contains some block
741 * numbers that could satisfy caller's request.
742 */
743 if (reader->consumed_chunks > 0)
744 {
745 uint32 chunkno = reader->consumed_chunks - 1;
746 uint16 chunk_size = reader->chunk_size[chunkno];
747
748 if (chunk_size == MAX_ENTRIES_PER_CHUNK)
749 {
750 /* Bitmap format, so search for bits that are set. */
751 while (reader->chunk_position < BLOCKS_PER_CHUNK &&
752 blocks_found < nblocks)
753 {
755 uint16 w;
756
758 if ((w & (1u << (chunkoffset % BLOCKS_PER_ENTRY))) != 0)
759 blocks[blocks_found++] =
761 ++reader->chunk_position;
762 }
763 }
764 else
765 {
766 /* Not in bitmap format, so each entry is a 2-byte offset. */
767 while (reader->chunk_position < chunk_size &&
768 blocks_found < nblocks)
769 {
771 + reader->chunk_data[reader->chunk_position];
772 ++reader->chunk_position;
773 }
774 }
775 }
776
777 /* We found enough blocks, so we're done. */
778 if (blocks_found >= nblocks)
779 break;
780
781 /*
782 * We didn't find enough blocks, so we must need the next chunk. If
783 * there are none left, though, then we're done anyway.
784 */
785 if (reader->consumed_chunks == reader->total_chunks)
786 break;
787
788 /*
789 * Read data for next chunk and reset scan position to beginning of
790 * chunk. Note that the next chunk might be empty, in which case we
791 * consume the chunk without actually consuming any bytes from the
792 * underlying file.
793 */
794 next_chunk_size = reader->chunk_size[reader->consumed_chunks];
795 if (next_chunk_size > 0)
796 BlockRefTableRead(reader, reader->chunk_data,
797 next_chunk_size * sizeof(uint16));
798 ++reader->consumed_chunks;
799 reader->chunk_position = 0;
800 }
801
802 return blocks_found;
803}
static void BlockRefTableRead(BlockRefTableReader *reader, void *data, size_t length)
uint16 chunk_data[MAX_ENTRIES_PER_CHUNK]

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 
)
extern

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 /* Sanity-check the fork number. */
661 if (sentry.forknum < 0 || sentry.forknum > MAX_FORKNUM)
662 {
663 reader->error_callback(reader->error_callback_arg,
664 "file \"%s\" has invalid fork number %d",
665 reader->error_filename, sentry.forknum);
666 return false;
667 }
668
669 /*
670 * Sanity-check the nchunks value. In the backend, palloc_array would
671 * enforce this anyway (with a more generic error message); but in
672 * frontend it would not, potentially allowing BlockRefTableRead's length
673 * parameter to overflow.
674 */
675 if (sentry.nchunks > MaxAllocSize / sizeof(uint16))
676 {
677 reader->error_callback(reader->error_callback_arg,
678 "file \"%s\" has oversized chunk size array",
679 reader->error_filename);
680 return false;
681 }
682
683 /* Read chunk size array. */
684 if (reader->chunk_size != NULL)
685 pfree(reader->chunk_size);
686 reader->chunk_size = palloc_array(uint16, sentry.nchunks);
687 BlockRefTableRead(reader, reader->chunk_size,
688 sentry.nchunks * sizeof(uint16));
689
690 /* Sanity-check the chunk sizes. */
691 for (unsigned i = 0; i < sentry.nchunks; ++i)
692 {
693 if (reader->chunk_size[i] > MAX_ENTRIES_PER_CHUNK)
694 {
695 reader->error_callback(reader->error_callback_arg,
696 "file \"%s\" chunk %u has invalid size %u",
697 reader->error_filename, i,
698 (unsigned) reader->chunk_size[i]);
699 return false;
700 }
701 }
702
703 /* Set up for chunk scan. */
704 reader->total_chunks = sentry.nchunks;
705 reader->consumed_chunks = 0;
706
707 /* Return data to caller. */
708 memcpy(rlocator, &sentry.rlocator, sizeof(RelFileLocator));
709 *forknum = sentry.forknum;
710 *limit_block = sentry.limit_block;
711 return true;
712}
#define MaxAllocSize
Definition fe_memutils.h:22
uint32 pg_crc32c
Definition pg_crc32c.h:38
#define EQ_CRC32C(c1, c2)
Definition pg_crc32c.h:42
#define FIN_CRC32C(crc)
Definition pg_crc32c.h:182
#define MAX_FORKNUM
Definition relpath.h:70
BlockRefTableBuffer buffer
report_error_fn error_callback

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, i, MAX_ENTRIES_PER_CHUNK, MAX_FORKNUM, 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 
)
extern

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}
void BlockRefTableEntrySetLimitBlock(BlockRefTableEntry *entry, BlockNumber limit_block)

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

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

◆ BlockRefTableWriteEntry()

void BlockRefTableWriteEntry ( BlockRefTableWriter writer,
BlockRefTableEntry entry 
)
extern

Definition at line 853 of file blkreftable.c.

854{
856 unsigned j;
857
858 /* Convert to serialized entry format. */
859 sentry.rlocator = entry->key.rlocator;
860 sentry.forknum = entry->key.forknum;
861 sentry.limit_block = entry->limit_block;
862 sentry.nchunks = entry->nchunks;
863
864 /* Trim trailing zero entries. */
865 while (sentry.nchunks > 0 && entry->chunk_usage[sentry.nchunks - 1] == 0)
866 sentry.nchunks--;
867
868 /* Write the serialized entry itself. */
871
872 /* Write the untruncated portion of the chunk length array. */
873 if (sentry.nchunks != 0)
874 BlockRefTableWrite(&writer->buffer, entry->chunk_usage,
875 sentry.nchunks * sizeof(uint16));
876
877 /* Write the contents of each chunk. */
878 for (j = 0; j < entry->nchunks; ++j)
879 {
880 if (entry->chunk_usage[j] == 0)
881 continue;
882 BlockRefTableWrite(&writer->buffer, entry->chunk_data[j],
883 entry->chunk_usage[j] * sizeof(uint16));
884 }
885}
static void BlockRefTableWrite(BlockRefTableBuffer *buffer, void *data, size_t length)
BlockRefTableKey key
RelFileLocator rlocator
Definition blkreftable.c:49
ForkNumber forknum
Definition blkreftable.c:50

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 
)
extern

Definition at line 911 of file blkreftable.c.

912{
914
915 memcpy(&entry->key.rlocator, &rlocator, sizeof(RelFileLocator));
916 entry->key.forknum = forknum;
918
919 return entry;
920}
#define palloc0_object(type)
Definition fe_memutils.h:90

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 
)
extern

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}
#define BLOCKREFTABLE_MAGIC
Definition blkreftable.h:30
#define INIT_CRC32C(crc)
Definition pg_crc32c.h:41
io_callback_fn io_callback

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 
)
extern

Definition at line 826 of file blkreftable.c.

828{
831
832 /* Prepare buffer and CRC check and save callbacks. */
834 writer->buffer.io_callback = write_callback;
835 writer->buffer.io_callback_arg = write_callback_arg;
836 INIT_CRC32C(writer->buffer.crc);
837
838 /* Write magic number. */
839 BlockRefTableWrite(&writer->buffer, &magic, sizeof(uint32));
840
841 return writer;
842}

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

◆ DestroyBlockRefTableReader()

void DestroyBlockRefTableReader ( BlockRefTableReader reader)
extern

Definition at line 809 of file blkreftable.c.

810{
811 if (reader->chunk_size != NULL)
812 {
813 pfree(reader->chunk_size);
814 reader->chunk_size = NULL;
815 }
816 pfree(reader);
817}

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

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

◆ DestroyBlockRefTableWriter()

void DestroyBlockRefTableWriter ( BlockRefTableWriter writer)
extern

Definition at line 891 of file blkreftable.c.

892{
894 pfree(writer);
895}
static void BlockRefTableFileTerminate(BlockRefTableBuffer *buffer)

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

◆ WriteBlockRefTable()

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

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}
static int BlockRefTableComparator(const void *a, const void *b)
#define qsort(a, b, c, d)
Definition port.h:496

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