diff --git a/doc/DATABASE.md b/doc/DATABASE.md index 15ecde9114a..7a2458af677 100644 --- a/doc/DATABASE.md +++ b/doc/DATABASE.md @@ -139,9 +139,12 @@ DATABASE_APPLICATION_NAME=paperclip # application_name in pg_stat_activity; def `withDedicatedDbConnection` gives an action exclusive use of one session outside the main pool (application name `paperclip-workspace-finalization-lock`). Each -`createDb` owner keeps at most `DEDICATED_CONNECTION_IDLE_MAX` (4) of these -single-connection clients idle for reuse; they follow the same idle timeout and -max lifetime as the main pool. At most `DATABASE_DEDICATED_CONNECTION_MAX` +`createDb` owner keeps released single-connection clients idle for reuse: always +up to `DEDICATED_CONNECTION_IDLE_MAX` (4), and more while idle plus in-use +clients stay within `DATABASE_DEDICATED_CONNECTION_MAX`, so a recurring burst +reuses the sessions the previous burst opened instead of reconnecting. They +follow the same idle timeout and max lifetime as the main pool, so a retained +client closes its session after `DATABASE_IDLE_TIMEOUT_SECONDS` without use. At most `DATABASE_DEDICATED_CONNECTION_MAX` (default 24) dedicated clients are in use at once per owner. A caller that finds the limit reached waits in a first-in, first-out queue. A caller that already holds a dedicated client, or runs inside an open transaction of the diff --git a/packages/db/src/client.ts b/packages/db/src/client.ts index 61d2e52b9d4..c4ea948081a 100644 --- a/packages/db/src/client.ts +++ b/packages/db/src/client.ts @@ -446,7 +446,8 @@ export async function withDedicatedDbConnection(db: Db, action: (dedicated: D } finally { hold.active = false; try { - if (dedicated && completed && !set.closed && !set.ended.has(dedicated) && set.idle.length < DEDICATED_CONNECTION_IDLE_MAX) { + if (dedicated && completed && !set.closed && !set.ended.has(dedicated) + && (set.idle.length < DEDICATED_CONNECTION_IDLE_MAX || set.idle.length + set.inUse <= set.limit)) { set.idle.push(dedicated); } else if (dedicated && !set.ended.has(dedicated)) { await dedicated.$client.end({ timeout: 1 }); diff --git a/packages/db/src/connection-reuse-census.test.ts b/packages/db/src/connection-reuse-census.test.ts new file mode 100644 index 00000000000..094eade4998 --- /dev/null +++ b/packages/db/src/connection-reuse-census.test.ts @@ -0,0 +1,69 @@ +import { sql } from "drizzle-orm"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { checkpointDb, closeCheckpointDb, closeDedicatedDbConnections, createDb, withDedicatedDbConnection, type Db } from "./client.js"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./test-embedded-postgres.js"; + +const support = await getEmbeddedPostgresTestSupport(); + +async function sessionsOpened(observer: Db) { + const [row] = await observer.execute<{ sessions: string }>(sql`select sessions::text as sessions from pg_stat_database where datname = current_database()`); + return Number(row!.sessions); +} + +async function runConcurrently(total: number, concurrency: number, work: () => Promise) { + let next = 0; + await Promise.all(Array.from({ length: concurrency }, async () => { + while (next < total) { next++; await work(); } + })); +} + +describe.skipIf(!support.supported)("connections opened per 1,000 operations", () => { + let temporary: Awaited>; + let observer: Db; + + beforeAll(async () => { + temporary = await startEmbeddedPostgresTestDatabase("connection-reuse-"); + observer = createDb(temporary.connectionString, { maxConnections: 1, applicationName: "connection-reuse-observer" }); + await sessionsOpened(observer); + }, 90_000); + afterAll(async () => { + await observer?.$client.end({ timeout: 1 }); + await temporary?.cleanup(); + }); + + async function measure(open: (db: Db) => Promise) { + const db = createDb(temporary.connectionString, { maxConnections: 10 }); + await db.execute(sql`select 1`); + const before = await sessionsOpened(observer); + try { + await open(db); + return await sessionsOpened(observer) - before; + } finally { + await closeCheckpointDb(db, 1); + await closeDedicatedDbConnections(db, 1); + await db.$client.end({ timeout: 1 }); + } + } + + it.each([1, 7, 24])("checkpoint saves at concurrency %i reuse the checkpoint pool", async concurrency => { + const opened = await measure(db => runConcurrently(1_000, concurrency, () => + checkpointDb(db).transaction(async tx => { await tx.execute(sql`select 1`); }))); + expect(opened).toBeLessThanOrEqual(3); + }); + + it.each([1, 4, 8, 16])("dedicated connection uses at concurrency %i", async concurrency => { + const opened = await measure(db => runConcurrently(1_000, concurrency, () => + withDedicatedDbConnection(db, connection => connection.transaction(async tx => { await tx.execute(sql`select 1`); })))); + expect(opened).toBeLessThanOrEqual(concurrency); + }); + + it.each([8, 16])("bursts of %i dedicated connection uses reuse the connections the last burst opened", async width => { + const opened = await measure(async db => { + for (let burst = 0; burst < 1_000 / width; burst++) { + await Promise.all(Array.from({ length: width }, () => + withDedicatedDbConnection(db, connection => connection.transaction(async tx => { await tx.execute(sql`select 1`); })))); + } + }); + expect(opened).toBeLessThanOrEqual(width); + }); +}); diff --git a/packages/db/src/dedicated-connection-pool.test.ts b/packages/db/src/dedicated-connection-pool.test.ts index 74933f3ec31..835883fbeb8 100644 --- a/packages/db/src/dedicated-connection-pool.test.ts +++ b/packages/db/src/dedicated-connection-pool.test.ts @@ -73,8 +73,9 @@ describe.skipIf(!support.supported)("withDedicatedDbConnection connection reuse" expect(seen.size).toBeLessThanOrEqual(DEDICATED_CONNECTION_IDLE_MAX); }); - it("gives concurrent callers exclusive sessions and keeps at most the cap open afterwards", async () => { - const db = owner(); + it("gives concurrent callers exclusive sessions, reuses them for the next burst and closes them once idle", async () => { + const db = createDb(temporary.connectionString, { maxConnections: 2, idleTimeoutSeconds: 1 }); + owners.push(db); const concurrency = DEDICATED_CONNECTION_IDLE_MAX + 3; let release!: () => void; const gate = new Promise(resolve => { release = resolve; }); @@ -94,12 +95,16 @@ describe.skipIf(!support.supported)("withDedicatedDbConnection connection reuse" release(); const sessions = await Promise.all(held); expect(new Set(sessions.map(key)).size).toBe(concurrency); - expect((await eventually(openDedicatedSessions, open => open.length <= DEDICATED_CONNECTION_IDLE_MAX)).length) - .toBeLessThanOrEqual(DEDICATED_CONNECTION_IDLE_MAX); const retained = new Set(sessions.map(key)); - for (let i = 0; i < 20; i++) { - expect(retained.has(key(await withDedicatedDbConnection(db, session)))).toBe(true); - } + const nextBurst = await Promise.all(Array.from({ length: concurrency }, () => + withDedicatedDbConnection(db, dedicated => dedicated.transaction(async tx => { + const mine = await session(tx); + await tx.execute(sql`select pg_sleep(0.05)`); + return mine; + })))); + expect(nextBurst.every(value => retained.has(key(value)))).toBe(true); + expect(await eventually(openDedicatedSessions, open => open.length === 0)).toEqual([]); + expect(retained.has(key(await withDedicatedDbConnection(db, session)))).toBe(false); }); it("discards a connection whose action failed mid-transaction", async () => {