Definition of PostgreSQL Partition
PostgreSQL partition is used on large table sizes, also we have used partition on large table rows. It is used to speed the performance of a query, we can increase the performance of the query by creating partitioning on the table. Basically, It is divided into list partition, range partition, hash partition, and multilevel partition, there are multiple forms of each type of partition. We can increase the performance of select operations on a large table, partition wise aggregate and join is increases the performance of our query.
Syntax:
Below is the syntax of partition in PostgreSQL.
- List partition
Create table name_of_table (name_of_column1 data_type, name_of_column2 data_type, name_of_column3 data_type, …, name_of_columnN data_type) Partition BY List (name_of_column);
Create table name_of_table PARTITION of partition_table_name for values in (‘partition value’);
- Range partition
Create table name_of_table (name_of_column1 data_type, name_of_column2 data_type, name_of_column3 data_type, …, name_of_columnN data_type) Partition BY Range (name_of_column);
Create table name_of_table PARTITION of partition_table_name for values from value1 to value2;
- Hash partition
Create table name_of_table (name_of_column1 data_type, name_of_column2 data_type, name_of_column3 data_type, …, name_of_columnN data_type) Partition BY Hash (name_of_column);
Create table name_of_table PARTITION of partition_table_name for values with(Modulus, remainder);
Parameter:
- Create table –This is defined as the create partition table by using list, range, and hash partition. We are creating table partition at the time of table creation.
- Name of table –This is defined as to create table by using a partition. We can define a partition to the table at the time of table creation.
- Name of column 1 to name of column N –This is defined as creating a column on a table and defined partition on the same.
- Data type –This is defined as an assigned data type to the table column at the time of table creation. We can define data type as per partitioning which was we have configured on the table.
- Partition by hash –This is defined as creating a hash partition on the table. We can use modulus and remainder while creating a partition.
- Partition by list – This is defined as create a list partition on the table. We can use the column name of the table while creating a partition.
- Partition by range – This is defined as create range partition on table. We can use the column name of the table while creating a partition.
- Modulus and remainder–This is the argument of hash partition which was used at the time of creating a hash partition.
How to perform Partition in PostgreSQL?
We can perform partition which are as follows. It is basically divided into three types as follows.
- List partition.
- Range partition.
- Hash partition.
PostgreSQL 10 supports the range and list type partition, and from PostgreSQL version 11 hash partition is available. We can discuss partition in detail as follows.
-
List Partition
List partition in PostgreSQL is created on predefined values to hold the value of the partitioned table. List partition holds the values which was not part of any other partition in PostgreSQL.
-
Range Partition
Range partition holds the values within the range provided in the partitioning in PostgreSQL. We need to specify the values of minimum and maximum range at the time of range partition creation. The minimum value in range partition is inclusive and the maximum value in range partition is exclusive.
-
Hash Partition
We can create hash partition by using modulus and remainder of each partition in PostgreSQL. In hash, partition rows will insert by generating hash value using the remainder and modulus.
- Using partition in PostgreSQL we can increase the speed of query, we can increase the speed of select query in PostgreSQL.
- Using partition bulk load data and data deletion from the table is faster as compared to the normal table. Operation is performed in each partition so it will faster than a normal table.
- Each partition in PostgreSQL will contain the data based on a frequency which was we have defined at the time of partition creation.
- Declarative partition is very flexible in PostgreSQL to provide good control on the user which was we have used to access the data in PostgreSQL.
- We can create a partition on a table column, as per column data we have decided the type of partitioning.
- Basically, we have using list and range partition in PostgreSQL.
Examples
Below is the example of partition in PostgreSQL.
Example #1: Create List Partition on Table
The below example shows that create list partition on the table. We have created list partition on stud_status column.
CREATE TABLE list_test (stud_id INTEGER, stud_status TEXT, stud_arr NUMERIC) PARTITION BY LIST(stud_status);
CREATE TABLE stud_pass PARTITION OF list_test FOR VALUES IN ('Pass');
CREATE TABLE stud_fail PARTITION OF list_test FOR VALUES IN ('Fail');
\d+ list_test;
Example #2: Create Range Partition on Table
Below example shows that create range partition on the table. We have creating a range partition on stud_arr column.
CREATE TABLE range_test (stud_id INTEGER, stud_status TEXT, stud_arr NUMERIC) PARTITION BY RANGE(stud_arr);
CREATE TABLE stud_arr_small PARTITION OF range_test FOR VALUES FROM (MINVALUE) TO (100);
CREATE TABLE stud_arr_medium PARTITION OF range_test FOR VALUES FROM (100) TO (200);
CREATE TABLE stud_arr_large PARTITION OF range_test FOR VALUES FROM (200) TO (MAXVALUE);
\d+ range_test;
Example #3: Create hash Partition on Table
The below example shows that create a hash partition on the table. We have creating a hash partition on hash_id column.
CREATE TABLE hash_test (hash_id INTEGER, hash_status TEXT, hash_arr NUMERIC) PARTITION BY HASH(hash_id);
CREATE TABLE hast_test1 PARTITION OF hash_test FOR VALUES WITH (modulus 6, remainder 0);
CREATE TABLE hast_test2 PARTITION OF hash_test FOR VALUES WITH (modulus 6, remainder 1);
CREATE TABLE hast_test3 PARTITION OF hash_test FOR VALUES WITH (modulus 6, remainder 2);
\d+ hash_test;
Recommended Articles
This is a guide to PostgreSQL Partition. Here we discuss the definition, How to perform Partition in PostgreSQL? and examples with code implementation. You may also have a look at the following articles to learn more –