作者
digoal
日期
2020-03-24
标签
PostgreSQL , hash , uuid
背景
https://github.com/iCyberon/pg_hashids
pg_hashids, generate short unique ids from integers
Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers. It converts numbers like 347 into strings like “yr8”. You can also decode those ids back. This is useful in bundling several parameters into one or simply using them as short UIDs.
You can use hashids to hide primary keys in your database. I've used the extension on several production databases.
Tested PostgreSQL versions : 9.5.X and 9.6.X (Should work on older versions, just not tested)
It's using hashids.c under the hood. More information about hashids and it's implementations here: hashids.org
http://hashids.org/
Usage
Encoding
Returns a hash using the default alphabet and empty salt.
SELECT id_encode(1001); -- Result: jNl
Returns a hash using the default alphabet and supplied salt.
SELECT id_encode(1234567, 'This is my salt'); -- Result: Pdzxp
Returns a hash using the default alphabet, salt and minimum hash length.
SELECT id_encode(1234567, 'This is my salt', 10); -- Result: PlRPdzxpR7
Returns a hash using the supplied alphabet, salt and minimum hash length.
SELECT id_encode(1234567, 'This is my salt', 10, 'abcdefghijABCDxFGHIJ1234567890'); -- Result: 3GJ956J9B9
Decoding
You can also decode previouslt generated hashes. Just use the same salt, otherwise you'll get wrong results.
SELECT id_decode('PlRPdzxpR7', 'This is my salt', 10); -- Result: 1234567
Using a custom alphabet
SELECT id_decode('3GJ956J9B9', 'This is my salt', 10, 'abcdefghijABCDxFGHIJ1234567890'); -- Result: 1234567
PostgreSQL 许愿链接
您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.
9.9元购买3个月阿里云RDS PostgreSQL实例
PostgreSQL 解决方案集合
德哥 / digoal's github - 公益是一辈子的事.





