Skip to content

Free batch context on re-scan#282

Merged
theory merged 1 commit into
mainfrom
memleak2
Jun 16, 2026
Merged

Free batch context on re-scan#282
theory merged 1 commit into
mainfrom
memleak2

Conversation

@theory

@theory theory commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

For a normal foreign scan, it's fine to let Postgres clean up the batch_cxt when it clears the memory context. But for a scan that repeatedly re-scans (e.g., clickhouseEndForeignScan is called via the ReScanForeignScan callback), it Postgres doesn't clear the context until the query finishes. Since pg_clickhouse creates the ChFdwScanState anew for each scan (in clickhouseBeginForeignScan()), that memory from previous scans would just hang around.

Delete batch_cxt in clickhouseEndForeignScan to ensure the context is cleaned up for every scan. Resolves #273.

@theory theory requested review from JoshDreamland and serprex June 15, 2026 15:49
@theory theory self-assigned this Jun 15, 2026
@theory theory added the bug Something isn't working label Jun 15, 2026
For a normal foreign scan, it's fine to let Postgres clean up the
`batch_cxt` when it clears the memory context. But for a scan that
repeatedly re-scans (e.g., `clickhouseEndForeignScan` is called via the
`ReScanForeignScan` callback), it Postgres doesn't clear the context
until the query finishes. Since pg_clickhouse creates the
`ChFdwScanState` anew for each scan (in `clickhouseBeginForeignScan()`),
that memory from previous scans would just hang around.

Delete `batch_cxt` in `clickhouseEndForeignScan` to ensure the context
is cleaned up for every scan. Resolves #273.
@theory theory merged commit da76426 into main Jun 16, 2026
23 checks passed
@theory theory deleted the memleak2 branch June 16, 2026 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak across rescans: scan's batch_cxt is never reset, accumulates parameter allocations for the whole query

2 participants