Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lfittl committed Jan 6, 2025
1 parent 72d65e7 commit 5e7d30f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
18 changes: 10 additions & 8 deletions input/postgres/explain_analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ func RunExplainAnalyzeForQueryRun(ctx context.Context, db *sql.DB, query string,

// Warm up caches without collecting timing info (slightly faster)
_, err = runExplainAnalyze(ctx, db, query, parameters, parameterTypes, []string{"ANALYZE", "TIMING OFF"}, marker)
if err != nil {
if !strings.Contains(err.Error(), "statement timeout") {
return
}

// Run again if it was a timeout error, to make sure we got the caches warmed up all the way
if err != nil && strings.Contains(err.Error(), "statement timeout") {
// Run again if it was a timeout error, to make sure we got the caches warmed up all the way
_, err = runExplainAnalyze(ctx, db, query, parameters, parameterTypes, []string{"ANALYZE", "TIMING OFF"}, marker)
if err != nil {
if !strings.Contains(err.Error(), "statement timeout") {
return
}

// If it timed out again, capture a non-ANALYZE EXPLAIN instead
if err != nil && strings.Contains(err.Error(), "statement timeout") {
// If it timed out again, capture a non-ANALYZE EXPLAIN instead
return runExplainAnalyze(ctx, db, query, parameters, parameterTypes, []string{}, marker)
} else if err != nil {
return
}
} else if err != nil {
return
}

// Run EXPLAIN ANALYZE once more to get a warm cache result (this is the one we return)
Expand Down
8 changes: 4 additions & 4 deletions runner/generate_helper_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ func GenerateExplainAnalyzeHelperSql(ctx context.Context, server *state.Server,
for _, dbName := range postgres.GetDatabasesToCollect(server, databases) {
output.WriteString(fmt.Sprintf("\\c %s\n", pq.QuoteIdentifier(dbName)))
output.WriteString("CREATE SCHEMA IF NOT EXISTS pganalyze;\n")
output.WriteString(fmt.Sprintf("GRANT USAGE ON SCHEMA pganalyze TO %s;\n", server.Config.GetDbUsername()))
output.WriteString(fmt.Sprintf("GRANT CREATE ON SCHEMA pganalyze TO %s;\n", globalCollectionOpts.GenerateExplainAnalyzeHelperRole))
output.WriteString(fmt.Sprintf("SET ROLE %s;\n", globalCollectionOpts.GenerateExplainAnalyzeHelperRole))
output.WriteString(fmt.Sprintf("GRANT USAGE ON SCHEMA pganalyze TO %s;\n", pq.QuoteIdentifier(server.Config.GetDbUsername())))
output.WriteString(fmt.Sprintf("GRANT CREATE ON SCHEMA pganalyze TO %s;\n", pq.QuoteIdentifier(globalCollectionOpts.GenerateExplainAnalyzeHelperRole)))
output.WriteString(fmt.Sprintf("SET ROLE %s;\n", pq.QuoteIdentifier(globalCollectionOpts.GenerateExplainAnalyzeHelperRole)))
output.WriteString(util.ExplainAnalyzeHelper + "\n")
output.WriteString("RESET ROLE;\n")
output.WriteString(fmt.Sprintf("REVOKE CREATE ON SCHEMA pganalyze FROM %s;\n", globalCollectionOpts.GenerateExplainAnalyzeHelperRole))
output.WriteString(fmt.Sprintf("REVOKE CREATE ON SCHEMA pganalyze FROM %s;\n", pq.QuoteIdentifier(globalCollectionOpts.GenerateExplainAnalyzeHelperRole)))
output.WriteString("\n")
}

Expand Down

0 comments on commit 5e7d30f

Please sign in to comment.