pg_rewrite
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pg_rewrite | 2.2 | ADMIN | BSD-3-Clause | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 5020 | pg_rewrite | No | Yes | Yes | Yes | No | Yes | - |
PIGSTY RPM and PGDG DEB are aligned at 2.2 for PostgreSQL 14-18; shared_preload_libraries=pg_rewrite and wal_level=logical are required.
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | MIXED | 2.2 | 1817161514 | pg_rewrite | - |
| RPM | PIGSTY | 2.2 | 1817161514 | pg_rewrite_$v | - |
| DEB | PGDG | 2.2 | 1817161514 | postgresql-$v-pg-rewrite | - |
Build
You can build the RPM packages for pg_rewrite using pig build:
Install
You can install pg_rewrite directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:
Install the extension using pig or apt/yum/dnf:
Preload:
Create Extension:
Usage
pg_rewrite: Tool allows read write to the table during the rewriting
pg_rewrite requires wal_level = logical and must be added to shared_preload_libraries. Common use cases include changing column data types, partitioning tables, reordering columns, and moving tables to different tablespaces – all while allowing concurrent reads and writes.
Rewrite a Table
Create the target table with the desired schema, then call rewrite_table():
Both source and target tables must have an identity index (typically a primary key). The function copies all rows, replays concurrent changes via logical decoding, then atomically renames the tables.
Progress Monitoring
Shows ins_initial (initial rows copied), ins, upd, del (concurrent changes applied).
Configuration
rewrite.max_xlock_time– Maximum time (ms) the exclusive lock is held during the final rename stage. Default0(unlimited). Set to e.g.100to limit lock duration to 0.1s; the function will retry if exceeded.
Constraints Handling
- PRIMARY KEY, UNIQUE, EXCLUDE: add to target table before calling
rewrite_table() - CHECK, NOT NULL (PG18+), FOREIGN KEY: created automatically as NOT VALID; validate with
ALTER TABLE ... VALIDATE CONSTRAINT ...
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)