Search code examples
postgresqldatabase-partitioning

How to use partition inside partition(multilevel partition or partition of partitions) in PostgreSQL


How to use partition inside partition in PostgreSQL I want to do multilevel partition in PostgreSQL Like in first table on column id and in 2nd partitioned table with column date so that will be three level hierarchy


Solution

  • this is explained well in this article with examples https://joaodlf.com/postgresql-10-partitions-of-partitions.html

    CREATE TABLE dt_totals (
        dt_total date NOT NULL,
        geo varchar(2) not null,
        impressions integer DEFAULT 0 NOT NULL,
        sales integer DEFAULT 0 NOT NULL
    )
    PARTITION BY RANGE (dt_total);
    
    CREATE TABLE dt_totals_201801
    PARTITION OF dt_totals
    FOR VALUES FROM ('2018-01-01') TO ('2018-01-31');
    
    CREATE TABLE dt_totals_201801
    PARTITION OF dt_totals
    FOR VALUES FROM ('2018-01-01') TO ('2018-01-31')
    PARTITION BY LIST (geo);
    
    CREATE TABLE dt_totals_UK_201801 PARTITION OF dt_totals_201801 FOR VALUES IN ('UK');
    CREATE TABLE dt_totals_US_201801 PARTITION OF dt_totals_201801 FOR VALUES IN ('US');
    CREATE TABLE dt_totals_AU_201801 PARTITION OF dt_totals_201801 FOR VALUES IN ('AU');