fix(b6): correct buffer attribution and pct_of_rel calculation#87
Merged
fix(b6): correct buffer attribution and pct_of_rel calculation#87
Conversation
- Filter by reldatabase to avoid filenode collisions across databases - Use pg_filenode_relation() for accurate OID resolution - Filter relforknumber = 0 (main fork only) to match pg_relation_size() - Use ceil() on numeric division for pct_of_rel to avoid >100% artifacts Thanks to Tembo for the review on #85.
| select count(*) from pg_buffercache where relfilenode is not null | ||
| select count(*) | ||
| from pg_buffercache | ||
| where reldatabase = (select oid from pg_database where datname = current_database()) |
There was a problem hiding this comment.
pct_of_cache denominator currently counts all forks, but the main query filters to b.relforknumber = 0. If the intent is “main fork only” everywhere, consider adding the same filter to the denominator so the percentage stays consistent.
Suggested change
| where reldatabase = (select oid from pg_database where datname = current_database()) | |
| where reldatabase = (select oid from pg_database where datname = current_database()) | |
| and relfilenode is not null | |
| and relforknumber = 0 |
Comment on lines
+37
to
+40
| join pg_class as c | ||
| on c.oid = ( | ||
| select pg_filenode_relation(b.reltablespace, b.relfilenode) | ||
| ) |
There was a problem hiding this comment.
Minor readability/perf nit: the scalar subquery wrapper around pg_filenode_relation(...) doesn’t buy much here and can be inlined in the join condition.
Suggested change
| join pg_class as c | |
| on c.oid = ( | |
| select pg_filenode_relation(b.reltablespace, b.relfilenode) | |
| ) | |
| on c.oid = pg_filenode_relation(b.reltablespace, b.relfilenode) |
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.
Addresses review feedback from Tembo on #85.
Fixes
Filenode collision:
relfilenodeisn't globally unique across databases. Now filters byreldatabase = current_database()and usespg_filenode_relation()for accurate OID resolution.relforknumber filter: Only count main fork buffers (
relforknumber = 0) to matchpg_relation_size()which also returns main fork size.pct_of_rel >100%: Integer division in
pg_relation_size() / block_sizetruncated, causing percentages like 100.3%. Usingceil()on numeric division fixes this.Before/After
Testing
Tested on PG17 with pg_buffercache + shared_preload_libraries.