Skip to content

Commit 65fc101

Browse files
authored
Disable background workers (#948)
* disable background workers with params
1 parent ff2802b commit 65fc101

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

.changeset/soft-carpets-kneel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@electric-sql/pglite': patch
3+
---
4+
5+
Disable background workers.

docs/components/Repl.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import { PGlite } from '@electric-sql/pglite'
66
import { vector } from '@electric-sql/pglite/vector'
77
88
const pg = new PGlite({
9+
startParams: [
10+
...PGlite.defaultStartParams,
11+
'-c',
12+
'application_name=PGlite REPL',
13+
],
914
extensions: {
1015
vector,
1116
},

docs/docs/api.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ Path to the directory for storing the Postgres database. You can provide a URI s
9090
})
9191
```
9292

93+
- `startParams?: string[]` <br />
94+
An array of strings that will be passed to the Postgres process. This is the set of parameters one would pass to a native PostgreSQL instance.
95+
96+
```ts
97+
import { PGlite } from '@electric-sql/pglite'
98+
99+
const pg = await PGlite.create({
100+
startParams: [
101+
...PGlite.defaultStartParams,
102+
'-c',
103+
'application_name=My awesome backend',
104+
],
105+
})
106+
```
107+
93108
#### `options.extensions`
94109

95110
PGlite and Postgres extensions are loaded into a PGLite instance on start, and can include both a WASM build of a Postgres extension and/or a PGlite client plugin.

docs/repl/ReplPlayground.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ async function loadPg() {
5353
try {
5454
console.log(`Creating PGlite instance with idb://${dbName}`)
5555
return await PGlite.create({
56+
startParams: [
57+
...PGlite.defaultStartParams,
58+
'-c',
59+
'application_name=PGlite REPL Playground',
60+
],
5661
dataDir: `idb://${dbName}`,
5762
extensions,
5863
})
@@ -93,7 +98,7 @@ async function loadPg() {
9398
}
9499
95100
onMounted(async () => {
96-
pg.value = await loadPg()
101+
doLoadPg()
97102
})
98103
99104
const rootStyle = window.getComputedStyle(document.body)
@@ -179,7 +184,7 @@ async function clearDb() {
179184
if (closed) break
180185
await new Promise((resolve) => setTimeout(resolve, 10))
181186
}
182-
pg.value = await loadPg()
187+
doLoadPg()
183188
}
184189
</script>
185190

packages/pglite/src/pglite.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ export class PGlite
123123
'exit_on_error=false',
124124
'-c',
125125
'log_checkpoints=false',
126+
'-c',
127+
'max_worker_processes=0',
128+
'-c',
129+
'max_parallel_workers=0',
130+
'-c',
131+
'max_parallel_workers_per_gather=0',
126132
]
127133

128134
/**

packages/pglite/tests/basic.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,5 +650,45 @@ await testEsmCjsAndDTC(async (importType) => {
650650
current_role: 'postgres',
651651
})
652652
})
653+
654+
// this tests the parameter 'max_parallel_workers_per_gather=0',
655+
it('it shouldnt use parallel workers on gather', async () => {
656+
const db = await PGlite.create()
657+
658+
const ROWS = 400_000
659+
660+
await db.exec(`
661+
CREATE TABLE t (id SERIAL PRIMARY KEY, val TEXT);
662+
INSERT INTO t (val)
663+
SELECT md5(i::text) FROM generate_series(1, ${ROWS}) AS i;
664+
`)
665+
666+
// when using workers for GATHER, the query plans contains Gather
667+
const plan = await db.query('EXPLAIN SELECT COUNT(*) FROM t')
668+
669+
const hasGather = plan.rows.some((r: any) =>
670+
r['QUERY PLAN'].includes('Gather'),
671+
)
672+
expect(hasGather).toBeFalsy()
673+
674+
const result = await db.query<any>('SELECT COUNT(*) FROM t')
675+
expect(result.rows[0].count).toEqual(ROWS)
676+
})
677+
678+
it('altering startParams should work"', async () => {
679+
const dateTime = Date.now().toString()
680+
const db = await PGlite.create({
681+
startParams: [
682+
...PGlite.defaultStartParams,
683+
'-c',
684+
`application_name=${dateTime}`,
685+
],
686+
})
687+
688+
const databaseAndRole = await db.exec(
689+
`SELECT setting FROM pg_settings WHERE name='application_name'`,
690+
)
691+
expect(databaseAndRole[0].rows[0].setting).toEqual(dateTime)
692+
})
653693
})
654694
})

0 commit comments

Comments
 (0)