Skip to content

Conversation

vincent-pochet
Copy link
Collaborator

@vincent-pochet vincent-pochet commented May 15, 2025

Context

In some context, when a migration is adding a new column to a table, Postgres might return a cached plan must not change result type in a transaction because the cached query plan is not more valid. This error is then raised as a ActiveRecord::PreparedStatementCacheExpired by Rails

See https://flexport.engineering/avoiding-activerecord-preparedstatementcacheexpired-errors-4499a4f961cf

Description

This fix is forcing rails to query explicitly every columns in select instead of using SELECT *, so that the cached plan remains valid in the transaction. The only drawback will be with column removal, but it should never happen on a used field and would be prevented by strong_migrations

@vincent-pochet vincent-pochet force-pushed the misc-avoid-prepared-statement-errors branch 6 times, most recently from 423f963 to 7f3728c Compare May 15, 2025 08:02
@vincent-pochet vincent-pochet force-pushed the misc-avoid-prepared-statement-errors branch from 7f3728c to 8569be0 Compare May 15, 2025 08:05
@vincent-pochet vincent-pochet marked this pull request as ready for review May 15, 2025 08:12
@julienbourdeau
Copy link
Contributor

The only drawback will be with column removal

Before deleting a column, it will live in ignored_columns for a long time, so we're good.

For future reference: #3640

@vincent-pochet vincent-pochet merged commit 16c5b34 into main May 15, 2025
14 checks passed
@vincent-pochet vincent-pochet deleted the misc-avoid-prepared-statement-errors branch May 15, 2025 13:48
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.

3 participants