Skip to content

Commit a0d5a20

Browse files
authored
[PGPRO-5691] move mmapped ptrack map into shared postgres memory (#19)
1 parent 2f15771 commit a0d5a20

10 files changed

+215
-228
lines changed

Diff for: Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ OBJS = ptrack.o datapagemap.o engine.o $(WIN32RES)
55
PGFILEDESC = "ptrack - block-level incremental backup engine"
66

77
EXTENSION = ptrack
8-
EXTVERSION = 2.2
9-
DATA = ptrack--2.1.sql ptrack--2.0--2.1.sql ptrack--2.1--2.2.sql
8+
EXTVERSION = 2.3
9+
DATA = ptrack--2.1.sql ptrack--2.0--2.1.sql ptrack--2.1--2.2.sql ptrack--2.2--2.3.sql
1010

1111
TAP_TESTS = 1
1212

Diff for: README.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ postgres=# CREATE EXTENSION ptrack;
5757

5858
## Configuration
5959

60-
The only one configurable option is `ptrack.map_size` (in MB). Default is `-1`, which means `ptrack` is turned off. In order to reduce number of false positives it is recommended to set `ptrack.map_size` to `1 / 1000` of expected `PGDATA` size (i.e. `1000` for a 1 TB database).
60+
The only one configurable option is `ptrack.map_size` (in MB). Default is `0`, which means `ptrack` is turned off. In order to reduce number of false positives it is recommended to set `ptrack.map_size` to `1 / 1000` of expected `PGDATA` size (i.e. `1000` for a 1 TB database).
6161

6262
To disable `ptrack` and clean up all remaining service files set `ptrack.map_size` to `0`.
6363

@@ -74,7 +74,7 @@ Usage example:
7474
postgres=# SELECT ptrack_version();
7575
ptrack_version
7676
----------------
77-
2.2
77+
2.3
7878
(1 row)
7979

8080
postgres=# SELECT ptrack_init_lsn();
@@ -115,27 +115,34 @@ Usually, you have to only install new version of `ptrack` and do `ALTER EXTENSIO
115115

116116
Since version 2.2 we use a different algorithm for tracking changed pages. Thus, data recorded in the `ptrack.map` using pre 2.2 versions of `ptrack` is incompatible with newer versions. After extension upgrade and server restart old `ptrack.map` will be discarded with `WARNING` and initialized from the scratch.
117117

118+
#### Upgrading from 2.2.* to 2.3.*:
119+
120+
* Stop your server
121+
* Update ptrack binaries
122+
* Remove global/ptrack.map.mmap if it exist in server data directory
123+
* Start server
124+
* Do `ALTER EXTENSION 'ptrack' UPDATE;`.
125+
118126
## Limitations
119127

120128
1. You can only use `ptrack` safely with `wal_level >= 'replica'`. Otherwise, you can lose tracking of some changes if crash-recovery occurs, since [certain commands are designed not to write WAL at all if wal_level is minimal](https://www.postgresql.org/docs/12/populate.html#POPULATE-PITR), but we only durably flush `ptrack` map at checkpoint time.
121129

122130
2. The only one production-ready backup utility, that fully supports `ptrack` is [pg_probackup](https://github.com/postgrespro/pg_probackup).
123131

124-
3. Currently, you cannot resize `ptrack` map in runtime, only on postmaster start. Also, you will loose all tracked changes, so it is recommended to do so in the maintainance window and accompany this operation with full backup. See [TODO](#TODO) for details.
132+
3. You cannot resize `ptrack` map in runtime, only on postmaster start. Also, you will loose all tracked changes, so it is recommended to do so in the maintainance window and accompany this operation with full backup.
125133

126-
4. You will need up to `ptrack.map_size * 3` of additional disk space, since `ptrack` uses two additional temporary files for durability purpose. See [Architecture section](#Architecture) for details.
134+
4. You will need up to `ptrack.map_size * 2` of additional disk space, since `ptrack` uses additional temporary file for durability purpose. See [Architecture section](#Architecture) for details.
127135

128136
## Benchmarks
129137

130138
Briefly, an overhead of using `ptrack` on TPS usually does not exceed a couple of percent (~1-3%) for a database of dozens to hundreds of gigabytes in size, while the backup time scales down linearly with backup size with a coefficient ~1. It means that an incremental `ptrack` backup of a database with only 20% of changed pages will be 5 times faster than a full backup. More details [here](benchmarks).
131139

132140
## Architecture
133141

134-
We use a single shared hash table in `ptrack`, which is mapped in memory from the file on disk using `mmap`. Due to the fixed size of the map there may be false positives (when some block is marked as changed without being actually modified), but not false negative results. However, these false postives may be completely eliminated by setting a high enough `ptrack.map_size`.
142+
We use a single shared hash table in `ptrack`. Due to the fixed size of the map there may be false positives (when some block is marked as changed without being actually modified), but not false negative results. However, these false postives may be completely eliminated by setting a high enough `ptrack.map_size`.
135143

136-
All reads/writes are made using atomic operations on `uint64` entries, so the map is completely lockless during the normal PostgreSQL operation. Because we do not use locks for read/write access and cannot control `mmap` eviction back to disk, `ptrack` keeps a map (`ptrack.map`) since the last checkpoint intact and uses up to 2 additional temporary files:
144+
All reads/writes are made using atomic operations on `uint64` entries, so the map is completely lockless during the normal PostgreSQL operation. Because we do not use locks for read/write access, `ptrack` keeps a map (`ptrack.map`) since the last checkpoint intact and uses up to 1 additional temporary file:
137145

138-
* working copy `ptrack.map.mmap` for doing `mmap` on it (there is a [TODO](#TODO) item);
139146
* temporary file `ptrack.map.tmp` to durably replace `ptrack.map` during checkpoint.
140147

141148
Map is written on disk at the end of checkpoint atomically block by block involving the CRC32 checksum calculation that is checked on the next whole map re-read after crash-recovery or restart.
@@ -165,8 +172,6 @@ Available test modes (`MODE`) are `basic` (default) and `paranoia` (per-block ch
165172

166173
### TODO
167174

168-
* Use POSIX `shm_open()` instead of `open()` to do not create an additional working copy of `ptrack` map file.
169175
* Should we introduce `ptrack.map_path` to allow `ptrack` service files storage outside of `PGDATA`? Doing that we will avoid patching PostgreSQL binary utilities to ignore `ptrack.map.*` files.
170176
* Can we resize `ptrack` map on restart but keep the previously tracked changes?
171-
* Can we resize `ptrack` map dynamicaly?
172177
* Can we write a formal proof, that we never loose any modified page with `ptrack`? With TLA+?

0 commit comments

Comments
 (0)