Sorry for the delay, I was away from my desk for a while
Postgreql is now supporting partitioned tables. They are somehow more refined from inherited tables. I recommend using that instead as all new developments are focused on partitioned tables.
Syntax is very similar to inherited tables.
- You first create a main table which does not contain any data.
- Then create tables that are partition of the main table this is where you explicitly set ranges.
- You can attach a new partition table to partitioned table, or detach an existing partition.
You should be aware that all ranges have to be disjoint. you can't have overlapping ranges. (PG11 brings notion of default range as fall back partition when no other matches)
From your description, I understand that eventdate is always increasing field, therefore there is no risk of inserting to old partitions. If it is the case you can use following model
- create main table
create table main_event (eventdate int,username character varying(1024), bytes bigint) partition by range (eventdate);
-- create a sufficiently large partition to hold incoming data
create table default_partition partition of main_event for values from (20180101) to (21000101);
- when you need to perform partition work1. create a new cstore table
create foreign table week1 (eventdate int,username character varying(1024), bytes bigint) server cstore_server options (compression 'pglz');2. copy data from default_partition to new table
insert into week1 select * from default_partition where eventdate >= 20180101 and eventdate < 20180108;3. delete copied data from default_partition
delete from default_partition where eventdate < 20180108;4. detach default_partition from main_table
alter table main_event detach partition default_partition;5. attach cstore table as a partition specifying its range (start range is inclusive end range is exclusive)
alter table main_event attach partition week1 for values from (20180101) to (20180108);6. re-attach default_partition such that it now has range starting from a later data.
alter table main_event attach partition default_partition for values from (20180108) to (21000101);
You would need to make a compromise here depending on whether you are willing to stop query or data ingestion during steps 3,4,5 and 6.
If you want to keep inserting new data but can afford to stop queries for a while. You can do in the exact same order. You would need to but steps 4,5,6 into a transaction block.
If you want continuous querying but can afford to stop data ingestion. Put steps 3,4,5,6 into a transaction. Only difference between two is insertion would be blocked when deleting copied data from default_partition.
Hope it helps
Murat