Skip to content

Commit 9d8b4d8

Browse files
committed
Extens page bitmap by doubling its size
1 parent 46c7dba commit 9d8b4d8

File tree

5 files changed

+146
-45
lines changed

5 files changed

+146
-45
lines changed

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434

3535
# Extra files
3636
/src/pg_crc.c
37-
/src/datapagemap.c
38-
/src/datapagemap.h
39-
/src/logging.h
4037
/src/receivelog.c
4138
/src/receivelog.h
4239
/src/streamutil.c

Makefile

+3-15
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ OBJS = src/utils/configuration.o src/utils/json.o src/utils/logger.o \
77
OBJS += src/archive.o src/backup.o src/catalog.o src/checkdb.o src/configure.o src/data.o \
88
src/delete.o src/dir.o src/fetch.o src/help.o src/init.o src/merge.o \
99
src/parsexlog.o src/ptrack.o src/pg_probackup.o src/restore.o src/show.o src/util.o \
10-
src/validate.o
10+
src/validate.o src/datapagemap.o
1111

1212
# borrowed files
13-
OBJS += src/pg_crc.o src/datapagemap.o src/receivelog.o src/streamutil.o \
13+
OBJS += src/pg_crc.o src/receivelog.o src/streamutil.o \
1414
src/xlogreader.o
1515

16-
EXTRA_CLEAN = src/pg_crc.c src/datapagemap.c src/datapagemap.h \
16+
EXTRA_CLEAN = src/pg_crc.c \
1717
src/receivelog.c src/receivelog.h src/streamutil.c src/streamutil.h \
1818
src/xlogreader.c src/instr_time.h
1919

@@ -34,9 +34,6 @@ ifneq (,$(wildcard $(srchome)/src/bin/pg_basebackup/walmethods.c))
3434
OBJS += src/walmethods.o
3535
EXTRA_CLEAN += src/walmethods.c src/walmethods.h
3636
endif
37-
ifneq (,$(wildcard $(srchome)/src/bin/pg_rewind/logging.h))
38-
EXTRA_CLEAN += src/logging.h
39-
endif
4037

4138
ifdef USE_PGXS
4239
PG_CONFIG = pg_config
@@ -56,16 +53,9 @@ PG_LIBS_INTERNAL = $(libpq_pgport) ${PTHREAD_CFLAGS}
5653
src/utils/configuration.o: src/datapagemap.h
5754
src/archive.o: src/instr_time.h
5855
src/backup.o: src/receivelog.h src/streamutil.h
59-
ifneq (,$(wildcard $(srchome)/src/bin/pg_rewind/logging.h))
60-
src/datapagemap.o: src/logging.h
61-
endif
6256

6357
src/instr_time.h: $(srchome)/src/include/portability/instr_time.h
6458
rm -f $@ && $(LN_S) $(srchome)/src/include/portability/instr_time.h $@
65-
src/datapagemap.c: $(srchome)/src/bin/pg_rewind/datapagemap.c
66-
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_rewind/datapagemap.c $@
67-
src/datapagemap.h: $(srchome)/src/bin/pg_rewind/datapagemap.h
68-
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_rewind/datapagemap.h $@
6959
src/pg_crc.c: $(srchome)/src/backend/utils/hash/pg_crc.c
7060
rm -f $@ && $(LN_S) $(srchome)/src/backend/utils/hash/pg_crc.c $@
7161
src/receivelog.c: $(srchome)/src/bin/pg_basebackup/receivelog.c
@@ -82,8 +72,6 @@ src/streamutil.h: $(srchome)/src/bin/pg_basebackup/streamutil.h
8272
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_basebackup/streamutil.h $@
8373
src/xlogreader.c: $(srchome)/src/backend/access/transam/xlogreader.c
8474
rm -f $@ && $(LN_S) $(srchome)/src/backend/access/transam/xlogreader.c $@
85-
src/logging.h: $(srchome)/src/bin/pg_rewind/logging.h
86-
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_rewind/logging.h $@
8775
src/walmethods.c: $(srchome)/src/bin/pg_basebackup/walmethods.c
8876
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_basebackup/walmethods.c $@
8977
src/walmethods.h: $(srchome)/src/bin/pg_basebackup/walmethods.h

