blog/source/_posts/Postgresql Partitioning.md
Ching 9690121403 feat(init project): add all existing files
add all existing files

Signed-off-by: Ching <loooching@gmail.com>
2022-02-02 19:04:18 +08:00

2.2 KiB
Raw Blame History

title date tags
Postgresql Partitioning 2019-03-12 00:08:48

Partitioning refers to splitting what is logically one large table inot smaller physical pieces.

Currently, PostgreSQL supports partitioning via table inheritance. Each partition must be created as a child table of a single parent table. The parent table itself is normally empty; It exists just to represent the entire data set.

There are two forms of partitioning can be implemented in PostgreSQL:

  • Range Partitioning

    The table is partitioning into "range" defined by a key column or a set of columns, with no overlap between the ranges of values assigned to different partitions. eg. partition by date ranges or by identifiers.

  • List Partitioning

    The table is partitioned by explicitly listing which key values appear in each partition.

Implementing Partitioning

  1. Create the "master" / "parent" table, from which all the partitions will inherit.

    This table will not contain any data. Do not define any check on this table, unless you intend them to be applied equally to all partitions. There is no point in defining any indexes or unique constraints on it either.

  2. Create "child" tables that each inherit form the master table. Normally, these tables will not add any columns to the set inherited from the master.

  3. Add table constraints to the partition tables to define the allowed key values in each partitions.

    Ensure that the constraints guarantee that there is no overlap between the key values premitted in different partitions. And there is no difference in syntax between range and list partitioning.

  4. Create indexes on column(s) for each partitions.

  5. Optionally, define a trigger or rule to redirect data inserted into the master table to the appropriate partition.

  6. Ensure hte constraint_exclusion configuration parameter is not disabled in postgresql.conf. If it is, queries will not be optimized as desired.

Trigger

As we are creating new table and hopping data insered to right partition, a trigger function and a trigger are needed.