Read this text Tracing PostgreSQL with perf. Or this perf CPU sampling. This text contains short summary.

  1. Install perf:
    sudo apt-get install linux-tools-3.16

    (or other version – depends on your OS)

  2. Install postgresql server debug symbols:
    sudo apt-get install postgresql-9.6-dbg

    (or other version based on your PG)

  3. check postgres processes:
    sudo perf top -u postgres