Skip to content

fix(loader): stream PostgreSQL JDBC rows instead of buffering the whole result set - #909

Open
koyr-exem wants to merge 4 commits into
apache:masterfrom
koyr-exem:fix-loader-jdbc-fetch-size
Open

koyr-exem wants to merge 4 commits into
apache:masterfrom
koyr-exem:fix-loader-jdbc-fetch-size

Conversation

@koyr-exem

@koyr-exem koyr-exem commented Nov 17, 2026 •

Copy link
Copy Markdown

Purpose of the PR

JDBCFetcher sets the fetch size on the ResultSet after executeQuery and leaves auto-commit on. The PostgreSQL
driver therefore buffers the whole table in memory, and large JDBC sources fail with
OutOfMemoryError: Java heap space regardless of batch_size.

This PR targets PostgreSQL. MySQL Connector/J still buffers the result set unless the JDBC URL sets
useCursorFetch=true (or the fetch size is Integer.MIN_VALUE); that is left out of scope.

Main Changes

  • Set fetchSize (= batch_size) on the Statement before executeQuery.
  • Turn off auto-commit before creating the statement, which PostgreSQL needs to use a cursor. The fetcher is read-only;
    if a driver does not support it, log a warning and keep the driver default.

Verifying these changes

  • Need tests and can be verified as follows:
    • PostgreSQL 16, 1,000,000-row table (~301 MB), loader run with -Xmx256m:
      • before: OutOfMemoryError: Java heap space
      • after: all 1,000,000 vertices inserted (input read / parse / insert success 1,000,000, failure 0)
    • before, with -Xmx1g: OK (process peak ~910 MB), so memory grows with the table size

Does this PR potentially affect the following parts?

  • Dependencies
  • Modify configurations
  • The public API
  • Other affects
  • Nope

Documentation Status

  • Doc - No Need

JDBCFetcher set the fetch size on the ResultSet after executeQuery and
left auto-commit on, so the PostgreSQL driver (and MySQL by default)
buffered the whole table in memory and large JDBC sources failed with
OutOfMemoryError regardless of batch_size.

Set the fetch size on the Statement before executing the query and turn
off auto-commit when the driver supports it.

close apache#908

@bitflicker64 bitflicker64 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Blocking: yes. Summary: Setting the fetch size before executeQuery with auto-commit off makes PostgreSQL stream rows, but the call to getAutoCommit breaks the existing JDBCFetcherTest in the unit suite. MySQL still buffers the whole result set, which the description says this change covers.


