Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

297
Vistas
fastapi leaving Postgres idle connection

I want to decrease the number of idle PostgreSQL requests coming in from fast-api calls. I am not able to figure out what exactly is leaving this many idle connections as multiple people are using this DB and APIs associated with it.

Can someone suggest what I might have done wrong to leave this many idle connections or an efficient way to figure out what is causing this so that I can accordingly fix that portion and decrease it somehow.

Not sure if I have provided sufficient information to explain this but if anything else is required, I will be more than happy to provide that information.

postgresql idle connection screenshot

This is how I am creating a PostgreSQL object via fastapi

class postgres:
    def __init__(self, config):
        try:
            SQLALCHEMY_DATABASE_URL = "postgresql://" + \
                config['postgresql']['user']+":"+config['postgresql']['password']+"@" + \
                config['postgresql']['host']+":5432" + \
                "/"+config['postgresql']['database']
            # print(SQLALCHEMY_DATABASE_URL)
            engine = create_engine(SQLALCHEMY_DATABASE_URL, future=True)
            self.SessionLocal = sessionmaker(
                autocommit=False, autoflush=True, bind=engine)
        except Exception as e:
            raise

    def get_db(self):
        """
        Function to return session variable used by ORM
        :return: SessionLocal
        """
        try:
            db = self.SessionLocal()
            yield db
        finally:
            db.close()
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I believe this is caused by the get_db(self), as it creates (yields) a new connection/session object every time you call SessionLocal().

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda