Skip to content

Commit

Permalink
Aggregate stats for partition tables
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlinsley committed Nov 18, 2024
1 parent 0ffac89 commit 7628d3b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 18 deletions.
5 changes: 1 addition & 4 deletions input/postgres/relation_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ locked_relids_with_parents AS (
UNION SELECT relid FROM locked_relids
)
SELECT c.oid,
COALESCE(pg_catalog.pg_table_size(c.oid), 0) +
COALESCE((SELECT pg_catalog.sum(pg_catalog.pg_table_size(inhrelid))
FROM pg_catalog.pg_inherits
WHERE inhparent = c.oid), 0) AS size_bytes,
COALESCE(pg_catalog.pg_table_size(c.oid), 0) AS size_bytes,
CASE c.reltoastrelid WHEN NULL THEN 0 ELSE COALESCE(pg_catalog.pg_total_relation_size(c.reltoastrelid), 0) END AS toast_bytes,
COALESCE(pg_stat_get_numscans(c.oid), 0) AS seq_scan,
COALESCE(pg_stat_get_tuples_returned(c.oid), 0) AS seq_tup_read,
Expand Down
56 changes: 56 additions & 0 deletions output/transform/merge_partition_sizes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package transform

import (
snapshot "github.com/pganalyze/collector/output/pganalyze_collector"
"github.com/pganalyze/collector/state"
)

func mergePartitionSizes(s snapshot.FullSnapshot, newState state.PersistedState, ts state.TransientState, databaseOidToIdx OidToIdx) snapshot.FullSnapshot {
for idx, info := range s.IndexInformations {
rel := s.RelationInformations[info.RelationIdx]
if !rel.HasParentRelation || rel.PartitionBoundary == "" {
continue
}
for parentIdx, pi := range s.IndexInformations {
if pi.RelationIdx != rel.ParentRelationIdx {
continue
}
if info.IndexType == pi.IndexType && info.IsUnique == pi.IsUnique && intArrayEqual(info.Columns, pi.Columns) {
s.IndexStatistics[parentIdx].SizeBytes += s.IndexStatistics[idx].SizeBytes
break
}
}
}

for idx, rel := range s.RelationInformations {
if !rel.HasParentRelation || rel.PartitionBoundary == "" {
continue
}
stat := s.RelationStatistics[idx]
parent := s.RelationStatistics[rel.ParentRelationIdx]
parent.NTupIns += stat.NTupIns
parent.NTupUpd += stat.NTupUpd
parent.NTupDel += stat.NTupDel
parent.NTupHotUpd += stat.NTupHotUpd
parent.NLiveTup += stat.NLiveTup
parent.NDeadTup += stat.NDeadTup
parent.SizeBytes += stat.SizeBytes
parent.ToastSizeBytes += stat.ToastSizeBytes
parent.CachedDataBytes += stat.CachedDataBytes
parent.CachedToastBytes += stat.CachedToastBytes
}

return s
}

func intArrayEqual(a []int32, b []int32) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
1 change: 1 addition & 0 deletions output/transform/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func transformPostgres(s snapshot.FullSnapshot, newState state.PersistedState, d
s = transformPostgresBackendCounts(s, transientState, roleOidToIdx, databaseOidToIdx)
s = transformPostgresExtensions(s, transientState, databaseOidToIdx)
s = transformPostgresBufferCache(s, transientState, databaseOidToIdx)
s = mergePartitionSizes(s, newState, transientState, databaseOidToIdx)

return s
}
Expand Down
25 changes: 11 additions & 14 deletions output/transform/postgres_relations.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,17 @@ func transformPostgresRelations(s snapshot.FullSnapshot, newState state.Persiste

// Statistic
if diffedSchemaStatsExist {
indexStats, exists := diffedSchemaStats.IndexStats[index.IndexOid]
if exists {
statistic := snapshot.IndexStatistic{
IndexIdx: indexIdx,
SizeBytes: indexStats.SizeBytes,
IdxScan: indexStats.IdxScan,
IdxTupRead: indexStats.IdxTupRead,
IdxTupFetch: indexStats.IdxTupFetch,
IdxBlksRead: indexStats.IdxBlksRead,
IdxBlksHit: indexStats.IdxBlksHit,
CachedBytes: index.CachedBytes,
}
s.IndexStatistics = append(s.IndexStatistics, &statistic)
}
indexStats := diffedSchemaStats.IndexStats[index.IndexOid]
s.IndexStatistics = append(s.IndexStatistics, &snapshot.IndexStatistic{
IndexIdx: indexIdx,
SizeBytes: indexStats.SizeBytes,
IdxScan: indexStats.IdxScan,
IdxTupRead: indexStats.IdxTupRead,
IdxTupFetch: indexStats.IdxTupFetch,
IdxBlksRead: indexStats.IdxBlksRead,
IdxBlksHit: indexStats.IdxBlksHit,
CachedBytes: index.CachedBytes,
})
}
}
}
Expand Down

0 comments on commit 7628d3b

Please sign in to comment.