Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions scripts/terminate_idle_connections.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a temporary workaround, so I'm not entirely at ease with merging this and risking to make it a long lived hack.

Unless that's what the koji developers recommend?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not what koji developers recommend at all. Quoting Mike McLean from :

Koji works on a one connection per thead model. If httpd leaves threads active, then they will keep their db connection.
See discussion in https://pagure.io/koji/issue/4175

Issue #4175: Add an limit for the database connections - koji - Pagure.io

Most likely the solution to your issue lies in httpd configuration
That said, it is normal for kojihub to keep a lot of connections open.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed a temporary workaround.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python3

import psycopg2

IDLE_TIMEOUT_MINUTES = 10

def main():
with psycopg2.connect(dbname="postgres") as conn, conn.cursor() as cur:
sql_query = f"""
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE
state = 'idle'
AND NOW() - state_change > INTERVAL '{IDLE_TIMEOUT_MINUTES} minutes'
AND pid <> pg_backend_pid();
"""
cur.execute(sql_query)
num_terminated = len(cur.fetchall())
if num_terminated > 0:
print(f"terminated {num_terminated} connections")

if __name__ == "__main__":
main()