@@ -11,6 +11,7 @@ import (
1111 "slices"
1212 "time"
1313
14+ "github.com/cockroachdb/crlib/crmath"
1415 "github.com/cockroachdb/crlib/crtime"
1516 "github.com/cockroachdb/errors"
1617 "github.com/cockroachdb/pebble/internal/base"
@@ -487,7 +488,7 @@ func (d *DB) loadTableRangeDelStats(
487488 // the size of the range key block relative to the overall size of the
488489 // table is expected to be small.
489490 if level == numLevels - 1 && meta .SmallestSeqNum < maxRangeDeleteSeqNum {
490- size , err := r . EstimateDiskUsage ( start , end , env , meta . IterTransforms () )
491+ size , err := estimateDiskUsageInTableAndBlobReferences ( r , start , end , env , meta )
491492 if err != nil {
492493 return nil , 0 , err
493494 }
@@ -689,7 +690,7 @@ func (d *DB) estimateReclaimedSizeBeneath(
689690 // The range fully contains the file, so skip looking it up in table
690691 // cache/looking at its indexes and add the full file size.
691692 if updateEstimates {
692- estimate += file .Size
693+ estimate += file .Size + file . EstimatedReferenceSize ()
693694 }
694695 if updateHints && hintSeqNum > file .SmallestSeqNum {
695696 hintSeqNum = file .SmallestSeqNum
@@ -705,7 +706,7 @@ func (d *DB) estimateReclaimedSizeBeneath(
705706 var size uint64
706707 err := d .fileCache .withReader (ctx , block .NoReadEnv , file ,
707708 func (r * sstable.Reader , env sstable.ReadEnv ) (err error ) {
708- size , err = r . EstimateDiskUsage ( start , end , env , file . IterTransforms () )
709+ size , err = estimateDiskUsageInTableAndBlobReferences ( r , start , end , env , file )
709710 return err
710711 })
711712 if err != nil {
@@ -752,6 +753,24 @@ func sanityCheckStats(
752753 }
753754}
754755
756+ // estimateDiskUsageInTableAndBlobReferences estimates the disk usage within a
757+ // sstable and its referenced values. The size of blob files is computed using
758+ // linear interpolation.
759+ func estimateDiskUsageInTableAndBlobReferences (
760+ r * sstable.Reader , start , end []byte , env sstable.ReadEnv , meta * manifest.TableMetadata ,
761+ ) (uint64 , error ) {
762+ size , err := r .EstimateDiskUsage (start , end , env , meta .IterTransforms ())
763+ if err != nil {
764+ return 0 , err
765+ }
766+
767+ estimatedTableSize := max (size , 1 )
768+ originalTableSize := max (meta .Size , 1 )
769+ referenceSize := crmath .ScaleUint64 (meta .EstimatedReferenceSize (),
770+ estimatedTableSize , originalTableSize )
771+ return size + referenceSize , nil
772+ }
773+
755774// maybeSetStatsFromProperties sets the table backing properties and attempts to
756775// set the table stats from the properties, for a table that was created by an
757776// ingestion or compaction.
0 commit comments