DO
$$
DECLARE
exp integer;
jsonb jsonb;
t0 timestamptz;
n bigint;
BEGIN
FOR exp IN 1..3 LOOP
  n := (1000*10^exp)::bigint;
  RAISE NOTICE 'n=%', n;

  SELECT jsonb_object_agg(format('foo%s',i), jsonb_build_object('i', i))
  INTO jsonb
  FROM generate_series(1,n) AS i;

  t0 := clock_timestamp();
  jsonb := jsonb_set(jsonb, ARRAY['foo47'], '{}'::jsonb);
  RAISE NOTICE '% jsonb := jsonb_set(jsonb, ARRAY[existing key], value);', clock_timestamp()-t0;

  t0 := clock_timestamp();
  jsonb := jsonb_set(jsonb, ARRAY['some_new_key'], '{}'::jsonb);
  RAISE NOTICE '% jsonb := jsonb_set(jsonb, ARRAY[new key], value);', clock_timestamp()-t0;

  t0 := clock_timestamp();
  jsonb['foo47'] := '{}'::jsonb;
  RAISE NOTICE '% jsonb[existing key] := value;', clock_timestamp()-t0;

  t0 := clock_timestamp();
  jsonb['some_other_new_key'] := '{}'::jsonb;
  RAISE NOTICE '% jsonb[new key] := value;', clock_timestamp()-t0;

END LOOP;
END
$$;
