-
Notifications
You must be signed in to change notification settings - Fork 84
Skip known query texts from pg_stat_statements #776
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
Draft
seanlinsley
wants to merge
13
commits into
main
Choose a base branch
from
statements-skip-known
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.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
218608f
Add pg_query fingerprint cache
seanlinsley be28a72
Update lock usage
seanlinsley 122ab27
Fix missing initialization of Collection struct
seanlinsley cdd4cd5
Take lock sooner in Cleanup()
seanlinsley 5a75db4
Remove debug logging
seanlinsley 0110cb2
Remove TODO comment
seanlinsley d8ecab7
Decrease cache size to 500k entries
seanlinsley 13432a4
Avoid adding virtual fingerprints to the cache for truncated query text
seanlinsley 5af80cd
No need to pass server to ForCurrentDatabase
seanlinsley 2ab3d4f
Remove intintmap, use simple map instead
seanlinsley 1abfc0c
Retain 50% of entries
seanlinsley a0daecd
Switch to unsigned int fingerprint since that's now possible
seanlinsley a7be413
Skip known query texts from pg_stat_statements
seanlinsley 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 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
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
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package state | ||
|
|
||
| import ( | ||
| "github.com/pganalyze/collector/util" | ||
| "sync" | ||
| ) | ||
|
|
||
| // 500,000 entries use around 12 MB | ||
| const MAX_SIZE = 500_000 | ||
|
|
||
| type Fingerprints struct { | ||
| lock sync.RWMutex | ||
| cache map[int64]uint64 | ||
| newQueryIDs []int64 | ||
| } | ||
|
|
||
| func NewFingerprints() *Fingerprints { | ||
| return &Fingerprints{ | ||
| lock: sync.RWMutex{}, | ||
| cache: make(map[int64]uint64, MAX_SIZE), | ||
| } | ||
| } | ||
|
|
||
| func (c *Fingerprints) Get(queryID int64) (uint64, bool) { | ||
| c.lock.RLock() | ||
| defer c.lock.RUnlock() | ||
| fingerprint, exists := c.cache[queryID] | ||
| return fingerprint, exists | ||
| } | ||
|
|
||
| func (c *Fingerprints) Add(queryID int64, text string, filterQueryText string, trackActivityQuerySize int) uint64 { | ||
| if queryID == 0 { | ||
| return util.FingerprintQuery(text, filterQueryText, trackActivityQuerySize) | ||
| } | ||
| fingerprint, exists := c.Get(queryID) | ||
| if exists { | ||
| return fingerprint | ||
| } | ||
| fingerprint, virtual := util.TryFingerprintQuery(text, filterQueryText, trackActivityQuerySize) | ||
| if virtual { | ||
| c.lock.Lock() | ||
| c.newQueryIDs = append(c.newQueryIDs, queryID) | ||
| c.lock.Unlock() | ||
| return fingerprint | ||
| } | ||
| c.cleanup() | ||
| c.lock.Lock() | ||
| c.cache[queryID] = fingerprint | ||
| c.lock.Unlock() | ||
| return fingerprint | ||
| } | ||
|
|
||
| // Called by GetStatementTexts to only look up query texts for new, unknown query IDs | ||
| func (c *Fingerprints) TakeNewQueryIDs() []int64 { | ||
| c.lock.Lock() | ||
| defer c.lock.Unlock() | ||
| newQueryIDs := c.newQueryIDs | ||
| c.newQueryIDs = nil | ||
| return newQueryIDs | ||
| } | ||
|
|
||
| func (c *Fingerprints) size() int { | ||
| c.lock.RLock() | ||
| defer c.lock.RUnlock() | ||
| return len(c.cache) | ||
| } | ||
|
|
||
| // Retains a random 50% sample of entries if the cache grows too large | ||
| func (c *Fingerprints) cleanup() { | ||
| if c.size() < MAX_SIZE { | ||
| return | ||
| } | ||
| c.lock.Lock() | ||
| cache := make(map[int64]uint64, MAX_SIZE) | ||
| index := 0 | ||
| for key, value := range c.cache { | ||
| if index%2 == 0 { | ||
| cache[key] = value | ||
| } | ||
| index += 1 | ||
| } | ||
| c.cache = cache | ||
| c.lock.Unlock() | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
As discussed, doing this filtering has three main benefits that I can see:
Just for clarity, because of how pg_stat_statements works today, this unfortunately won't reduce the effort done to read the query text file and put the query text and statistics into the tuplestore, since the WHERE condition won't be pushed down into pg_stat_statements_internal.