json_table2 is declared as a variable in pgsql. It is not a jsonb table column. It is loaded from a statement like:
execute 'select jsonb_agg(row_to_json(zzz)) from (...extremely complex query goes here) zzz' into json_table2;
Later, additional rows are added like:
for a in select * from [table with the same columns as json_table_2]
loop
json_table2 = coalesce (json_table2 || jsonb_agg(row_to_json (a)), jsonb_agg(row_to_json (a)) );
end loop;
So, because of the fact that the json_table2 is appended to in a random order, json_table2 now must be sorted.
thanks