I have the following table stored in Postgresql
table:
=========================================================================================
ctx_id ctx_type entity_id entity_type customer_id
=========================================================================================
CTX_ID_1 ctx_type_A entity_id_1 typeA C_ID
CTX_ID_1 ctx_type_A entity_id_2 typeB C_ID
CTX_ID_2 ctx_type_B entity_id_3 typeB C_ID
CTX_ID_3 ctx_type_B entity_id_4 typeA C_ID
CTX_ID_4 ctx_type_A entity_id_5 typeC C_ID
Basically, I want to get all rows of certain customer_id, but since customer can have ~10,000 rows I would like to reduce the query response size, so instead of returning the result using a basic select statement - select ctx_id, ctx_type, entity_id, entity_type from "table" where customer_id = "C_ID"
I would like to aggregate results in postgres by ctx_id and ctx_type so db query result will look like this:
That way the query response size would be reduced, since the same ctx_id and ctx_type are not returned multiple times but only once.
{
"CTX_ID_1|ctx_type_A": [ <-- a concatenation of ctx_id | context_type
{
"entity_id": "entity_id_1",
"entity_type": "type_A"
},
{
"entity_id": "entity_id_1",
"entity_type": "type_B"
}
],
"CTX_ID_2|ctx_type_B": [
{
"entity_id": "entity_id_3",
"entity_type": "type_B"
}
],
"CTX_ID_3|ctx_type_B": [
{
"entity_id": "entity_id_4",
"entity_type": "type_A"
}
],
"CTX_ID_4|ctx_type_A": [
{
"entity_id": "entity_id_5",
"entity_type": "type_C"
}
]
}
My question is - How this cab be achieved using Postgresql JSON Functions?
I couldn't find anyway doing that with Use jsonb_build_array() and json_object_agg().
if not what are the alternatives?
P.S - Just to elaborate about what I mean in query data response size..
Take customer_id for example,
response size without selecting the customer_id is:
select ctx_id, ctx_type, entity_id, entity_type from "table" where customer_id = "C_ID"
response size with selecting the customer_id is:
select ctx_id, ctx_type, entity_id, entity_type, customer_id from "table" where customer_id = "C_ID"
So what I'm trying to say, If I know that the customer_id has a lot of occurrences, instead of returning the same customer_id in each of the rows it can be returned once like this.
{
"C_ID": [...all rows here]
}


select ctx_id, ctx_type, entity_id, entity_type, customer_id from "table" where customer_id = "C_ID"vs. the query above ehich is without the customer id, and I noticed a difference in the response size. Don't you think that less data will be transferred in the network? since byte size is smaller?