|
| 1 | +/** |
| 2 | + * Turns off crash durability on a CI job's throwaway Postgres service. No suite survives a crash of |
| 3 | + * its database, and flushing each commit to the runner's disk is most of a bulk write's cost. |
| 4 | + * Visibility, locking and isolation are unchanged, so every suite checks the same behavior. |
| 5 | + * `synchronous_commit` stays on: asynchronous commits delay hint bits and VACUUM's all-visible |
| 6 | + * marking until the WAL flush, which turns the index-only plans the plan tests assert into heap |
| 7 | + * fetches. With `fsync` off that flush costs nothing anyway. |
| 8 | + * |
| 9 | + * Service containers take no `-c` flags; each setting reloads without a restart. Exits non-zero |
| 10 | + * unless a fresh session reports all of them applied. |
| 11 | + * |
| 12 | + * Usage (from packages/db): DATABASE_URL=... bun scripts/tune-ci-postgres.ts |
| 13 | + */ |
| 14 | +import postgres from 'postgres' |
| 15 | + |
| 16 | +const SETTINGS = { fsync: 'off', full_page_writes: 'off' } as const |
| 17 | + |
| 18 | +const url = process.env.DATABASE_URL |
| 19 | +if (!url) throw new Error('DATABASE_URL is required') |
| 20 | + |
| 21 | +const admin = postgres(url, { max: 1, onnotice: () => {} }) |
| 22 | +for (const [name, value] of Object.entries(SETTINGS)) { |
| 23 | + await admin.unsafe(`ALTER SYSTEM SET ${name} = ${value}`) |
| 24 | +} |
| 25 | +await admin`SELECT pg_reload_conf()` |
| 26 | +await admin.end() |
| 27 | + |
| 28 | +let applied: Record<string, string> = {} |
| 29 | +for (let attempt = 0; attempt < 10; attempt++) { |
| 30 | + // pg_reload_conf only signals the server, so read from a new session until it has reloaded. |
| 31 | + const probe = postgres(url, { max: 1 }) |
| 32 | + const rows = await probe<{ name: string; setting: string }[]>` |
| 33 | + SELECT name, setting FROM pg_settings WHERE name IN ${probe(Object.keys(SETTINGS))}` |
| 34 | + await probe.end() |
| 35 | + applied = Object.fromEntries(rows.map((row) => [row.name, row.setting])) |
| 36 | + if (Object.entries(SETTINGS).every(([name, value]) => applied[name] === value)) { |
| 37 | + console.log('Postgres durability off:', applied) |
| 38 | + process.exit(0) |
| 39 | + } |
| 40 | + await Bun.sleep(500) |
| 41 | +} |
| 42 | +console.error('::error::Postgres did not apply the CI durability settings:', applied) |
| 43 | +process.exit(1) |
0 commit comments