Recover Lost PostgreSQL Data with pg_recovery: Step‑by‑Step Guide
This article introduces the open‑source pg_recovery tool for PostgreSQL, explains how to install it, and demonstrates recovering data lost through UPDATE, DELETE, ROLLBACK, and DROP COLUMN operations with practical command‑line examples.
Quickly restoring lost data is a critical requirement for any database, and PostgreSQL offers several official tools; the open‑source community also provides useful alternatives.
What is pg_recovery?
pg_recovery is a PostgreSQL data‑recovery extension that can restore changes caused by COMMIT, DELETE, UPDATE, ROLLBACK, and DROP COLUMN operations and returns the recovered rows as a table. The source code is available at https://github.com/radondb/pg_recovery .
Quick Installation
$ make PG_CONFIG=/home/lzzhang/PG/postgresql/base/bin/pg_config
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -g -O0 -fPIC -I. -I./ -I/home/lzzhang/PG/postgresql/base/include/server -I/home/lzzhang/PG/postgresql/base/include/internal -D_GNU_SOURCE -c -o pg_recovery.o pg_recovery.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -g -O0 -fPIC -shared -o pg_recovery.so pg_recovery.o -L/home/lzzhang/PG/postgresql/base/lib -Wl,--as-needed -Wl,-rpath,'/home/lzzhang/PG/postgresql/base/lib',--enable-new-dtags
$ make install PG_CONFIG=/home/lzzhang/PG/postgresql/base/bin/pg_config
/usr/bin/mkdir -p '/home/lzzhang/PG/postgresql/base/lib'
/usr/bin/mkdir -p '/home/lzzhang/PG/postgresql/base/share/extension'
/usr/bin/install -c -m 755 pg_recovery.so '/home/lzzhang/PG/postgresql/base/lib/pg_recovery.so'
/usr/bin/install -c -m 644 .//pg_recovery.control '/home/lzzhang/PG/postgresql/base/share/extension/'
/usr/bin/install -c -m 644 .//pg_recovery--1.0.sql '/home/lzzhang/PG/postgresql/base/share/extension/'After installing the shared library, create the extension in the database:
$ create extension pg_recovery ;
CREATE EXTENSIONData Recovery Demonstration
Prepare Initial Data
$ create table lzzhang(id int, dp int);
CREATE TABLE
$ insert into lzzhang values(1, 1);
INSERT 0 1
$ insert into lzzhang values(2, 2);
INSERT 0 1Recover UPDATE Data
$ update lzzhang set id=3, dp=3;
UPDATE 2
lzzhang=# select * from pg_recovery('lzzhang') as (id int, dp int);
id | dp
----+----
1 | 1
2 | 2
(2 rows)
$ select * from lzzhang;
id | dp
----+----
3 | 3
3 | 3
(2 rows)Recover DELETE Data
$ delete from lzzhang;
DELETE 2
lzzhang=# select * from lzzhang;
id | dp
----+----
(0 rows)
$ select * from pg_recovery('lzzhang') as (id int, dp int);
id | dp
----+----
1 | 1
2 | 2
3 | 3
3 | 3
(4 rows)Recover ROLLBACK Data
$ begin ;
BEGIN
$ insert into lzzhang values(4, 4);
INSERT 0 1
$ rollback ;
ROLLBACK
$ select * from lzzhang;
id | dp
----+----
(0 rows)
$ select * from pg_recovery('lzzhang') as (id int, dp int);
id | dp
----+----
1 | 1
2 | 2
3 | 3
3 | 3
4 | 4
(5 rows)Recover DROP COLUMN Data
$ alter table lzzhang drop column dp;
ALTER TABLE
$ select attnum from pg_attribute, pg_class where attrelid = pg_class.oid and pg_class.relname='lzzhang' and attname ~ 'dropped';
attnum
--------
2
(1 row)
$ select * from lzzhang;
id
----
(0 rows)
$ select * from pg_recovery('lzzhang') as (id int, dropped_attnum_2 int);
id | dropped_attnum_2
----+------------------
1 | 1
2 | 2
3 | 3
3 | 3
4 | 4
(5 rows)Show All Recovered Data
$ insert into lzzhang values(5);
INSERT 0 1
$ select * from lzzhang;
id
----
5
(1 row)
$ select * from pg_recovery('lzzhang', recoveryrow => false) as (id int, recoveryrow bool);
id | recoveryrow
----+-------------
1 | t
2 | t
3 | t
3 | t
4 | t
5 | f
(6 rows)Important Notes
Supported PostgreSQL versions : pg_recovery works with PostgreSQL 12, 13, and 14.
Recoverable transaction count : The number of transactions that can be recovered depends on the vacuum_defer_cleanup_age setting. Increase this parameter if you need to recover a larger volume of data.
Lock behavior : While pg_recovery is active, normal read‑locks are allowed; when it is not in use, the extension imposes no additional overhead and does not require service downtime.
Qingyun Technology Community
Official account of the Qingyun Technology Community, focusing on tech innovation, supporting developers, and sharing knowledge. Born to Learn and Share!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
