Skip to content

Alter statement_timeout on existing pool. #2016

Closed

Description

@dbauszus-glx

We set a statement_timeout of 10 seconds on the pool when we create it.

    const pool = new require('pg').Pool({
      connectionString: ***,
      statement_timeout: 10000
    });

Every now and then we have a query of which we know that it will run longer.

Is it possible to disable the timeout in a single query?

Is it possible to alter the statement_timeout on the pool prior to the query then set it back to it's original value?

Thanks for looking into this.

Best,
Dennis

Activity

  1. sehrope commented on Nov 29, 2019

    @sehrope
    Contributor

    There's nothing built-in to support this but you can do it with a custom wrapper:

    const DEFAULT_STATEMENT_TIMEOUT_MILLIS = 10 * 1000;
    
    const pool = new pg.Pool({ 
        statement_timeout: DEFAULT_STATEMENT_TIMEOUT_MILLIS,
     });
    
    const customQuery = async (query, params) => {
        const client = await pool.connect();
        try {
            // Set our custom timeout:
            await client.query('SET statement_timeout = 0');
            
            // Run the query
            const result = await client.query(query, params);
    
            // Revert the timeout
            await client.query(`SET statement_timeout = ${DEFAULT_STATEMENT_TIMEOUT_MILLIS}`);
    
            // All successful so release the connection:
            await client.release();
    
            // Return back the actual result from the executed command
            return result;
        } catch (err) {
            // Some error so discard the connection
            await client.release(err);
    
            // Bubble up the error
            throw err;
        }
    }
  2. charmander commented on Nov 30, 2019

    @charmander
    Collaborator

    You could also have a separate pool for long queries. Its use of resources isn’t as nice, but it is simple.

  3. dbauszus-glx commented on Dec 1, 2019

    @dbauszus-glx
    Author

    That works great. I already had my query in an async function anyways. Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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