|
| 1 | +# Dagster Retention & PostgreSQL Backups |
| 2 | + |
| 3 | +How this deployment prunes Dagster history safely and how to back up / restore the |
| 4 | +Dagster PostgreSQL database. Addresses issue #132 (the previous cleanup deleted |
| 5 | +materialization/observation/asset-check state, which broke automation and audit |
| 6 | +history). |
| 7 | + |
| 8 | +## Retention by event class |
| 9 | + |
| 10 | +| Class | What it is | Where it lives | Retention | Managed by | |
| 11 | +|---|---|---|---|---| |
| 12 | +| **Materializations / observations / asset checks** | Asset state that AutomationConditions, "latest materialization", asset checks, and lineage read | `event_logs` (structured) + `asset_materializations`, `asset_observations`, `asset_check_executions` | **Never deleted** | — | |
| 13 | +| **Structured run/step events** | `RUN_*`, `STEP_*`, `ENGINE_EVENT`, etc. — run history | `event_logs` (`dagster_event_type IS NOT NULL`) | **Never deleted** | — | |
| 14 | +| **Plain log-message rows** | High-volume `context.log.*` lines | `event_logs` (`dagster_event_type IS NULL`) | `DAGSTER_LOG_RETENTION_DAYS` (default **30d**) | `dagster_db_cleanup` (`cleanup_event_logs.sh`) | |
| 15 | +| **Schedule ticks** | Schedule evaluation ticks | `job_ticks` | `purge_after_days: 90` | Dagster `retention:` (dagster.yaml) | |
| 16 | +| **Sensor ticks** | Sensor evaluation ticks | `job_ticks` | skipped 7d / failure 30d / success 90d | Dagster `retention:` (dagster.yaml) | |
| 17 | + |
| 18 | +Key rule: **orchestration and audit state is never deleted by a cleanup job.** |
| 19 | +Only disposable log-message rows and schedule/sensor ticks age out, and the tick |
| 20 | +purge uses Dagster's own supported `retention:` config rather than raw SQL |
| 21 | +against internal tables. |
| 22 | + |
| 23 | +### Why not delete the asset index tables? |
| 24 | + |
| 25 | +`asset_materializations` / `asset_observations` / `asset_check_executions` are how |
| 26 | +Dagster answers "what is the latest materialization of this asset?" Deleting them |
| 27 | +makes materialized assets look un-materialized, which can retrigger |
| 28 | +AutomationConditions and destroys asset-check and lineage history. The cleanup |
| 29 | +job therefore never touches them, and only deletes `event_logs` rows that carry |
| 30 | +no structured event (`dagster_event_type IS NULL`) — those never have a row in |
| 31 | +the index tables, so no state can be orphaned. |
| 32 | + |
| 33 | +## Configuration |
| 34 | + |
| 35 | +Set in `.env` (see `.env.dagster.example`): |
| 36 | + |
| 37 | +| Variable | Default | Effect | |
| 38 | +|---|---|---| |
| 39 | +| `DAGSTER_LOG_RETENTION_DAYS` | `30` | Age after which plain log rows are pruned | |
| 40 | +| `DAGSTER_LOG_CLEANUP_BATCH_SIZE` | `5000` | Rows deleted per batch (bounds lock time) | |
| 41 | +| `DAGSTER_EVENT_CLEANUP_INTERVAL_SECONDS` | `3600` | Cleanup loop interval | |
| 42 | +| `DAGSTER_BACKUP_INTERVAL_SECONDS` | `86400` | Backup cadence (daily) | |
| 43 | +| `DAGSTER_BACKUP_RETENTION_DAYS` | `14` | Age after which backups are pruned | |
| 44 | +| `DAGSTER_BACKUP_GPG_PASSPHRASE` | _(empty)_ | If set (and `gpg` present), symmetrically encrypts each dump | |
| 45 | + |
| 46 | +> The legacy `DAGSTER_EVENT_RETENTION_HOURS` is still honored by |
| 47 | +> `cleanup_event_logs.sh` (converted to days) for backward compatibility, but it |
| 48 | +> now affects **only** plain log rows — never structured events. |
| 49 | +
|
| 50 | +## Backups |
| 51 | + |
| 52 | +The `dagster_db_backup` service runs `pg_dump` on `DAGSTER_BACKUP_INTERVAL_SECONDS`, |
| 53 | +writing `dagster_<UTC-timestamp>.sql.gz` to the `dagster_backups` volume and |
| 54 | +pruning dumps older than `DAGSTER_BACKUP_RETENTION_DAYS`. |
| 55 | + |
| 56 | +**Encryption.** GCP persistent disks are encrypted at rest by default (AES-256), |
| 57 | +so a backups volume on a GCP PD is already encrypted. Set |
| 58 | +`DAGSTER_BACKUP_GPG_PASSPHRASE` to add application-level symmetric encryption |
| 59 | +(produces `.sql.gz.gpg`); this requires `gpg` in the image. |
| 60 | + |
| 61 | +**Off-host copies (recommended).** The volume lives on the same VM as the DB. For |
| 62 | +real disaster recovery, sync `dagster_backups` to object storage, e.g.: |
| 63 | + |
| 64 | +```bash |
| 65 | +gcloud storage rsync -r /var/lib/docker/volumes/<project>_dagster_backups/_data \ |
| 66 | + gs://<your-backup-bucket>/dagster-postgres/ |
| 67 | +``` |
| 68 | + |
| 69 | +## Objectives |
| 70 | + |
| 71 | +- **RPO (Recovery Point Objective): 24 hours.** With daily backups, at most one |
| 72 | + day of Dagster metadata (run/materialization history) can be lost. Lower |
| 73 | + `DAGSTER_BACKUP_INTERVAL_SECONDS` for a tighter RPO. |
| 74 | +- **RTO (Recovery Time Objective): ~15 minutes.** Restore is a single |
| 75 | + `gunzip | psql` into a fresh database plus a service restart. |
| 76 | + |
| 77 | +Note: the warehouse data itself (BigQuery) is the system of record for analytics; |
| 78 | +these backups protect Dagster's **orchestration metadata**, not the analytical |
| 79 | +tables. |
| 80 | + |
| 81 | +## Restore drill |
| 82 | + |
| 83 | +Run this in a non-production environment periodically to prove backups are valid. |
| 84 | + |
| 85 | +```bash |
| 86 | +# 1. Pick a backup from the volume. |
| 87 | +docker compose run --rm --entrypoint sh dagster_db_backup \ |
| 88 | + -c 'ls -1t /backups/dagster_*.sql.gz* | head' |
| 89 | + |
| 90 | +# 2. Create a scratch database to restore into (does not touch the live DB). |
| 91 | +docker compose exec dagster_postgresql \ |
| 92 | + psql -U dagster_user -d postgres -c 'CREATE DATABASE dagster_restore_test;' |
| 93 | + |
| 94 | +# 3. Restore the dump into the scratch DB. |
| 95 | +# (If the file ends in .gpg, first: gpg --batch --passphrase "$PASS" -d file.sql.gz.gpg > file.sql.gz) |
| 96 | +docker compose exec dagster_db_backup sh -c \ |
| 97 | + 'gunzip -c /backups/<chosen>.sql.gz | psql -h dagster_postgresql -U dagster_user -d dagster_restore_test' |
| 98 | + |
| 99 | +# 4. Verify asset state survived — expect a non-zero count. |
| 100 | +docker compose exec dagster_postgresql psql -U dagster_user -d dagster_restore_test \ |
| 101 | + -c 'SELECT count(*) AS materializations FROM asset_materializations;' |
| 102 | + |
| 103 | +# 5. Clean up. |
| 104 | +docker compose exec dagster_postgresql \ |
| 105 | + psql -U dagster_user -d postgres -c 'DROP DATABASE dagster_restore_test;' |
| 106 | +``` |
| 107 | + |
| 108 | +**Production restore:** stop the Dagster services, restore the dump into the |
| 109 | +`dagster` database (drop/recreate or restore into an empty DB), then restart: |
| 110 | + |
| 111 | +```bash |
| 112 | +docker compose stop dagster_webserver dagster_daemon dagster_user_code |
| 113 | +gunzip -c /backups/<chosen>.sql.gz | \ |
| 114 | + docker compose exec -T dagster_postgresql psql -U dagster_user -d dagster |
| 115 | +docker compose start dagster_user_code dagster_daemon dagster_webserver |
| 116 | +``` |
0 commit comments