EDUCBA

EDUCBA

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

PostgreSQL cluster

By Payal UdhaniPayal Udhani

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL cluster

PostgreSQL cluster

Introduction to PostgreSQL cluster

The database’s main purpose is to store the data required by the user and make it available for use and manipulation whenever required. Even though this statement seems to be easy to say and fulfill, in reality, it is very tough to make sure that the single database is available to all the users present in huge numbers. High availability is always a challenge faced by database engineers. To overcome these, multiple techniques are used. One of the crucial ones is the clustering of the tables of the database. Clustering is an operation that is performed once, and that leads to reordering of the data in the tables based on a certain index that is specified, and if not, any index present on that table is considered for clustering.

Syntax :

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

CLUSTER [VERBOSE] nameOfTable [ USING nameOfIndex ]
or
CLUSTER [VERBOSE]

  • nameOfTable: It is the name of the table that you wish to cluster.
  • NameOfIndex: The name of the index is already defined on the table named nameOfTable on which you want to do clustering. It is the optional parameter. When not specified, the index defined on the table is considered for clustering. Even the name of the table is optional. When the table name is not specified, all the current database tables are clustered based on their respective indexes.
  • Verbose: This is an optional parameter which, when specified, the information about the clustering process being performed after firing the CLUSTER query is printed on the console.

Working of the PostgreSQL cluster

This is a one-time operation. Once done on the table, all the records are reordered based on the table’s defined index. After that, whenever the manipulations are done on the table like if we insert, update or delete the records in it, the order during clustering is not maintained. For that, you can again perform clustering operation periodically as and when possible or can take the help of the storage parameter named FILLFACTOR that can be set against the table, which, when set to less than 100%, can maintain the clustering order while performing the update operation as the values are tried to store on the same page provided if the space is available on that page.

If clustering is being done inside the transaction block, then it won’t be cluster operation won’t be performed. When none of the parameters is specified while firing the cluster command, then depending on which user is firing, the command steps are taken. If the user is a superuser, then all the tables present in that database are clustered and reclustered in case if they were already clustered based on their respective index. If it is not the superuser who is issuing the command, then the tables that that user owns are reclustered is previously clustered and are clustered if the operation is being performed for the first time on that table.

When we do clustering on a particular table, then during that time, we cannot perform any read or write operation on it as the table is exclusively locked for access and can only be accessed once the cluster operation is completed. Also, note that SQL does not support any statement like CLUSTER, while in PostgreSQL cluster operation is compatible with all the latest versions as well as versions previous to PostgreSQL 8.3.

Examples to Implement of PostgreSQL cluster

Below are the examples of PostgreSQL cluster:

For performing C to your PostgreSQL database psql command shell using the psql command and enter the password if required as follows –

Example #1 – PostgreSQL Database Command Shell

Command: psql

Output:

PostgreSQL cluster Example 1

Now, let us check the available tables in my database using the command \dt that results in the following output in my case.

Command: \dt

Output:

PostgreSQL cluster Example 2

As we can see, we have 5 tables present in my database. Let us describe the table named educba to find out keys defined on that table using the \d educba command that results in the following output –

Command:\d educba;

Output:

PostgreSQL cluster Example 3

We can see that a primary key constraint named the educba_pkey index has been defined on the column technical_id for table educba. If you don’t have any table present in your database, you can create the one using the CREATE TABLE statement and define the index on that table. This table will be further used for clustering.

Example #2 – Clustering the Table

Now, we will cluster the educba table on the index named educba_pkey using the following query statement –

Command: CLUSTER educba USING educba_pkey;

Output:

PostgreSQL cluster Example 4

As we can see, the only CLUSTER is the output we get after clustering the educba table, which is correct if the cluster operation is performed successfully. But none of the information related to the cluster operation that was performed is printed on the console. To do so, we can make use of the VERBOSE parameter. After specifying it, all the cluster operation-related information is printed. Let us try to execute the following command.

Command: CLUSTER VERBOSE educba USING educba_pkey;

Output:

educba_pkey Example 5

After using VERBOSE, we can see all the CLUSTER operation-related information printed on the console specifying how many dead rows were removed and how much time the query execution took.

After this, whenever we operate CLUSTER on the educba table, it will consider educba_pkey for reclustering as PostgreSQL remembers all the keys on which the tables were clustered.

Command: CLUSTER VERBOSE educba;

Output:

educba_pkey Example 6

We can perform the cluster operation once manually based on a certain key on which we wish to perform reordering. After that, write the script that will recluster the table periodically by performing the CLUSTER tableName command. The reordering is maintained even after multiple updations and insertions in the table.

Conclusion

Clustering highly increases the performance and retrieves the query resultset very quickly as the execution, i.e. lowers done because the records are now present in the single location after clustering and are not in the distorted format. This gradually affects the availability of the database for users. As execution time decreases, the access time decreases, making the database available for other users, thus achieving high performance and database availability.

Recommended Articles

This is a guide to the PostgreSQL cluster. Here we discuss the Introduction to PostgreSQL cluster and the practical examples and different subquery expressions. You can also go through our suggested articles to learn more –

  1. Introduction to PostgreSQL Timestamp
  2. PostgreSQL log | Top 9 Parameters
  3. Guide to PostgreSQL While Loop
  4. PostgreSQL Functions | How to Work?
  5. PostgreSQL Auto Increment
  6. PostgreSQL Trunc() | How to Work?
  7. PostgreSQL round | Examples

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
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • 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 INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK 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
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • 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 datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • 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 UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • 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 Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • 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 Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

© 2022 - 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

By signing up, you agree to our Terms of Use and Privacy Policy.

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

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

By signing up, you agree to our Terms of Use and Privacy Policy.

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