func fetchStatDatabase(db *sqlx.DB) (map[string]interface{}, error) { db = db.Unsafe() rows, err := db.Queryx(`SELECT * FROM pg_stat_database`) if err != nil { logger.Errorf("Failed to select pg_stat_database. %s", err) return nil, err } type pgStat struct { XactCommit uint64 `db:"xact_commit"` XactRollback uint64 `db:"xact_rollback"` BlksRead uint64 `db:"blks_read"` BlksHit uint64 `db:"blks_hit"` BlkReadTime *float64 `db:"blk_read_time"` BlkWriteTime *float64 `db:"blk_write_time"` TupReturned uint64 `db:"tup_returned"` TupFetched uint64 `db:"tup_fetched"` TupInserted uint64 `db:"tup_inserted"` TupUpdated uint64 `db:"tup_updated"` TupDeleted uint64 `db:"tup_deleted"` Deadlocks *uint64 `db:"deadlocks"` TempBytes *uint64 `db:"temp_bytes"` } totalStat := pgStat{} for rows.Next() { p := pgStat{} if err := rows.StructScan(&p); err != nil { logger.Warningf("Failed to scan. %s", err) continue } totalStat.XactCommit += p.XactCommit totalStat.XactRollback += p.XactRollback totalStat.BlksRead += p.BlksRead totalStat.BlksHit += p.BlksHit if p.BlkReadTime != nil { if totalStat.BlkReadTime == nil { totalStat.BlkReadTime = p.BlkReadTime } else { *totalStat.BlkReadTime += *p.BlkReadTime } } if p.BlkWriteTime != nil { if totalStat.BlkWriteTime == nil { totalStat.BlkWriteTime = p.BlkWriteTime } else { *totalStat.BlkWriteTime += *p.BlkWriteTime } } totalStat.TupReturned += p.TupReturned totalStat.TupFetched += p.TupFetched totalStat.TupInserted += p.TupInserted totalStat.TupUpdated += p.TupUpdated totalStat.TupDeleted += p.TupDeleted if p.Deadlocks != nil { if totalStat.Deadlocks == nil { totalStat.Deadlocks = p.Deadlocks } else { *totalStat.Deadlocks += *p.Deadlocks } } if p.TempBytes != nil { if totalStat.TempBytes == nil { totalStat.TempBytes = p.TempBytes } else { *totalStat.TempBytes += *p.TempBytes } } } stat := make(map[string]interface{}) stat["xact_commit"] = totalStat.XactCommit stat["xact_rollback"] = totalStat.XactRollback stat["blks_read"] = totalStat.BlksRead stat["blks_hit"] = totalStat.BlksHit if totalStat.BlkReadTime != nil { stat["blk_read_time"] = *totalStat.BlkReadTime } if totalStat.BlkWriteTime != nil { stat["blk_write_time"] = *totalStat.BlkWriteTime } stat["tup_returned"] = totalStat.TupReturned stat["tup_fetched"] = totalStat.TupFetched stat["tup_inserted"] = totalStat.TupInserted stat["tup_updated"] = totalStat.TupUpdated stat["tup_deleted"] = totalStat.TupDeleted if totalStat.Deadlocks != nil { stat["deadlocks"] = *totalStat.Deadlocks } if totalStat.TempBytes != nil { stat["temp_bytes"] = *totalStat.TempBytes } return stat, nil }