作者
digoal
日期
2021-03-24
标签
PostgreSQL , bit_count
背景
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=a6715af1e72da289474011be1e2d536f991eda34
+/*
+ * bit_count
+ *
+ * Returns the number of bits set in a bit string.
+ */
+Datum
+bit_bit_count(PG_FUNCTION_ARGS)
+{
+ VarBit *arg = PG_GETARG_VARBIT_P(0);
+
+ PG_RETURN_INT64(pg_popcount((char *) VARBITS(arg), VARBITBYTES(arg)));
+}
+
```
Add bit_count SQL function
author Peter Eisentraut peter@eisentraut.org
Tue, 23 Mar 2021 07:45:51 +0000 (08:45 +0100)
committer Peter Eisentraut peter@eisentraut.org
Tue, 23 Mar 2021 09:13:58 +0000 (10:13 +0100)
commit a6715af1e72da289474011be1e2d536f991eda34
tree 48ae8ef2c5c858baf43611b8a65c4ec22cbe47bf tree
parent 5aed6a1fc214913de9ac69c1717dc64a2483e16d commit | diff
Add bit_count SQL function
This function for bit and bytea counts the set bits in the bit or byte
string. Internally, we use the existing popcount functionality.
For the name, after some discussion, we settled on bit_count, which
also exists with this meaning in MySQL, Java, and Python.
Author: David Fetter david@fetter.org
Discussion: https://www.postgresql.org/message-id/flat/20201230105535.GJ13234@fetter.org
```
```
+-- bit_count
+SELECT bit_count(B'0101011100'::bit(10));
+ bit_count
+-----------
+ 5
+(1 row)
+
+SELECT bit_count(B'1111111111'::bit(10));
+ bit_count
+-----------
+ 10
+(1 row)
+SELECT bit_count('\x1234567890'::bytea);
+ bit_count
+-----------
+ 31
+(1 row)
```
PostgreSQL 许愿链接
您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.
9.9元购买3个月阿里云RDS PostgreSQL实例
PostgreSQL 解决方案集合
德哥 / digoal's github - 公益是一辈子的事.





