All Projects → postgrespro → ptrack

postgrespro / ptrack

Licence: PostgreSQL License
Block-level incremental backup engine for PostgreSQL

Programming Languages

c
50402 projects - #5 most used programming language
perl
6916 projects
shell
77523 projects
PLpgSQL
1095 projects
Makefile
30231 projects

Projects that are alternatives of or similar to ptrack

Pg auto failover
Postgres extension and service for automated failover and high-availability
Stars: ✭ 564 (+2585.71%)
Mutual labels:  postgres, postgresql-extension
Pgx
Build Postgres Extensions with Rust!
Stars: ✭ 903 (+4200%)
Mutual labels:  postgres, postgresql-extension
Pggraphblas
High Performance Graph Processing with Postgres and GraphBLAS
Stars: ✭ 316 (+1404.76%)
Mutual labels:  postgres, postgresql-extension
pgsentinel
postgresql extension providing Active session history
Stars: ✭ 110 (+423.81%)
Mutual labels:  postgres, postgresql-extension
Wasmer Postgres
💽🕸 Postgres library to run WebAssembly binaries.
Stars: ✭ 245 (+1066.67%)
Mutual labels:  postgres, postgresql-extension
Plv8
V8 Engine Javascript Procedural Language add-on for PostgreSQL
Stars: ✭ 1,195 (+5590.48%)
Mutual labels:  postgres, postgresql-extension
pg-audit-json
Simple, easily customised trigger-based auditing for PostgreSQL (Postgres). See also pgaudit.
Stars: ✭ 34 (+61.9%)
Mutual labels:  postgres, postgresql-extension
Wal E
Continuous Archiving for Postgres
Stars: ✭ 3,313 (+15676.19%)
Mutual labels:  backups, postgres
Pg hashids
Short unique id generator for PostgreSQL, using hashids
Stars: ✭ 164 (+680.95%)
Mutual labels:  postgres, postgresql-extension
Bgworker
Background Worker Processes for PostgreSQL written in Go
Stars: ✭ 77 (+266.67%)
Mutual labels:  postgres, postgresql-extension
Pg similarity
set of functions and operators for executing similarity queries
Stars: ✭ 250 (+1090.48%)
Mutual labels:  postgres, postgresql-extension
snapbtrex
snapbtrex is a small utility that keeps snapshots of btrfs filesystems and optionally send them to a remote system or syncs them locally.
Stars: ✭ 29 (+38.1%)
Mutual labels:  backups, incremental-backups
oesophagus
Enterprise Grade Single-Step Streaming Data Infrastructure Setup. (Under Development)
Stars: ✭ 12 (-42.86%)
Mutual labels:  postgres
fostgres
RESTful APIs for Postgres databases
Stars: ✭ 22 (+4.76%)
Mutual labels:  postgres
plow
👨‍🌾 Postgres migrations and seeding made easy
Stars: ✭ 13 (-38.1%)
Mutual labels:  postgres
docker-postgres-windows
No description or website provided.
Stars: ✭ 19 (-9.52%)
Mutual labels:  postgres
DAR
DAR - Disk ARchive
Stars: ✭ 58 (+176.19%)
Mutual labels:  incremental-backups
subsocial-offchain
Off-chain storage for Subsocial blockchain. This app builds user feeds and notifications by subscribing to Substrate events.
Stars: ✭ 24 (+14.29%)
Mutual labels:  postgres
astro
Astro allows rapid and clean development of {Extract, Load, Transform} workflows using Python and SQL, powered by Apache Airflow.
Stars: ✭ 79 (+276.19%)
Mutual labels:  postgres
postgres exporter
Postgres exporter
Stars: ✭ 14 (-33.33%)
Mutual labels:  postgres

Build Status codecov GitHub release

ptrack

Overview

Ptrack is a block-level incremental backup engine for PostgreSQL. You can effectively use ptrack engine for taking incremental backups with pg_probackup backup and recovery manager for PostgreSQL.

It is designed to allow false positives (i.e. block/page is marked in the ptrack map, but actually has not been changed), but to never allow false negatives (i.e. loosing any PGDATA changes, excepting hint-bits).

Currently, ptrack codebase is split between small PostgreSQL core patch and extension. All public SQL API methods and main engine are placed in the ptrack extension, while the core patch contains only certain hooks and modifies binary utilities to ignore ptrack.map.* files.

This extension is compatible with PostgreSQL 11, 12, 13, 14.

Installation

  1. Get latest ptrack sources:
