I can imagine table partition by a date (in particular for logs) is something widely used, but I am not able to find a good answer to my problem.
I want to create a table partition by week (the number of records is to big to make it monthly). The reason for it to be weekly is that I need the data for an algorithm that will be looking for dates as part of the process.
My problem is I want it to create the partitions considering the week and using the "typical" approach I would have to be creating it manually. Something like this.
CREATE TABLE measurement_y2013w01 (
CHECK ( logdate >= DATE '2013-01-07' AND logdate < DATE '2013-01-14' )
) INHERITS (measurement);
CREATE TABLE measurement_y2006w02 (
CHECK ( logdate >= DATE '2013-01-14' AND logdate < DATE '2013-01-21' )
) INHERITS (measurement);
...
But I want it to be made automatically. I don't want to be creating a partition for every week one by one.
My rule for name giving would be yYYYYwWW for the naming of the partition or start datadYYYYMMDD.
I thought in checking for partitions when inserting using something like this:
SELECT
nmsp_parent.nspname AS parent_schema,
parent.relname AS parent,
nmsp_child.nspname AS child,
child.relname AS child_schema
FROM pg_inherits
JOIN pg_class parent ON pg_inherits.inhparent = parent.oid
JOIN pg_class child ON pg_inherits.inhrelid = child.oid
JOIN pg_namespace nmsp_parent ON nmsp_parent.oid = parent.relnamespace
JOIN pg_namespace nmsp_child ON nmsp_child.oid = child.relnamespace
and if the partition is not there create it before the insert, but this seams a lot inefficient, considering the number of records inserted.
My other alternative is to have an external process running weekly creating this partition, but I was trying to avoid this.
Is there a more efficient solution for this that I am missing, that is used, for example, for monthly checks?