I am trying to automate partitions in Postgres 10 in a huge table by BY RANGE (date_created).
I've noticed that there is no an automatic creation of partitioned tables, therefore I want to write a procedure to automate the creation of those tables.
I was thinking something like that:
CREATE OR REPLACE FUNCTION cdi.automating_partitions()
RETURNS TABLE(natural_id text, name text, natural_id_numeric text) AS
$func$
DECLARE
formal_table text;
BEGIN
FOR formal_table IN
select '2017-01-01'::date + (n || ' months')::interval months,
'2013-02-01'::date + (n || ' months')::interval monthsplus
from generate_series(0, 12) n
LOOP
RETURN QUERY EXECUTE
'CREATE TABLE cdi.' || 'document' || to_char(months, 'YYYY') || '' || to_char(months, 'MM') || ' PARTITION OF cdi.document
FOR VALUES FROM (''' || to_char(months, 'YYYY') || to_char(months, 'MM') || ''',
''' to_char(monthsplus, 'YYYY') || to_char(monthsplus, 'MM') ''');'
END LOOP;
END
$func$ LANGUAGE plpgsql;
But I get a syntax error near (
executeoutside of a PL/pgSQL block (and never as part of a SQL query)