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:
I love it when the solution is something really simple!