Skip to content

Commit ae21bdc

Browse files
committed
improvement(ci): turn off crash durability on throwaway CI postgres
1 parent 170279a commit ae21bdc

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎.github/workflows/checks.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ jobs:
145145
with:
146146
provider: ${{ vars.CI_PROVIDER }}
147147

148+
# No suite survives a database crash; see the script for what this does and does not change.
149+
- name: Turn off Postgres crash durability
150+
run: bun scripts/tune-ci-postgres.ts
151+
148152
- name: Provision a fresh database through the supported command
149153
working-directory: packages/db
150154
run: |
@@ -265,6 +269,10 @@ jobs:
265269
with:
266270
provider: github
267271

272+
# No suite survives a database crash; see the script for what this does and does not change.
273+
- name: Turn off Postgres crash durability
274+
run: bun scripts/tune-ci-postgres.ts
275+
268276
# Migrations create their own extensions, as on a fresh self-hosted install.
269277
- name: Provision the database through migrations
270278
if: matrix.group != 'project-files'
@@ -383,6 +391,10 @@ jobs:
383391
with:
384392
provider: ${{ vars.CI_PROVIDER }}
385393

394+
# No suite survives a database crash; see the script for what this does and does not change.
395+
- name: Turn off Postgres crash durability
396+
run: bun scripts/tune-ci-postgres.ts
397+
386398
- name: Provision the database through migrations
387399
working-directory: packages/db
388400
run: bun run db:migrate
@@ -502,6 +514,10 @@ jobs:
502514
with:
503515
provider: ${{ vars.CI_PROVIDER }}
504516

517+
# No suite survives a database crash; see the script for what this does and does not change.
518+
- name: Turn off Postgres crash durability
519+
run: bun scripts/tune-ci-postgres.ts
520+
505521
- name: Provision the database through migrations
506522
working-directory: packages/db
507523
run: bun run db:migrate

‎scripts/tune-ci-postgres.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
*
6+
* Service containers take no `-c` flags; each setting reloads without a restart. Exits non-zero
7+
* unless a fresh session reports all of them applied.
8+
*
9+
* Usage: DATABASE_URL=... bun scripts/tune-ci-postgres.ts
10+
*/
11+
import postgres from 'postgres'
12+
13+
const SETTINGS = { fsync: 'off', full_page_writes: 'off', synchronous_commit: 'off' } as const
14+
15+
const url = process.env.DATABASE_URL
16+
if (!url) throw new Error('DATABASE_URL is required')
17+
18+
const admin = postgres(url, { max: 1, onnotice: () => {} })
19+
for (const [name, value] of Object.entries(SETTINGS)) {
20+
await admin.unsafe(`ALTER SYSTEM SET ${name} = ${value}`)
21+
}
22+
await admin`SELECT pg_reload_conf()`
23+
await admin.end()
24+
25+
let applied: Record<string, string> = {}
26+
for (let attempt = 0; attempt < 10; attempt++) {
27+
// pg_reload_conf only signals the server, so read from a new session until it has reloaded.
28+
const probe = postgres(url, { max: 1 })
29+
const rows = await probe<{ name: string; setting: string }[]>`
30+
SELECT name, setting FROM pg_settings WHERE name IN ${probe(Object.keys(SETTINGS))}`
31+
await probe.end()
32+
applied = Object.fromEntries(rows.map((row) => [row.name, row.setting]))
33+
if (Object.entries(SETTINGS).every(([name, value]) => applied[name] === value)) {
34+
console.log('Postgres durability off:', applied)
35+
process.exit(0)
36+
}
37+
await Bun.sleep(500)
38+
}
39+
console.error('::error::Postgres did not apply the CI durability settings:', applied)
40+
process.exit(1)

0 commit comments

Comments
 (0)