Data retention policy lifecycle

From CarlosCaPe/octorato · View interactive version

Data retention policy lifecycle is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Data Retention Policy Lifecycle

How to use this skill

Skill file

Data Retention Policy Lifecycle

What

A five-phase approach to implementing data retention policies in PostgreSQL:

size analysis, rule definition, maintenance procedure, scheduled execution,

and validation with rollback.

Why

Tables like audit logs, error logs, and shift history grow unbounded. Without

a retention policy, they consume storage, slow backups, and degrade query

performance. A proper retention lifecycle ensures data is purged safely,

automatically, and with full audit trail.

How

Phase 1: Size analysis

-- How big is the table? How fast is it growing?
SELECT
    pg_size_pretty(pg_total_relation_size('"ShiftAuditLog"')) AS total_size,
    (SELECT COUNT(*) FROM public."ShiftAuditLog") AS row_count,
    MIN("CreatedDate") AS oldest_row,
    MAX("CreatedDate") AS newest_row
FROM public."ShiftAuditLog";

Phase 2: Rule definition

Document the retention rule with business justification:

| Table | Retention | Justification |
|-------|-----------|---------------|
| ShiftAuditLog | 90 days | Audit requirement: 90-day lookback |
| ErrorLog | 30 days | Debugging window: 30 days max |
| RefreshTokens | 7 days past expiry | Expired tokens have no value |

Phase 3: Maintenance procedure

CREATE OR REPLACE PROCEDURE maintenance.purge_shift_audit_log(
    p_retention_days int DEFAULT 90
)
LANGUAGE plpgsql AS $$
DECLARE
    v_cutoff timestamptz;
    v_deleted bigint;
BEGIN
    v_cutoff := now() - (p_retention_days || ' days')::interval;

    DELETE FROM public."ShiftAuditLog"
    WHERE "CreatedDate" < v_cutoff;

    GET DIAGNOSTICS v_deleted = ROW_COUNT;
    RAISE NOTICE 'Purged % rows older than %', v_deleted, v_cutoff;
END;
$$;

Phase 4: Schedule via pg_cron (see Skill #16 for full pg_cron patterns)

SELECT cron.schedule(
    'purge-shift-audit-log',
    '0 3 * * 0',  -- weekly, Sunday 3 AM
    $$CALL maintenance.purge_shift_audit_log(90)$$
);

Phase 5: Validate + rollback plan

-- Validate: check job ran and purged rows
SELECT * FROM cron.job_run_details
WHERE jobid = (SELECT jobid FROM cron.job WHERE jobname = 'purge-shift-audit-log')
ORDER BY start_time DESC LIMIT 5;

-- Rollback: disable if needed
SELECT cron.unschedule('purge-shift-audit-log');

Supporting indexes

Always ensure the retention column is indexed (Skill #10):

CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_shiftauditlog_createddate
    ON public."ShiftAuditLog" ("CreatedDate");

Without this index, the DELETE does a sequential scan of the entire table.

When to Use

Where We Used It

Related Skills

References

-- see Backlog #13 (Log rotation without partitioning) and

"Maintenance, vacuum, and bloat" section

Gotchas

to know WHY 90 days was chosen


Category: Process | Origin: , ,

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.