Aggregated JSONB result in update query
I'm not sure if this is the solution you're looking for, but IMHO you should get the aggregated values and then build the array.


WITH t AS 
(
  SELECT
      user_id,
      jsonb_build_object('start', MIN(timestamp), 
                         'end', MAX(timestamp),
                         'external_id', external_id,
                         'is_fetched', true) ev
  FROM 
      events
  GROUP BY
      user_id,
      external_id
), t2 AS
(
  SELECT
    user_id,
    jsonb_agg(ev) history
  FROM
    t
  GROUP BY
    user_id
)
UPDATE users AS usr
SET    history = t2.history
FROM   t2
WHERE  usr.id = t2.user_id;





SELECT * FROM users;



id | name  | history                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
-: | :---- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1 | Mike  | [{&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 1}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 5}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 3}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 4}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 2}]
 2 | Jake  | [{&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 3}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 2}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 4}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 1}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 5}]
 3 | Toots | [{&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 3}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 1}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 4}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 2}, {&quot;end&quot;: &quot;2019-01-26T17:14:37.349739&quot;, &quot;start&quot;: &quot;2018-10-18T17:14:37.349739&quot;, &quot;is_fetched&quot;: true, &quot;external_id&quot;: 5}]



db&lt;>fiddle here