private void disableAutoCommit() {
try {
if (this.conn.getAutoCommit()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important: JDBCFetcherTest.testLocalDateTimeUsesDriverTimestamp fails on this head with a NullPointerException from getAutoCommit, because its Connection proxy returns null for every method other than createStatement and isClosed, and disableAutoCommit catches only SQLException. Make the proxy answer getAutoCommit, and assert that setAutoCommit(false) and setFetchSize run before executeQuery.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks — the test's Connection proxy returned null for getAutoCommit. It now answers getAutoCommit/setAutoCommit, and the test asserts the call order setAutoCommit(false) → setFetchSize(batch_size) → executeQuery. Fixed in 38b7c10.

disableAutoCommit();
stmt = this.conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
java.sql.ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(source.batchSize());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important: MySQL Connector/J streams only when the fetch size is Integer.MIN_VALUE or the URL sets useCursorFetch=true, and JDBCVendor.MYSQL.buildUrl sets neither, so a fetch size of batch_size still buffers the whole MySQL table. Make MySQL stream too, or scope the title and commit message to PostgreSQL.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point. I scoped the title and description to PostgreSQL. For MySQL, users can add useCursorFetch=true to the URL; making MySQL stream by default (Integer.MIN_VALUE) changes connection behavior, so I'd rather leave that to a separate issue.

…CFetcherTest

Scope: this change makes PostgreSQL stream JDBC rows. MySQL Connector/J
still needs useCursorFetch=true (or fetch size Integer.MIN_VALUE).
@koyr-exem koyr-exem changed the title fix(loader): stream JDBC rows instead of buffering the whole result set fix(loader): stream PostgreSQL JDBC rows instead of buffering the whole result set Nov 18, 2026
@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.26%. Comparing base (2080729) to head (6ef986e).

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #909      +/-   ##
============================================
- Coverage     57.59%   57.26%   -0.33%     
+ Complexity     6539     5185    -1354     
============================================
  Files           623      397     -226     
  Lines         33781    26774    -7007     
  Branches       4517     4046     -471     
============================================
- Hits          19456    15333    -4123     
+ Misses        12610     9933    -2677     
+ Partials       1715     1508     -207     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bitflicker64 bitflicker64 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Blocking: no. Score: 9/10. Summary: Setting fetchSize on the Statement before executeQuery with auto-commit off meets the pgjdbc cursor conditions, so PostgreSQL streams rows instead of buffering the table. Both earlier points are addressed: the test proxy answers getAutoCommit and asserts the call order, and the title scopes the change to PostgreSQL.

@imbajin imbajin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Score: 7.0/10 · Confidence: medium

Blocking: yes

Summary

The PostgreSQL cursor change also leaves MySQL reads in an open transaction for the duration of the load, potentially retaining undo history on busy InnoDB sources.

Evidence

The shared fetcher disables auto-commit before executing the query for every vendor and closes the connection only when the reader closes. MySQL documents this transaction pattern as a cause of purge lag.

Validation notes

Source and MySQL documentation review only; no live MySQL or PostgreSQL integration test was run.

// Some drivers (e.g. PostgreSQL) only stream rows through a cursor when
// auto-commit is off and the fetch size is set before the query runs;
// otherwise the whole result set is buffered in memory.
disableAutoCommit();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

⚠️ Avoid opening a long transaction for MySQL reads

This fetcher is also used for MySQL, so disabling auto-commit here starts a transaction for its SELECT that remains open until the reader closes, even when Connector/J buffers the result. With InnoDB's REPEATABLE READ and concurrent writes, a long load can retain undo history and cause purge lag (MySQL documentation). Could this transaction change be limited to PostgreSQL, or otherwise ended promptly for non-streaming drivers? This risk follows from the source and documented database behavior; I have not reproduced it on a live MySQL instance.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, thanks. Fixed in 6ef986e: auto-commit is now disabled only for JDBCVendor.POSTGRESQL, the only driver that needs it for cursor streaming. MySQL, Oracle and SQL Server keep the driver default, so no transaction is held open for the load; the fetch size is still set on the statement before the query for every vendor. Added unit tests for both paths (PostgreSQL turns auto-commit off before executeQuery; the other vendors leave it on).

Note that PostgreSQL itself still holds one read-only transaction while the rows are read, which is inherent to pgjdbc cursor mode.

Keep the driver default for MySQL, Oracle and SQL Server so the SELECT does
not hold a transaction open for the whole load; pgjdbc is the only driver
that needs auto-commit off to stream rows through a cursor.
@koyr-exem
koyr-exem requested a review from imbajin November 20, 2026 00:01

@imbajin imbajin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Score: 8.5/10 · Confidence: medium

Blocking: no

Summary

No new blocking issue was verified. The previous MySQL long-transaction concern is addressed by limiting the auto-commit change to PostgreSQL.

Evidence

Reviewed the fetcher, reader lifecycle, vendor branches, proxy tests and prior discussion. Fetch size is set on the statement before execution; the current vendor guard leaves MySQL auto-commit unchanged.

Validation notes

Source-based review; no live JDBC driver or database test was run independently. The current CI is still running, so this is not an approval.

@bitflicker64 bitflicker64 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Blocking: no. Score: 9/10. Summary: Limiting setAutoCommit(false) to JDBCVendor.POSTGRESQL resolves the MySQL long-transaction concern, and setting fetchSize on the Statement before executeQuery still meets the pgjdbc cursor conditions. The new JDBCFetcherTest cases cover the PostgreSQL, other-vendor, already-off and unsupported paths, and pass in loader-ci.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Loader JDBC source buffers the whole PostgreSQL table in memory (OOM on large tables)

3 participants