I have a table called Customer
with three columns and table was partitioned by score
into 10 partitions, from customer_0
to customer_9
Customer table
id | name | score
1 | Light | 90
2 | Missa | 10
There are indexes created on each partition and global index on column score
customer_0
cust_score_0_idx" "CREATE INDEX cust_score_0_idx ON customer_0 USING btree (score)
customer
cust_score_idx" "CREATE INDEX cust_score_idx ON customer_0 USING btree (score)
As you can see in the above the partition-0
has index and table as well has same index with different name, now when i’m trying to delete the one on partition-0
i’m facing issue with a unknown constraint
ERROR: cannot drop index cust_score_0_idx because index cust_score_idx requires it
HINT: You can drop index cust_score_idx instead.
SQL state: 2BP01
So these tables are created long back and i don’t know what sql/constraint they used, but now i’m trying to figure out what is stopping dropping the partition index independently.
But as to note, once i drop index on table, it automatically drops the index on partitions, and after that i’m able to create and drop index independently on table and partition.
so i’m more curious how to setup partition index as dependent on global index ?
2
Answers
Finally found from the PostgreSQL documentation, for the partitioned tables, indices have to be created individually on each partition and then finally need to be attached to index created on table. That is the specific limitation from postgres and if partition indices are not attached then table index remains invalid
Example :
An index on a partitioned table is a partitioned index, that is, it consists of an index on every partition. You cannot drop
cust_score_0_idx
because it is a part ofcust_score_idx
, and without itcust_score_idx
would be incomplete.To get rid of
cust_score_0_idx
, you have to drop the index on the partitioned table (cust_score_idx
); then all its partitions are dropped automatically.