Skip to content

Instantly share code, notes, and snippets.

@hyperion-cs
hyperion-cs / probably_orphaned.sql
Last active March 12, 2025 07:19
PostgreSQL probably orphaned files
WITH opt AS (
SELECT '1 day'::interval older_than
),base_files AS (
SELECT
format('base/%s/%s', db.oid::text, ls.file) path,
ls.file, substring(ls.file, '^\d+') parent
FROM pg_database db
CROSS JOIN LATERAL pg_ls_dir('base/' || db.oid::text) ls(file)
WHERE db.datname = current_database() AND ls.file ~ '^\d+'
),probably_orphaned_parents AS (