Files
usenet-indexer/database/migrations/1770000000500_create_release_chunks.ts
T
Daan MeijerandClaude Opus 5.5 821de668d0
Test and deploy / test (push) Successful in 21s
Test and deploy / deploy (push) Skipped
Rebuild: the merger stops when a pass over the file keys decides nothing
Waiting files counted as work, so the merge loop re-read them for its
whole 30 s budget every minute: Postgres used ~a core even without
workers. Now ~4% at rest, and storing a boneless range costs Postgres
~0.7–1.9 s of CPU (it was 33 s with the loop running). Chunk payloads
are stored EXTERNAL (zstd already; no pglz attempt).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 02:49:45 +02:00

37 lines
1.4 KiB
TypeScript

import { BaseSchema } from '@adonisjs/lucid/schema'
import env from '#start/env'
/**
* The chunk store (REBUILD.md, part 3; decision a): one write-once row per
* release per batch, its files encoded by lib/chunks. Partitioned by month
* of writing, in CHUNK_TABLESPACE (the HDD) when set; ChunkPartitions keeps
* the next months' partitions ready. A re-run of a range replaces its chunks
* (by range_id).
*/
export default class extends BaseSchema {
async up() {
const tablespace = env.get('CHUNK_TABLESPACE')
const where = tablespace ? ` TABLESPACE ${tablespace}` : ''
this.schema.raw(`
CREATE TABLE release_chunks (
id bigserial,
release_id bigint NOT NULL,
range_id bigint NOT NULL,
batch_from bigint NOT NULL,
files integer NOT NULL,
segments integer NOT NULL,
payload bytea NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at)${where}`)
// The payload is zstd already: no pglz attempt on it.
this.schema.raw(`ALTER TABLE release_chunks ALTER COLUMN payload SET STORAGE EXTERNAL`)
this.schema.raw(`CREATE INDEX release_chunks_release_idx ON release_chunks (release_id)`)
this.schema.raw(`CREATE INDEX release_chunks_range_idx ON release_chunks (range_id)`)
}
async down() {
this.schema.raw(`DROP TABLE IF EXISTS release_chunks CASCADE`)
}
}