#26270·duckdb

Streamed Arrow export reserves batch_size rows per column, even for a 1-row result

Author: emilheCreated Sep 29, 2026Updated Sep 29, 2026

What happens?

con.execute(q).to_arrow_table() (and fetch_arrow_table(), to_arrow_reader()) on a streamed result allocates buffers for the full batch_size (default 1 000 000 rows) per column before it knows the row count. It does so twice per fetch: once for the data and once for the empty end-of-stream call. For a 1-row result of INT, BIGINT, DOUBLE, VARCHAR, DATE that is about 29 MB malloc'd per call, of which a few KB are written. The returned table keeps its buffers alive, so every retained small result pins that capacity.

Under 4 KB pages the unwritten capacity never becomes resident, so this is mostly invisible. glibc 2.43 on aarch64 applies MADV_HUGEPAGE to the main heap by default (BZ 34081). The kernel then backs those buffers with 2 MB pages, and the allocated-but-unwritten capacity becomes RSS: 6× in the reproducer below, 12× (1.2 → 15.2 GB) in a test suite running through fakesnow (tekumara/fakesnow#435). Each piece is reasonable alone; the three combine.

Source (v1.5.6; unchanged on main)

To reproduce

python
# Many 1-300-row results fetched as Arrow; 32 kept alive, as open cursors or caches do.
import random, sys, time
import duckdb

kw = {"batch_size": int(sys.argv[1])} if len(sys.argv) > 1 else {}
con = duckdb.connect()
con.execute("""CREATE TABLE t AS SELECT i::INT a, (i * 7)::BIGINT b, i / 3.0 c,
               'name_' || i d, DATE '2020-01-01' + (i % 1000)::INT e FROM range(100000) r(i)""")
kept, rng, t0 = [], random.Random(0), time.time()
for _ in range(2000):
    lo, n = rng.randrange(99000), rng.choice((1, 3, 10, 50, 300))
    q = f"SELECT * FROM t WHERE a >= {lo} AND a < {lo + n}"
    kept.append(con.execute(q).to_arrow_table(**kw))
    con.execute(f"DESCRIBE {q}").to_arrow_table(**kw)
    if len(kept) > 32:
        kept.pop(rng.randrange(len(kept)))

mem = {l.split()[0]: int(l.split()[1]) >> 10 for l in open("/proc/self/smaps_rollup") if l[0].isupper()}
print(f"duckdb {duckdb.__version__} {kw or 'default'}: Rss {mem['Rss:']} MB,"
      f" AnonHugePages {mem['AnonHugePages:']} MB, {time.time() - t0:.1f} s")

duckdb 1.5.6 and pyarrow 24.0.0 from PyPI, Python 3.12/3.14, arm64, kernel 6.8, THP madvise; medians of 3 runs (1.5.4 gives the same picture):

container Rss MB AnonHugePages MB wall s
ubuntu:24.04, glibc 2.39 93 0 0.6
ubuntu:26.04, glibc 2.43 555 470 2.7
glibc 2.43, GLIBC_TUNABLES=glibc.malloc.hugetlb=0 97 0 0.6
glibc 2.43, batch_size=2048 (argument 2048) 106 24 0.5

Suggested fix

Size the appender from the rows at hand rather than from batch_size: start at MinValue(batch_size, scan_state.RemainingInChunk()) (or one vector) and let the existing power-of-two growth take over, as the materialized collector does. Skipping the appender entirely when the stream is already exhausted also removes the second allocation. Happy to test a patch.