2

In PostgreSQL, how can we tell if each index of a table is clustered or not?

This is counterpart to In MySQL, how can we tell if an index of a table is clustered or not?

Thanks.

  • That's easy: **no** index will be clustered. Postgres does not have support clustered indexes –  Jul 09 '18 at 05:36

1 Answers1

0

Postgres does not support clustered indexes in the sense like in MySql. There may be an index which was used to cluster the table. You can check this by querying the column indisclustered in the system catalog pg_index.

indisclustered - If true, the table was last clustered on this index

Example:

create table my_table(id serial primary key, str text unique);

select relname, indisclustered
from pg_index i
join pg_class c on c.oid = indexrelid
where indrelid = 'public.my_table'::regclass

     relname      | indisclustered 
------------------+----------------
 my_table_str_key | f
 my_table_pkey    | f
(2 rows)

cluster my_table using my_table_str_key;

select relname, indisclustered
from pg_index i
join pg_class c on c.oid = indexrelid
where indrelid = 'public.my_table'::regclass

     relname      | indisclustered 
------------------+----------------
 my_table_str_key | t
 my_table_pkey    | f
(2 rows)

Read in the documentation about CLUSTER:

When a table is clustered, it is physically reordered based on the index information. Clustering is a one-time operation: when the table is subsequently updated, the changes are not clustered. That is, no attempt is made to store new or updated rows according to their index order.

klin
  • 112,967
  • 15
  • 204
  • 232
  • Thanks. I heard in PostgreSQL, all indexes are secondary. Is "secondary" synonym of "unclustered"? If not, what does "secondary" mean in postgresql? –  Jul 09 '18 at 03:44
  • Postgres does not support "clustered indexes". The `cluster` operation is a one time thing, it's not creating a "clustered index" @Ben: there is no such thing as a "secondary index" in Postgres. All indexes are treated equally –  Jul 09 '18 at 05:37
  • @a_horse_with_no_name Thanks. "The cluster operation is a one time thing, it's not creating a "clustered index"", so what does it create? –  Jul 11 '18 at 20:00
  • 1
    @Ben - nothing. It reorders the table's rows, see the updated answer. – klin Jul 11 '18 at 20:31