Skip to content

Commit

Permalink
Respect :skip_transaction option in PostgreSQL Dataset#paged_each (Fixes
Browse files Browse the repository at this point in the history
 #2097)

This turns on the hold option, which does have effects beyond just
not using a transaction, but you cannot use a non-HOLD cursor
outside of a transaction.
  • Loading branch information
jeremyevans committed Nov 16, 2023
1 parent 4637356 commit 0239207
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== master

* Respect :skip_transaction option in PostgreSQL Dataset#paged_each (jeremyevans) (#2097)

* Add TimestampMigrator.run_single to run a single migration file up or down (opya, jeremyevans) (#2093)

* Support INSERT RETURNING on MariaDB 10.5+, and use it when saving new model objects (jeremyevans)
Expand Down
7 changes: 4 additions & 3 deletions lib/sequel/adapters/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ def paged_each(opts=OPTS, &block)
# 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:
#
Expand Down Expand Up @@ -764,13 +765,13 @@ def call_procedure(name, args)

# Use a cursor to fetch groups of records at a time, yielding them to the block.
def cursor_fetch_rows(sql)
server_opts = {:server=>@opts[:server] || :read_only}
cursor = @opts[:cursor]
hold = cursor[:hold]
hold = cursor.fetch(:hold){cursor[:skip_transaction]}
server_opts = {:server=>@opts[:server] || :read_only, :skip_transaction=>hold}
cursor_name = quote_identifier(cursor[:cursor_name] || 'sequel_cursor')
rows_per_fetch = cursor[:rows_per_fetch].to_i

db.public_send(*(hold ? [:synchronize, server_opts[:server]] : [:transaction, server_opts])) do
db.transaction(server_opts) do
begin
execute_ddl("DECLARE #{cursor_name} NO SCROLL CURSOR WITH#{'OUT' unless hold} HOLD FOR #{sql}", server_opts)
rows_per_fetch = 1000 if rows_per_fetch <= 0
Expand Down

0 comments on commit 0239207

Please sign in to comment.