JSON Serialization of BigDecimal values in Postgres 14

Follow up from !114624 (comment 1324656412)

Context

Due to a change in behavior of Postgres 14, the EXTRACT function now returns numeric, instead of double precision (See Change return type of EXTRACT to numeric). Rails will treat numeric values as BigDecimal, being parsed as String by as_json, to_json methods:

PostgreSQL: 14.0

query = ActiveRecord::Base.connection.execute("SELECT AVG(EXTRACT(EPOCH FROM created_at - updated_at)) FROM projects LIMIT 10;")

query.to_a.as_json
=> [{"avg"=>"-24.2183307368421053"}]

query.to_a.to_json
=> "[{\"avg\":\"-25.213847842105267\"}]"

For Postgres 13, however, these values were stored as Float, which is parsed differently:

PostgreSQL: 13.9

query = ActiveRecord::Base.connection.execute("SELECT AVG(EXTRACT(EPOCH FROM created_at - updated_at)) FROM projects LIMIT 10;")

query.to_a.as_json
=> [{"avg"=>-25.213847842105267}]

query.to_a.to_json
=> "[{\"avg\":-25.213847842105267}]"

We should consider if updating to Postgres 14 will affect JSON outputs across the application.

Edited by Leonardo da Rosa