Skip to content

Commit 83516d7

Browse files
committed
PMM-7 Update collector tests and queries for PostgreSQL 18 compatibility
Refactored IO and statistics queries to accommodate PostgreSQL 18 features, including byte-level statistics and WAL I/O. Updated tests to validate new columns and ensure query expectation checks are correctly enforced. Improved test coverage across different PostgreSQL versions.
1 parent fea454c commit 83516d7

File tree

4 files changed

+72
-22
lines changed

4 files changed

+72
-22
lines changed

collector/pg_stat_database_test.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func TestPGStatDatabaseCollector(t *testing.T) {
5353
"blk_read_time",
5454
"blk_write_time",
5555
"stats_reset",
56+
"parallel_workers_to_launch",
57+
"parallel_workers_launched",
5658
}
5759

5860
srT, err := time.Parse("2006-01-02 15:04:05.00000-07", "2023-05-25 17:10:42.81132-07")
@@ -80,7 +82,10 @@ func TestPGStatDatabaseCollector(t *testing.T) {
8082
925,
8183
16,
8284
823,
83-
srT)
85+
srT,
86+
nil,
87+
nil,
88+
)
8489

8590
mock.ExpectQuery(sanitizeQuery(statDatabaseQueryPre18)).WillReturnRows(rows)
8691

@@ -160,6 +165,8 @@ func TestPGStatDatabaseCollectorNullValues(t *testing.T) {
160165
"blk_read_time",
161166
"blk_write_time",
162167
"stats_reset",
168+
"parallel_workers_to_launch",
169+
"parallel_workers_launched",
163170
}
164171

165172
rows := sqlmock.NewRows(columns).
@@ -182,7 +189,10 @@ func TestPGStatDatabaseCollectorNullValues(t *testing.T) {
182189
925,
183190
16,
184191
823,
185-
srT).
192+
srT,
193+
nil,
194+
nil,
195+
).
186196
AddRow(
187197
"pid",
188198
"postgres",
@@ -202,7 +212,10 @@ func TestPGStatDatabaseCollectorNullValues(t *testing.T) {
202212
925,
203213
16,
204214
823,
205-
srT)
215+
srT,
216+
nil,
217+
nil,
218+
)
206219
mock.ExpectQuery(sanitizeQuery(statDatabaseQueryPre18)).WillReturnRows(rows)
207220

208221
ch := make(chan prometheus.Metric)
@@ -276,6 +289,8 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
276289
"blk_read_time",
277290
"blk_write_time",
278291
"stats_reset",
292+
"parallel_workers_to_launch",
293+
"parallel_workers_launched",
279294
}
280295

281296
srT, err := time.Parse("2006-01-02 15:04:05.00000-07", "2023-05-25 17:10:42.81132-07")
@@ -303,7 +318,10 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
303318
925,
304319
16,
305320
823,
306-
srT).
321+
srT,
322+
nil,
323+
nil,
324+
).
307325
AddRow(
308326
nil,
309327
nil,
@@ -324,6 +342,8 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
324342
nil,
325343
nil,
326344
nil,
345+
nil,
346+
nil,
327347
).
328348
AddRow(
329349
"pid",
@@ -344,7 +364,10 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
344364
926,
345365
17,
346366
824,
347-
srT)
367+
srT,
368+
nil,
369+
nil,
370+
)
348371
mock.ExpectQuery(sanitizeQuery(statDatabaseQueryPre18)).WillReturnRows(rows)
349372

350373
ch := make(chan prometheus.Metric)
@@ -436,6 +459,8 @@ func TestPGStatDatabaseCollectorTestNilStatReset(t *testing.T) {
436459
"blk_read_time",
437460
"blk_write_time",
438461
"stats_reset",
462+
"parallel_workers_to_launch",
463+
"parallel_workers_launched",
439464
}
440465

441466
rows := sqlmock.NewRows(columns).
@@ -458,7 +483,10 @@ func TestPGStatDatabaseCollectorTestNilStatReset(t *testing.T) {
458483
925,
459484
16,
460485
823,
461-
nil)
486+
nil,
487+
nil,
488+
nil,
489+
)
462490

463491
mock.ExpectQuery(sanitizeQuery(statDatabaseQueryPre18)).WillReturnRows(rows)
464492

collector/pg_stat_io.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ var (
8787
)
8888

