17

I'm using PostgreSQL 9.3 version to create database.

I have the following table test with some columns list.

create table test
(
  cola varchar(10),
  colb varchar(10),
  colc varchar(10),
  cold varchar(10)
);

Now I want to create a indexs on some columns.

For example:

I want to create clustered index for columns cola and colb.

And I want to create non clustered index for columns colc and cold.

As I referred this And this ,I come to know that there is no clustered and non clustered index in PostgreSQL.

My Question: What type of index I can use instead of clustered and non clustered index in PostgreSQL,Which does the same job as clustered and non clustered indexes does?

5
  • 3
    Every index in Postgres is "non-clustered". Postgres does not have clustered indexes.If you run create index it will create a (non-clustered) B-Tree index. Why do you think there is no non-clustered index? Commented Jan 16, 2015 at 6:49
  • @a_horse_with_no_name, Okay! My bad. But how can I create clustered index? Commented Jan 16, 2015 at 6:54
  • 1
    As I said: Postgres doesn't have clustered indexes. So you can't create one. Why do you think you need one? Commented Jan 16, 2015 at 6:55
  • @a_horse_with_no_name, So create index does both job in PostgreSQL. Commented Jan 16, 2015 at 6:57
  • 2
    It only creates non-clustered indexes because Postgres does not have a clustered indexes Commented Jan 16, 2015 at 6:58

2 Answers 2

43

My Question: What type of index I can use instead of clustered and non clustered index in PostgreSQL,Which does the same job as clustered and non clustered indexes does?

PostgreSQL doesn't have the concept of clustered indexes at all. Instead, all tables are heap tables and all indexes are non-clustered indexes.

Just create a non-clustered index when you'd usually create a clustered index.

More details:

Sign up to request clarification or add additional context in comments.

10 Comments

@mak: I also find this blog from Markus very interesting: use-the-index-luke.com/blog/2014-01/…
@a_horse_with_no_name, Yeah! It is.
It appears the Postgres implements something that at least approximates a clustered index: "CLUSTER instructs PostgreSQL to cluster the table specified by table_name based on the index specified by index_name" From the postgres docs: postgresql.org/docs/9.1/static/sql-cluster.html
@mangotang See this related question. In short, CLUSTER reorganises the current data in a table based on a particular index, but does not create a different data structure or maintain that order.
@IMSoP Thank you for the clarification. By saying that postgres "approximates a clustered index", I was attempting to be clear that it is in fact not a clustered index, but may be a viable alternative.
|
0

For a clustered index, each of the desired fields must be the primary key. And for the non-clustered index, we act according to the following command :

CREATE INDEX IX_Test_Colc ON test(colc);

CREATE INDEX IX_Test_Cold ON test(cold);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.