[ActiveRecord] Add an option for binary results to exec_query

It would be useful for our application to query the database directly and obtain a binary response.

Currently, this is possible by calling exec_params in a Postgres connection’s raw_connection. E.g., ActiveRecord::Base.connection.raw_connection.exec_params(sql, [], 1). However, it is not possible (easy?) to leverage prepared statements when doing this. An alternative at the moment is to call ActiveRecord::Base.connection.unescape_bytea(ActiveRecord::Base.connection.select_value(sql)), but this leads to performance degradation associated with converting the result to text and back to binary.

Would it be possible to expose the results_format parameter in exec_prepared and exec_params when calling exec_query? I realize that this is applicable to Postgres and I am not sure how this would apply to other adapters.

Like so:

exec_query(sql, name = "SQL", binds = [], prepare: false, results_format: 0)


exec_query(sql, name = "SQL", binds = [], prepare: false, binary: false)

For context, this is useful when using, for example, Postgis’s ST_AsMVT(), which returns a binary response.

1 Like