Question: Is the primary key in PostgreSQL clustered?
Answer
In PostgreSQL, a primary key is not automatically clustered. This differs from some other database systems like SQL Server, where a primary key is clustered by default unless explicitly specified otherwise.
A clustered index determines the physical order of data in a table based on the columns included in the index. However, PostgreSQL does not support clustered indexes in the same way. Instead, it offers something similar through the CLUSTER
command, which allows you to physically reorder a table based on the index specified. This reordering is manual and not maintained automatically during subsequent inserts or updates.
Here's how you can use the CLUSTER
command with a primary key:
-- Assume we have a table defined as follows:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
department_id INT
);
-- Creating an index on the primary key
CREATE INDEX idx_employees_id ON employees (id);
-- Clustering the table based on the primary key index
CLUSTER employees USING idx_employees_id;
After executing the CLUSTER
command, the rows of the table employees
are reordered on disk to follow the order of the id
index. It's important to note that this order will not be maintained after insertions, updates, or deletions. If you need to reapply the clustering, you must run the CLUSTER
command again.
Therefore, while PostgreSQL does not have clustered indexes in the traditional sense, it provides tools for manually clustering data around an index, including a primary key.
Was this content helpful?
Other Common PostgreSQL Questions (and Answers)
- How do you manage Postgres replication lag?
- How can I limit the number of rows updated in a PostgreSQL query?
- How does sharding work in PostgreSQL?
- How do you limit the number of rows deleted in PostgreSQL?
- How do you use the PARTITION OVER clause in PostgreSQL?
- What are PostgreSQL replication slots and how do they work?
- How can you partition an existing table in PostgreSQL?
- How do you partition a table by multiple columns in PostgreSQL?
- How do you check the replication status in PostgreSQL?
- What are the scaling limits of PostgreSQL?
- How do you scale Azure PostgreSQL?
- How do you use the limit clause in PostgreSQL to get the top N rows of a query result?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost