1 year ago

#362430

test-img

jonotree

Why are there always at least 10 sessions for a postgreSQL database? Why can't they be terminated?

Original aim: rename a database using ALTER DATABASE via psql.

Problem: rename fails due to other sessions accessing target database.
・All terminals/applications I am aware of have been closed.
・querying pg_stat_activity shows that there are 10 processes(=sessions?) accessing the db.
・The username for each session is the same user I have been using for psql and for some local phoenix and django apps. The client_addr is also local host for all of them.
・When I use pg_terminate_backend, on any of the pids, another process gets immediately spawned.
・After restarting my pc, 10 processes are again spawned.

Concern: As I can't account for these 10 processes that I can't get rid of, I think I'm misunderstanding how postgres works somewhere.

Question: Why do 10 session/processes connected to a particular one of my databases, and why can't I terminate them using pg_terminate_backend?

Note: In the phoenix project I set up recently, I set the and set the pool_size of the Repo config to 10 - which makes me think it's related...but I'm pretty sure that project isn't running in any way.

Update - Solved

As a_horse_with_no_name suggested, the by doing the following I was able to put a stop to the 10 mystery sessions.

(1) prevent login of user responsible for the sessions (identifiable by querying `pg_stat_activity`), by doing `alter user .... with nologin`

(2)-running pg_terminate_backend on each of the session's pids.

After those steps I was able to change the table name.

The remaining puzzle is, how did those sessions get in that status in the first place... from the contents of pg_stat_activity, the wait_event value for each was clientRead. From this post, it seems that the application may have been forcibly stopped halfway through a transaction or something, leaving postgres hanging.

postgresql

session

ecto

terminate

0 Answers

Your Answer

Accepted video resources