Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda