Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. CREATE OR REPLACE FUNCTION public.utc_year(text)
  2. RETURNS integer AS
  3. $BODY$
  4. SELECT date_part('year', cast($1 as timestamp) at time zone 'UTC')::integer
  5. $BODY$
  6. LANGUAGE sql IMMUTABLE PARALLEL SAFE RETURNS NULL ON NULL INPUT
  7. COST 100;
  8.  
  9.  
  10. CREATE OR REPLACE FUNCTION public.utc_month(text)
  11. RETURNS integer AS
  12. $BODY$
  13. SELECT date_part('month', cast($1 as timestamp) at time zone 'UTC')::integer
  14. $BODY$
  15. LANGUAGE sql IMMUTABLE PARALLEL SAFE RETURNS NULL ON NULL INPUT
  16. COST 100;
  17.  
  18.  
  19. CREATE TABLE raw_master (
  20. id serial,
  21. document jsonb
  22. ) PARTITION BY LIST (utc_year(document ->> 'TIMESTAMP_UTC'));
  23.  
  24. CREATE LANGUAGE plpythonu;
  25.  
  26. DO
  27. $$
  28. for year in xrange(2000, 2021):
  29. stmt = ("CREATE TABLE raw_{0} PARTITION OF raw_master FOR VALUES IN ({0}) "
  30. "PARTITION BY LIST (utc_month(document ->> 'TIMESTAMP_UTC'))"
  31. .format(year))
  32. plpy.execute(stmt)
  33. $$
  34. LANGUAGE plpythonu;
  35.  
  36.  
  37. DO
  38. $$
  39. for year in xrange(2000, 2021):
  40. for month in xrange(1, 13):
  41. stmt = ("CREATE TABLE raw_{0}_{1} PARTITION OF raw_{0} FOR VALUES IN ({1})"
  42. .format(year, month))
  43. plpy.execute(stmt)
  44. $$
  45. LANGUAGE plpythonu;
  46.  
  47. SET constraint_exclusion = 'on';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement