我正在使用Postgres 11,并且希望在主键为UUID的表上使用哈希分区。我知道我需要预先选择多个分区,并且主键上的哈希函数的模数将用于为每个分区分配行。
类似这样的东西:
CREATE TABLE new_table ( id uuid ) PARTITION BY HASH (id);
CREATE TABLE new_table_0 PARTITION OF new_table FOR VALUES WITH (MODULUS 3, REMAINDER 0);
CREATE TABLE new_table_1 PARTITION OF new_table FOR VALUES WITH (MODULUS 3, REMAINDER 1);
CREATE TABLE new_table_2 PARTITION OF new_table FOR VALUES WITH (MODULUS 3, REMAINDER 2);
documentation提及“分区键的哈希值”,但未指定该哈希如何发生。我想针对现有数据测试此哈希函数,以查看不同数量的分区的分布模式。像这样的东西:
SELECT unknown_partition_hash_function(id) AS hash_value, COUNT(id) AS number_of_records
FROM existing_table
GROUP BY 1
是否可以在SELECT语句中使用此哈希函数?
我建议在postgres / src / backend / partitioning / partbounds.c中查看PostgreSQL源代码,其中引用了可以从SQL调用的2个C函数:
/ *
* satisfies_hash_partition
*
* This is an SQL-callable function for use in hash partition constraints.
* The first three arguments are the parent table OID, modulus, and remainder.
* The remaining arguments are the value of the partitioning columns (or
* expressions); these are hashed and the results are combined into a single
* hash value by calling hash_combine64.
*
* Returns true if remainder produced when this computed single hash value is
* divided by the given modulus is equal to given remainder, otherwise false.
*
* See get_qual_for_hash() for usage.
*/
Datum
satisfies_hash_partition(PG_FUNCTION_ARGS)
可以在src / test / regress / sql / hash_part.sql]中找到示例。