Extensions
- pg_repack_safe 1.0.0
- Guard for the built-in REPACK (CONCURRENTLY)
Documentation
- CLAUDE
- pg_repack_safe
- development
- Development Guide
- SKILL
- Documentation Example Test Generator
- SKILL
- Test Coverage Analysis for PostgreSQL Extensions
- limits
- Limits
- fix-design
- Preventing wrong results from non-MVCC-safe rewrites: design research
- standbys
- Standby errors caused by pg_repack_safe
- history
- REPACK (CONCURRENTLY): how it came to be not MVCC-safe
- AGENTS
- pg_repack_safe: agent guidelines
- removal
- Removing the extension
- problem
- The problem in detail
- SKILL
- PostgreSQL Hook & Callback Audit
- design
- Design
- installation
- Installation Guide
- CHANGELOG
- Changelog
README
Contents
The problem
PostgreSQL 19’s REPACK (CONCURRENTLY) rewrites a table without blocking
reads or writes. Its reference page carries this warning, under
“Notes on Concurrent Operation”:
[!WARNING]
REPACKwith theCONCURRENTLYoption is not MVCC-safe. After the repacking transaction commits, the table will appear empty to concurrent transactions, if they are using a snapshot taken before repacking started. See Section 13.6 for more details.
In stock PostgreSQL 19, once REPACK (CONCURRENTLY) commits, a snapshot
taken before that commit, in a transaction that had not yet touched the
table, reads the table as empty, however many rows it holds. An UPDATE
or DELETE in that transaction finds no rows and changes nothing. No error
is raised. Snapshots that last beyond one statement are exposed:
REPEATABLE READ and SERIALIZABLE transactions, cursors, and pg_dump,
which exits with status 0 and a dump without the table’s rows. So is
logical replication’s initial table copy, after which a subscriber never
gets the table’s existing rows.
The reference page’s warning says “before repacking started”; Section 13.6,
which it links to, says “before the command committed”, and that is the real
window: it includes the whole repack and the wait for its final lock, which
lasts as long as the longest transaction already reading the table. A dump
that starts while REPACK (CONCURRENTLY) waits for its final lock:
REPACK (CONCURRENTLY) orders pg_dump
───────────────────────────── ──────────────────────────────
1 copies the rows into new storage
2 asks for its final lock on
orders; waits for a report that
is still reading orders
3 takes its snapshot
4 asks to lock orders; queues
behind REPACK
5 the report ends; REPACK swaps
the storage and commits
6 gets its lock, reads orders
with the snapshot from step 3:
no rows, exit status 0
doc/problem.md has the details, the command’s lineage and a demo that shows four cases on a throwaway server.
What the extension does
A report reads customers, then orders, in one transaction, and another
session repacks orders in between. On stock PostgreSQL 19 the report is
incorrectly told there are no orders:
SELECT count(*) AS orders, sum(amount) AS revenue FROM orders;
orders | revenue
--------+---------
0 |
With pg_repack_safe the same statement fails, and the report, run again in a new transaction, gets the right answer:
SELECT count(*) AS orders, sum(amount) AS revenue FROM orders;
ERROR: cannot read table "orders" with this snapshot
DETAIL: REPACK (CONCURRENTLY) rewrote the table after the snapshot was
taken, and row visibility for older snapshots was not preserved.
HINT: Retry the transaction.
COMMIT;
SELECT count(*) AS orders, sum(amount) AS revenue FROM orders;
orders | revenue
--------+---------
123 | 456789
pg_dump is the case most likely to bite: on stock PostgreSQL 19 a dump
that starts during a REPACK (CONCURRENTLY) exits with status 0 and the
table’s rows missing. With the extension it fails and is run again.
Installation
doc/installation.md has the requirements in full,
the settings, pg_upgrade, logical replication and other extensions. In
short: on the primary and on every standby, build and install, add the
library to shared_preload_libraries, and restart (standbys first):
make
sudo make install # omit sudo when the PostgreSQL install is yours
shared_preload_libraries = 'pg_repack_safe' # add to any existing list
Then, in each database where REPACK (CONCURRENTLY) will run:
CREATE EXTENSION pg_repack_safe;
Check, in each database and on each server, that the guard is in force:
SELECT repack_safe.status();
It must return t. If it returns f, with a WARNING that says why, the
server did not load the library through shared_preload_libraries: the
extension is installed but guards nothing. The extension’s description in
\dx names this check.
The primary and its standbys must run the library from the same major
version (the same first part, 1.x.y). To upgrade, install the new build
on the standbys first, then on the primary, and run any ALTER EXTENSION
UPDATE last. On each server:
SELECT module_name, version FROM pg_get_loaded_modules()
WHERE module_name = 'pg_repack_safe';
The extension’s table and functions are always in the schema
repack_safe.
What it costs
- Old snapshots get an error. A transaction whose snapshot predates a
REPACK (CONCURRENTLY)of a table and that then reads the table fails with SQLSTATE 40001 and has to be retried. That includes apg_dumpthat started during the repack. - Standbys without
hot_standby_feedbackcancel long transactions some time after aREPACK (CONCURRENTLY)on the primary, including transactions that never read the repacked table (doc/standbys.md). - A server that replays the WAL without the library stops at the first
REPACK (CONCURRENTLY), with “resource manager with ID 149 not registered”: a standby, or a restore of a base backup with WAL. That is deliberate, since such a server could not check its reads. - A fixed cost per query, measured at 3% or less on select-only
pgbenchwith one client and within noise with eight (doc/design.md).
What pg_repack_safe guarantees
With the library in shared_preload_libraries on the primary and on every
standby, and the extension created in the database:
- A read of a table, including the read an
UPDATEorDELETEdoes to find its rows, whose snapshot does not see a committedREPACK (CONCURRENTLY)of that table, fails withERROR: cannot read table "..." with this snapshot, SQLSTATE 40001 (a serialization failure: retry the transaction), instead of returning wrong rows. This covers every query through the executor (views, functions, cursors, parallel plans, partitions, the TOAST table),COPY ... TO(including partitioned tables,pg_dump, and logical replication’s initial copy), on the primary and on hot standbys. AnINSERTwrites its target table without reading it, so the target is not checked; a read of the same table in the same statement is (INSERT ... SELECT, a subquery, the query around a data-modifyingWITH), and so is a target with a foreign key to itself, whose check reads it.ON CONFLICTon a row the snapshot cannot see fails with stock PostgreSQL’s own serialization failure (specs/reader_guard_edges.spec). - Reads that do not need an error get none: snapshots taken after the
repack committed, snapshots that had already read the table (the repack
waits for them), empty tables,
VACUUM FULL,CLUSTER, plainREPACK. The exceptions, where the guard errors and stock PostgreSQL would have been right, are listed under Errors, not wrong rows.
The reproducers that fail on stock PostgreSQL 19 and pass with the
extension are demo/demo.sh (which shows both), and
specs/repack_concurrently_*.spec, t/001_repack_pg_dump.pl,
t/008_copy_guard.pl and t/013_tablesync.pl.
How the guard works, and the argument that it is sound, are in doc/design.md.
What pg_repack_safe does not guarantee
The extension guards REPACK (CONCURRENTLY) and nothing else. Other
commands keep their stock behaviour, including those that rewrite a table
without preserving visibility. All of them predate PostgreSQL 19, and a
test pins the stock behaviour of each with the extension loaded:
TRUNCATEand rewritingALTER TABLE(for example a column type change): a snapshot taken before them reads the table as empty. Upstream documents both as not MVCC-safe (specs/stock_rewrites.spec).REFRESH MATERIALIZED VIEWwithoutCONCURRENTLY: a snapshot taken before it reads the view’s new contents, because the refresh writes its rows frozen. Upstream does not document this.REFRESH ... CONCURRENTLYis not affected (specs/stock_rewrites.spec).VACUUM FULL,CLUSTERand plainREPACKwith hot standbys that run withouthot_standby_feedback: the primary can drop rows that a standby snapshot still needs, and the standby query silently misses them. Reported on pgsql-hackers in 2012 and still unfixed. Turning onhot_standby_feedbackprevents it (Requirements;t/012_standby_safe_rewrite.pl).
Within REPACK (CONCURRENTLY) itself, Limits lists the errors
the guard raises where a result might have been correct, and what is lost
when a requirement is not met.
[!NOTE] The guard checks a reader’s snapshot against a recorded rewrite, not anything specific to
REPACK, so the same approach may extend to the older commands above, in this extension or in a separate one, including guarding standby reads afterVACUUM FULLas it guards them afterREPACK (CONCURRENTLY). That work has not been designed or tested. If it would help you, open an issue and say which commands matter to you.
Three gaps are known within REPACK (CONCURRENTLY) itself, each described
in doc/limits.md, the first two with a reproducer. The
third is reasoned but not reproduced, as it requires a REPACK paused
between its swap and its commit.
- Functions of the contrib modules
pgrowlocksandamcheckread a table outside the executor. With an old snapshot they return a wrong answer and no error. - A foreign-key check against a repacked table, from an old snapshot, fails
with a foreign-key violation (23503) where the other reads fail with
- A standby without the library whose replay starts between a repack’s swap and its commit is not stopped.
Turning pg_repack_safe off
doc/removal.md has the check for each step, what holds
the cleanup back, and what to do before taking the library out of
shared_preload_libraries. In short: stop running REPACK (CONCURRENTLY),
then, as a superuser, in each database that has the extension:
SELECT repack_safe.prune_records();
VACUUM (TRUNCATE) repack_safe.rewrites;
SELECT pg_relation_size('repack_safe.rewrites') = 0 AS empty;
DROP EXTENSION pg_repack_safe;
Repeat the VACUUM until empty is t; until then DROP EXTENSION is
refused. The record table empties once no snapshot taken before a recorded
REPACK (CONCURRENTLY) is left, on the primary or on any standby, so no
snapshot reads a repacked table as empty. With the library still
preloaded, REPACK (CONCURRENTLY) is refused in a database without the
extension. No restart is needed.
If no such snapshot can exist, a superuser can drop the extension without waiting, in one session:
SET pg_repack_safe.allow_unprotected_removal = on;
DROP EXTENSION pg_repack_safe;
Set it with SET, never in a configuration file, so that it ends with the
session. If a snapshot taken before a recorded REPACK (CONCURRENTLY)
does exist, on the primary or on any standby, it then reads the repacked
table as empty.
To switch REPACK (CONCURRENTLY) off for a whole server, with or without
the extension, set max_repack_replication_slots = 0 and restart
(doc/limits.md).
Outlook
Raising an error for older snapshots was proposed on pgsql-hackers while
REPACK (CONCURRENTLY) was being developed, and was not adopted: one
session’s REPACK would make other sessions fail. This extension is that
trade, offered to those who prefer an error to a wrong result.
A patch that makes REPACK (CONCURRENTLY) MVCC-safe, so that nobody gets an
error or a wrong result, is under review for PostgreSQL 20. It is not
committed, and PostgreSQL 19 will not get it either way. That fix cannot be
delivered as an extension: it changes how the heap, the WAL and logical
decoding handle transaction IDs. The history, with its sources, and the
comparison are in doc/research/history.md.
Documentation
- The problem in detail, and the demo
- Installation, settings, upgrades, logical replication
- Design: why the guard is sound, and what it costs
- Limits: errors, known gaps, unmet requirements
- Standby errors without
hot_standby_feedback - Removing the extension
- Development
License
MIT; see LICENSE.