feat(s3): add workload profile by query type#86
Merged
Conversation
New s3 report groups pg_stat_statements by first SQL keyword (SELECT, INSERT, UPDATE, DELETE, etc.) to show workload composition. The tricky part: SQL comments before the keyword. The regex: 1. Strips block comments: /* ... */ (including multi-line) 2. Strips leading line comments: -- ... (including chained) 3. Extracts first remaining word as the query type Shows per-type: calls, calls%, exec time, exec%, plan time, avg time per call, and row count. PG13+ path uses total_exec_time/total_plan_time split. Older PG path uses total_time. Closes #37
| select | ||
| lower((regexp_match( | ||
| regexp_replace( | ||
| regexp_replace(query, '/\*.*?\*/', '', 'gs'), |
There was a problem hiding this comment.
Since you only need the leading keyword, you can avoid the global block-comment replace and strip only leading comments (block + line) in one anchored regexp_replace (cheaper and avoids touching comment-like text later in the query). Same change applies in the \else branch.
Suggested change
| regexp_replace(query, '/\*.*?\*/', '', 'gs'), | |
| regexp_replace( | |
| query, | |
| '^\\s*(/\\*.*?\\*/\\s*|--[^\\n]*\\n\\s*)*', | |
| '' | |
| ), |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
New s3 report: workload composition from pg_stat_statements, grouped by first SQL keyword.
The comment problem (#37)
Simple
regexp_replace(query, '^\W*(\w+)...')breaks on queries like:The fix strips comments before extracting:
/\*.*?\*/(withgsflags for multi-line)^\s*(--[^\n]*\n\s*)*^\s*(\w+)Tested regex against
SELECT 1/* comment */ SELECT 1/* multi\nline */ INSERT ...-- comment\nSELECT 1-- c1\n-- c2\nUPDATE .../* c1 */ -- c2\nDELETE ...Output columns (PG13+)
Closes #37