Methods
Public Instance
Included modules
Classes and Modules
Constants
| BindArgumentMethods | = | prepared_statements_module(:bind, [ArgumentMapper], %w'execute execute_dui') | ||
| PREPARED_ARG_PLACEHOLDER | = | LiteralString.new('$').freeze |
:nocov: |
|
| PreparedStatementMethods | = | prepared_statements_module(:prepare, BindArgumentMethods, %w'execute execute_dui') |
Public Instance methods
# File lib/sequel/adapters/postgres.rb 739 def bound_variable_modules 740 [BindArgumentMethods] 741 end
# File lib/sequel/adapters/postgres.rb 650 def fetch_rows(sql) 651 return cursor_fetch_rows(sql){|h| yield h} if @opts[:cursor] 652 execute(sql){|res| yield_hash_rows(res, fetch_rows_set_cols(res)){|h| yield h}} 653 end
Use a cursor for paging.
# File lib/sequel/adapters/postgres.rb 656 def paged_each(opts=OPTS, &block) 657 unless defined?(yield) 658 return enum_for(:paged_each, opts) 659 end 660 use_cursor(opts).each(&block) 661 end
PostgreSQL uses $N for placeholders instead of ?, so use a $ as the placeholder.
# File lib/sequel/adapters/postgres.rb 749 def prepared_arg_placeholder 750 PREPARED_ARG_PLACEHOLDER 751 end
# File lib/sequel/adapters/postgres.rb 743 def prepared_statement_modules 744 [PreparedStatementMethods] 745 end
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. |
| :skip_transaction |
Same as :hold, but :hold takes priority. |
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.
# File lib/sequel/adapters/postgres.rb 686 def use_cursor(opts=OPTS) 687 clone(:cursor=>{:rows_per_fetch=>1000}.merge!(opts)) 688 end
Replace the WHERE clause with one that uses CURRENT OF with the given cursor name (or the default cursor name). This allows you to update a large dataset by updating individual rows while processing the dataset via a cursor:
DB[:huge_table].use_cursor(rows_per_fetch: 1).each do |row| DB[:huge_table].where_current_of.update(column: ruby_method(row)) end
# File lib/sequel/adapters/postgres.rb 698 def where_current_of(cursor_name='sequel_cursor') 699 clone(:where=>Sequel.lit(['CURRENT OF '], Sequel.identifier(cursor_name))) 700 end