Search code examples
postgresqldatabase-performance

How do I gather statistics about function calls occurring in a Postgres schema?


I am looking for a way to efficiently gather statistics that would tell me how many times each function gets called in a given period of time.

By efficient, I mean the stat collector should use as few disk I/O operations as possible.

It would be nice if I can also track some more stats like the return time however this is not necessary.


Solution

  • It's already built-in.

    Check out the system view pg_stat_user_functions
    http://www.postgresql.org/docs/current/static/monitoring-stats.html#PG-STAT-USER-FUNCTIONS-VIEW

    Note that you need to enable that in your postgresql.conf:
    http://www.postgresql.org/docs/current/static/monitoring-stats.html#MONITORING-STATS-SETUP

    To reset the collected statistics, use pg_stat_reset():
    http://www.postgresql.org/docs/current/static/monitoring-stats.html#MONITORING-STATS-FUNCTIONS