在postgres中,有一个基于
hash
的分区。但是 postgres
没有清楚地解释如何计算给定列值的哈希值。
我搜索了 Postgres 文档,但一无所获。除了在一些邮箱帖子中,还有人提到
hashtext()
内部功能。有谁有关于用于散列值的实际函数(以及进一步使用模数运算符)的任何信息吗?我的意思是 postgres 如何对一个值进行哈希处理,将其转换为 uint64
以进行最终的模运算。
更新: 阅读 postgres 源代码,我发现分区函数在尝试查找给定散列的
uint64
值时使用这样的方法:
/*
* DatumGetUInt64
* Returns 64-bit unsigned integer value of a datum.
*
* Note: this macro hides whether int64 is pass by value or by reference.
*/
#ifdef USE_FLOAT8_BYVAL
#define DatumGetUInt64(X) ((uint64) (X))
#else
#define DatumGetUInt64(X) (* ((uint64 *) DatumGetPointer(X)))
#endif
pg_amproc
系统目录中找到它们;与 pg_opfamily
连接并将查询限制为 hash
访问方法 的运算符系列
此查询列出了每种类型的标准哈希支持函数:
SELECT DISTINCT
ap.amproclefttype::regtype AS data_type,
ap.amproc::regproc AS hash_function
FROM pg_amproc AS ap
JOIN pg_opfamily AS of ON ap.amprocfamily = of.oid
JOIN pg_am ON of.opfmethod = pg_am.oid
WHERE pg_am.amname = 'hash'
AND ap.amprocnum = 1
ORDER BY amproclefttype::regtype::text;
功能应该是无关紧要的,但我理解你的好奇心。
请注意,除非您将分区放在不同的存储上以分散 I/O 负载,否则哈希分区几乎没有任何用处。
如果你看看哈希函数的实际实现 https://doxygen.postgresql.org/hashfn_8c.html#a80da40639eb177d209c938e1d5599728 谷歌搜索神奇的数字,所有这些都链接回 postgres 实现或从中取出的样本。 似乎是一个定制的哈希函数,尽管 0x9e3779b9 幻数在 Rosettacode 上的 ISAAC 密码实现中被称为“黄金比例”: https://rosettacode.org/wiki/The_ISAAC_cipher#C。