-- extract i/v pairs from permutation count
TRUNCATE perm_vals;
SELECT MAX(perm % 10) AS n FROM perm_count \gset
INSERT INTO perm_vals AS pv (i, v, cnt)
  SELECT i, (perm / POW(10,i))::INT % 10 AS v, SUM(cnt)
  FROM perm_count
  CROSS JOIN generate_series(0, :n) AS i
  GROUP BY i, v
  ON CONFLICT (i, v) DO UPDATE SET cnt = pv.cnt + EXCLUDED.cnt;
