Re: popcount

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "David Fetter" <david(at)fetter(dot)org>
Cc: PostgreSQL Development <pgsql-hackers(at)postgresql(dot)org>, Vik Fearing <vik(dot)fearing(at)2ndquadrant(dot)com>
Subject: Re: popcount
Date: 2020-12-30 16:27:04
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

David Fetter wrote:

+ bytea *t1 = PG_GETARG_BYTEA_PP(0);
+ int len, result;
+ len = VARSIZE_ANY_EXHDR(t1);
+ result = pg_popcount(VARDATA_ANY(t1), len);
+ PG_RETURN_INT32(result);

The input may have more than 2 billion bits set to 1. The biggest possible
result should be 8 billion for bytea (1 GB with all bits set to 1).
So shouldn't this function return an int8?

There is a pre-existing function in core: bit_length(bytea) returning int4,
but it errors out for large values (in fact it's a SQL function that returns

For the varbit case, it doesn't seem like it can hold so many bits, although
I don't see the limit documented in

Best regards,
Daniel Vérité
PostgreSQL-powered mailer:
Twitter: @DanielVerite

In response to

  • popcount at 2020-12-30 10:55:36 from David Fetter


Browse pgsql-hackers by date

  From Date Subject
Next Message David Fetter 2020-12-30 16:41:09 Re: popcount
Previous Message Stephen Frost 2020-12-30 15:39:22 Re: [PATCH] Simplify permission checking logic in user.c