Postgres的文件說,在表分區的條件不應重疊使用具有重疊約束的postgres表分區安全嗎?
確保約束保證有不同的分區裏 鍵值之間沒有重疊。
,但我不明白爲什麼,因爲確切的分區數據已經被插入到仍然被觸發決定,這可能是知道的重疊限制
我有以下的情況,與表大量的文本信息和update_date時間戳,這個表按季度分區,所有新的或更新的行進入最新的分區, 問題在於gin trigram索引太慢,所以我想避免構建trigram索引當前日期
當前在主表上觸發負責創建parti我打算在當天添加新的分區,這將與四分區重疊,並且我想要禁用當前分區的trigram索引(將數據合併回四分區將是另一個維護任務)
我試過用重疊的update_date約束手動創建分區表,並且postgres沒有抱怨,我甚至沒有搜索,並且兩個表都用在了這個計劃中,所以它似乎工作得很好,但是文檔說約束可以' t重疊,爲什麼呢?
如果我有適當的觸發器和維護,是否可以安全地創建具有重疊約束的分區?
UPD:
CREATE TABLE master (text_value text, update_date timestamp);
CREATE TABLE partition_year (
CHECK (update_date >= DATE '2015-01-01' AND update_date < DATE '2015-12-31')
) INHERITS (master);
CREATE TABLE partition_month (
CHECK (update_date >= DATE '2015-07-01' AND update_date < DATE '2015-8-01')
) INHERITS (master);
-- in production this would be handled by trigger
insert into partition_year(text_value, update_date) values ('year', '2015-01-02');
insert into partition_month(text_value, update_date) values ('month', '2015-07-02');
-- this scans only year partition
explain select * from master where update_date = '2015-01-02';
-- this scans both year and month partition
explain select * from master where update_date = '2015-07-02';
這個例子表明,Postgres的同時讀取年份和月份partititon,不那麼在意它們的重疊
非常有趣的問題。我不確定。約束排除可用於選擇多個表滿足約束條件的多個表,因此我同樣不太清楚爲什麼應用此限制。但是,請注意,PostgreSQL將選擇掃描所有存在重疊的分區。 –