8989
// PostgreSQL 18+ query with byte statistics and WAL I/O
90-
statIOQuery18Plus = `
90+
StatIOQuery18Plus = `
9191
SELECT
9292
backend_type,
9393
io_context,
@@ -104,7 +104,7 @@ var (
104104
`
105105

106106
// Pre-PostgreSQL 18 query without byte statistics
107-
statIOQueryPre18 = `
107+
StatIOQueryPre18 = `
108108
SELECT
109109
backend_type,
110110
io_context,
@@ -130,9 +130,9 @@ func (c *PGStatIOCollector) Update(ctx context.Context, instance *instance, ch c
130130
db := instance.getDB()
131131

132132
// Use version-specific query for PostgreSQL 18+
133-
query := statIOQueryPre18
133+
query := StatIOQueryPre18
134134
if instance.version.GTE(semver.Version{Major: 18}) {
135-
query = statIOQuery18Plus
135+
query = StatIOQuery18Plus
136136
}
137137

138138
rows, err := db.QueryContext(ctx, query)

collector/pg_stat_io_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestPGStatIOCollector(t *testing.T) {
3434
columns := []string{"backend_type", "io_context", "io_object", "reads", "read_time", "writes", "write_time", "extends", "read_bytes", "write_bytes", "extend_bytes"}
3535
rows := sqlmock.NewRows(columns).
3636
AddRow("client backend", "normal", "relation", 100, 50.5, 75, 25.2, 10, nil, nil, nil)
37-
mock.ExpectQuery("SELECT.*FROM pg_stat_io").WillReturnRows(rows)
37+
mock.ExpectQuery("SELECT.*backend_type.*FROM pg_stat_io").WillReturnRows(rows)
3838

3939
ch := make(chan prometheus.Metric)
4040
go func() {
@@ -47,9 +47,6 @@ func TestPGStatIOCollector(t *testing.T) {
4747
}()
4848

4949
expected := 5 // reads, read_time, writes, write_time, extends (no byte metrics for v16)
50-
if err := mock.ExpectationsWereMet(); err != nil {
51-
t.Errorf("There were unfulfilled expectations: %s", err)
52-
}
5350

5451
metricCount := 0
5552
for m := range ch {
@@ -60,6 +57,10 @@ func TestPGStatIOCollector(t *testing.T) {
6057
if metricCount != expected {
6158
t.Errorf("Expected %d metrics, got %d", expected, metricCount)
6259
}
60+
61+
if err := mock.ExpectationsWereMet(); err != nil {
62+
t.Errorf("There were unfulfilled expectations: %s", err)
63+
}
6364
}
6465

6566
func TestPGStatIOCollectorPostgreSQL18(t *testing.T) {
@@ -74,7 +75,7 @@ func TestPGStatIOCollectorPostgreSQL18(t *testing.T) {
7475
columns := []string{"backend_type", "io_context", "io_object", "reads", "read_time", "writes", "write_time", "extends", "read_bytes", "write_bytes", "extend_bytes"}
7576
rows := sqlmock.NewRows(columns).
7677
AddRow("client backend", "normal", "relation", 100, 50.5, 75, 25.2, 10, 1024, 2048, 512)
77-
mock.ExpectQuery("SELECT.*FROM pg_stat_io").WillReturnRows(rows)
78+
mock.ExpectQuery("SELECT.*backend_type.*FROM pg_stat_io").WillReturnRows(rows)
7879

7980
ch := make(chan prometheus.Metric)
8081
go func() {
@@ -87,9 +88,6 @@ func TestPGStatIOCollectorPostgreSQL18(t *testing.T) {
8788
}()
8889

8990
expected := 8 // reads, read_time, writes, write_time, extends, read_bytes, write_bytes, extend_bytes
90-
if err := mock.ExpectationsWereMet(); err != nil {
91-
t.Errorf("There were unfulfilled expectations: %s", err)
92-
}
9391

9492
metricCount := 0
9593
for m := range ch {
@@ -100,6 +98,10 @@ func TestPGStatIOCollectorPostgreSQL18(t *testing.T) {
10098
if metricCount != expected {
10199
t.Errorf("Expected %d metrics, got %d", expected, metricCount)
102100
}
101+
102+
if err := mock.ExpectationsWereMet(); err != nil {
103+
t.Errorf("There were unfulfilled expectations: %s", err)
104+
}
103105
}
104106

105107
func TestPGStatIOCollectorPrePostgreSQL16(t *testing.T) {

collector/pg_stat_user_tables_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ func TestPGStatUserTablesCollector(t *testing.T) {
7272
"autovacuum_count",
7373
"analyze_count",
7474
"autoanalyze_count",
75-
"total_size"}
75+
"total_size",
76+
"total_vacuum_time",
77+
"total_autovacuum_time",
78+
"total_analyze_time",
79+
"total_autoanalyze_time",
80+
}
7681
rows := sqlmock.NewRows(columns).
7782
AddRow("postgres",
7883
"public",
@@ -96,7 +101,12 @@ func TestPGStatUserTablesCollector(t *testing.T) {
96101
12,
97102
13,
98103
14,
99-
15)
104+
15,
105+
nil,
106+
nil,
107+
nil,
108+
nil,
109+
)
100110
mock.ExpectQuery(sanitizeQuery(statUserTablesQueryPre18)).WillReturnRows(rows)
101111
ch := make(chan prometheus.Metric)
102112
go func() {
@@ -173,7 +183,12 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
173183
"autovacuum_count",
174184
"analyze_count",
175185
"autoanalyze_count",
176-
"total_size"}
186+
"total_size",
187+
"total_vacuum_time",
188+
"total_autovacuum_time",
189+
"total_analyze_time",
190+
"total_autoanalyze_time",
191+
}
177192
rows := sqlmock.NewRows(columns).
178193
AddRow("postgres",
179194
nil,
@@ -197,7 +212,12 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
197212
nil,
198213
nil,
199214
nil,
200-
nil)
215+
nil,
216+
nil,
217+
nil,
218+
nil,
219+
nil,
220+
)
201221
mock.ExpectQuery(sanitizeQuery(statUserTablesQueryPre18)).WillReturnRows(rows)
202222
ch := make(chan prometheus.Metric)
203223
go func() {

0 commit comments

Comments
 (0)