Skip to content
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

Improve query runner error handling #642

Merged
merged 2 commits into from
Nov 28, 2024
Merged

Conversation

seanlinsley
Copy link
Member

@seanlinsley seanlinsley commented Nov 26, 2024

This PR updates the query runner to submit the first error it sees, to avoid reporting an unhelpful current transaction is aborted error back to the server. BEGIN READ ONLY was a late addition to #628, so this wasn't caught in manual testing.

This also fixes an error I saw while testing this, where server.QueryRuns is nil:

panic: assignment to entry in nil map

goroutine 99 [running]:
runner.connect.func3()
    runner/websocket.go:150 +0x4ac
created by runner.connect in goroutine 64
    runner/websocket.go:113 +0xa44

@@ -94,7 +95,7 @@ func run(ctx context.Context, server *state.Server, collectionOpts state.Collect
db.ExecContext(ctx, postgres.QueryMarkerSQL+"BEGIN READ ONLY")

err = db.QueryRowContext(ctx, comment+prefix+query.QueryText).Scan(&result)
firstErr := err
firstErr = err

// Run EXPLAIN ANALYZE a second time to get a warm cache result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #641 (comment), you mentioned that this is executed even if the firstErr is not nil for the case of first one failed with a timeout. However, even though the cause was a timeout, the second one will fail regardless:

pgaweb=# set statement_timeout = 1000;
SET
pgaweb=# begin read only;
BEGIN
pgaweb=*# select pg_sleep(2);
ERROR:  canceling statement due to statement timeout
pgaweb=!# select pg_sleep(0.5);
ERROR:  current transaction is aborted, commands ignored until end of transaction block

Meaning this code doesn't quite do what you want it to do. When the firstErr is not nil (for whatever the reason), this second query execution will always be ignored.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Maybe the query text should contain BEGIN READ ONLY and COMMIT so each query is in its own transaction.

@seanlinsley seanlinsley requested a review from keiko713 November 27, 2024 19:06
Copy link
Contributor

@keiko713 keiko713 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, confirmed that it's reporting the correct error message 👍

@seanlinsley seanlinsley merged commit c477eaf into main Nov 28, 2024
3 checks passed
@seanlinsley seanlinsley deleted the query-run-error-handling branch November 28, 2024 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants