EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Partition

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL Partition

PostgreSQL Partition

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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;

PostgreSQL Partition 1

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;

PostgreSQL Partition 2

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;

example 3

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 –

  1. PostgreSQL Lock Table
  2. PostgreSQL Clustered Index
  3. PostgreSQL Float
  4. PostgreSQL MAX

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More