Prevent "cached plan must not change result type" errors with the Ruby pg driver

255 views
Skip to first unread message

natha...@gmail.com

unread,
Aug 30, 2019, 2:43:29 PM8/30/19
to ruby-pg
Hello!

I've been trying to find a way to prevent these crashes after running rake db:migrate:

ActiveRecord::PreparedStatementCacheExpired: ERROR: cached plan must not change result type

This sometimes happens when my migration adds a new column to a table, but my deploy hadn't finished restarting the Rails server. I think it happens if I run migrations in the middle of a transaction that is already in progress, and this causes my controller action to crash.

I saw the the pgjdbc driver for Java can automatically handle this case with the following option: autosave=conservative
(From StackOverflow.) I'm not 100% sure how this works, but it sounds like the pgjdbc driver can automatically flush the cached plan and handle this case without any errors.

Is there an equivalent setting for the pg driver for Ruby? Or is this something that could be handled further up the stack (e.g. in ActiveRecord, or in my own application?)

Thanks for your time! (I have also posted a question on StackOverflow.)


Best,
Nathan

Jeremy Evans

unread,
Aug 30, 2019, 3:07:43 PM8/30/19
to ruby-pg
Nathan,

You have to make it so all prepared statements use explicit columns (no SELECT * or SELECT table.*). I think that's a bit of a pain in ActiveRecord, since I think SELECT table.* is the default there. Sequel has the column_select plugin that will handle this for you.

One way to prevent this may be disabling the use of prepared statements in ActiveRecord.  I'm not sure how to do that, though.  FWIW, Sequel requires manually enabling prepared statements.

Thanks,
Jeremy
Reply all
Reply to author
Forward
0 new messages