Skip to content

Fix jdbc_fetch_size with postgresql #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/logstash/plugin_mixins/jdbc/jdbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def load_driver
require "java"
require "sequel"
require "sequel/adapters/jdbc"
require "sequel/adapters/jdbc/transactions"

load_driver_jars
begin
Expand Down Expand Up @@ -193,6 +194,7 @@ def open_jdbc_connection

@database = jdbc_connect()
@database.extension(:pagination)
@database.extend(Sequel::JDBC::Transactions)
if @jdbc_default_timezone
@database.extension(:named_timezones)
@database.timezone = @jdbc_default_timezone
Expand Down
18 changes: 11 additions & 7 deletions lib/logstash/plugin_mixins/jdbc/statement_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ class NormalStatementHandler < StatementHandler
# @yieldparam row [Hash{Symbol=>Object}]
def perform_query(db, sql_last_value, jdbc_paging_enabled, jdbc_page_size)
query = build_query(db, sql_last_value)
if jdbc_paging_enabled
query.each_page(jdbc_page_size) do |paged_dataset|
paged_dataset.each do |row|
# Execute query in transaction cause PG driver require autocommit off for set fetch count
# See: https://jdbc.postgresql.org/documentation/head/query.html
db.transaction(rollback: :always) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might be possible to potentially use paged_each here?

If not, do we need to use transactions when paging is not enabled?

if jdbc_paging_enabled
query.each_page(jdbc_page_size) do |paged_dataset|
paged_dataset.each do |row|
yield row
end
end
else
query.each do |row|
yield row
end
end
else
query.each do |row|
yield row
end
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/inputs/jdbc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,6 @@
{
"statement" => "SELECT * FROM test_table",
"jdbc_pool_timeout" => 0,
"jdbc_connection_string" => 'mock://localhost:1527/db',
"sequel_opts" => {
"max_connections" => 1
}
Expand Down Expand Up @@ -1076,7 +1075,7 @@
end

it "should report the statements to logging" do
expect(plugin.logger).to receive(:debug).once
expect(plugin.logger).to receive(:debug).thrice
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know why this needed to change?

plugin.run(queue)
end
end
Expand Down