Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
bitflicker64
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
imbajin
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
Purpose of the PR
JDBCFetchersets the fetch size on theResultSetafterexecuteQueryand leaves auto-commit on. The PostgreSQLdriver therefore buffers the whole table in memory, and large JDBC sources fail with
OutOfMemoryError: Java heap spaceregardless ofbatch_size.This PR targets PostgreSQL. MySQL Connector/J still buffers the result set unless the JDBC URL sets
useCursorFetch=true(or the fetch size isInteger.MIN_VALUE); that is left out of scope.Main Changes
fetchSize(=batch_size) on theStatementbeforeexecuteQuery.if a driver does not support it, log a warning and keep the driver default.
Verifying these changes
-Xmx256m:OutOfMemoryError: Java heap space-Xmx1g: OK (process peak ~910 MB), so memory grows with the table sizeDoes this PR potentially affect the following parts?
Documentation Status
Doc - No Need