ufk*_*ufk 12 postgresql indexing postgresql-14
我正在使用 postgresql 14.1,并且我使用某些表的分区重新创建了实时数据库。
因为我这样做了,所以我可以在服务器不活动时创建索引,但是当服务器活动时我只能创建 usingconcurrently但不幸的是,当我尝试同时创建索引时,我收到错误。
运行这个:
create index concurrently foo on foo_table(col1,col2,col3));
Run Code Online (Sandbox Code Playgroud)
提供错误:
ERROR: cannot create index on partitioned table "foo_table" concurrently
Run Code Online (Sandbox Code Playgroud)
现在它是一个实时服务器,我无法同时创建索引,我需要创建一些索引以提高性能。有什么想法怎么办?
谢谢
Lau*_*lbe 15
没问题。首先,使用CREATE INDEX CONCURRENTLY在每个分区上创建索引。然后使用CREATE INDEX在分区表上创建索引。这样会很快,分区上的索引将成为索引的分区。
步骤 1:在分区(父)表上创建索引
CREATE INDEX foo_idx ON ONLY foo (col1, col2, col3);
Run Code Online (Sandbox Code Playgroud)
此步骤创建无效索引。这样,任何表分区都不会自动应用索引。
CONCURRENTLY步骤 2:使用并附加到父索引为每个分区创建索引
CREATE INDEX CONCURRENTLY foo_idx_1
ON foo_1 (col1, col2, col3);
ALTER INDEX foo_idx
ATTACH PARTITION foo_idx_1;
Run Code Online (Sandbox Code Playgroud)
对每个分区索引重复此步骤。
步骤3:验证开始时(步骤1)创建的父索引是否有效。一旦所有分区的索引都附加到父索引,父索引就会自动标记为有效。
SELECT * FROM pg_index WHERE pg_index.indisvalid = false;
Run Code Online (Sandbox Code Playgroud)
查询应返回零结果。如果情况并非如此,请检查您的脚本是否有错误。