Skip to content

Commit

Permalink
Merge pull request cockroachdb#22991 from tschottdorf/delete-oom
Browse files Browse the repository at this point in the history
sql: chunk deletions
  • Loading branch information
tbg authored Feb 23, 2018
2 parents 43e20cc + a2da685 commit 494b091
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/sql/tablewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down

0 comments on commit 494b091

Please sign in to comment.