I wrote the following query on a local dev environment not realizing that the postgres version on my local dev was 9.3+ but only 9.1 on production. I am hesitant to upgrade the production postgres. Is there any way to write the following query that is compatible with 9.1? I believe the row_to_json is the problem. Is there a 9.1 equivalent? The schema is SalesOrders has many SalesOrderItems. Each SalesOrderItem has many mdns. The below returns me a nice compact nested JSON object when run on 9.3.
select row_to_json(t)
from (select s.*,
( select array_to_json(array_agg(row_to_json(d)))
from ( select soi.* , (select array_to_json(array_agg(row_to_json(e)))
from( select m.* FROM "Mdns" m where m."SalesOrderItemId" = soi.id) e) mdns
from "SalesOrderItems" soi where soi."SalesOrderId" =s.id ) d
) as order_items
from "SalesOrders" s where s.order_id = 'SO-83562-0185') t