Skip to content

How to keep connection open and reuse clients? #3011

@Rc85

Description

@Rc85

Currently, I am seeing a new row added to pg_stat_activity on every request. Once it reaches around 30 rows, I can no longer perform anymore queries. I could lower the idle timeout to like 1 second but it would still hit 30 rows if requests were made faster than the timeout. Here is my setup

// db.js
import { Pool } from 'pg';

const confings = { ... };

export const db = new Pool(configs);

// profile.js
import { db } from './configs/db';
import express from 'express';

const app = express();

app.post('/profile', async(req, resp, next) => {
  const client = await db.connect();

  try {
    await client.query('BEGIN');

    await client.query('COMMIT');
  } catch (e) {
    console.log(e);
  } finally {
    client.release();
  }
});

If 30+ simultaneous requests hits this route, I would get a connection timeout error and my app would crash.

Is this the right way to do it? How can I handle more than 30+ requests at the same or short time?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions