diff --git a/input/postgres/relation_stats.go b/input/postgres/relation_stats.go index a9ea2ed08..398210e62 100644 --- a/input/postgres/relation_stats.go +++ b/input/postgres/relation_stats.go @@ -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, diff --git a/output/transform/merge_partition_sizes.go b/output/transform/merge_partition_sizes.go new file mode 100644 index 000000000..6127b5212 --- /dev/null +++ b/output/transform/merge_partition_sizes.go @@ -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 +} diff --git a/output/transform/postgres.go b/output/transform/postgres.go index 549a94e55..b333b3803 100644 --- a/output/transform/postgres.go +++ b/output/transform/postgres.go @@ -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 } diff --git a/output/transform/postgres_relations.go b/output/transform/postgres_relations.go index 8806ba022..fcd6d5295 100644 --- a/output/transform/postgres_relations.go +++ b/output/transform/postgres_relations.go @@ -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, + }) } } }