Memory regression in 1.42.0: async Parquet prefetch count raised to 512 → ~2× peak RAM on streaming scan_parquet → sink_parquet, OOMs scripts that worked on 1.41.2
Checks
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of Polars.
Reproducible example
Reproduction (self-contained; needs ~16 GB free to run the 1.42.x/32-thread case — lower ROWS/threads otherwise):
import os, resource, sys
import polars as pl
DIR, OUT, FILES, ROWS, NCOL = "wide_parquet", "out.parquet", 32, 16_000_000, 89
# A wide, highly-compressible table: ~200 MB on disk, several GB once decoded.
os.makedirs(DIR, exist_ok=True)
if len([f for f in os.listdir(DIR) if f.endswith(".parquet")]) < FILES:
per = ROWS // FILES
for f in range(FILES):
idx = pl.int_range(f * per, f * per + per, dtype=pl.Int64)
cols = {"id": idx.cast(pl.Utf8)}
for k in range(45):
cols[f"i{k}"] = pl.when(idx % 7 == 0).then(idx % 50).otherwise(None).cast(pl.Int64)
for k in range(25):
cols[f"s{k}"] = pl.when(idx % 5 == 0).then(pl.lit("c") + (idx % 12).cast(pl.Utf8)).otherwise(None)
for k in range(NCOL - 1 - 45 - 25):
cols[f"f{k}"] = (idx % 100).cast(pl.Float64)
pl.select(**cols).write_parquet(f"{DIR}/{f:03}.parquet", row_group_size=50_000, compression="snappy")
os.environ.setdefault("POLARS_FORCE_ASYNC", "1") # automatic for cloud scans (s3://, gcs://, http://, ...)
if os.path.exists(OUT):
os.remove(OUT)
pl.scan_parquet(f"{DIR}/*.parquet").sink_parquet(OUT)
rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
mb = rss / 1048576 if sys.platform == "darwin" else rss / 1024
print(f"polars={pl.__version__} threads={os.environ.get('POLARS_MAX_THREADS','all')} "
f"prefetch_size={os.environ.get('POLARS_ROW_GROUP_PREFETCH_SIZE','default')} peak_rss={mb:.0f}MB")Run:
pip install "polars==1.41.2" && POLARS_MAX_THREADS=32 python repro.py # ~8 GB
pip install "polars==1.42.1" && POLARS_MAX_THREADS=32 python repro.py # ~15 GB (regression)
POLARS_MAX_THREADS=32 POLARS_ROW_GROUP_PREFETCH_SIZE=64 python repro.py # ~7 GB (workaround) Workaround: set POLARS_ROW_GROUP_PREFETCH_SIZE back to ~num_pipelines * 2.
Environment:
Log output
Issue description
Upgrading 1.41.2 → 1.42.x roughly doubles peak memory for a plain streaming copy of a wide Parquet dataset read through the async reader (any cloud scan, or POLARS_FORCE_ASYNC=1 locally). On memory-limited machines this turns a job that completed on 1.41.2 into an OOM on 1.42.x, with no user code change.
Root cause is the default row-group prefetch count. POLARS_VERBOSE=1 shows it jump from prefetch_limit: 64 (1.41.2) to prefetch_limit: 512 (1.42.x) at 32 threads. Each prefetched row group is decoded into memory, so 8× more concurrent decode ⇒ multiples more resident RAM. The accompanying prefetch_kbytes_limit byte budget does not bound this — setting it alone has no effect, because (per the code comment) it doesn't account for the decompression/decode expansion, and the count limit dominates. Peak RAM scales ~linearly with the prefetch count:
polars 1.42.x, 16M rows × 89 cols, 32 threads: POLARS_ROW_GROUP_PREFETCH_SIZE = 8 → 2.9 GB 16 → 3.8 GB 32 → 5.3 GB 64 → 7.0 GB (≈ 1.41.2 default) default (512) → 14.9 GB
Version comparison (same script, default settings):
16 threads 32 threads
1.41.2 4.8 GB 8.1 GB
1.42.x 9.3 GB 14.9 GB (~2×; OOMs a 16 GB host)We're aware of the // TODO: Dynamically adapt the max memory to the observed BDP next to this logic — but as shipped it's functioning as a bug: a fixed default of 512 concurrent prefetched row groups, chosen without regard to row-group width or available memory, is a silent ~2× memory regression on upgrade that breaks previously-working scripts. num_pipelines * 2 (1.41.2's behavior) was safe; a hard floor/target of 512 is not.
Relevant PR: https://github.com/pola-rs/polars/pull/27924
Expected behavior
upgrading should not ~2× peak memory for the same streaming scan. The default prefetch count should scale with row-group width / available memory (or stay at the 1.41.2 num_pipelines * 2) rather than a fixed 512.
Installed versions
polars 1.41.2 (good) vs 1.42.0/1.42.1 (regressed); Python 3.10; Linux/macOS; reproduces on the async reader path (cloud scans or POLARS_FORCE_ASYNC=1).
Source: pola-rs/polars