varbitx
The built-in varbit extension in PostgreSQL Community Edition supports only basic BIT type operations. PolarDB for PostgreSQL extends the varbit extension with additional BIT operations for broader use cases, such as real-time user profiling recommendation systems, access control advertising systems, and ticketing systems.
Prerequisites
The version of PolarDB for PostgreSQL is PostgreSQL 11.
Functions
|
Function |
Description |
|
|
Extracts c bits starting from position b and returns a varbit value. Example: |
|
|
Sets the bits at the index positions specified in array d to b (0 or 1). Bits beyond the original length are filled with c (0 or 1). Example: |
|
|
Starting from position c, counts the number of b (0 or 1) values within d bits. If the range exceeds the bit string length, only the existing bits are counted. Example: |
|
|
Counts the number of b (1 or 0) values. Example: |
|
|
Fills a bit string of length b with the value a (0 or 1). Example: |
|
|
Generates a random bit string of length a, with a random ratio of b (0 or 1) specified by c. Example: |
|
|
Returns the index position array of b (0 or 1). Indexing starts from 0. If c is true, positions are returned in ascending order. If c is false, positions are returned in descending order. Example: |
|
|
Returns the index position array of b (0 or 1), up to c positions. Indexing starts from 0. If d is true, positions are returned in ascending order. If d is false, positions are returned in descending order. Example: |
|
|
Extracts c bits starting from position b and returns the index position array of d (0 or 1) values. Example: |
|
|
Checks the bits at the index positions specified in array c for value b (0 or 1) and returns the matching index positions. Positions that exceed the bit string length are not included. Example: |
|
|
Sets the bits at the index positions specified in array d to b (0 or 1). Stops after setting e bits. Bits beyond the original length are filled with c (0 or 1). Example: |
|
|
Sets the bits at the index positions specified in array d to b (0 or 1). Bits beyond the original length are filled with c (0 or 1). Returns the updated varbit value and the index position array of bits that were set to b in this operation. Example: |
|
|
Sets the bits at the index positions specified in array d to b (0 or 1). Bits beyond the original length are filled with c (0 or 1). Stops after successfully setting e bits. Returns the updated varbit value and the index position array of bits that were set to b in this operation. Example: |
|
|
Counts the number of 0 or 1 values at the specified index positions. Example: |
Usage
-
Create the extension
CREATE EXTENSION varbitx; -
Delete the extension
DROP EXTENSION varbitx; -
Use functions
Use
select <function>. Examples:-
bit_count function
select bit_count('1111000011110000', 1, 5, 4);The following output is returned:
bit_count ----------- 1 (1 row) -
set_bit_array_record function
select set_bit_array_record('111100001111', 1, 0, array[1,4,5,6,7], 2);The following output is returned:
set_bit_array_record ------------------------ (111111001111,"{4,5}") (1 row)
For more information about the functions, see Functions.
-