3

I'm looking for help with TypeORM and PostgreSQL. To avoid long running queries, I would like to set a statement timeout at the connection level.

How can I do this?

3 Answers 3

6

Connection config that's working for me:

{
    name: "default",
    // .....
    extra: {
        application_name: "your_app_name",
        statement_timeout: 30000 // 30s
    }
}

We can check how these extra options are used in the driver: https://github.com/typeorm/typeorm/blob/68a5c230776f6ad4e3ee7adea5ad4ecdce033c7e/src/driver/postgres/PostgresDriver.ts#L1361

Available options are listed here: https://node-postgres.com/api/pool

And here: https://node-postgres.com/api/client

The config passed to the pool is also passed to every client instance within the pool when the pool creates that client.

Sign up to request clarification or add additional context in comments.

Comments

3

TypeORM documentation

maxQueryExecutionTime If query execution time exceed this given max execution time (in milliseconds) then logger will log this query.

If that doesn't do what you want you can use extra to send postgres driver configuration.

extra - Extra connection options to be passed to the underlying driver. Use it if you want to pass extra settings to underlying database driver.

3 Comments

According to the documentation it will only log to the console. Does it also abort the query? maxQueryExecutionTime - If query execution time exceed this given max execution time (in milliseconds) then logger will log this query.
You "said set a statement timeout" which was a bit vague... That's true for maxQueryExecutionTime which is why I also pointed out extra and you can send more postgres specifc params through that. node-postgres.com/api/client
Indeed you did mention extra. Thank you very much for your time Jim, I'll take a look further. I already upvoted your answer.
0

For PostgreSQL you can pass query_timeout option directly to pg driver through extra.

{
   type: 'postgres',
   ...
   extra: {
       query_timeout: 2500
   }
}

https://github.com/typeorm/typeorm/issues/5429#issuecomment-939919873

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.