Skip to content

Convenient access to pg_stat_* statistics, allowing to monitor PostgreSQL instances inside go applications


Notifications You must be signed in to change notification settings


Repository files navigation


Documentation Go Report Card Coverage Status Build Status

pgstats provides convenient access to pg_stat_* statistics, allowing to monitor PostgreSQL instances inside go applications.


go get

API reference

Check out the quick API overview or full documentation on


Want it simple?

  1. Define your connection. You can do it anywhere, at any time and as many times as you want. However, you cannot override the settings once you define the connection. If you want to play with many connections, see the next section

    err := pgstats.DefineConnection("foo", "username", "password")
  2. Now you can collect statistics in any part of your code. If the connection has not been defined before, an error is returned.

    // pg_stat_bgwriter - returns single row
    b, _ := pgstats.PgStatBgWriter()
    // Example result:
    // {446 true}
    // pg_stat_user_tables - returns many rows
    uts, _ := pgstats.PgStatUserTables()
    for _, ut := range uts {
       fmt.Printf("%s - seq_tup_read: %v\n", ut.Relname, ut.SeqTupRead.Int64)
    // Example result:
    // foo - seq_tup_read: 9273
    // bar - seq_tup_read: 10

Want to have multiple connections?

  1. Define them. If you want to free the connection pool after you are done (and you are not exiting your application right away), you can close the connection with Close() method.

    connFoo, _ := pgstats.Connect("foo", "username", "password")
    defer connFoo.Close()
    connBar, _ := pgstats.Connect("bar", "username", "password")
    defer connBar.Close()
  2. Use them.

    // Query both connections
    utf, _ := connFoo.PgStatUserTables()
    utb, _ := connBar.PgStatUserTables()
    // Print first entries in pg_stat_user_tables for both databases
    fmt.Printf("foo: %s - seq_tup_read: %v\n", utf[0].Relname, utf[0].SeqTupRead)
    fmt.Printf("bar: %s - seq_tup_read: %v\n", utb[0].Relname, utb[0].SeqTupRead)
    // foo: example - seq_tup_read: 9273
    // bar: test - seq_tup_read: 10

Want to specify optional connection parameters?

No problem - use functional options:

err := pgstats.DefineConnection("foo", "username", "password", pgstats.Host(""), pgstats.Port(6432))
conn, err := pgstats.Connect("foo", "username", "password", pgstats.SslMode("disable"))

Full reference

Supported PostgreSQL versions

  • 11
  • 10
  • 9.6
  • 9.5
  • 9.4


The library is licensed under the MIT License.