Roaring bitmaps are compressed bitmaps optimized for set operations such as intersection, union, difference, and deduplication. They use significantly less memory than conventional bitmaps while delivering faster performance, making them well-suited for user profiling, personalized recommendation, and precision marketing workloads.
Prerequisites
Before you begin, ensure that you have:
An AnalyticDB for PostgreSQL instance running V6.3.8.9 or later. To check your instance's minor version, see View the minor engine version.
The
roaringbitmapextension installed on the instance. See Install, update, and uninstall extensions.
How it works
A roaring bitmap encodes each 32-bit integer by splitting it into 16 most significant bits (stored as a chunk key) and 16 least significant bits (stored in a container). Each chunk holds up to 2^16 integers, and chunks are sorted in a dynamically expanding array so that binary search can locate any value quickly.
Containers are chosen automatically based on data characteristics:
Container type | When used | Capacity |
Array container | Sparse, non-consecutive values | Fewer than 4,096 integers |
Bitmap container | Dense, consecutive integers | 4,096 or more integers |
Run container | Long runs of consecutive increasing values | Smaller than both array and bitmap containers |
This adaptive storage layout keeps the compression ratio high and enables AND, OR, and XOR operations directly between container types. For more details on the underlying data structure, see the CRoaring repository.
Get started
The following steps walk through a complete workflow: install the extension, create a table, insert data, and run bitmap queries.
1. Install the extension.
On the Extensions page of your instance, install the roaringbitmap extension.
2. Create a table with a `roaringbitmap` column.
CREATE TABLE t1 (id integer, bitmap roaringbitmap);3. Insert roaring bitmap data.
Use RB_BUILD to build a bitmap from an explicit integer array, or RB_BUILD_AGG to aggregate a series of integers into a bitmap.
-- Build a bitmap from an explicit array (sets bits at positions 1-9 and 200)
INSERT INTO t1 SELECT 1, RB_BUILD(ARRAY[1,2,3,4,5,6,7,8,9,200]);
-- Build a bitmap by aggregating a generated series of integers 1-100
INSERT INTO t1 SELECT 2, RB_BUILD_AGG(e) FROM GENERATE_SERIES(1,100) e;4. Query bit positions.
RB_ITERATE returns each integer position where a bit is set to 1.
SELECT RB_ITERATE(bitmap) FROM t1 WHERE id = 1;5. Perform bitmap set operations.
Use function syntax or the equivalent operator syntax—both produce the same result.
-- OR: union of two bitmaps (function syntax)
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;
-- OR: union of two bitmaps (operator syntax)
SELECT a.bitmap | b.bitmap
FROM (SELECT bitmap FROM t1 WHERE id = 1) AS a,
(SELECT bitmap FROM t1 WHERE id = 2) AS b;6. Aggregate bitmaps across rows.
SELECT RB_OR_AGG(bitmap) FROM t1; -- Union of all bitmaps
SELECT RB_AND_AGG(bitmap) FROM t1; -- Intersection of all bitmaps
SELECT RB_XOR_AGG(bitmap) FROM t1; -- Symmetric difference of all bitmaps7. Count set bits (cardinality).
SELECT RB_CARDINALITY(bitmap) FROM t1;Bitmap calculation functions
Function | Input | Output | Description | Example | Result |
rb_build | integer[] | roaringbitmap | Creates a roaring bitmap from an integer array. |
|
|
rb_and | roaringbitmap, roaringbitmap | roaringbitmap | Performs an AND operation. |
|
|
rb_or | roaringbitmap, roaringbitmap | roaringbitmap | Performs an OR operation. |
|
|
rb_xor | roaringbitmap, roaringbitmap | roaringbitmap | Performs an XOR operation. |
|
|
rb_andnot | roaringbitmap, roaringbitmap | roaringbitmap | Performs an ANDNOT operation. |
|
|
rb_cardinality | roaringbitmap | integer | Returns the number of set bits. |
|
|
rb_and_cardinality | roaringbitmap, roaringbitmap | integer | Returns the cardinality of the AND result. |
|
|
rb_or_cardinality | roaringbitmap, roaringbitmap | integer | Returns the cardinality of the OR result. |
|
|
rb_xor_cardinality | roaringbitmap, roaringbitmap | integer | Returns the cardinality of the XOR result. |
|
|
rb_andnot_cardinality | roaringbitmap, roaringbitmap | integer | Returns the cardinality of the ANDNOT result. |
|
|
rb_is_empty | roaringbitmap | boolean | Checks whether a roaring bitmap is empty. |
|
|
rb_equals | roaringbitmap, roaringbitmap | boolean | Checks whether two roaring bitmaps are equal. |
|
|
rb_intersect | roaringbitmap, roaringbitmap | boolean | Checks whether two roaring bitmaps intersect. |
|
|
rb_remove | roaringbitmap, integer | roaringbitmap | Removes a specific offset. |
|
|
rb_remove | roaringbitmap, integer, integer | roaringbitmap | Removes a specific offset range. |
|
|
rb_flip | roaringbitmap, integer | roaringbitmap | Flips the bit at a specific offset. |
|
|
rb_flip | roaringbitmap, integer, integer | roaringbitmap | Flips all bits in a specific offset range. |
| |
rb_minimum | roaringbitmap | integer | Returns the smallest set offset. Returns |
|
|
rb_maximum | roaringbitmap | integer | Returns the largest set offset. Returns |
|
|
rb_rank | roaringbitmap, integer | integer | Returns the count of set offsets less than or equal to the specified value. |
|
|
rb_iterate | roaringbitmap | setof integer | Returns each set offset as a row. |
|
|
rb_iterate_decrement | roaringbitmap | integer[] | Returns all set offsets in descending order as an array. |
|
|
rb_contains | roaringbitmap, integer | boolean | Checks whether the bitmap contains a specific offset. |
|
|
rb_contains | roaringbitmap, integer, integer | boolean | Checks whether the bitmap contains a specific offset range. |
| |
rb_contains | roaringbitmap, roaringbitmap | boolean | Checks whether a bitmap contains another bitmap. |
|
|
rb_becontained | integer, roaringbitmap | boolean | Checks whether a specific offset is contained by the bitmap. |
|
|
rb_becontained | roaringbitmap, roaringbitmap | boolean | Checks whether a bitmap is contained by another bitmap. |
|
|
rb_add | roaringbitmap, integer | roaringbitmap | Adds a specific offset. |
|
|
rb_add | roaringbitmap, integer, integer | roaringbitmap | Adds a specific offset range. |
|
|
rb_add_2 | integer, roaringbitmap | roaringbitmap | Adds a specific offset to a bitmap (argument order reversed). |
|
|
rb_jaccard_index | roaringbitmap, roaringbitmap | float8 | Calculates the Jaccard similarity coefficient between two bitmaps. |
|
|
rb_to_array | roaringbitmap | integer[] | Converts a roaring bitmap to an integer array. |
|
|
Bitmap aggregate functions
Function | Input | Output | Description | Example | Result |
rb_build_agg | integer | roaringbitmap | Aggregates a set of offsets into a roaring bitmap. |
|
|
rb_or_agg | roaringbitmap | roaringbitmap | Performs an OR aggregate operation across rows. |
|
|
rb_and_agg | roaringbitmap | roaringbitmap | Performs an AND aggregate operation across rows. |
|
|
rb_xor_agg | roaringbitmap | roaringbitmap | Performs an XOR aggregate operation across rows. |
|
|
rb_or_cardinality_agg | roaringbitmap | integer | Returns the cardinality of the OR aggregate result. |
|
|
rb_and_cardinality_agg | roaringbitmap | integer | Returns the cardinality of the AND aggregate result. |
|
|
rb_xor_cardinality_agg | roaringbitmap | integer | Returns the cardinality of the XOR aggregate result. |
|
|
Operators
All operators have equivalent function forms listed in the previous section. Operator syntax is more concise for inline SQL expressions.
Operator | Left | Right | Output | Description | Example |
| roaringbitmap | roaringbitmap | roaringbitmap | AND operation. |
|
| roaringbitmap | roaringbitmap | roaringbitmap | OR operation. |
|
| roaringbitmap | roaringbitmap | roaringbitmap | XOR operation. |
|
| roaringbitmap | roaringbitmap | roaringbitmap | ANDNOT operation. |
|
| roaringbitmap | integer | roaringbitmap | Adds a specific offset. |
|
| roaringbitmap | integer | roaringbitmap | Removes a specific offset. |
|
| roaringbitmap | roaringbitmap | boolean | Checks whether two bitmaps are equal. |
|
| roaringbitmap | roaringbitmap | boolean | Checks whether two bitmaps differ. |
|
| roaringbitmap | roaringbitmap | boolean | Checks whether two bitmaps intersect. |
|
| roaringbitmap | roaringbitmap | boolean | Checks whether the left bitmap contains the right bitmap. |
|
| roaringbitmap | integer | boolean | Checks whether the bitmap contains a specific offset. |
|
| roaringbitmap | roaringbitmap | boolean | Checks whether the left bitmap is contained by the right bitmap. |
|
| integer | roaringbitmap | boolean | Checks whether the offset is contained by the bitmap. |
|