I've been struggling with this issue for the past 48 hours and after reading lots of answers here, blogs, articles, documentation, etc... I still cannot find a solution!
Basically, I have a lambda function with a 2-minute timeout. Based on logs and insights, it processes fine most of the requests, but it randomly fails with a "timed out" error when trying to execute the transaction below.
Lambda code (chopped for legibility):
import pgp from 'pg-promise'
import logger from '../lib/logger'
const Database = pgp()
const db = Database({
connectionString: process.env.DATABASE_URL,
max: 3,
idleTimeoutMillis: 10000,
})
db.connect()
.then(() => logger.info('Successfully connected to the PG database'))
.catch(err => logger.error({ err }))
export const handler = async (event, context) => {
logger.info('transaction start...')
await db.tx(async tx => {
await tx.none(
`
INSERT INTO...`,
[someValue1, someValue2]
)
const updatedRow = await tx.one(
`
UPDATE Something...`,
[somethingId]
)
return someFunction(updatedRow)
})
logger.info('transaction end...')
}
const someFunction = async (data) => {
return db.task('someTask', async ctx => {
const value = await ctx.oneOrNone(
`SELECT * FROM Something...`,
[data.id]
)
if (!value) {
return
}
const doStuff = async (points) =>
ctx.none(
`UPDATE Something WHERE id =.....`,
[points]
)
// increment points x miles
if (data.condition1) {
await doStuff(10)
}
if (data.condition2) {
await doStuff(20)
}
if (data.condition3) {
await doStuff(30)
}
})
}
I see that the transaction starts but never ends, so the function is inevitably killed by timeout.
I read the whole wiki in pg-promise and understood everything about tweaks, performance, good practices, etc. But still, something is very wrong.
You can see that I also changed the pool size and max timeout for experimenting, but it didn't fix the issue.
Any ideas?
Thanks!