@@ -595,13 +595,25 @@ func (ib *IndexBackfiller) ContainsInvertedIndex() bool {
595
595
// InitForLocalUse initializes an IndexBackfiller for use during local execution
596
596
// within a transaction. In this case, the entire backfill process is occurring
597
597
// on the gateway as part of the user's transaction.
598
+ //
599
+ // Non-nil memory monitor must be provided. If an error is returned, it'll be
600
+ // stopped automatically; otherwise, the backfiller takes ownership of the
601
+ // monitor.
598
602
func (ib * IndexBackfiller ) InitForLocalUse (
599
603
ctx context.Context ,
600
604
evalCtx * eval.Context ,
601
605
semaCtx * tree.SemaContext ,
602
606
desc catalog.TableDescriptor ,
603
607
mon * mon.BytesMonitor ,
604
- ) error {
608
+ ) (retErr error ) {
609
+ if mon == nil {
610
+ return errors .AssertionFailedf ("memory monitor must be provided" )
611
+ }
612
+ defer func () {
613
+ if retErr != nil {
614
+ mon .Stop (ctx )
615
+ }
616
+ }()
605
617
606
618
// Initialize ib.added.
607
619
if err := ib .initIndexes (ctx , evalCtx , desc , nil /* allowList */ , 0 /*sourceIndex*/ , nil ); err != nil {
@@ -626,7 +638,8 @@ func (ib *IndexBackfiller) InitForLocalUse(
626
638
ib .valNeededForCol .Add (ib .colIdxMap .GetDefault (col ))
627
639
})
628
640
629
- return ib .init (evalCtx , predicates , colExprs , mon )
641
+ ib .init (evalCtx , predicates , colExprs , mon )
642
+ return nil
630
643
}
631
644
632
645
// constructExprs is a helper to construct the index and column expressions
@@ -737,14 +750,27 @@ func constructExprs(
737
750
// backfill operation manages its own transactions. This separation is necessary
738
751
// due to the different procedure for accessing user defined type metadata as
739
752
// part of a distributed flow.
753
+ //
754
+ // Non-nil memory monitor must be provided. If an error is returned, it'll be
755
+ // stopped automatically; otherwise, the backfiller takes ownership of the
756
+ // monitor.
740
757
func (ib * IndexBackfiller ) InitForDistributedUse (
741
758
ctx context.Context ,
742
759
flowCtx * execinfra.FlowCtx ,
743
760
desc catalog.TableDescriptor ,
744
761
allowList []catid.IndexID ,
745
762
sourceIndexID catid.IndexID ,
746
763
mon * mon.BytesMonitor ,
747
- ) error {
764
+ ) (retErr error ) {
765
+ if mon == nil {
766
+ return errors .AssertionFailedf ("memory monitor must be provided" )
767
+ }
768
+ defer func () {
769
+ if retErr != nil {
770
+ mon .Stop (ctx )
771
+ }
772
+ }()
773
+
748
774
// We'll be modifying the eval.Context in BuildIndexEntriesChunk, so we need
749
775
// to make a copy.
750
776
evalCtx := flowCtx .NewEvalCtx ()
@@ -801,7 +827,8 @@ func (ib *IndexBackfiller) InitForDistributedUse(
801
827
ib .valNeededForCol .Add (ib .colIdxMap .GetDefault (col ))
802
828
})
803
829
804
- return ib .init (evalCtx , predicates , colExprs , mon )
830
+ ib .init (evalCtx , predicates , colExprs , mon )
831
+ return nil
805
832
}
806
833
807
834
// Close releases the resources used by the IndexBackfiller. It can be called
@@ -933,12 +960,15 @@ func (ib *IndexBackfiller) initIndexes(
933
960
}
934
961
935
962
// init completes the initialization of an IndexBackfiller.
963
+ //
964
+ // The IndexBackfiller takes ownership of the monitor which must be non-nil.
965
+ // It'll be closed when the backfiller is closed.
936
966
func (ib * IndexBackfiller ) init (
937
967
evalCtx * eval.Context ,
938
968
predicateExprs map [descpb.IndexID ]tree.TypedExpr ,
939
969
colExprs map [descpb.ColumnID ]tree.TypedExpr ,
940
970
mon * mon.BytesMonitor ,
941
- ) error {
971
+ ) {
942
972
ib .evalCtx = evalCtx
943
973
ib .predicates = predicateExprs
944
974
ib .colExprs = colExprs
@@ -959,12 +989,8 @@ func (ib *IndexBackfiller) init(
959
989
}
960
990
961
991
// Create a bound account associated with the index backfiller monitor.
962
- if mon == nil {
963
- return errors .AssertionFailedf ("no memory monitor linked to IndexBackfiller during init" )
964
- }
965
992
ib .mon = mon
966
993
ib .muBoundAccount .boundAccount = mon .MakeBoundAccount ()
967
- return nil
968
994
}
969
995
970
996
// BuildIndexEntriesChunk reads a chunk of rows from a table using the span sp
0 commit comments