

-- 10M groups of 1
CREATE UNLOGGED TABLE hashagg_singleton(groupkey1 int, arg1 int, groupkey2 numeric, arg2 numeric);
INSERT INTO hashagg_singleton SELECT g, g, g, g
  FROM generate_series(1,10000000) g ORDER BY random();

-- 1M groups of 10
CREATE UNLOGGED TABLE hashagg_even(groupkey1 int, arg1 int, groupkey2 numeric, arg2 numeric);
INSERT INTO hashagg_even SELECT g%1000000, g, g%1000000, g
  FROM generate_series(1,10000000) g ORDER BY random();

-- skewed groups
CREATE UNLOGGED TABLE hashagg_skew(groupkey1 int, arg1 int, groupkey2 numeric, arg2 numeric);
INSERT INTO hashagg_skew
  SELECT groupkey, arg, groupkey, arg FROM
    (SELECT g as arg, case
       when g %  2 = 0 then -1
       when g %  3 = 0 then -2
       when g %  5 = 0 then -3
       when g %  7 = 0 then -4
       when g % 11 = 0 then -5
       when g % 13 = 0 then -6
       when g % 17 = 0 then -7
       when g % 19 = 0 then -8
       when g % 23 = 0 then -9
       else g
     end as groupkey
     FROM generate_series(1,10000000) g ORDER BY random()) s;

VACUUM hashagg_singleton;
VACUUM hashagg_even;
VACUUM hashagg_skew;
VACUUM ANALYZE hashagg_singleton;
VACUUM ANALYZE hashagg_even;
VACUUM ANALYZE hashagg_skew;
CHECKPOINT;
