Skip to content

Commit 935b968

Browse files
mstaebleclaude
andcommitted
Replace failed-tests matviews with direct partitioned query
The prow_job_failed_tests_by_day and prow_job_failed_tests_by_hour materialized views scanned all ~3,500 partitions of prow_job_run_tests with no time or release filter, taking ~3 minutes per refresh. A direct query with partition pruning keys (prow_job_run_release and prow_job_run_timestamp BETWEEN start AND end) runs in ~13ms — faster than reading the matview itself (46ms) — making the matviews unnecessary overhead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent dd64c4f commit 935b968

3 files changed

Lines changed: 10 additions & 81 deletions

File tree

pkg/api/job_analysis.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,18 @@ func PrintJobAnalysisJSONFromDB(
109109
}
110110
tr := make([]testResult, 0)
111111

112-
jr := dbc.DB.Table("prow_job_failed_tests_by_day_matview")
113-
if period == PeriodHour {
114-
jr = dbc.DB.Table("prow_job_failed_tests_by_hour_matview")
112+
if err := dbc.DB.Table("prow_job_run_tests pjrt").
113+
Select("date_trunc(?, pjrt.prow_job_run_timestamp) AS period, tests.name AS test_name, count(tests.name) AS count", period).
114+
Joins("JOIN tests ON pjrt.test_id = tests.id").
115+
Where("pjrt.status = ?", v1sippyprocessing.TestStatusFailure).
116+
Where("pjrt.prow_job_id IN ?", jobs).
117+
Where("pjrt.prow_job_run_release = ?", release).
118+
Where("pjrt.prow_job_run_timestamp BETWEEN ? AND ?", start, end).
119+
Group("tests.name, period, pjrt.prow_job_id").
120+
Scan(&tr).Error; err != nil {
121+
return results, err
115122
}
116123

117-
jr.Select("period, test_name, count").
118-
Where("prow_job_id IN ?", jobs).Scan(&tr)
119-
120124
for _, t := range tr {
121125
dateKey := t.Period.UTC().Format(formatter)
122126
if _, ok := results.ByPeriod[dateKey]; ok {

pkg/db/views.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ var PostgresMatViews = []PostgresView{
4040
Definition: jobRunsReportMatView,
4141
IndexColumns: []string{"id"},
4242
},
43-
{
44-
Name: "prow_job_failed_tests_by_day_matview",
45-
Definition: prowJobFailedTestsMatView,
46-
IndexColumns: []string{"period", "prow_job_id", "test_name"},
47-
ReplaceStrings: map[string]string{
48-
"|||BY|||": "day",
49-
},
50-
},
51-
{
52-
Name: "prow_job_failed_tests_by_hour_matview",
53-
Definition: prowJobFailedTestsMatView,
54-
IndexColumns: []string{"period", "prow_job_id", "test_name"},
55-
ReplaceStrings: map[string]string{
56-
"|||BY|||": "hour",
57-
},
58-
},
5943
{
6044
Name: "prow_test_report_7d_collapsed_matview",
6145
Definition: testReportCollapsedMatView,
@@ -421,17 +405,6 @@ GROUP BY
421405
tests.name, tests.id, date(prow_job_run_tests.prow_job_run_timestamp), prow_job_run_tests.prow_job_run_release, prow_jobs.name
422406
`
423407

424-
const prowJobFailedTestsMatView = `
425-
SELECT date_trunc('|||BY|||'::text, pjrt.prow_job_run_timestamp) AS period,
426-
pjrt.prow_job_id,
427-
tests.name AS test_name,
428-
count(tests.name) AS count
429-
FROM prow_job_run_tests pjrt
430-
JOIN tests tests ON pjrt.test_id = tests.id
431-
WHERE pjrt.status = 12
432-
GROUP BY tests.name, (date_trunc('|||BY|||'::text, pjrt.prow_job_run_timestamp)), pjrt.prow_job_id
433-
`
434-
435408
// TODO: remove distinct once bug fixed re dupes in release_job_runs
436409
const payloadTestFailuresMatView = `
437410
SELECT DISTINCT

pkg/flags/postgres_benchmarking_test.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -730,54 +730,6 @@ func getMatviewBenchmarkCases(asOf time.Time) []benchmarkCase {
730730
return err
731731
},
732732
},
733-
{
734-
name: "MatviewFailedTestsByDay",
735-
fn: func(dbc *db.DB) error {
736-
var prowJob models.ProwJob
737-
if err := dbc.DB.Where("name = ? AND release = ?", benchmarkJobName, benchmarkRelease).First(&prowJob).Error; err != nil {
738-
return err
739-
}
740-
type testResult struct {
741-
Period time.Time
742-
TestName string
743-
Count int
744-
}
745-
var results []testResult
746-
res := dbc.DB.Table("prow_job_failed_tests_by_day_matview").
747-
Select("period, test_name, count").
748-
Where("prow_job_id = ?", prowJob.ID).
749-
Scan(&results)
750-
if res.Error != nil {
751-
return res.Error
752-
}
753-
log.Printf("MatviewFailedTestsByDay: %d results for job %s", len(results), benchmarkJobName)
754-
return nil
755-
},
756-
},
757-
{
758-
name: "MatviewFailedTestsByHour",
759-
fn: func(dbc *db.DB) error {
760-
var prowJob models.ProwJob
761-
if err := dbc.DB.Where("name = ? AND release = ?", benchmarkJobName, benchmarkRelease).First(&prowJob).Error; err != nil {
762-
return err
763-
}
764-
type testResult struct {
765-
Period time.Time
766-
TestName string
767-
Count int
768-
}
769-
var results []testResult
770-
res := dbc.DB.Table("prow_job_failed_tests_by_hour_matview").
771-
Select("period, test_name, count").
772-
Where("prow_job_id = ?", prowJob.ID).
773-
Scan(&results)
774-
if res.Error != nil {
775-
return res.Error
776-
}
777-
log.Printf("MatviewFailedTestsByHour: %d results for job %s", len(results), benchmarkJobName)
778-
return nil
779-
},
780-
},
781733
{
782734
name: "MatviewPayloadTestFailures",
783735
fn: func(dbc *db.DB) error {

0 commit comments

Comments
 (0)