I've been impressed by the performance improvements achieved with clustering, but not with how long it takes.
I know clustering needs to be rebuilt if a table or partition is changed after the clustering, but unless I've made a note of when I last clustered a table, how can I tell when I need to do it again?
I can use this query to tell me what table(s) have one or more clustered indexes
SELECT *
FROM pg_class c
JOIN pg_index i ON i.indrelid = c.oid
WHERE relkind = 'r' AND relhasindex AND i.indisclustered
My questions are.
I've noticed that it takes just as long to re-build a clustered index as it does to build it in the first place (even if the table hasn't been touched in the meantime). So I want to avoid re-clustering unless I know the table needs it.
UPDATE for clarity (I hope)
If I use this command....
CLUSTER tableA USING tableA_idx1;
PostgreSQL provides clustered index functionality to the user in which every table of the database has a unique clustered index. Clustered index means it stores another value of table on secondary storage. Clustered index is used to uniquely identify rows from a table.
PostgreSQL automatically creates indexes for PRIMARY KEY and every UNIQUE constraints of a table. Login to a database in PostgreSQL terminal and type \d table_name . All stored indexes will be visualized. If there is a clustered index then it will also be identified.
In Non-Clustered index, index key defines order of data within index. A Clustered index is a type of index in which table records are physically reordered to match the index. A Non-Clustered index is a special type of index in which logical order of index does not match physical stored order of the rows on disk.
The cluster name appears in the process title for all server processes in this cluster. Moreover, it is the default application name for a standby connection (see synchronous_standby_names.) The name can be any string of less than NAMEDATALEN characters (64 characters in a standard build).
To tell which index was last used to cluster the table, use the pg_index
system catalog.
Query the table for all indexes that belong to your table and see which one has indisclustered
set. A table can only be clustered by a single index at a time.
There is no way to find out when the table was last clustered, but that's not very interesting anyway. What you want to know is how good the clustering still is.
To find that, query the pg_stats
line for the column on which you clustered. If correlation
is close to 1, you are still good. The smaller the value gets, the more clustering is indicated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With