Add update_stats query for HOT and same-page update analysis - #39
Conversation
## Summary Add `update_stats` to report per-table HOT, same-page non-HOT, and new-page update metrics from `pg_stat_user_tables`, including fillfactor and percentages. ## Why Update-heavy workloads benefit from HOT updates, but diagnosing low HOT rates needs separating space pressure (`n_tup_newpage_upd`) from indexed-column updates. PostgreSQL 16 exposes that split; older versions still get a HOT vs non-HOT fallback.
|
@pawurb once you review and approve this, my end goal is to change It would be akin to But yeah it is quite insane, how adding an innocent-looking index on a column, or changing an Overall, I think it's beneficial for ruby/rails application developers to be aware of which tables are "update-heavy" and then act accordingly. this photo from the Adyen video summarizes the gotchas for application developers
|
## Summary Introduce a new `new_page_updates` check in the `diagnose` module to identify tables in PostgreSQL 16 and newer that experience a high ratio of new-page updates. This check helps diagnose potential page-space pressure and the effectiveness of the table's `fillfactor`. ## Details - The check reports tables with at least 10,000 cumulative updates and 20% or more of updates resulting in new-page placements. - Users can override default thresholds using environment variables. - The implementation includes detailed reporting on new-page ratios, fillfactor, and HOT update percentages. ## Why This feature enhances the ability to diagnose performance issues related to update-heavy workloads, providing insights into how table configurations may impact update efficiency.
205cc79 to
d739417
Compare
## Summary Enhance the `update_stats` and `update_stats_legacy` SQL queries to include the calculation of average row size in bytes (`avg_row_bytes`). This metric is derived from the relation size divided by the number of tuples, providing better insights into table storage efficiency. ## Details - Added `reltuples` to the selected columns to facilitate the average row size calculation. - Implemented the `avg_row_bytes` calculation in both queries. - Updated smoke tests to verify the correctness of the new metric after running `ANALYZE`. ## Why This addition improves the diagnostic capabilities of the tool by allowing users to assess the average size of rows in their tables, which can be crucial for performance tuning and storage optimization.
## Summary Introduce a new `low_hot_same_page` check in the `diagnose` module to identify tables in PostgreSQL 16 and newer that have a low ratio of HOT updates among same-page updates. This check helps diagnose potential issues with indexed column updates. ## Details - The check reports tables with at least 10,000 cumulative updates and fewer than 10% of updates being HOT. - Users can override default thresholds using environment variables. - The implementation includes detailed reporting on HOT ratios, same-page and new-page update percentages, and current fillfactor. ## Why This feature enhances the ability to diagnose performance issues related to update-heavy workloads, providing insights into how table configurations may impact update efficiency.
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@pawurb I can show the results : After
after :
After this gets merged, I'm going to open a PR on rails-pg- |
|
@chaadow thanks! I'll go ahead and release so that you can start the rails-pg-extras pr. |



Summary
This PR adds a new
update_stats.sqlquery that exposes how updates are physically handled for each table in a schema.It builds on the update counters available in
pg_stat_user_tablesto distinguish between:Motivation
Looking only at
n_tup_hot_upddoes not explain why an update was not HOT.A low HOT ratio can have very different causes:
Using
n_tup_newpage_upd, the query provides more context around page placement and helps evaluate:fillfactorsettingsReported metrics
The query includes:
fillfactorThis makes it possible to interpret update behavior more precisely. For example, a table may have a low overall HOT ratio while still keeping most updates on the same page, indicating that indexed-column changes—not page space—are the main limiting factor.
Compatibility and usage notes
n_tup_newpage_updis available starting with PostgreSQL 16, so this query requires PostgreSQL 16 or newer.The underlying statistics are cumulative. For before-and-after comparisons, such as evaluating a
fillfactorchange, the metrics should ideally be captured at two points in time and compared using their deltas.Reference :