diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index ead3aff1634..78bb91b7f53 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -265,6 +265,11 @@ jobs: with: provider: github + # No suite survives a database crash; see the script for what this does and does not change. + - name: Turn off Postgres crash durability + working-directory: packages/db + run: bun scripts/tune-ci-postgres.ts + # Migrations create their own extensions, as on a fresh self-hosted install. - name: Provision the database through migrations if: matrix.group != 'project-files' @@ -386,6 +391,11 @@ jobs: with: provider: ${{ vars.CI_PROVIDER }} + # No suite survives a database crash; see the script for what this does and does not change. + - name: Turn off Postgres crash durability + working-directory: packages/db + run: bun scripts/tune-ci-postgres.ts + - name: Provision the database through migrations working-directory: packages/db run: bun run db:migrate @@ -505,6 +515,11 @@ jobs: with: provider: ${{ vars.CI_PROVIDER }} + # No suite survives a database crash; see the script for what this does and does not change. + - name: Turn off Postgres crash durability + working-directory: packages/db + run: bun scripts/tune-ci-postgres.ts + - name: Provision the database through migrations working-directory: packages/db run: bun run db:migrate diff --git a/packages/db/scripts/tune-ci-postgres.ts b/packages/db/scripts/tune-ci-postgres.ts new file mode 100644 index 00000000000..fcd0692fd05 --- /dev/null +++ b/packages/db/scripts/tune-ci-postgres.ts @@ -0,0 +1,43 @@ +/** + * Turns off crash durability on a CI job's throwaway Postgres service. No suite survives a crash of + * its database, and flushing each commit to the runner's disk is most of a bulk write's cost. + * Visibility, locking and isolation are unchanged, so every suite checks the same behavior. + * `synchronous_commit` stays on: asynchronous commits delay hint bits and VACUUM's all-visible + * marking until the WAL flush, which turns the index-only plans the plan tests assert into heap + * fetches. With `fsync` off that flush costs nothing anyway. + * + * Service containers take no `-c` flags; each setting reloads without a restart. Exits non-zero + * unless a fresh session reports all of them applied. + * + * Usage (from packages/db): DATABASE_URL=... bun scripts/tune-ci-postgres.ts + */ +import postgres from 'postgres' + +const SETTINGS = { fsync: 'off', full_page_writes: 'off' } as const + +const url = process.env.DATABASE_URL +if (!url) throw new Error('DATABASE_URL is required') + +const admin = postgres(url, { max: 1, onnotice: () => {} }) +for (const [name, value] of Object.entries(SETTINGS)) { + await admin.unsafe(`ALTER SYSTEM SET ${name} = ${value}`) +} +await admin`SELECT pg_reload_conf()` +await admin.end() + +let applied: Record = {} +for (let attempt = 0; attempt < 10; attempt++) { + // pg_reload_conf only signals the server, so read from a new session until it has reloaded. + const probe = postgres(url, { max: 1 }) + const rows = await probe<{ name: string; setting: string }[]>` + SELECT name, setting FROM pg_settings WHERE name IN ${probe(Object.keys(SETTINGS))}` + await probe.end() + applied = Object.fromEntries(rows.map((row) => [row.name, row.setting])) + if (Object.entries(SETTINGS).every(([name, value]) => applied[name] === value)) { + console.log('Postgres durability off:', applied) + process.exit(0) + } + await Bun.sleep(500) +} +console.error('::error::Postgres did not apply the CI durability settings:', applied) +process.exit(1)