Skip to content

fix(supabase-wrappers): allocate aggregate scan tuples in the slot context - #680

Open
hsusul wants to merge 1 commit into
supabase:mainfrom
hsusul:fix/agg-scan-tuple-double-free
Open

hsusul wants to merge 1 commit into
supabase:mainfrom
hsusul:fix/agg-scan-tuple-double-free

Conversation

@hsusul

@hsusul hsusul commented Nov 8, 2026

Copy link
Copy Markdown

Why

A pushed-down aggregate read by a consumer that stops at the row, such as SELECT count(*) INTO n FROM ft in PL/pgSQL, frees memory that is already freed. The issue shows pfree called with invalid pointer at executor end. On a pgrx pg15 build without assertions, the backend segfaults instead.

Fixes #660.

What changed

The aggregate branch of iterate_foreign_scan formed its heap tuple in the scan's tmp_ctx and stored it with shouldFree = true. end_foreign_scan drops the FdwState, which deletes tmp_ctx. Then ExecEndPlan clears the slot and frees the tuple from that deleted context.

The tuple is now formed in the slot's own context (tts_mcxt), which outlives the scan state. The slot still owns it and frees it on the next clear, as before.

Tests

clickhouse_aggregate_pushdown_test now runs SELECT COUNT(*) INTO n FROM agg_test in a DO block. On main the pg15 test backend dies with signal 11 at that statement. With this change it passes.

Run locally on pg15 against a native ClickHouse server:

  • cargo pgrx test --features "clickhouse_fdw pg15" pg15: 19 passed
  • cargo test -p supabase-wrappers --no-default-features --features pg15 --lib: 36 passed
  • RUSTFLAGS="-D warnings" cargo clippy --tests --no-deps --features "clickhouse_fdw pg15" in wrappers/: clean
  • cargo fmt --check -p supabase-wrappers -p wrappers: clean

Not run: the other native FDW suites.

…ntext

The aggregate scan stored a heap tuple formed in the scan's tmp_ctx with
shouldFree = true. When a consumer stops at that row (SELECT ... INTO, SPI with
tcount = 1), end_foreign_scan deletes tmp_ctx first and ExecEndPlan then frees
the tuple from deleted memory.

Form the tuple in the slot's memory context, which outlives the scan state.

Fixes supabase#660

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aggregate pushdown: result row double-freed at executor end (pfree called with invalid pointer)

1 participant