diff --git a/apps/backend/scripts/archive-cost-exceptions.ts b/apps/backend/scripts/archive-cost-exceptions.ts index b029db4a..a694c615 100644 --- a/apps/backend/scripts/archive-cost-exceptions.ts +++ b/apps/backend/scripts/archive-cost-exceptions.ts @@ -7,6 +7,7 @@ * npm run cost-exceptions:archive -- --dry-run * npm run cost-exceptions:archive -- --days=90 * npm run cost-exceptions:archive -- --days=60 --batch-size=2000 + * npm run cost-exceptions:archive -- --days=0 # 一次性清理:归档全部已处理行 */ import process from 'node:process' @@ -19,13 +20,17 @@ import { const options = parseOptions(process.argv.slice(2)) const defaultDays = Number(runtimeConfig.retention?.costExceptionDays) || 60 -const retentionDays = normalizeInteger(options.days, defaultDays, 1, 3650) +// 0 表示不按时间过滤:一次性把已处理的历史异常全部归档(日常任务永远 >= 1 天)。 +const retentionDays = normalizeInteger(options.days, defaultDays, 0, 3650) const batchSize = normalizeInteger(options.batchSize, 1000, 1, 10_000) const dryRun = options.dryRun === 'true' async function main() { const startedAt = Date.now() const archivable = await countArchivableCostExceptions(retentionDays) + if (retentionDays === 0) { + console.warn('[archive] days=0:一次性清理,将归档全部已处理异常(open 行不受影响)') + } if (dryRun) { console.log( diff --git a/docs/成本中心与履约模型.md b/docs/成本中心与履约模型.md index 0c25c919..c99f9f2d 100644 --- a/docs/成本中心与履约模型.md +++ b/docs/成本中心与履约模型.md @@ -482,9 +482,14 @@ npm run revenue:backfill -- --batch-size=2000 --max-batches=100 | 落点 | `cost_exceptions_archive`(保留原 id 与时间字段,加 `archived_at`) | | 执行 | `data-retention` 每日任务;每批 1000 行,`DELETE ... RETURNING` + `INSERT` 同语句,单批原子 | | 手动 | `npm run cost-exceptions:archive -- --dry-run` / `-- --days=90` | +| 一次性清理 | `npm run cost-exceptions:archive -- --days=0`(不按时间过滤,归档全部已处理行;open 仍不归档) | 归档不删除审计痕迹,但热表查询与页面上不会再出现这些历史行;需要追溯时查归档表。 +> **2026-10-25 已执行一次性清理**:`--days=0` 口径归档 151,939 条 resolved 异常(152 批,3.9 秒)。 +> 热表 260,253 → 108,314 行(全部为 open),`VACUUM FULL` 后体积 155 MB → 51 MB(heap 38 / idx 13), +> 归档表 61 MB;主键重叠校验 0 条。生产环境不要在业务时段跑 `VACUUM FULL`,日常由 autovacuum 复用空间即可。 + --- ## 十、关联文档