Skip to content

Fix stale fieldsCache in withPgDebug across AtOnce queries#803

Merged
LaurentRDC merged 1 commit intohaskell-beam:masterfrom
compilerbauer:fix/fields-cache-stale-802
Apr 23, 2026
Merged

Fix stale fieldsCache in withPgDebug across AtOnce queries#803
LaurentRDC merged 1 commit intohaskell-beam:masterfrom
compilerbauer:fix/fields-cache-stale-802

Conversation

@compilerbauer
Copy link
Copy Markdown
Contributor

Summary

  • renderExecReturningList (the AtOnce path) was calling cachedGetFields and then freeing the result via Pg.unsafeFreeResult. libpq can reuse the freed pointer address on the next Pg.exec, causing the next query to get a false cache hit and inherit stale field OID metadata from the previous query.
  • Fix: renderExecReturningList now calls getFields directly. The cachedGetFields one-entry cache is preserved for stepProcess (CursorBatching), where unsafeFreeResult is never called and the cache is both safe and beneficial (avoids getFields per row within a 256-row batch).
  • Regression test added: two tables with different column types (TEXT vs INT4) queried in sequence within one runBeamPostgres call.

Fixes #802. Regression introduced in #797.

Test plan

  • cabal test beam-postgres-tests --test-options="--pattern fieldsCache" passes
  • Full cabal test beam-postgres-tests passes

🤖 Generated with Claude Code

…eam#802)

renderExecReturningList called cachedGetFields but then freed the result
via unsafeFreeResult, allowing libpq to reuse the pointer address on the
next Pg.exec. A subsequent query with different column types would get a
false cache hit and inherit stale field OID metadata, causing a
BeamRowReadError on column 0.

Fix: renderExecReturningList calls getFields directly. The cache remains
for stepProcess (CursorBatching), where it is safe and beneficial.

Regression test added to beam-postgres-tests.

Fixes haskell-beam#802, regression introduced in haskell-beam#797.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@LaurentRDC
Copy link
Copy Markdown
Member

Ooof, thanks for sending a patch. This is very unfortunate

@LaurentRDC LaurentRDC merged commit b154224 into haskell-beam:master Apr 23, 2026
13 checks passed
@LaurentRDC
Copy link
Copy Markdown
Member

beam-postgres-0.5.6.0 has been deprecated in Hackage. I'm preparing release 0.5.6.1 which will include this fix. It will be released tomorrow

@compilerbauer
Copy link
Copy Markdown
Contributor Author

Thank you!

@LaurentRDC
Copy link
Copy Markdown
Member

beam-postgres-0.5.6.1 has been released with this patch. Thank you!

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.

fieldsCache in withPgDebug causes stale field metadata across queries (regression from #797)

3 participants