Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

294
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!