Chapter 28. Partitioning Data by Year and Month

This scenario describes how to store data in a storage as a partitioned analytical table. For the steps below, it is assumed that the data is stored in the axe_heap_table heap table.

To partition the data by year and month on the Postgres Pro AXE server:

  1. Create a storage for the data.

    For example, create a local storage:

    SELECT axe.create_storage(
        'axe_storage',
        'file:///tmp/axe_storage/'
    );
    
  2. Create a partitioned analytical table and copy the data from the heap table to it.

    For example:

    CREATE TABLE public.axe_archive_table (
        id int4 NULL,
        "name" text NULL,
        price numeric(10,2) NULL,
        created_at timestamp NULL,
        year int4 NULL,
        month int4 NULL
    ) USING axe
    WITH (
        storage = 'axe_storage',
        partitioned_by = 'year, month'
    );
    
    INSERT INTO public.axe_archive_table (id, "name", price, created_at, year, month)
    SELECT
        id, "name", price, created_at,
        EXTRACT(year FROM created_at)::int,
        EXTRACT(month FROM created_at)::int
    FROM
        public.axe_heap_table;
    

    The data copied to a partitioned analytical table is split into multiple Parquet files based on partition columns, and these files are stored in the leaves of a directory tree. For more information about Hive partitioning, refer to Chapter 23.

    In this example, the year and month partition columns are filled from the created_at column when the data is copied to the analytical table, so Parquet files are grouped into directories in the year=2024/month=01/ format.

  3. Check that the data is split by partitions.

    For example:

    SELECT year, month, count(*) AS rows
    FROM public.axe_archive_table
    GROUP BY year, month
    ORDER BY year, month;
    

Note

You do not need to create partitions manually — they are created automatically each time new partition column values are inserted into the analytical table.

FAQ