Skip to content

Instantly share code, notes, and snippets.

@brenes
Last active June 29, 2020 11:23
Show Gist options
  • Save brenes/ca6a45c1399e413642cafa87f1c8b19d to your computer and use it in GitHub Desktop.
Save brenes/ca6a45c1399e413642cafa87f1c8b19d to your computer and use it in GitHub Desktop.
ActiveRecord PostgreSQL adapter extension to deal with the Aurora Serverless hanging issue.

There's an issue reported in ruby-pg (ged/ruby-pg#325) where a query hangs and never return results to the application.

This adapter extension controls it with a ruby timeout set to 5 seconds. If the query doesn't return results in 5 seconds it aborts the async_exec method and tries the exec one.

We're not proud of this vut it saved a lot of erratic errors.

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
def exec_no_cache(sql, name, binds)
begin
Timeout::timeout(5) {
log(sql, name, binds) { @connection.async_exec(sql, []) }
}
rescue
Rails.logger.error("[SLOW QUERY] #{sql}")
log(sql, name, binds) { @connection.exec(sql, []) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment