View on GitHub

samrat.github.io

Setting up Partman - Postgres

What is partitioning?

Splitting up the large table into a logical group of subtables

There are multiple ways data can be partitioned.

Why is it required?

What is Partman?

Partman is a Postgres extension to maintain both serial and time-based partitioning.

How to do table partitioning?

Consider we have a table for ticket management called ticket. It has following columns

Use case is we have to partition table such that we need to store tickets day wise

create table ticket (id serial, status text, created_at date) partition by range (created_at);

Partion by range will create a partition table

https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F205cdbf3-1b26-4c71-802e-5fdf3f5b5923_1554x248.png

Create child tables for the parent partition tables

create table ticket_2021_05_16 partition of ticket for values from ('2021-06-16 00:00:00')
 to ('2021-06-17 00:00:00');create table ticket_2021_05_17 partition of ticket for values
 from ('2021-06-17 00:00:00') to ('2021-06-18 00:00:00');

https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F54aeb3cc-185c-4624-bb03-e38eb55ecd3d_1531x261.png

Lets try to insert data

INSERT INTO public.ticket (status, created_at) VALUES('PASS', '2021-06-17');

INSERT INTO public.ticket (status, created_at) VALUES('PASS', '2021-06-16');

We have inserted 2 tickets one on date 17th and one more on 16th

Let’s try and query ticket table

https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb87b9be-349f-4ce9-bf14-b659c37b5857_498x156.png

We can see both the records are in ticket table

Now lets query child tables

https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2f17aa7d-a5ba-4fbd-b080-45444ecba93c_644x282.png

Since partition was created per dat basis, we can see record which was created_at 17th is in ticket_2021_05_17 table and 16th is in ticket_2021_05_16 th table.

Partman

Cosider a use case where we need to keep only last 2 days record and delete old records regularly and created new tables for next day

Lets create more partitions for this use case

create table ticket_2021_05_14 partition of ticket for values from ('2021-06-14 00:00:00') 
to ('2021-06-15 00:00:00');
create table ticket_2021_05_15 partition of ticket for values from ('2021-06-15 00:00:00') 
to ('2021-06-16 00:00:00');

Add some data to the partition tables

INSERT INTO public.ticket (status, created_at) VALUES('PASS', '2021-06-14');
INSERT INTO public.ticket (status, created_at) VALUES('PASS', '2021-06-15');

https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0c0818b3-4d89-4732-b4ed-d1ce83a3bbc0_430x209.png

To support our use case where we need to delete old partition tables and create new tables for upcoming days

We can do that using multiple way

And many other ways which are either difficult to setup or maintain

This is where we can use partman to setup. Advantages are as below

Installation

Setup

update partman.part_config set retention = '1 days' and retention_keep_table= false and infinite_time_partitions = true;

Testing

Closing Notes