The varbit plug-in that is provided in the PostgreSQL Community edition supports only simple BIT-type operation functions. The varbitx plug-in is an extension of the varbit plug-in. The varbitx plug-in is provided in ApsaraDB RDS for PostgreSQL to support more BIT-type operations in more scenarios. These scenarios include real-time user profile recommendation, access control advertising, and ticketing.
Prerequisites
- PostgreSQL 11
- PostgreSQL 10
Functions supported
Function | Description |
---|---|
get_bit (varbit a, int b, int c) returns varbit |
Obtains a specified number c of bits that start at position b and returns a VARBIT-type
string.
For example, |
set_bit_array (varbit a, int b, int c, int[] d) returns varbit |
Changes the values of the bits that are specified by the subscript array d to values
b (0 or 1), and fills the bits that exceed the original length with values c (0 or
1).
For example, |
bit_count (varbit a, int b, int c, int d) returns int |
Counts the number of values b (0 or 1) that start at position c among a specified
number d of bits. The bits that are beyond the specified length are not counted.
For example, |
bit_count (varbit a, int b) returns int |
Counts the total number of values b (0 or 1).
For example, |
bit_fill (int a, int b) returns varbit |
Fills a specified number b of bits with values a (0 or 1).
For example, |
bit_rand (int a, int b, float c) returns varbit |
Obtains a random percentage c of bits from a specified number a of bits, and randomly
fills the obtained bits with values b (0 or 1).
For example, |
bit_posite (varbit a, int b, boolean c) returns int[] |
Returns a subscript array. The subscript array indicates the positions of the bits
whose values are b (0 or 1). Subscripts start at 0. If the value of c is true, subscripts
are returned in a positive sequence. If the value of c is false, subscripts are returned
in a negative sequence.
For example, |
bit_posite (varbit a, int b, int c, boolean d) returns int[] |
Returns a subscript array. The subscript array indicates the positions of the bits
whose values are b (0 or 1). The number of subscripts in the subscript array is c.
Subscripts start at 0. If the value of d is true, subscripts are returned in a positive
sequence. If the value of d is false, subscripts are returned in a negative sequence.
For example, |
get_bit_array (varbit a, int b, int c, int d) returns int[] |
Obtains a specified number c of bits that start at position b, identifies the bits
whose values are d (0 or 1) among the obtained bits, and returns a subscript array.
The subscript array indicates the positions of the identified bits.
For example, |
get_bit_array (varbit a, int b, int[] c) returns int[] |
Obtains the bits that are specified by a subscript array c, identifies the bits whose
values are b (0 or 1) among the obtained bits, and returns a subscript array. The
subscript array indicates the positions of the identified bits. The bits that are
not included in the subscript array c are not counted.
For example, |
set_bit_array (varbit a, int b, int c, int[] d, int e) returns varbit |
Changes the values of the bits that are specified by a subscript array d to values
b (0 or 1), and fills the bits that are beyond the original length with values c (0
or 1). The number of bits that are returned is e.
For example, |
set_bit_array_record (varbit a, int b, int c, int[] d) returns (varbit,int[]) |
Changes the values of the bits that are specified by a subscript array d to values
b (0 or 1), and fills the bits that are beyond the original length with values c (0
or 1). This function not only returns a bit string but also a subscript array. The
subscript array indicates the positions of the bits whose values are changed.
For example, |
set_bit_array_record (varbit a, int b, int c, int[] d, int e) returns (varbit,int[]) |
Changes the values of the bits that are specified by a subscript array d to values
b (0 or 1), and fills the bits that are beyond the original length with values c (0
or 1). This function not only returns a bit string but also a subscript array. The
subscript array indicates the positions of the bits whose values are changed. This
function returns results immediately after it changes values for a total of e bits.
For example, |
bit_count_array (varbit a, int b, int[] c) returns int |
Counts the number of values b (0 or 1) among the bits that are specified by a subscript
array c.
For example, |
Basic usage
- Create the varbitx plug-in.
CREATE EXTENSION varbitx;
- Delete the varbitx plug-in.
DROP EXTENSION varbitx;
- Call the functions that are supported by the varbitx plug-in.
You can execute the
SELECT <function>
statement to call the functions.- Execute the following statement to call the bit_count function:
select bit_count('1111000011110000', 1, 5, 4);
The following result is returned:
bit_count ----------- 1 (1 row)
- Execute the following statement to call the set_bit_array_record function:
select set_bit_array_record('111100001111', 1, 0, array[1,4,5,6,7], 2);
The following result is returned:
set_bit_array_record ------------------------ (111111001111,"{4,5}") (1 row)
For more information about the functions and their descriptions, see Functions supported.
- Execute the following statement to call the bit_count function: