Method: Sequel::Postgres::Dataset#use_cursor

Defined in:
lib/sequel/adapters/postgres.rb

#use_cursor(opts = OPTS) ⇒ Object

Uses a cursor for fetching records, instead of fetching the entire result set at once. Note this uses a transaction around the cursor usage by default and can be changed using `hold: true` as described below. Cursors can be used to process large datasets without holding all rows in memory (which is what the underlying drivers may do by default). Options:

:cursor_name

The name assigned to the cursor (default 'sequel_cursor'). Nested cursors require different names.

:hold

Declare the cursor WITH HOLD and don't use transaction around the cursor usage.

:rows_per_fetch

The number of rows per fetch (default 1000). Higher numbers result in fewer queries but greater memory use.

Usage:

DB[:huge_table].use_cursor.each{|row| p row}
DB[:huge_table].use_cursor(:rows_per_fetch=>10000).each{|row| p row}
DB[:huge_table].use_cursor(:cursor_name=>'my_cursor').each{|row| p row}

This is untested with the prepared statement/bound variable support, and unlikely to work with either.



707
708
709
# File 'lib/sequel/adapters/postgres.rb', line 707

def use_cursor(opts=OPTS)
  clone(:cursor=>{:rows_per_fetch=>1000}.merge!(opts))
end