Loading

Database Connector Select does not stream large result sets on PostgreSQL

Publiceringsdatum: Sep 29, 2026
Beskrivning

Summary

A db:select (or any query operation) against PostgreSQL does not stream its results as expected. Even though the Database Connector documents that query results are streamed automatically, the PostgreSQL JDBC driver fetches the entire result set in a single round trip instead of paging through it with a server-side cursor. For large result sets this defeats streaming — the full result set is retrieved from the database before the flow starts processing rows.

Typical signals:

  • Setting fetchSize on the operation has no effect — the driver fetches every row regardless.
  • The connector does not begin processing rows until the complete result set has been transferred from the database.
  • The PostgreSQL driver holds the full result set internally rather than fetching rows on demand.

Environment

  • Database Connector — all current versions (reproduced on 1.14.x through 1.16.4).
  • PostgreSQL, via the PostgreSQL JDBC driver (pgjdbc) — any version.
  • Mule Runtime 4.x.

Cause

This is a PostgreSQL JDBC (pgjdbc) behavior, not a connector defect. On PostgreSQL a server-side cursor — the thing that lets the driver fetch rows in batches instead of materializing them all — only exists inside a transaction. pgjdbc therefore only honors fetchSize (only streams) when the connection has autoCommit = false.

When autoCommit = true, pgjdbc ignores fetchSize and retrieves the entire result set in a single round trip instead of fetching it in batches through a cursor.

How this interacts with the Database Connector:

  • The connector does apply fetchSize to the JDBC Statement.
  • The connector only sets autoCommit = false when a Mule transaction is started.
  • A Select run outside a transaction (the default, transactionalAction="NOT_SUPPORTED") keeps autoCommit = true. So pgjdbc never opens a cursor and fetches the whole result set at once instead of streaming it.

There is no simple property to flip this:

  • pgjdbc has no autoCommit connection/URL property — it cannot be set on the JDBC URL.
  • db:generic-connection and the pooling profile do not expose an autoCommit setting.
  • The connector's built-in c3p0 pool cannot be made to hand out autoCommit=false connections.

So the only two levers are: be inside a transaction, or supply a connection that already has autoCommit=false. That gives the two workarounds below.

Note on other databases: databases whose JDBC drivers stream without a transaction (for example, where the fetch size alone is enough) are not affected. This limitation is specific to drivers — PostgreSQL among them — that bind the cursor to a transaction.


Workaround 1 — Wrap the Select AND its consumption in a transaction (Try scope)

Put the query inside a <try> scope with transactionalAction="ALWAYS_BEGIN". The transaction sets autoCommit = false, so pgjdbc opens a server-side cursor and streams.

Critical: the transaction must span the consumption of the result, not just the Select. If the scope commits/exits before the stream is read, the cursor and ResultSet are closed and you get:

org.postgresql.util.PSQLException: This ResultSet is closed.

Notes:

  • Set writes to a second database to transactionalAction="NOT_SUPPORTED" so they don't try to join the source transaction. They commit independently.
  • No XA is required. XA would only matter if you needed a single all-or-nothing transaction across both databases; it is not needed to stream.

Workaround 2 — Supply an application-provided pooled DataSource with autoCommit=false

Instead of relying on a Mule transaction, hand the connector a javax.sql.DataSource that already hands out connections with autoCommit = false, and reference it with db:data-source-connection.

When a DataSource is supplied this way, the connector uses it directly and does not build its internal c3p0 pool. Because autoCommit is off at the connection level (not via a Mule transaction), the cursor streams and the connection is held for the whole message — so the stream survives the Select returning from a sub-flow, with no transaction scope anywhere in the flow. Only the source db:config changes.

Back the DataSource with a connection pool so connections are pooled/reused and the read transaction is rolled back on return (no "idle in transaction" leak). The example below uses HikariCP; Apache DBCP2 (defaultAutoCommit=false) works equally well. HikariCP is convenient because autoCommit=false is a single property and it rolls back dirty connections on return automatically.

 

  1. A small pooled DataSource (packaged with the application).
  2. Add the pool dependency to the application pom.xml (so it's packaged with the app):
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>4.0.3</version>
</dependency>
  1. Wire it up and reference it from the source db:config:
<object name="pgStreamingDataSource" class="com.mycompany.util.PooledAutoCommitOffDataSource">
    <property key="url" value="jdbc:postgresql://host:5432/sourcedb"/>
    <property key="user" value="${source.user}"/>
    <property key="password" value="${source.password}"/>
    <property key="maxPoolSize" value="4"/>
</object>

<db:config name="Source_DB">
    <db:data-source-connection dataSourceRef="pgStreamingDataSource"/>
</db:config>

Additional information / References

The contents of this article are provided for informational purposes only, and the workarounds are intended to be used solely as references.

Knowledge-artikelnummer

005460802

 
Laddar
Salesforce Help | Article