diff --git a/pkg/sql/tablewriter.go b/pkg/sql/tablewriter.go index 2caa59ae78c1..84385d8243d8 100644 --- a/pkg/sql/tablewriter.go +++ b/pkg/sql/tablewriter.go @@ -715,8 +715,9 @@ func (tu *tableUpserter) close(ctx context.Context) { // tableDeleter handles writing kvs and forming table rows for deletes. type tableDeleter struct { - rd sqlbase.RowDeleter - alloc *sqlbase.DatumAlloc + rd sqlbase.RowDeleter + alloc *sqlbase.DatumAlloc + batchSize int // Set by init. txn *client.Txn @@ -736,6 +737,18 @@ func (td *tableDeleter) init(txn *client.Txn, evalCtx *tree.EvalContext) error { func (td *tableDeleter) row( ctx context.Context, values tree.Datums, traceKV bool, ) (tree.Datums, error) { + // Rudimentarily chunk the deletions to avoid memory blowup in queries such + // as `DELETE FROM mytable`. + const maxBatchSize = 10000 + if td.batchSize >= maxBatchSize { + if err := td.txn.Run(ctx, td.b); err != nil { + return nil, err + } + td.b = td.txn.NewBatch() + td.batchSize = 0 + } + td.batchSize++ + return nil, td.rd.DeleteRow(ctx, td.b, values, sqlbase.CheckFKs, traceKV) }