postgres declare partitioning pg_partman pg_slice - ghdrako/doc_snipets GitHub Wiki

Partition Manager(pg_partman)

It is simple and straight-forward time partitioning.

With declarative partitioning, much of the partitioning management is automated, but for example, creating new partitions still requires manual intervention—unless you're using tools like pg_partman. Pg_partman helps to automate the creation and management of partitioned tables and partitions through a SQL API. Although new partitions aren’t added and removed automatically, this can be managed by adding another extension like pg_cron to schedule jobs.

A few examples:

  • It’s essential to ensure that the necessary partitions have been created when ingesting data to avoid a No Partition of Relation Found for Rowerror, which may block your writes.
  • If your workload involves sporadic or irregular data ingestions, you’ll need to ensure you aren't creating excessive, unnecessary partitions, as they could degrade query performance and lead to table bloat.
  • You must ensure that there are no gaps or overlaps between partitions, especially when dealing with manual partition modifications.
  • If you want to implement a retention policy to regularly drop old partitions regularly, you'll need to set this up.
  • If you need to alter the schema of your tables, such as adding or dropping columns, you'll often have to handle these changes manually to ensure they propagate correctly to all partitions.

The pg_partman extension provides automation for creating and managing partitions based on predefined criteria, such as date or numeric ranges.

CREATE SCHEMA partman;
CREATE EXTENSION pg_partman WITH SCHEMA partman;
CREATE SCHEMA mk;      ------- create user your schema
CREATE TABLE mkt.sales(
        sale_id        int,
        sales_cout     int,
        prod           char(20),
        create_dt      timestamp,
        CONSTRAINT pk_mkt_event PRIMARY KEY (sale_id, create_dt)
) PARTITION BY RANGE (create_dt);
---Create Interval partition: The default is for 7 days 
SELECT partman.create_parent('mkt.sales', 'create_dt','1 day');
 create_parent
---------------
 t

obraz