Skip to content

Comments

fix(b6): correct buffer attribution and pct_of_rel calculation#87

Merged
NikolayS merged 1 commit intomasterfrom
fix/b6-buffercache-improvements
Feb 10, 2026
Merged

fix(b6): correct buffer attribution and pct_of_rel calculation#87
NikolayS merged 1 commit intomasterfrom
fix/b6-buffercache-improvements

Conversation

@NikolayS
Copy link
Owner

Addresses review feedback from Tembo on #85.

Fixes

  1. Filenode collision: relfilenode isn't globally unique across databases. Now filters by reldatabase = current_database() and uses pg_filenode_relation() for accurate OID resolution.

  2. relforknumber filter: Only count main fork buffers (relforknumber = 0) to match pg_relation_size() which also returns main fork size.

  3. pct_of_rel >100%: Integer division in pg_relation_size() / block_size truncated, causing percentages like 100.3%. Using ceil() on numeric division fixes this.

Before/After

-- Before: 100.1%, 100.3%, 100.7% (integer division artifacts)
-- After:  100.0% for fully cached relations

Testing

Tested on PG17 with pg_buffercache + shared_preload_libraries.

- 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.
@NikolayS NikolayS merged commit e482e6b into master Feb 10, 2026
6 checks passed
@NikolayS NikolayS deleted the fix/b6-buffercache-improvements branch February 10, 2026 03:14
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())
Copy link

Choose a reason for hiding this comment

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

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)
)
Copy link

Choose a reason for hiding this comment

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

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)

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.

1 participant