Skip to content

Commit 5cf6b3c

Browse files
committed
set session timeouts in toast-vacuum
1 parent 6cb7b30 commit 5cf6b3c

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/utils/toast-vacuum.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ async function checkAndVacuumToast(pool: Pool): Promise<void> {
5454
}
5555
}
5656

57+
async function configureSessionTimeouts(pool: Pool): Promise<void> {
58+
const client = await pool.connect()
59+
try {
60+
const dbName = process.env.DB_NAME || 'squid'
61+
await client.query(`ALTER DATABASE "${dbName}" SET idle_in_transaction_session_timeout = '2min'`)
62+
await client.query(`ALTER DATABASE "${dbName}" SET idle_session_timeout = '5min'`)
63+
console.log('[toast-vacuum] Configured database session timeouts (idle_in_transaction=2min, idle_session=5min)')
64+
} catch (err) {
65+
console.error('[toast-vacuum] Error configuring session timeouts:', err)
66+
} finally {
67+
client.release()
68+
}
69+
}
70+
5771
export function startToastVacuumMonitor(): void {
5872
const pool = new Pool({
5973
host: process.env.DB_HOST,
@@ -67,8 +81,8 @@ export function startToastVacuumMonitor(): void {
6781
statement_timeout: 3600000,
6882
})
6983

70-
// Run immediately on launch
71-
checkAndVacuumToast(pool).then(() => {
84+
// Configure database-level session timeouts to prevent idle transactions from blocking VACUUM
85+
configureSessionTimeouts(pool).then(() => checkAndVacuumToast(pool)).then(() => {
7286
// Then repeat every 30 minutes
7387
setInterval(() => {
7488
checkAndVacuumToast(pool)

0 commit comments

Comments
 (0)