Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions internal/pgmetrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,22 @@ func (d *defaultStatsCollector) QueryStats(ctx context.Context, db *sql.DB) (Pos
COALESCE(SUM(xact_commit), 0) AS xact_commit,
COALESCE(SUM(xact_rollback), 0) AS xact_rollback,
COALESCE(SUM(blks_read), 0) AS blks_read,
COALESCE(MIN(stats_reset), now()) AS stats_reset
FROM pg_stat_database
WHERE stats_reset IS NOT NULL;
MIN(stats_reset) AS stats_reset
FROM pg_stat_database;
`
var stats PostgresStats
var statsReset sql.NullTime
err := db.QueryRowContext(ctx, query).Scan(
&stats.XactCommit,
&stats.XactRollback,
&stats.BlksRead,
&stats.StatsReset,
&statsReset,
)
if err != nil {
return PostgresStats{}, fmt.Errorf("query stats: %w", err)
}
if statsReset.Valid {
stats.StatsReset = statsReset.Time
}
return stats, nil
}
40 changes: 39 additions & 1 deletion test/integration/pgmetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func setupPostgresContainer(t *testing.T) (*postgres.PostgresContainer, credenti
ctx := context.Background()

pgContainer, err := postgres.Run(ctx,
"postgres:16-alpine",
"postgres:18-alpine",
postgres.WithDatabase(testDatabase),
postgres.WithUsername(testUser),
postgres.WithPassword(testPassword),
Expand Down Expand Up @@ -165,6 +165,44 @@ func TestCollector_Collect_CacheHitRatio(t *testing.T) {
"cache hit ratio should be greater than 0 after queries")
}

func TestCollector_Collect_DeltaBasedTPS(t *testing.T) {
_, cred := setupPostgresContainer(t)

// Generate some initial activity to ensure stats_reset is populated
connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
cred.Host, cred.Port, cred.Username, *cred.Password, cred.Database)
db, err := sql.Open("postgres", connStr)
require.NoError(t, err)
defer db.Close()

ctx := context.Background()
_, err = db.ExecContext(ctx, "SELECT 1")
require.NoError(t, err)

collector := pgmetrics.New()

// First collection establishes baseline, returns 0 for rate metrics
metrics1, err := collector.Collect(cred)
require.NoError(t, err)
assert.Equal(t, 0.0, metrics1.TransactionsPerSecond, "first collection should return 0 TPS")
assert.Equal(t, 0.0, metrics1.CommittedTxPerSecond, "first collection should return 0 committed TPS")

// Generate transactions between collections
for range 100 {
_, err = db.ExecContext(ctx, "SELECT 1")
require.NoError(t, err)
}

// PostgreSQL stats flush at minimum 1-second intervals
time.Sleep(1 * time.Second)

// Second collection calculates delta-based TPS
metrics2, err := collector.Collect(cred)
require.NoError(t, err)
assert.Greater(t, metrics2.TransactionsPerSecond, 0.0, "second collection should have TPS > 0")
assert.Greater(t, metrics2.CommittedTxPerSecond, 0.0, "second collection should have committed TPS > 0")
}

func BenchmarkCollector_Collect(b *testing.B) {
_, cred := setupPostgresContainer(&testing.T{})
setupTestData(&testing.T{}, cred)
Expand Down