如果我在分区表上创建索引,则每个分区都有自己的索引。如果我删除主索引,这些不会被删除。
是否有捷径可寻?
我编写了一个函数来查找与顶级索引名称与分区后缀匹配的所有索引,并且对于在分区上创建的索引工作正常。
但是,当 Greenplum 通过默认分区拆分添加新分区时,它会为该分区生成一个具有完全不同命名约定的新索引。
现有分区的名称类似于 indexname_1_prt_partitionname
新分区的名称类似于 tablename_1_prt_partitionname_indexcolumn
知道当名称不匹配时如何识别新分区索引是父索引的一部分吗?或者我只是用两种不同的模式来匹配我的索引删除器两次?
我将使用下面的 Bell 查询,或者这个适合采用分区索引的查询(以防我们已经删除了头索引):
SELECT child_index.indexrelid::regclass
FROM pg_index AS partition_index
-- Find the partition that the partition index is on
INNER JOIN pg_partition_rule parindex_rule ON parindex_rule.parchildrelid = partition_index.indrelid
-- Follup up to the partitioning scheme
INNER JOIN pg_partition ON pg_partition.oid = parindex_rule.paroid
-- Follow the links through to the individual partitions
INNER JOIN pg_partition_rule ON pg_partition_rule.paroid = pg_partition.oid
-- Find the indexes on each partition
INNER JOIN pg_index AS child_index ON child_index.indrelid = pg_partition_rule.parchildrelid
-- Which are on the same field as the named index
AND child_index.indkey = partition_index.indkey
-- Using the same comparison operator
AND child_index.indclass = partition_index.indclass
-- Filtered for the index we're trying to drop
WHERE partition_index.indexrelid = 'schema.partitionindexname'::regclass
Run Code Online (Sandbox Code Playgroud)
Greenplum 不(在版本 4.3.8 中)在将分区上的索引链接到基表上的索引的目录中维护记录。最好的选择是跟踪分区并在与基本索引的定义匹配的分区上查找索引。
CREATE OR REPLACE FUNCTION drop_child_indexes (index_name varchar)
RETURNS VOID
AS
$functionBody$
DECLARE
child_index_name varchar;
BEGIN
FOR child_index_name IN
SELECT child_index.indexrelid::regclass
FROM pg_index AS parent_index
-- Find the partitioning scheme for the table the index is on
INNER JOIN pg_partition ON pg_partition.parrelid = parent_index.indrelid
-- Follow the links through to the individual partitions
INNER JOIN pg_partition_rule ON pg_partition_rule.paroid = pg_partition.oid
-- Find the indexes on each partition
INNER JOIN pg_index AS child_index ON child_index.indrelid = pg_partition_rule.parchildrelid
-- Which are on the same field as the named index
AND child_index.indkey = parent_index.indkey
-- Using the same comparison operator
AND child_index.indclass = parent_index.indclass
-- Filtered for the index we're trying to drop
WHERE parent_index.indexrelid = $1::regclass::oid
-- Drop leaves first, even if it doesn't really matter in this case
ORDER BY pg_partition.parlevel DESC
LOOP
RAISE NOTICE '%', child_index_name||' ';
EXECUTE 'DROP INDEX '||child_index_name||';';
END LOOP;
END
$functionBody$
LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
如果有另一个具有相同定义(字段、比较运算符)的索引,它的分区索引也将被删除。不是一个完美的答案,但比子字符串匹配更好。
归档时间: |
|
查看次数: |
1374 次 |
最近记录: |