Partitioning Tables

It is splitting table into

  • logical division

  • multiple smaller pieces

  • more manageable pieces table

Partition leads to a huge performance boost.

Types of ranges

  • Range : The table is partitioned into "range" defined by a key column or set of columns, with no overlap between the ranges of values assigned to different partitions

  • List : According to a key in table, ex : country, sales

  • Hash : The partition specifying a modulus and a reminder for each partition.

Table Inheritance

create table master (
    pk INTEGER primary key ,
    tag text,
    parent integer
);

create table master_child() inherits (master);

ALTER TABLE public.master_child
    ADD PRIMARY KEY (pk);


select * from master;
select * from master_child;

insert into master (pk, tag, parent) values (1,'pencil',0);
insert into master_child (pk, tag, parent) values (2,'pen',0);

update master set tag = 'monitor' where pk = 2;

select * from only master;
select * from only master_child;

-- error
drop table master;
drop table master cascade ;

Range Partitioning

List Partitioning

Hash Partitioning

Default Partitioning

Multilevel Partitioning

Attach and DeAttach Partitions

Altering Partitions

Partition Indexes

  • Creating an index on the master/parent table will automatically create same indexes to every attached table partition

  • PostgreSQL doesnt allow a way to create a single index covering every partition of the parent table. You have to create indexes for each table

Last updated

Was this helpful?