If you are using Rails and ActiveModelSerializers, I wrote a gem to take your serializer definition and build the whole JSON result in Postgres: [0] It hasn't gotten much use, but I know at least one company that has used it to get 100x performance boosts on #index endpoints.
It generates jsonapi right now, since that is standard for Ember projects, but it'd be pretty easy to add an adapter for some other format.