Skip to content

Feature: query timeout #1713

Closed
Closed
@juliusza

Description

@juliusza

I want to start a discussion about built in query timeout.

In our production environment, we encountered a problem with TCP connections getting stuck. Knex pool would fill up with connections that are not responsive. Note that this would be different from statement timeout, where DB decides to terminate query. Instead pg client would decide that connection is unresponsive and emit an error.

This situation can be simulated with firewall rule:

sudo iptables -A INPUT -j DROP -p tcp --destination-port 5432

Or just use this code snippet:

const { Client } = require('pg')
const client = new Client({user: "user", password: "pass", database: "db"})
const util = require('util');
const exec = util.promisify(require('child_process').exec);

async function main() {
   await exec("sudo iptables -D INPUT -j DROP -p tcp --destination-port 5432");
   await client.connect()

   console.log("Client connected");
   await exec("sudo iptables -A INPUT -j DROP -p tcp --destination-port 5432");

   console.log("Now waiting for DB to respond... forever")
   const res = await client.query('SELECT $1::text as message', ['Hello world!'])
   console.log(res.rows[0].message) // Hello world!
   await client.end()
   process.exit(0)
}

main()

A possible implementation can be found here: master...juliusza:query_timeout
I know I need to add tests and also edit the native driver code for a proper PR. But first I'm looking for feedback from the community to assert if such a thing would be useful.

Also perhaps

socket.setTimeout(3000);

could be a viable solution. I need to look into that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions