Quick revision
The short answer
JdbcCursorItemReader streams rows over one open cursor and connection, which is fast but keeps that connection for the whole step and is not thread-safe. JdbcPagingItemReader runs one query per page ordered by a sort key and continues after the last key, so it is restartable and works with parallel steps, and JpaPagingItemReader loads entities into a persistence context per page with offset paging and more overhead. For 20 million PostgreSQL rows I would choose a JDBC reader with a unique sort key, usually JdbcPagingItemReader on the primary key, and if I use the cursor reader I must set a fetch size with autocommit off, or the driver loads every row into memory.
What to remember
In this answer, we compared three Spring Batch readers for a 20 million row PostgreSQL table. JdbcCursorItemReader streams one query over one held connection and is fast but not thread-safe, and on PostgreSQL it needs a fetch size with autocommit off to avoid loading every row. JdbcPagingItemReader uses keyset paging on a unique sort key, costs the same per page throughout, restarts precisely and suits parallel work. JpaPagingItemReader adds entity overhead and offset paging that slows down as it goes, so for this volume a JDBC reader with a unique sort key is the right choice.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
JdbcCursorItemReader streams rows over one open cursor and connection, which is fast but keeps that connection for the whole step and is not thread-safe. JdbcPagingItemReader runs one query per page ordered by a sort key and continues after the last key, so it is restartable and works with parallel steps, and JpaPagingItemReader loads entities into a persistence context per page with offset paging and more overhead. For 20 million PostgreSQL rows I would choose a JDBC reader with a unique sort key, usually JdbcPagingItemReader on the primary key, and if I use the cursor reader I must set a fetch size with autocommit off, or the driver loads every row into memory.
Why this question is asked
Reading is often half the runtime of a large batch step, and the wrong reader can make a job slow, run it out of memory, or make it impossible to parallelise. With 20 million rows, these differences become very visible: a reader that is fine for ten thousand rows can need gigabytes of memory or get slower with every page. Interviewers ask this to see whether we know how each reader talks to the database. In this answer, we will recap what a reader must do, look at each of the three readers in turn with code, compare them in one table, and then make and justify a choice, including the PostgreSQL fetch-size trap.
1A Quick Recap of What a Reader Must Do
An ItemReader returns one item per call to read() and null when there are no more. For a database, that means running a query and handing over rows one at a time without loading all 20 million into memory. A good reader for a batch job also needs to support restart: it saves its position in the step's ExecutionContext at each commit, so after a failure the step can continue from the last committed row. And if we want to parallelise later, the reader must either be safe to share between threads or be easy to split into ranges.
2JdbcCursorItemReader: One Query, Streamed
The cursor reader runs one SELECT and walks through the result set row by row, like an iterator over the whole table. The database sends rows in batches as we read them, controlled by the fetch size. It is usually the fastest way to read a large table in one thread, because there is only one query and no repeated planning or sorting.
There are costs. The cursor holds one connection open for the entire step, maybe an hour, which also keeps a long-running query and its snapshot open on the database. It is not thread-safe, so it cannot be shared in a multi-threaded step. On restart, it re-runs the query and skips forward past the rows already processed, which works but costs time.
And there is a PostgreSQL trap. The PostgreSQL JDBC driver ignores the fetch size and loads the entire result into memory unless the connection has autocommit turned off, the result set is forward-only, and a fetch size is set. With 20 million rows that is an out-of-memory error. Connection pools usually hand out connections with autocommit on, so we must turn it off for the reader's connection; check your Spring Batch version for the exact builder option.
3JdbcPagingItemReader: One Query per Page
The paging reader asks for one page at a time, for example 1,000 rows, ordered by a sort key. For the first page it runs a plain ORDER BY with a LIMIT, and for later pages it adds a condition such as id > the last id it saw. This is keyset paging: every page uses the index on id and costs the same whether it is page 1 or page 20,000.
The sort key must be unique. If it were not, rows with the same value at a page boundary could be skipped or read twice. Each page is a short query on any connection from the pool, so no connection is held for an hour. On restart, the reader saves the last sort key value in its state and starts right after it. The paging reader is also safe for concurrent reads in a multi-threaded step, although we then set saveState(false), because the saved position is not meaningful when chunks finish out of order. For parallel work, partitioning by id range with one paging reader per range is the cleaner approach.
4JpaPagingItemReader: Entities per Page
The JPA paging reader runs a JPQL query page by page and returns managed entities. It creates its own EntityManager, loads a page into the persistence context, the first-level cache that JPA keeps for tracking entities, and clears it after each page. It pages using setFirstResult and setMaxResults, which become LIMIT and OFFSET in SQL. Offset paging means the database must step over all earlier rows to reach a page, so page 20,000 scans 20 million rows to return 1,000, and the job slows down as it proceeds. On top of that, every row becomes a managed entity with dirty-checking snapshots and possibly lazy associations, which costs memory and CPU even if we only read.
It is convenient when the processing truly needs entities and their relationships, and for small tables it is fine. Spring Batch also offers a JpaCursorItemReader, which streams results instead of paging, but it still materialises entities.
5Comparing the Three
Putting the readers side by side makes the choice clearer for our 20 million rows.
| Reader | How it reads | Connection | Threads and restart | Cost at row 19 million |
|---|---|---|---|---|
JdbcCursorItemReader | one query, streamed with fetchSize | held for whole step | not thread-safe; restart skips forward | same as at start |
JdbcPagingItemReader | query per page, keyset by sort key | short query per page | thread-safe reads; restart after last key | same as at start |
JpaPagingItemReader | JPQL per page, LIMIT and OFFSET | short query per page | restart by page; entity overhead | much slower: OFFSET grows |
6The Choice and Why
For 20 million rows I would choose JdbcPagingItemReader with the primary key as the sort key. It reads in constant time per page, it does not hold a connection or a long snapshot for the whole step, it restarts precisely after the last committed key, and it fits naturally with partitioning by id range if the job later needs to be faster. If the job is single-threaded and raw speed matters most, JdbcCursorItemReader with a fetch size of around 1,000 and autocommit off is a fine alternative, as long as the database can tolerate one long-running query. I would avoid JpaPagingItemReader for this volume because of offset paging and entity overhead, unless the processing truly needs entities, and even then I would read ids with JDBC and load entities in batches.
7Follow-up questions
What page size should I use for JdbcPagingItemReader?
It is usually set equal to or a multiple of the chunk size, for example 1,000, so that each chunk maps to a page. Very small pages mean many queries; very large pages use more memory per page. Measuring two or three values on real data is the reliable way to choose.
Why does a long-running cursor matter to PostgreSQL?
A query that stays open keeps its snapshot, and PostgreSQL cannot clean up row versions that the snapshot might still need. During an hour-long cursor on a busy database, dead rows accumulate in other tables too, and vacuum falls behind. Short paging queries avoid this.
Can I read with a cursor in a partitioned step?
Yes, each partition can have its own step-scoped cursor reader with a WHERE clause for its range, because each worker has its own reader instance. The restriction is only on sharing one cursor reader between threads.
8Conclusion
In this answer, we compared three Spring Batch readers for a 20 million row PostgreSQL table. JdbcCursorItemReader streams one query over one held connection and is fast but not thread-safe, and on PostgreSQL it needs a fetch size with autocommit off to avoid loading every row. JdbcPagingItemReader uses keyset paging on a unique sort key, costs the same per page throughout, restarts precisely and suits parallel work. JpaPagingItemReader adds entity overhead and offset paging that slows down as it goes, so for this volume a JDBC reader with a unique sort key is the right choice.
Open with
For 20 million rows I would use a JDBC reader, and my default is JdbcPagingItemReader sorted by the primary key. Let me compare the three. JdbcCursorItemReader runs one query and streams the result set, so it is very fast in a single thread, but it holds one connection and one open query for the whole step, it is not thread-safe, and on PostgreSQL it will load every row into memory unless I set a fetch size and make sure autocommit is off on that connection.
Then, if they want more
JdbcPagingItemReader issues a query per page with a sort key, and for later pages it uses a condition like id greater than the last id, which is keyset paging, so page twenty thousand costs the same as page one. The sort key must be unique.
It restarts right after the last committed key, does not hold a connection for an hour, and works with partitioning or multi-threaded steps, where I would turn off saveState. JpaPagingItemReader loads managed entities into a persistence context per page and pages with offset, so it gets slower as the offset grows and uses much more memory and CPU.
So JdbcPagingItemReader on the id is the safe choice, and a cursor reader with fetch size and autocommit off is a good alternative if the job stays single-threaded.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Batch reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You need to read customers from a PostgreSQL table with 20 million rows in a Spring...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/3526524043/