-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement new pganalyze.explain_analyze() helper #655
Open
lfittl
wants to merge
10
commits into
main
Choose a base branch
from
explain-function-helper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+819
−124
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9e27a6e
Implement new pganalyze.explain_analyze() helper
lfittl d02e1be
Query runner: Split out logic that uses active database connection
lfittl 65799af
EXPLAIN ANALYZE tests: Enable on CI, rework per feedback and remove t…
lfittl e615e9a
Query runner: Add parameter handling and Postgres settings support
lfittl c3faec6
Address review feedback
lfittl 8fcc1f9
Add "--generate-explain-analyze-helper-sql" command
lfittl f192625
Reword error messages for pganalyze.explain_analyze helper per feedback
lfittl e9bf24b
Address review feedback
lfittl 3ffc8b3
Helper SQL: Explicitly include QUERY_CANCELED in exception catching
lfittl 4ae7b1c
Enforce statement_timeout after we set parameters
lfittl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/guregu/null" | ||
"github.com/lib/pq" | ||
"github.com/pganalyze/collector/util" | ||
) | ||
|
||
func RunExplainAnalyzeForQueryRun(ctx context.Context, db *sql.DB, query string, parameters []null.String, parameterTypes []string, marker string) (result string, err error) { | ||
err = validateQuery(query) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Warm up caches without collecting timing info (slightly faster) | ||
_, err = runExplainAnalyze(ctx, db, query, parameters, parameterTypes, []string{"ANALYZE", "TIMING OFF"}, marker) | ||
|
||
// 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") { | ||
_, err = runExplainAnalyze(ctx, db, query, parameters, parameterTypes, []string{"ANALYZE", "TIMING OFF"}, marker) | ||
|
||
// If it timed out again, capture a non-ANALYZE EXPLAIN instead | ||
if err != nil && strings.Contains(err.Error(), "statement timeout") { | ||
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) | ||
return runExplainAnalyze(ctx, db, query, parameters, parameterTypes, []string{"ANALYZE", "BUFFERS"}, marker) | ||
} | ||
|
||
func runExplainAnalyze(ctx context.Context, db *sql.DB, query string, parameters []null.String, parameterTypes []string, analyzeFlags []string, marker string) (explainOutput string, err error) { | ||
tx, err := db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer tx.Rollback() | ||
|
||
err = tx.QueryRowContext(ctx, marker+"SELECT pganalyze.explain_analyze($1, $2, $3, $4)", marker+query, pq.Array(parameters), pq.Array(parameterTypes), pq.Array(analyzeFlags)).Scan(&explainOutput) | ||
|
||
return | ||
} | ||
|
||
func validateQuery(query string) error { | ||
var isUtil []bool | ||
// To be on the safe side never EXPLAIN a statement that can't be parsed, | ||
// or multiple statements in one (leading to accidental execution) | ||
isUtil, err := util.IsUtilityStmt(query) | ||
if err != nil || len(isUtil) != 1 || isUtil[0] { | ||
err = fmt.Errorf("query is not permitted to run (multi-statement or utility command?)") | ||
return err | ||
} | ||
|
||
// TODO: Consider adding additional checks here (e.g. blocking known bad function calls) | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using 14 as a minimum here since the EXPLAIN ANALYZE tests rely on being able to turn off
compute_query_id
in the output (and that was added in 14).