Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

79 lines (63 sloc) 4.094 kb
CREATE OR REPLACE FUNCTION part.create_time_partition (p_parent_table text, p_control text, p_interval interval, p_datetime_string text, p_partition_times timestamp[]) RETURNS text
LANGUAGE plpgsql SECURITY DEFINER
AS $$
DECLARE
v_partition_name text;
v_partition_timestamp_end timestamp;
v_partition_timestamp_start timestamp;
v_tablename text;
v_time timestamp;
BEGIN
FOREACH v_time IN ARRAY p_partition_times LOOP
v_partition_name := p_parent_table || '_p';
IF p_interval = '1 year' OR p_interval = '1 month' OR p_interval = '1 day' OR p_interval = '1 hour' OR p_interval = '30 mins' OR p_interval = '15 mins' THEN
v_partition_name := v_partition_name || to_char(v_time, 'YYYY');
IF p_interval = '1 month' OR p_interval = '1 day' OR p_interval = '1 hour' OR p_interval = '30 mins' OR p_interval = '15 mins' THEN
v_partition_name := v_partition_name || '_' || to_char(v_time, 'MM');
IF p_interval = '1 day' OR p_interval = '1 hour' OR p_interval = '30 mins' OR p_interval = '15 mins' THEN
v_partition_name := v_partition_name || '_' || to_char(v_time, 'DD');
IF p_interval = '1 hour' OR p_interval = '30 mins' OR p_interval = '15 mins' THEN
v_partition_name := v_partition_name || '_' || to_char(v_time, 'HH24');
IF p_interval <> '30 mins' AND p_interval <> '15 mins' THEN
v_partition_name := v_partition_name || '00';
ELSIF p_interval = '15 mins' THEN
IF date_part('minute', v_time) < 15 THEN
v_partition_name := v_partition_name || '00';
ELSIF date_part('minute', v_time) >= 15 AND date_part('minute', v_time) < 30 THEN
v_partition_name := v_partition_name || '15';
ELSIF date_part('minute', v_time) >= 30 AND date_part('minute', v_time) < 45 THEN
v_partition_name := v_partition_name || '30';
ELSE
v_partition_name := v_partition_name || '45';
END IF;
ELSIF p_interval = '30 mins' THEN
IF date_part('minute', v_time) < 30 THEN
v_partition_name := v_partition_name || '00';
ELSE
v_partition_name := v_partition_name || '30';
END IF;
END IF;
END IF; -- end hour IF
END IF; -- end day IF
END IF; -- end month IF
ELSIF p_interval = '1 week' THEN
v_partition_name := v_partition_name || to_char(v_time, 'IYYY') || 'w' || to_char(v_time, 'IW');
END IF; -- end year/week IF
-- pull out datetime portion of last partition's tablename
v_partition_timestamp_start := to_timestamp(substring(v_partition_name from char_length(p_parent_table||'_p')+1), p_datetime_string);
v_partition_timestamp_end := to_timestamp(substring(v_partition_name from char_length(p_parent_table||'_p')+1), p_datetime_string) + p_interval;
SELECT schemaname ||'.'|| tablename INTO v_tablename FROM pg_catalog.pg_tables WHERE schemaname ||'.'|| tablename = v_partition_name;
IF v_tablename IS NOT NULL THEN
CONTINUE;
END IF;
IF position('.' in p_parent_table) > 0 THEN
v_tablename := substring(v_partition_name from position('.' in v_partition_name)+1);
END IF;
EXECUTE 'CREATE TABLE '||v_partition_name||' (LIKE '||p_parent_table||' INCLUDING DEFAULTS INCLUDING INDEXES) INHERITS ('||p_parent_table||')';
EXECUTE 'ALTER TABLE '||v_partition_name||' ADD CONSTRAINT '||v_tablename||'_partition_check
CHECK ('||p_control||'>='||quote_literal(v_partition_timestamp_start)||' AND '||p_control||'<'||quote_literal(v_partition_timestamp_end)||')';
---- Call post_script() for given parent table
END LOOP;
RETURN v_partition_name;
END
$$;
Jump to Line
Something went wrong with that request. Please try again.