PostgreSQL码农集散地

PostgreSQL 18 preview - 放宽非B-tree索partition keys、matviews的限制

PostgreSQL 18 preview - 放宽对非 B-tree 索引用于partition keys、matviews的限制

之前 PostgreSQL 18 增加了泛化索引AM排序接口, 排序功能不再依赖btree的接口, 同时也意味着和排序、比较(compare)相关的都不依赖btree.

  • 《PostgreSQL 18 preview - 泛化索引AM排序接口: PrepareSortSupportFromIndexRel() 函数》

下面这2个patch的目的则是, 放开之前限制的仅btree unique index才能用于matviews, partition keys的场景.

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=9d6db8bec19413cd0167f1e59d1af005a997bd3e

Allow non-btree unique indexes for matviews    
author Peter Eisentraut <[email protected]>     
Tue, 18 Mar 2025 10:29:15 +0000 (11:29 +0100)    
committer Peter Eisentraut <[email protected]>     
Tue, 18 Mar 2025 10:29:15 +0000 (11:29 +0100)    
commit 9d6db8bec19413cd0167f1e59d1af005a997bd3e    
tree 373ada1d0cdfa11ed9cf060a841d8ae733bbfe4f tree    
parent f278e1fe300ab1b7d43c3efb55a29aa17e5f5dda commit | diff    
Allow non-btree unique indexes for matviews    

We were rejecting non-btree indexes in some cases owing to the    
inability to determine the equality operators for other index AMs;    
that problem no longer exists, because we can look up the equality    
operator using COMPARE_EQ.    

Stop rejecting these indexes, but instead rely on all unique indexes    
having equality operators.  Unique indexes must have equality    
operators.    

Author: Mark Dilger <[email protected]>    
Discussion: https://www.postgresql.org/message-id/flat/[email protected]    

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=f278e1fe300ab1b7d43c3efb55a29aa17e5f5dda

Allow non-btree unique indexes for partition keys    
author Peter Eisentraut <[email protected]>     
Tue, 18 Mar 2025 10:25:36 +0000 (11:25 +0100)    
committer Peter Eisentraut <[email protected]>     
Tue, 18 Mar 2025 10:25:36 +0000 (11:25 +0100)    
commit f278e1fe300ab1b7d43c3efb55a29aa17e5f5dda    
tree b51f02d63bd72e47b884f2e74babb7a60b2a0283 tree    
parent 7317e641268fb9b08d32519920adf1f16c8591ea commit | diff    
Allow non-btree unique indexes for partition keys    

We were rejecting non-btree indexes in some cases owing to the    
inability to determine the equality operators for other index AMs;    
that problem no longer exists, because we can look up the equality    
operator using COMPARE_EQ.  The problem of not knowing the strategy    
number for equality in other index AMs is already resolved.    

Stop rejecting the indexes upfront, and instead reject any forwhich
the equality operator lookup fails.    

Author: Mark Dilger <[email protected]>    
Discussion: https://www.postgresql.org/message-id/flat/[email protected]    

AI 解读

这两个补丁都与 PostgreSQL 数据库系统有关,它们都放宽了对非 B-tree 索引的限制,具体来说:

核心问题:

之前,PostgreSQL 在某些情况下会拒绝使用非 B-tree 索引,因为系统无法确定这些索引访问方法 (AM) 的等值运算符。  简单来说,就是系统不知道如何判断两个值在这些非 B-tree 索引中是否相等。

解决方案:

现在,系统可以通过 COMPARE_EQ 来查找等值运算符,解决了之前无法确定等值运算符的问题。

补丁 1:Allow non-btree unique indexes for partition keys (允许分区键使用非 B-tree 唯一索引)

  • 之前: 系统会预先拒绝某些非 B-tree 索引用于分区键。
  • 之后: 系统不再预先拒绝,而是允许创建。 但是,如果系统在查找等值运算符时失败,则会拒绝该索引。  这意味着只有那些能够找到等值运算符的非 B-tree 索引才能用于分区键。

补丁 2:Allow non-btree unique indexes for matviews (允许物化视图使用非 B-tree 唯一索引)

  • 之前: 系统会拒绝某些非 B-tree 索引用于物化视图。
  • 之后: 系统不再拒绝,而是依赖于所有唯一索引都必须具有等值运算符这一事实。  如果一个非 B-tree 索引没有等值运算符,那么它就不能被用作唯一索引,因此也就不能用于物化视图。

总结:

这两个补丁都放宽了对非 B-tree 索引的限制,允许它们在更多场景下使用(分区键和物化视图)。  关键在于,系统现在能够通过 COMPARE_EQ 查找等值运算符,从而解决了之前无法判断非 B-tree 索引中值相等性的问题。  但是,这种放宽是有条件的:如果系统无法找到等值运算符,则仍然会拒绝该索引。  这确保了唯一索引(无论是 B-tree 还是非 B-tree)的正确性和一致性。

关键术语解释:

  • B-tree 索引: 一种常见的索引类型,适用于范围查询和等值查询。
  • 非 B-tree 索引:  其他类型的索引,例如 Hash 索引、GIN 索引、GiST 索引等,它们适用于不同的查询类型。
  • 索引访问方法 (AM):  索引的实现方式,例如 B-tree、Hash、GIN 等。
  • 等值运算符:  用于判断两个值是否相等的运算符(例如 =)。
  • 分区键:  用于将表数据分割成多个分区的列。
  • 物化视图:  预先计算并存储查询结果的视图,可以提高查询性能。
  • COMPARE_EQ:  一个用于查找等值运算符的函数或机制。

总而言之,这两个补丁使得 PostgreSQL 更加灵活,允许在更多场景下使用非 B-tree 索引,但同时保证了数据的完整性和一致性。