Node PostgreSQL timeout a query by the client

You can setup statement_timeout in the client:

const { Client } = require('pg')

const client = new Client({
  statement_timeout: 10000
})

or in the pool:

const { Pool } = require('pg')

const pool = new Pool({
  statement_timeout: 10000
})

Best practice is using an init query, to set query timeout for the session.

    SET statement_timeout TO 15000; # x milliseconds or 0 (turns off limitation)

This takes an argument of the timeout in ms, and is applied for the session. Afterwards, when a query takes longer than the value specified, you will receive an error from the server. Note this is on user's request:

    ERROR:  Query (150) cancelled on user's request

Also note this actually cancels the query on the server side, reducing load.