Tidbits | July 30, 2007

Which PostgreSQL backend am I using?

by Frank Wiles

Someone asked me how to determine which PostgreSQL backend a particular client was connected to. Everyone's first thought is to do a ps aux | grep postgres which will show you the IP and user, but if you have different processes connecting from the same IP with the same usernames, how do you know which is which?

One way to tell would be to see which queries are being executed by which backend and match that up to your client side. But you can quickly get confused, especially if the various connections are all executing the same SQL statements, a web application for example.

The simplest way was suggested by Jacob Kaplan-Moss, which is to use the pg_backend_pid() function like:

SELECT pg_backend_pid();

I love it when the solution is something really simple!

2007-07-30T04:25:38 2018-04-18T16:16:03.201038 2007 postgresql