Reclaiming Disk After Retention
Breeze’s nightly retention jobs delete old rows from high-volume history tables. PostgreSQL reuses the freed pages for new rows, but it does not hand them back to the operating system. On an install that has run for months before retention was enabled, device_commands and device_filesystem_snapshots can stay large on disk after the backlog is drained.
This page is an optional, one-time operator task. Nothing in Breeze requires it. Skip it if disk is not under pressure: the freed space is already reused by new rows.
Prerequisites
Section titled “Prerequisites”- Retention is enforcing. Both jobs are report-only by default and delete nothing until
DEVICE_COMMAND_RETENTION_ENFORCE=trueandFILESYSTEM_SNAPSHOT_RETENTION_ENFORCE=trueare set (see Environment Variables). AREPORT-ONLYlog line means no drain has happened yet. - The drain is done. The nightly retention run caps the batches it deletes, so a large backlog is cleared over several nights. Wait until the API logs stop showing
Hit the N-batch cap on <table> with rows still eligiblefor both tables. - Free disk headroom. Per the pg_repack documentation, a full-table repack needs free disk about twice the size of the target table and its indexes.
VACUUM FULLneeds room for a second full copy of the table (and its rebuilt indexes). Measure first (step 1) and make sure the data volume, plus WAL growth and any replicas, can absorb it. - A tested backup. See the Upgrade Guide for
./scripts/backup.sh --db. - The right role. Run as a superuser, or as the owner of the tables and their indexes with
--no-superuser-check. Thebreeze_appruntime role is deliberately unprivileged and is not the table owner; use your database owner or admin role. - A primary key. pg_repack requires each target table to have a primary key or a unique index on a
NOT NULLcolumn. Both tables have a primary key. - The extension. pg_repack is a PostgreSQL extension plus a client program. Check that your Postgres runs a version your pg_repack release supports (the 1.5.3 documentation lists PostgreSQL 9.5 through 18), then:
- Self-hosted Postgres: install the pg_repack package matching your Postgres major version in the database container or host, and the client program wherever you will run it.
- Managed Postgres: pg_repack is available only if your provider lists it among its supported extensions, and some providers restrict which role may create it. Check the provider’s extension list. If it is not offered, use the
VACUUM FULLfallback. - Create the extension in the Breeze database:
CREATE EXTENSION pg_repack; - Keep the client program and the installed extension on the same pg_repack version.
Repack with pg_repack
Section titled “Repack with pg_repack”-
Measure the starting size
SELECT c.relname,pg_size_pretty(pg_table_size(c.oid)) AS table_incl_toast,pg_size_pretty(pg_indexes_size(c.oid)) AS indexes,pg_size_pretty(pg_total_relation_size(c.oid)) AS totalFROM pg_class cWHERE c.relname IN ('device_commands', 'device_filesystem_snapshots')AND c.relkind = 'r'AND c.relnamespace = 'public'::regnamespace;Record the
totalvalues and the free space on the data volume. -
Preview
--dry-runlists what would be repacked and exits.Terminal window pg_repack --dry-run \--host <db-host> --port 5432 --username <admin-role> --dbname <breeze-db> \--table public.device_commands \--table public.device_filesystem_snapshots -
Repack one table at a time
Terminal window pg_repack --no-superuser-check \--host <db-host> --port 5432 --username <table-owner-role> --dbname <breeze-db> \--wait-timeout 300 --no-kill-backend \--table public.device_commandsRepeat for
public.device_filesystem_snapshots. Omit--no-superuser-checkwhen connecting as a superuser. Authenticate withPGPASSWORDor a.pgpassfile rather than a command-line password.--table(-t) limits the run to the named table. The switch can be repeated, but one table per run keeps the disk headroom requirement lower and a failure easier to reason about.--wait-timeout(-T) is how many seconds pg_repack waits for its exclusive locks (default 60).--no-kill-backend(-D) makes pg_repack skip the table instead of cancelling conflicting queries when it cannot get the lock. Without it, pg_repack may cancel other sessions that block it, so keep it for a production database. If a table is skipped, run it again in a quieter period.--jobs(-j) rebuilds indexes in parallel using extra connections. It speeds up a repack, at the cost of more load.
-
Verify (next section), then continue with the second table.
Lock caveats
Section titled “Lock caveats”-
pg_repack builds a new copy of the table while the original stays online. It holds an
ACCESS EXCLUSIVElock only briefly, at the start and during the final swap. For the rest of the run it takes aSHARE UPDATE EXCLUSIVElock, so inserts, updates, and deletes continue. -
Even the short exclusive locks queue behind long-running transactions on the same table, and queries then queue behind pg_repack. Start during a quiet period and check for old open transactions first:
SELECT pid, state, xact_start, left(query, 80) AS queryFROM pg_stat_activityWHERE xact_start < now() - interval '5 minutes'ORDER BY xact_start; -
Do not run DDL against the target tables while pg_repack is working, and do not apply a Breeze upgrade (which runs migrations) at the same time.
-
Expect extra write-ahead log and I/O while the new copy is built and indexes are rebuilt. If you stream WAL to replicas, confirm they can keep up.
-
After a fatal error, pg_repack can leave helper objects behind. Clean up by hand, as the pg_repack documentation describes: in the affected database run
DROP EXTENSION pg_repack CASCADE;and thenCREATE EXTENSION pg_repack;.
Verify the reclaimed space
Section titled “Verify the reclaimed space”- Re-run the size query from step 1 and compare the
totalvalues. - Compare free space on the data volume at the OS or provider level. The database-level sizes shrink first; the volume reflects it once the old files are dropped.
- Confirm the application is healthy: the API readiness endpoint responds and device command traffic is flowing.
- Plain
ANALYZEis not needed afterwards; pg_repack runs it after a full-table repack unless you pass--no-analyze.
Fallback: VACUUM FULL in a maintenance window
Section titled “Fallback: VACUUM FULL in a maintenance window”Use this when pg_repack is not available on your Postgres (common on managed services) or you cannot run the client program.
VACUUM FULL rewrites the table into a new file, which returns unused space to the operating system, but it takes an ACCESS EXCLUSIVE lock on the table for the whole run. Reads and writes to that table block until it finishes. For these tables that means agents cannot report command results or filesystem snapshots, and the API requests that touch them wait or time out. PostgreSQL documents it as not recommended for routine use, and it needs free disk for a full second copy of the table.
-
Schedule a window. Announce it, and stop or pause the API and worker processes that write to these tables so requests do not pile up.
-
Take a backup, then confirm the free-disk requirement from the prerequisites.
-
Run
VACUUM FULLper table as the table owner or a superuser:VACUUM (FULL, VERBOSE, ANALYZE) public.device_commands;VACUUM (FULL, VERBOSE, ANALYZE) public.device_filesystem_snapshots;VERBOSEprints progress per table, andANALYZErefreshes planner statistics in the same pass. -
Bring the API and workers back up and verify as above.
References
Section titled “References”Commands and flags on this page were checked against these upstream documents:
- pg_repack 1.5.3: pg_repack project documentation
- PostgreSQL 16,
VACUUM: PostgreSQL VACUUM reference