pg_orphaned

Deal with orphaned files

Overview

PackageVersionCategoryLicenseLanguage
pg_orphaned1.0ADMINPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
5200pg_orphanedNoYesNoYesNoNo-
Relatedpg_dirtyread pg_surgery amcheck pageinspect pg_visibility pg_checksums pg_catcheck pg_repack

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.01817161514pg_orphaned-
RPMPIGSTY1.01817161514pg_orphaned_$v-
DEBPIGSTY1.01817161514postgresql-$v-pg-orphaned-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d12.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d13.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d13.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0

Build

You can build the RPM / DEB packages for pg_orphaned using pig build:

pig build pkg pg_orphaned         # build RPM / DEB packages

Install

You can install pg_orphaned directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_orphaned;          # Install for current active PG version
pig ext install -y pg_orphaned -v 18  # PG 18
pig ext install -y pg_orphaned -v 17  # PG 17
pig ext install -y pg_orphaned -v 16  # PG 16
pig ext install -y pg_orphaned -v 15  # PG 15
pig ext install -y pg_orphaned -v 14  # PG 14
dnf install -y pg_orphaned_18       # PG 18
dnf install -y pg_orphaned_17       # PG 17
dnf install -y pg_orphaned_16       # PG 16
dnf install -y pg_orphaned_15       # PG 15
dnf install -y pg_orphaned_14       # PG 14
apt install -y postgresql-18-pg-orphaned   # PG 18
apt install -y postgresql-17-pg-orphaned   # PG 17
apt install -y postgresql-16-pg-orphaned   # PG 16
apt install -y postgresql-15-pg-orphaned   # PG 15
apt install -y postgresql-14-pg-orphaned   # PG 14

Create Extension:

CREATE EXTENSION pg_orphaned;

Usage

pg_orphaned: Deal with orphaned files

pg_orphaned provides functions to detect and manage orphaned data files in PostgreSQL. It handles corner cases like in-progress transactions that could cause false positives by using a dirty snapshot.

List Orphaned Files

-- List orphaned files (default: older than 1 day marked as "older")
SELECT * FROM pg_list_orphaned();

-- Custom age threshold
SELECT * FROM pg_list_orphaned('10 seconds');
SELECT * FROM pg_list_orphaned('1 minute');

Returns: dbname, path, name, size, mod_time, relfilenode, reloid, older (boolean).

Move Orphaned Files to Backup

-- Move files older than the threshold to orphaned_backup directory
SELECT pg_move_orphaned('1 minute');

List Moved Files

SELECT * FROM pg_list_orphaned_moved();

Move Files Back (if still orphaned)

SELECT pg_move_back_orphaned();

Remove Moved Files

SELECT pg_remove_moved_orphaned();

Typical Workflow

-- 1. Check for orphaned files
SELECT * FROM pg_list_orphaned('1 minute');

-- 2. Move them to backup (only those older than threshold)
SELECT pg_move_orphaned('1 minute');

-- 3. Verify what was moved
SELECT * FROM pg_list_orphaned_moved();

-- 4. After confirming, remove the backup files
SELECT pg_remove_moved_orphaned();

Note: functions operate on orphaned files for the database you are connected to. Always double-check carefully before moving or removing files.


Last Modified 2026-03-12: add pg extension catalog (95749bf)