src/datapagemap.c

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* datapagemap.c
4+
* A data structure for keeping track of data pages that have changed.
5+
*
6+
* This is a fairly simple bitmap.
7+
*
8+
* Copyright (c) 2013-2019, PostgreSQL Global Development Group
9+
*
10+
*-------------------------------------------------------------------------
11+
*/
12+
13+
#include "postgres_fe.h"
14+
15+
#include "datapagemap.h"
16+
17+
struct datapagemap_iterator
18+
{
19+
datapagemap_t *map;
20+
BlockNumber nextblkno;
21+
};
22+
23+
/*****
24+
* Public functions
25+
*/
26+
27+
/*
28+
* Add a block to the bitmap.
29+
*/
30+
void
31+
datapagemap_add(datapagemap_t *map, BlockNumber blkno)
32+
{
33+
int offset;
34+
int bitno;
35+
int oldsize = map->bitmapsize;
36+
37+
offset = blkno / 8;
38+
bitno = blkno % 8;
39+
40+
/* enlarge or create bitmap if needed */
41+
if (oldsize <= offset)
42+
{
43+
int newsize;
44+
45+
/*
46+
* The minimum to hold the new bit is offset + 1. But add some
47+
* headroom, so that we don't need to repeatedly enlarge the bitmap in
48+
* the common case that blocks are modified in order, from beginning
49+
* of a relation to the end.
50+
*/
51+
newsize = (oldsize == 0) ? 16 : oldsize;
52+
while (newsize <= offset) {
53+
newsize <<= 1;
54+
}
55+
56+
map->bitmap = pg_realloc(map->bitmap, newsize);
57+
58+
/* zero out the newly allocated region */
59+
memset(&map->bitmap[oldsize], 0, newsize - oldsize);
60+
61+
map->bitmapsize = newsize;
62+
}
63+
64+
/* Set the bit */
65+
map->bitmap[offset] |= (1 << bitno);
66+
}
67+
68+
/*
69+
* Start iterating through all entries in the page map.
70+
*
71+
* After datapagemap_iterate, call datapagemap_next to return the entries,
72+
* until it returns false. After you're done, use pg_free() to destroy the
73+
* iterator.
74+
*/
75+
datapagemap_iterator_t *
76+
datapagemap_iterate(datapagemap_t *map)
77+
{
78+
datapagemap_iterator_t *iter;
79+
80+
iter = pg_malloc(sizeof(datapagemap_iterator_t));
81+
iter->map = map;
82+
iter->nextblkno = 0;
83+
84+
return iter;
85+
}
86+
87+
bool
88+
datapagemap_next(datapagemap_iterator_t *iter, BlockNumber *blkno)
89+
{
90+
datapagemap_t *map = iter->map;
91+
92+
for (;;)
93+
{
94+
BlockNumber blk = iter->nextblkno;
95+
int nextoff = blk / 8;
96+
int bitno = blk % 8;
97+
98+
if (nextoff >= map->bitmapsize)
99+
break;
100+
101+
iter->nextblkno++;
102+
103+
if (map->bitmap[nextoff] & (1 << bitno))
104+
{
105+
*blkno = blk;
106+
return true;
107+
}
108+
}
109+
110+
/* no more set bits in this bitmap. */
111+
return false;
112+
}
113+

src/datapagemap.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* datapagemap.h
4+
*
5+
* Copyright (c) 2013-2019, PostgreSQL Global Development Group
6+
*
7+
*-------------------------------------------------------------------------
8+
*/
9+
#ifndef DATAPAGEMAP_H
10+
#define DATAPAGEMAP_H
11+
12+
#include "storage/relfilenode.h"
13+
#include "storage/block.h"
14+
15+
16+
struct datapagemap
17+
{
18+
char *bitmap;
19+
int bitmapsize;
20+
};
21+
22+
typedef struct datapagemap datapagemap_t;
23+
typedef struct datapagemap_iterator datapagemap_iterator_t;
24+
25+
extern void datapagemap_add(datapagemap_t *map, BlockNumber blkno);
26+
extern datapagemap_iterator_t *datapagemap_iterate(datapagemap_t *map);
27+
extern bool datapagemap_next(datapagemap_iterator_t *iter, BlockNumber *blkno);
28+
29+
#endif /* DATAPAGEMAP_H */

src/util.c

+1-27
Original file line numberDiff line numberDiff line change
@@ -538,33 +538,7 @@ datapagemap_is_set(datapagemap_t *map, BlockNumber blkno)
538538
offset = blkno / 8;
539539
bitno = blkno % 8;
540540

541-
/* enlarge or create bitmap if needed */
542-
if (map->bitmapsize <= offset)
543-
{
544-
int oldsize = map->bitmapsize;
545-
int newsize;
546-
547-
/*
548-
* The minimum to hold the new bit is offset + 1. But add some
549-
* headroom, so that we don't need to repeatedly enlarge the bitmap in
550-
* the common case that blocks are modified in order, from beginning
551-
* of a relation to the end.
552-
*/
553-
newsize = offset + 1;
554-
newsize += 10;
555-
556-
map->bitmap = pg_realloc(map->bitmap, newsize);
557-
558-
/* zero out the newly allocated region */
559-
memset(&map->bitmap[oldsize], 0, newsize - oldsize);
560-
561-
map->bitmapsize = newsize;
562-
}
563-
564-
//datapagemap_print(map);
565-
566-
/* check the bit */
567-
return map->bitmap[offset] & (1 << bitno);
541+
return (map->bitmapsize <= offset) ? false : (map->bitmap[offset] & (1 << bitno)) != 0;
568542
}
569543

570544
/*

0 commit comments

Comments
 (0)