The pg_stat_activity view is a wealth of information for a database administrator. Unfortunately, it is all but useless for monitoring due to the security measures that encumber it. Principally, some of these fields are obfuscated specifically to prevent system compromises and data leaks. Thus we must prevent abusing the view while still loosening the security enough to enable better monitoring.
The first step we take is to create a function that is capable of returning a set of rows similar to the pg_stat_activity view itself. The SETOF modifier tells PostgreSQL that our function does exactly that. It's no coincidence that the body of our function is merely a SELECT statement on the pg_stat_activity view.
Why did we use a ...