The PostgreSQL partition manager pg_partman is an open source extension widely supported and actively maintained by the PostgreSQL community. 

  • pg_partman is an extension that streamlines the creation and management of table partition sets, supporting both time-based and serial-based partitioning approaches.
  • You can use pg_partman to automate and create partitions by breaking large tables into smaller partitions, thereby enhancing performance.

In my previous blog post, PostgreSQL Partitioning Made Easy Using pg_partman (TimeBased), we discussed:

  • Installation of pg_partman
  • Native partitioning implementation using pg_partman (time-based)
  • Automatic maintenance of partitions

In this blog, we will discuss the partitioning of newly created and already existing tables via serial-based. 

Please read the previous blog post for the installation of pg_partman before continuing with this blog.

In setting up partitioning without a template table using pg_partman, you actively configure the extension to create partitions based on specific criteria.

Using pg_partman, only the range partitioning (more about types of partition) can be implemented, either time-based or serial-based, but list partitioning cannot be implemented, as it can be only predicted when a new partition has to be created with range partitioning on dates or timestamps or IDs.

Partitioning by range without template

You can create partitions based on a serial-based column using pg_partman. For example:

Step 1: Create a table with native partitioning type by range using serial-typed column.

Step 2: Create parent to create initial child partitioned tables without template table.

The structure of the parent table with its partitions created is as follows.

Here, we insert 1,000,000 rows to view data moving into partitioned tables.

Using functions, we can create child partitions and move the data from default to child tables.

partman.run_maintenance_proc → Create child partitions 

Here, we see that, while performing run_maintenance_proc to create needed partitions for existing data in default tables, it’s a violation of rows as it helps to create new partitions needed as per the data but not to insert data to new partitions. So, we can use the function partition_data_proc to move the data.

Using partman.partition_data_proc we can move data batch-wise to partitioned tables.

partman.partition_data_proc → Move data to partitioned tables

Here, we actively move the data from the default to the newly created partitioned table.

Partman type partitioning

Creating the parent table with the partman option (more about parent table creation options) allows us to create trigger-based partitions using pg_partman’s method of partitioning, which has less read-write performance compared to native/declarative partitioning.

Below, let’s see the implementation steps for trigger-based partitioning.

Partition creations using pg_partman for a non-declarative table: (trigger-based)

Step 1: Create a table with partman-type range partitioning using serial-typed columns and insert random data of 5,000 rows.

Step 2: Create parent to create initial child partitioned tables without template table.

The table structure has been created with child tables based on the default premake value of four on partman.part_config and a trigger named donors_part_trig.

Using partman.partition_data_proc, we can move data batch-wise to partitioned tables.

Consequently, upon inserting new data into the table, we notice that if the partition exists, the data seamlessly moves into the respective partitions; however, if the partition doesn’t exist, the data remains within the main table.

In this scenario, inserting data where the partition doesn’t exist results in directing the data to the main table. Subsequently, manual movement of this data occurs in batches to the newly created partitions.

Employing functions to proactively create partitioned tables and migrate data enables the proactive planning of partitions ahead of inserts, preventing data accumulation in default partitions or the main table.

The blog I mentioned earlier contains details about the steps for automatic partitioning.

For more details on pg_partman and its latest updates, check out https://www.postgresql.org/about/news/pg_partman-470-released-2495/.

References:

Performing ETL Using Inheritance in PostgreSQL

PostgreSQL Partitioning Using Traditional Methods

Percona Distribution for PostgreSQL provides the best and most critical enterprise components from the open-source community, in a single distribution, designed and tested to work together.

 

Download Percona Distribution for PostgreSQL Today!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments