pg_rewrite
pg_rewrite : Tool allows read write to the table during the rewriting
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 5020 | pg_rewrite
|
pg_rewrite
|
2.1.0 |
ADMIN
|
BSD 3-Clause
|
C
|
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--sLd-r
|
No
|
Yes
|
Yes
|
Yes
|
yes
|
no
|
| Relationships | |
|---|---|
| See Also | pg_repack
pg_squeeze
pgfincore
pg_prewarm
pgstattuple
amcheck
pageinspect
pg_visibility
|
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PGDG
|
2.1.0 |
18
17
16
15
14
|
pg_rewrite |
- |
| RPM | PGDG
|
2.1.0 |
18
17
16
15
14
|
pg_rewrite_$v |
- |
| DEB | PGDG
|
2.1.0 |
18
17
16
15
14
|
postgresql-$v-pg-rewrite |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
el8.aarch64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
el9.x86_64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
el9.aarch64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
el10.x86_64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
el10.aarch64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
d12.x86_64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
d12.aarch64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
d13.x86_64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
d13.aarch64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
u22.x86_64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
u22.aarch64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
u24.x86_64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
u24.aarch64
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
PGDG 2.1.0
|
Source
Install
Make sure PGDG repo available:
pig repo add pgdg -u # add pgdg repo and update cacheInstall this extension with pig:
pig install pg_rewrite; # install via package name, for the active PG version
pig install pg_rewrite -v 18; # install for PG 18
pig install pg_rewrite -v 17; # install for PG 17
pig install pg_rewrite -v 16; # install for PG 16
pig install pg_rewrite -v 15; # install for PG 15
pig install pg_rewrite -v 14; # install for PG 14Config this extension to shared_preload_libraries:
shared_preload_libraries = 'pg_rewrite';Create this extension with:
CREATE EXTENSION pg_rewrite;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():
-- Source table
CREATE TABLE measurement (id int, city_id int NOT NULL, logdate date NOT NULL, peaktemp int, PRIMARY KEY(id, logdate));
-- Target table with new schema (e.g., bigint id + partitioning)
CREATE TABLE measurement_aux (id bigint, city_id int NOT NULL, logdate date NOT NULL, peaktemp int, PRIMARY KEY(id, logdate))
PARTITION BY RANGE (logdate);
CREATE TABLE measurement_y2006m02 PARTITION OF measurement_aux FOR VALUES FROM ('2006-02-01') TO ('2006-03-01');
-- Perform the rewrite (copies data, applies concurrent changes, then renames)
SELECT rewrite_table('measurement', 'measurement_aux', 'measurement_old');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
SELECT * FROM pg_rewrite_progress;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.
SET rewrite.max_xlock_time TO 100;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 ...