We can set 2 timeouts for the Client:
statement_timeout
: number of milliseconds before a statement in query will time out, default is no timeoutquery_timeout
number of milliseconds before a query call will timeout, default is no timeoutI understand it like this:
statement_timeout
will be passed to the database (see postgres-docs:statement_timeout) and when a statement takes longer than this, the database will abort the query and return an errorquery_timeout
. This is handled by the drive (node-postgres
). When this timeout is reached, node-postgres
will just stop listening for a response, but the database may still be executing the queryQuestion 1 Should we set the query timeout slightly longer than the statement timeout?
I think so because then:
What is the situation when we use transactions?
e.g. when we look at the example from the docs:
try {
await client.query('BEGIN')
const queryText = 'INSERT INTO users(name) VALUES($1) RETURNING id'
const res = await client.query(queryText, ['brianc'])
const insertPhotoText = 'INSERT INTO photos(user_id, photo_url) VALUES ($1, $2)'
const insertPhotoValues = [res.rows[0].id, 's3.bucket.foo']
await client.query(insertPhotoText, insertPhotoValues)
await client.query('COMMIT')
} catch (e) {
await client.query('ROLLBACK')
throw e
}
So in the happy path, we call query()
5 times: and the query/statement timouts are applied per query()
call.
Question 3 So also the BEGIN
/COMMIT
queries have the same timeout a the INSERT
queries, right?
After some tests using pg-promise, I came to this conclusion: the query-timeout
should be slightly longer than the statement-timeout
query-timeout
= 10secstatement-timeout
= undefinedIn a transaction we send pgsleep(60)
(sleep for 60 seconds)
Then this will happen:
BEGIN
statement is sent to the dbpgsleep(60)
is sent to the dbROLLBACK
Query read timeout error
query-timeout
= undefinedstatement-timeout
= 10secIn a transaction we send pgsleep(60)
(sleep for 60 seconds)
Then this will happen:
BEGIN
statement is sent to the dbpgsleep(60)
is sent to the db57014
: canceling statement due to statement timeout
So we should prefer to get a statement-timeout
and set the query-timeout
slightly longer, in case that the database cannot send back the error (I guess this could be the case when there are e.g. network issues)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With