The roaringbitmap extension enables efficient bitmap set operations on large integer datasets in ApsaraDB RDS for PostgreSQL, improving query performance.
Prerequisites
Before you begin, ensure that:
Your RDS instance runs PostgreSQL 12 or later
(For PostgreSQL 17) The minor engine version is 20241030 or later
Update your RDS instance to the latest minor engine version before using the extension. For instructions, see Update the minor engine version.
How it works
A roaring bitmap partitions 32-bit integers into 2^16 chunks. Integers in each chunk share the same 16 most significant bits; the 16 least significant bits are stored in a container. Containers are held in a dynamic array that serves as the primary index.
Two container types adapt to data density:
Array container: stores up to 4,096 integers, used for sparse chunks
Bitmap container: stores more than 4,096 integers, used for dense chunks
This structure enables fast value retrieval and supports bitwise operations (AND, OR, XOR, ANDNOT) across container types, delivering strong storage and compute performance.
Enable the extension
Run the following SQL command to create the extension in your database:
CREATE EXTENSION roaringbitmap;Insert data
Create a table
Create a table to store roaringbitmap data:
CREATE TABLE t1 (id integer, bitmap roaringbitmap);Build bitmaps from arrays
Use rb_build to create a bitmap from an integer array, then insert it into the table:
INSERT INTO t1 SELECT 1, RB_BUILD(ARRAY[1,2,3,4,5,6,7,8,9,200]);As a result, a new row is inserted with id = 1 and a bitmap representing the integers {1, 2, 3, 4, 5, 6, 7, 8, 9, 200}.
Use rb_build_agg to aggregate a set of integers into a bitmap:
INSERT INTO t1 SELECT 2, RB_BUILD_AGG(e) FROM GENERATE_SERIES(1, 100) e;As a result, a new row is inserted with id = 2 and a bitmap representing all integers from 1 to 100.
Perform bitmap operations
Bitwise operations
Run OR, AND, XOR, or ANDNOT operations on two bitmaps:
SELECT RB_OR(a.bitmap, b.bitmap)
FROM (SELECT bitmap FROM t1 WHERE id = 1) AS a,
(SELECT bitmap FROM t1 WHERE id = 2) AS b;As a result, a new bitmap is returned containing all integers present in either bitmap.
Aggregate operations
Run aggregate operations across all bitmaps in the table to produce a single result bitmap:
SELECT RB_OR_AGG(bitmap) FROM t1;
SELECT RB_AND_AGG(bitmap) FROM t1;
SELECT RB_XOR_AGG(bitmap) FROM t1;
SELECT RB_BUILD_AGG(e) FROM GENERATE_SERIES(1, 100) e;Query bitmap data
Get cardinality
Cardinality is the count of bits set to 1 in a bitmap. Use rb_cardinality to calculate it:
SELECT RB_CARDINALITY(bitmap) FROM t1;As a result, the number of set bits (elements) in each bitmap is returned.
Iterate over set bits
Use rb_iterate to get the list of bit positions set to 1:
SELECT RB_ITERATE(bitmap) FROM t1 WHERE id = 1;As a result, each integer in the bitmap is returned as a separate row.
Function reference
Bitmap calculation functions
| Function | Input | Output | Description | Example |
|---|---|---|---|---|
rb_build | integer[] | roaringbitmap | Creates a bitmap from an integer array. | rb_build('{1,2,3,4,5}') |
rb_and | roaringbitmap, roaringbitmap | roaringbitmap | AND operation. | rb_and(rb_build('{1,2,3}'), rb_build('{3,4,5}')) |
rb_or | roaringbitmap, roaringbitmap | roaringbitmap | OR operation. | rb_or(rb_build('{1,2,3}'), rb_build('{3,4,5}')) |
rb_xor | roaringbitmap, roaringbitmap | roaringbitmap | XOR operation. | rb_xor(rb_build('{1,2,3}'), rb_build('{3,4,5}')) |
rb_andnot | roaringbitmap, roaringbitmap | roaringbitmap | ANDNOT operation. | rb_andnot(rb_build('{1,2,3}'), rb_build('{3,4,5}')) |
rb_cardinality | roaringbitmap | integer | Returns the cardinality (count of bits set to 1). | rb_cardinality(rb_build('{1,2,3,4,5}')) |
rb_and_cardinality | roaringbitmap, roaringbitmap | integer | Cardinality of an AND operation. | rb_and_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}')) |
rb_or_cardinality | roaringbitmap, roaringbitmap | integer | Cardinality of an OR operation. | rb_or_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}')) |
rb_xor_cardinality | roaringbitmap, roaringbitmap | integer | Cardinality of an XOR operation. | rb_xor_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}')) |
rb_andnot_cardinality | roaringbitmap, roaringbitmap | integer | Cardinality of an ANDNOT operation. | rb_andnot_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}')) |
rb_is_empty | roaringbitmap | boolean | Returns true if the bitmap is empty. | rb_is_empty(rb_build('{1,2,3,4,5}')) |
rb_equals | roaringbitmap, roaringbitmap | boolean | Returns true if two bitmaps are identical. | rb_equals(rb_build('{1,2,3}'), rb_build('{3,4,5}')) |
rb_intersect | roaringbitmap, roaringbitmap | boolean | Returns true if two bitmaps share at least one common element. | rb_intersect(rb_build('{1,2,3}'), rb_build('{3,4,5}')) |
rb_remove | roaringbitmap, integer | roaringbitmap | Removes a specific offset from the bitmap. | rb_remove(rb_build('{1,2,3}'), 3) |
rb_flip | roaringbitmap, integer, integer | roaringbitmap | Flips bits within a specified offset range. | rb_flip(rb_build('{1,2,3}'), 2, 3) |
rb_minimum | roaringbitmap | integer | Returns the smallest offset. Returns -1 if the bitmap is empty. | rb_minimum(rb_build('{1,2,3}')) |
rb_maximum | roaringbitmap | integer | Returns the largest offset. Returns 0 if the bitmap is empty. | rb_maximum(rb_build('{1,2,3}')) |
rb_rank | roaringbitmap, integer | integer | Returns the count of elements less than or equal to a specified offset. | rb_rank(rb_build('{1,2,3}'), 3) |
rb_iterate | roaringbitmap | setof integer | Returns all offsets in the bitmap as a set. | rb_iterate(rb_build('{1,2,3}')) |
Bitmap aggregate functions
| Function | Input | Output | Description | Example |
|---|---|---|---|---|
rb_build_agg | integer | roaringbitmap | Aggregates a set of integers into a bitmap. | rb_build_agg(1) |
rb_or_agg | roaringbitmap | roaringbitmap | OR aggregate operation. | rb_or_agg(rb_build('{1,2,3}')) |
rb_and_agg | roaringbitmap | roaringbitmap | AND aggregate operation. | rb_and_agg(rb_build('{1,2,3}')) |
rb_xor_agg | roaringbitmap | roaringbitmap | XOR aggregate operation. | rb_xor_agg(rb_build('{1,2,3}')) |
rb_or_cardinality_agg | roaringbitmap | integer | Cardinality from an OR aggregate operation. | rb_or_cardinality_agg(rb_build('{1,2,3}')) |
rb_and_cardinality_agg | roaringbitmap | integer | Cardinality from an AND aggregate operation. | rb_and_cardinality_agg(rb_build('{1,2,3}')) |
rb_xor_cardinality_agg | roaringbitmap | integer | Cardinality from an XOR aggregate operation. | rb_xor_cardinality_agg(rb_build('{1,2,3}')) |