git clone https://github.com/postgrespro/ptrack.git
  1. Get latest PostgreSQL sources:
git clone https://github.com/postgres/postgres.git -b REL_14_STABLE && cd postgres
  1. Apply PostgreSQL core patch:
git apply -3 ../ptrack/patches/REL_14_STABLE-ptrack-core.diff
  1. Compile and install PostgreSQL

  2. Set ptrack.map_size (in MB)

echo "shared_preload_libraries = 'ptrack'" >> postgres_data/postgresql.conf
echo "ptrack.map_size = 64" >> postgres_data/postgresql.conf
  1. Compile and install ptrack extension
USE_PGXS=1 make -C /path/to/ptrack/ install
  1. Run PostgreSQL and create ptrack extension
postgres=# CREATE EXTENSION ptrack;

Configuration

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

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

Public SQL API

  • ptrack_version() — returns ptrack version string.
  • ptrack_init_lsn() — returns LSN of the last ptrack map initialization.
  • ptrack_get_pagemapset(start_lsn pg_lsn) — returns a set of changed data files with a number of changed blocks and their bitmaps since specified start_lsn.
  • ptrack_get_change_stat(start_lsn pg_lsn) — returns statistic of changes (number of files, pages and size in MB) since specified start_lsn.

Usage example:

postgres=# SELECT ptrack_version();
 ptrack_version 
----------------
 2.3
(1 row)

postgres=# SELECT ptrack_init_lsn();
 ptrack_init_lsn 
-----------------
 0/1814408
(1 row)

postgres=# SELECT * FROM ptrack_get_pagemapset('0/185C8C0');
        path         | pagecount |                pagemap                 
---------------------+-----------+----------------------------------------
 base/16384/1255     |         3 | \x001000000005000000000000
 base/16384/2674     |         3 | \x0000000900010000000000000000
 base/16384/2691     |         1 | \x00004000000000000000000000
 base/16384/2608     |         1 | \x000000000000000400000000000000000000
 base/16384/2690     |         1 | \x000400000000000000000000
(5 rows)

postgres=# SELECT * FROM ptrack_get_change_stat('0/285C8C8');
 files | pages |        size, MB        
-------+-------+------------------------
    20 |    25 | 0.19531250000000000000
(1 row)

Upgrading

Usually, you have to only install new version of ptrack and do ALTER EXTENSION 'ptrack' UPDATE;. However, some specific actions may be required as well:

Upgrading from 2.0.0 to 2.1.*:

  • Put shared_preload_libraries = 'ptrack' into postgresql.conf.
  • Rename ptrack_map_size to ptrack.map_size.
  • Do ALTER EXTENSION 'ptrack' UPDATE;.
  • Restart your server.

Upgrading from 2.1.* to 2.2.*:

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.

Upgrading from 2.2.* to 2.3.*:

  • Stop your server
  • Update ptrack binaries
  • Remove global/ptrack.map.mmap if it exist in server data directory
  • Start server
  • Do ALTER EXTENSION 'ptrack' UPDATE;.

Limitations

  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, but we only durably flush ptrack map at checkpoint time.

  2. The only one production-ready backup utility, that fully supports ptrack is pg_probackup.

  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.

  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 for details.

Benchmarks

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.

Architecture

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.

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:

  • temporary file ptrack.map.tmp to durably replace ptrack.map during checkpoint.

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.

To gather the whole changeset of modified blocks in ptrack_get_pagemapset() we walk the entire PGDATA (base/**/*, global/*, pg_tblspc/**/*) and verify using map whether each block of each relation was modified since the specified LSN or not.

Contribution

Feel free to send pull requests, fill up issues, or just reach one of us directly (e.g. <Alexey Kondratov, @ololobus>) if you are interested in ptrack.

Tests

Everything is tested automatically with travis-ci.com and codecov.io, but you can also run tests locally via Docker:

export PG_BRANCH=REL_14_STABLE
export TEST_CASE=all
export MODE=paranoia

./make_dockerfile.sh

docker-compose build
docker-compose run tests

Available test modes (MODE) are basic (default) and paranoia (per-block checksum comparison of PGDATA content before and after backup-restore process). Available test cases (TEST_CASE) are tap (minimalistic PostgreSQL tap test), all or any specific pg_probackup test, e.g. test_ptrack_simple.

TODO

  • 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.
  • Can we resize ptrack map on restart but keep the previously tracked changes?
  • Can we write a formal proof, that we never loose any modified page with ptrack? With TLA+?
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].