Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect orphaned files? #299

Open
Krysztophe opened this issue Oct 21, 2021 · 0 comments
Open

Detect orphaned files? #299

Krysztophe opened this issue Oct 21, 2021 · 0 comments

Comments

@Krysztophe
Copy link
Collaborator

There is a query detecting orphaned files that has slipped into pgstats, and some of you may recognize it:
https://github.com/gleu/pgstats/blob/master/pgreport_queries.h#L136

Of course, there must be a big warning about the danger of blind deleting, but I think it could be another service.
To avoid useless warnings, only report files above a few Mb and older than 24h?

WITH ver AS (
    SELECT
        current_setting('server_version_num') pgversion,
        v::INTEGER / 10000 || '.' || MOD(v::INTEGER, 10000) / 100 AS version
    FROM
        current_setting('server_version_num') v
),
tbl_paths AS (
    SELECT
        tbs.oid AS tbs_oid,
        spcname,
        'pg_tblspc/' || tbs.oid || '/' || (
            SELECT
                dir
            FROM
                pg_ls_dir('pg_tblspc/' || tbs.oid || '/', TRUE, FALSE) dir
            WHERE
                dir LIKE E'PG\\_' || ver.version || E'\\_%') AS tbl_path
    FROM
        pg_tablespace tbs,
        ver
    WHERE
        tbs.spcname NOT IN ('pg_default', 'pg_global')
),
files AS (
    SELECT
        d.oid AS database_oid,
        0 AS tbs_oid,
        'base/' || d.oid AS path,
        file_name AS file_name,
        SUBSTRING(file_name FROM E'[0-9]+') AS base_name
    FROM
        pg_database d,
        pg_ls_dir('base/' || d.oid, TRUE, FALSE) AS file_name
    WHERE
        d.datname = current_database()
    UNION ALL
    SELECT
        d.oid,
        tbp.tbs_oid,
        tbl_path || '/' || d.oid,
        file_name,
        (SUBSTRING(file_name FROM E'[0-9]+')) AS base_name
    FROM
        pg_database d,
        tbl_paths tbp,
        pg_ls_dir(tbp.tbl_path || '/' || d.oid, TRUE, FALSE) AS file_name
    WHERE
        d.datname = current_database()
),
orphans AS (
    SELECT
        tbs_oid,
        base_name,
        file_name,
        current_setting('data_directory') || '/' || path || '/' || file_name AS orphaned_file,
        pg_filenode_relation(tbs_oid, base_name::oid) AS rel_without_pgclass
    FROM
        ver,
        files
        LEFT JOIN pg_class c ON (c.relfilenode::text = files.base_name
                OR (c.oid::text = files.base_name
                    AND c.relfilenode = 0
                    AND c.relname LIKE 'pg_%'))
    WHERE
        c.oid IS NULL
        AND LOWER(file_name)
        NOT LIKE 'pg_%'
)
SELECT
    orphaned_file,
    pg_size_pretty((pg_stat_file(orphaned_file)).size) AS file_size,
    (pg_stat_file(orphaned_file)).modification AS modification_date,
    current_database()
FROM
    orphans
WHERE
    rel_without_pgclass IS NULL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants