Skip to content

Commit e045b59

Browse files
[world-postgres] Add maxPoolSize config for graphile (#1527)
Co-authored-by: Peter Wielander <mittgfu@gmail.com>
1 parent 52db376 commit e045b59

5 files changed

Lines changed: 35 additions & 2 deletions

File tree

.changeset/large-ideas-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@workflow/world-postgres": patch
3+
---
4+
5+
Add maxPoolSize configuration

docs/content/docs/deploying/world/postgres-world.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ Prefix for graphile-worker queue job names. Useful when sharing a database betwe
160160

161161
Number of concurrent workers polling for jobs. Default: `10`
162162

163+
### `WORKFLOW_POSTGRES_MAX_POOL_SIZE`
164+
165+
Maximum size of the internal `pg.Pool` used when `createWorld()` constructs the pool. Default: `10`
166+
167+
For higher worker concurrency, Graphile Worker recommends setting `maxPoolSize` to `10` or `queueConcurrency + 2`, whichever is larger.
168+
163169
### Programmatic configuration
164170

165171
{/* @skip-typecheck: incomplete code sample */}
@@ -170,6 +176,7 @@ const world = createWorld({
170176
connectionString: "postgres://user:password@host:5432/database",
171177
jobPrefix: "myapp_",
172178
queueConcurrency: 20,
179+
maxPoolSize: 20, // overrides WORKFLOW_POSTGRES_MAX_POOL_SIZE
173180
});
174181
```
175182

packages/world-postgres/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export WORKFLOW_POSTGRES_JOB_PREFIX="myapp"
3535

3636
# Optional: Worker concurrency (default: 10)
3737
export WORKFLOW_POSTGRES_WORKER_CONCURRENCY="10"
38+
39+
# Optional: Internal pg.Pool max size (default: 10)
40+
export WORKFLOW_POSTGRES_MAX_POOL_SIZE="10"
3841
```
3942

4043
### Programmatic Usage
@@ -49,6 +52,7 @@ const world = createWorld({
4952
connectionString: "postgres://username:password@localhost:5432/database",
5053
jobPrefix: "myapp", // optional
5154
queueConcurrency: 10, // optional
55+
maxPoolSize: 10, // optional, overrides WORKFLOW_POSTGRES_MAX_POOL_SIZE when `pool` is omitted
5256
});
5357

5458
// Or pass an existing pg.Pool (shared with your app Drizzle, etc.); `world.close()` will not end it.
@@ -62,6 +66,7 @@ const worldFromPool = createWorld({ pool });
6266
| Option | Type | Default | Description |
6367
| ------------------ | --------- | -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
6468
| `connectionString` | `string` | `process.env.WORKFLOW_POSTGRES_URL` or `'postgres://world:world@localhost:5432/world'` | Used only when `pool` is omitted, to construct an internal pool |
69+
| `maxPoolSize` | `number` | `process.env.WORKFLOW_POSTGRES_MAX_POOL_SIZE` or `pg.Pool` default (`10`) | Optional. Sets the internal `pg.Pool` max size when `createWorld()` creates the pool |
6570
| `pool` | `pg.Pool` || Optional. When set, used for Drizzle, Graphile Worker, and stream writes. `world.close()` does not end it. |
6671
| `jobPrefix` | `string` | `process.env.WORKFLOW_POSTGRES_JOB_PREFIX` | Optional prefix for queue job names |
6772
| `queueConcurrency` | `number` | `10` | Number of concurrent active step executions per process |
@@ -74,6 +79,11 @@ const worldFromPool = createWorld({ pool });
7479
| `WORKFLOW_POSTGRES_URL` | PostgreSQL connection string | `'postgres://world:world@localhost:5432/world'` |
7580
| `WORKFLOW_POSTGRES_JOB_PREFIX` | Prefix for queue job names | - |
7681
| `WORKFLOW_POSTGRES_WORKER_CONCURRENCY` | Number of concurrent workers | `10` |
82+
| `WORKFLOW_POSTGRES_MAX_POOL_SIZE` | Internal `pg.Pool` max size | `10` |
83+
84+
When `pool` is omitted, `maxPoolSize` precedence is: `createWorld({ maxPoolSize })`, then `WORKFLOW_POSTGRES_MAX_POOL_SIZE`, then the `pg.Pool` default.
85+
86+
For higher worker concurrency, Graphile Worker recommends setting `maxPoolSize` to `10` or `queueConcurrency + 2`, whichever is larger.
7787

7888
## Database Setup
7989

packages/world-postgres/src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Pool } from 'pg';
22

33
type PgConnectionConfig =
4-
| { connectionString: string; pool?: undefined }
5-
| { pool: Pool; connectionString?: undefined };
4+
| { connectionString: string; maxPoolSize?: number; pool?: undefined }
5+
| { pool: Pool; connectionString?: undefined; maxPoolSize?: undefined };
66

77
export type PostgresWorldConfig = PgConnectionConfig & {
88
jobPrefix?: string;

packages/world-postgres/src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ function createStorage(drizzle: Drizzle): Storage {
2020
};
2121
}
2222

23+
function getDefaultMaxPoolSize(): number | undefined {
24+
const parsed = parseInt(
25+
process.env.WORKFLOW_POSTGRES_MAX_POOL_SIZE || '',
26+
10
27+
);
28+
29+
return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
30+
}
31+
2332
export function createWorld(
2433
config: PostgresWorldConfig = {
2534
connectionString:
@@ -31,12 +40,14 @@ export function createWorld(
3140
10,
3241
}
3342
): World & { start(): Promise<void> } {
43+
const maxPoolSize = config.maxPoolSize ?? getDefaultMaxPoolSize();
3444
const pool =
3545
config.pool ||
3646
new Pool({
3747
connectionString:
3848
config.connectionString ||
3949
'postgres://world:world@localhost:5432/world',
50+
...(maxPoolSize !== undefined ? { max: maxPoolSize } : {}),
4051
});
4152

4253
const drizzle = createClient(pool);

0 commit comments

Comments
 (0)