Skip to content

Commit

Permalink
introduce query rows limit to avoid OOM
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanueltouzery committed Dec 10, 2023
1 parent 738cfc2 commit 6bc5008
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
18 changes: 16 additions & 2 deletions autoload/db.vim
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,22 @@ function! s:vim_job_close_cb(state, channel) abort
endfunction

function! s:nvim_job_callback(lines, job_id, data, event) dict abort
let a:lines[-1] .= a:data[0]
call extend(a:lines, a:data[1:])
if len(a:lines) < g:db_query_rows_limit
let a:lines[-1] .= a:data[0]
call extend(a:lines, a:data[1:])
else
" remove the last line that was written.
" since we're killing the process, it could have been stopped in the
" middle of a line. I have seen the case that it was stopped in the middle
" of a multibyte unicode glyph, resulting in invalid utf8 for the file,
" causing a fallback to latin1 encoding in vim and bad display.
" side-effect: this could be called multiple times as killing the process
" won't be instantaneous, this ensures the 'query aborted' message is
" printed only once.
call remove(a:lines, -1)
call extend(a:lines, ['Query aborted: it exceeded the rows limit'])
call s:job_stop(a:job_id)
endif
endfunction

function! s:job_run(cmd, on_finish, in_file) abort
Expand Down
8 changes: 8 additions & 0 deletions doc/dadbod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,13 @@ the following variable to enable an experimental mode where dbext's
configuration always mirrors Dadbod's default.
>
let g:dadbod_manage_dbext = 1
<
QUERY ROWS LIMIT *g:db_query_rows_limit*

In order to avoid out of memory when querying vast amounts of data from the
database, dadbod will cancel queries exceeding the maximum number of rows that
you set up in this variable. The default is 10,000 rows.
>
let g:db_query_rows_limit= 10000
<
vim:tw=78:et:ft=help:norl:
2 changes: 2 additions & 0 deletions plugin/dadbod.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ if exists('g:loaded_dadbod') || &cp || v:version < 704
endif
let g:loaded_dadbod = 1

let g:db_query_rows_limit = get(g:, 'db_query_rows_limit', 10000)

call extend(g:, {'db_adapters': {}}, 'keep')

call extend(g:db_adapters, {
Expand Down

0 comments on commit 6bc5008

Please sign in to comment.