All Products
Search
Document Center

Hologres:roaringbitmap

Last Updated:Mar 26, 2026

Roaring bitmaps are efficiently compressed bitmaps supported across many programming languages and big data platforms. In Hologres, roaring bitmap functions are suited for ultra-high-cardinality workloads — deduplication, tag-based filtering, and time series collection.

How it works

A roaring bitmap divides 32-bit integers into 2^16 chunks. Integers in the same 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 as primary indexes.

Two container types balance storage and performance:

Container type Used for Capacity
Array container Sparse chunks Up to 4,096 integers
Bitmap container Dense chunks More than 4,096 integers

This structure enables fast value retrieval and efficient bitwise operations (AND, OR, XOR) between containers.

Limits

  • Only exclusive instances of Hologres V0.10 and later support roaring bitmap functions.

    Check your instance version in the Hologres console. If the version is earlier than V0.10, upgrade the instance in the console or join the DingTalk group for technical support. See Common upgrade preparation failure errors and Obtain online support for Hologres.
  • Roaring bitmap functions are loaded to the public schema by default and can only be loaded to the public schema.

  • Starting from Hologres V3.1, the RoaringBitmap64 data type is supported. Some roaring bitmap functions can process data of the RoaringBitmap64 type. When processing RoaringBitmap64 data, these functions do not support constant input parameters.

  • Before using roaring bitmap functions, enable the extension with the following statement. The extension is database-scoped — run this once per database. Repeat for any new database you create.

    -- Enable the extension.
    CREATE EXTENSION roaringbitmap;

    To drop the extension:

    DROP EXTENSION roaringbitmap;
    Important

    Avoid DROP EXTENSION <extension_name> CASCADE;. The CASCADE option removes the extension along with all its data and dependent objects — including PostGIS data, roaring bitmap data, Proxima data, binary log data, and BSI data, and dependent metadata, tables, views, and server objects.

  • Roaring bitmap columns cannot be specified as bitmap or dictionary indexes.

  • When creating a table with a roaring bitmap column, explicitly specify the column type as roaringbitmap (32-bit) or roaringbitmap64 (64-bit). Mixed calculations between the two types are not supported.

    -- Create a table with a 32-bit roaring bitmap column.
    CREATE TABLE t_rb_32 (
        bucket int,
        x roaringbitmap
    );
    
    -- Create a table with a 64-bit roaring bitmap column.
    CREATE TABLE t_rb_64 (
        bucket int,
        x roaringbitmap64
    );
    
    -- Mixed calculations return an error.
    -- ERROR: operator does not exist: roaringbitmap & roaringbitmap64
    SELECT
        a.x & b.x
    FROM
        t_rb_32 a
    JOIN t_rb_64 b ON a.bucket = b.bucket;

Operators

All operators below support both RoaringBitmap and RoaringBitmap64 types unless otherwise noted.

Operator Input type Output type Description Example Result
& RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 Same as input AND rb_build('{1,2,3}') & rb_build('{3,4,5}') {3}
| RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 Same as input OR rb_build('{1,2,3}') | rb_build('{3,4,5}') {1,2,3,4,5}
| RoaringBitmap | RoaringBitmap64, INTEGER RoaringBitmap | RoaringBitmap64 OR (bitmap, integer); V1.3.16+ rb_build('{1,2,3}') | 6 {1,2,3,6}
| INTEGER, RoaringBitmap | RoaringBitmap64 RoaringBitmap | RoaringBitmap64 OR (integer, bitmap); V1.3.16+ 6 | rb_build('{1,2,3}') {1,2,3,6}
# RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 Same as input XOR rb_build('{1,2,3}') # rb_build('{3,4,5}') {1,2,4,5}
<< RoaringBitmap | RoaringBitmap64, BIGINT RoaringBitmap | RoaringBitmap64 Left shift; V1.3.16+ rb_build('{1,2,3}') << 2 {3,4,5}
>> RoaringBitmap | RoaringBitmap64, BIGINT RoaringBitmap | RoaringBitmap64 Right shift; V1.3.16+ rb_build('{1,2,3}') >> 3
- RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 Same as input ANDNOT (bitmap, bitmap); V1.3.16+ rb_build('{1,2,3}') - rb_build('{3,4,5}') {1,2}
- RoaringBitmap | RoaringBitmap64, INTEGER RoaringBitmap | RoaringBitmap64 ANDNOT (bitmap, integer) rb_build('{1,2,3}') - 3 {1,2}
@> RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 BOOLEAN A contains B rb_build('{1,2,3}') @> rb_build('{3,4,5}') false
@> RoaringBitmap | RoaringBitmap64, INTEGER BOOLEAN A contains integer rb_build('{1,2,3}') @> 3 true
<@ RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 BOOLEAN A is contained by B rb_build('{1,2,3}') <@ rb_build('{3,4,5}') false
<@ INTEGER, RoaringBitmap | RoaringBitmap64 BOOLEAN Integer is contained by A 3 <@ rb_build('{1,2,3}') true
&& RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 BOOLEAN A intersects B rb_build('{1,2,3}') && rb_build('{3,4,5}') true
= RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 BOOLEAN Equal rb_build('{1,2,3}') = rb_build('{3,4,5}') false
<> RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 BOOLEAN Not equal rb_build('{1,2,3}') <> rb_build('{3,4,5}') true

Roaring bitmap functions

Functions supporting both RoaringBitmap and RoaringBitmap64

Function Input type Output type Description Example Result
rb_build_agg INTEGER | BIGINT RoaringBitmap | RoaringBitmap64 Aggregates offsets into a roaring bitmap. BIGINT input (returning RoaringBitmap64) requires V3.1+. rb_build_agg(1) {1}
rb_cardinality RoaringBitmap | RoaringBitmap64 INTEGER Returns the number of elements in a roaring bitmap. rb_cardinality(rb_build('{1,2,3,4,5}')) 5
rb_and_cardinality RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 INTEGER Returns the cardinality of the AND of two roaring bitmaps. rb_and_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}')) 1
rb_or_cardinality RoaringBitmap | RoaringBitmap64, RoaringBitmap | RoaringBitmap64 INTEGER Returns the cardinality of the OR of two roaring bitmaps. rb_or_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}')) 5
rb_range RoaringBitmap | RoaringBitmap64, BIGINT, BIGINT RoaringBitmap | RoaringBitmap64 Returns elements in the range [start, end), where start is 1-based. Requires V1.3.16+. rb_range(rb_build('{1,2,3}'), 2, 3)
rb_minimum RoaringBitmap | RoaringBitmap64 INTEGER Returns the minimum offset. Returns -1 if the bitmap is empty. rb_minimum(rb_build('{1,2,3}')) 1
rb_maximum RoaringBitmap | RoaringBitmap64 INTEGER Returns the maximum offset. Returns 0 if the bitmap is empty. rb_maximum(rb_build('{1,2,3}')) 3
rb_to_array RoaringBitmap | RoaringBitmap64 INTEGER[] Converts a roaring bitmap to an integer array. rb_to_array(rb_build('{1,2,3}')) {1,2,3}
rb_to_array_string RoaringBitmap | RoaringBitmap64, TEXT TEXT Converts a roaring bitmap to a string, joining elements with the specified delimiter. rb_to_array_string(rb_build('{1,2,3}'), ',') 1,2,3

Functions supporting only RoaringBitmap64

Function Input type Output type Description Example Result
rb64_build BIGINT[] RoaringBitmap64 Creates a 64-bit roaring bitmap from a BIGINT array. Requires V3.1+. See example below.
-- Prepare data.
CREATE TABLE public.tn (
    id INT,
    num BIGINT[]
);
INSERT INTO public.tn ("id", "num") VALUES (01, '{1,2}');

SELECT rb64_build(num) rb_num, num FROM public.tn;

Expected output:

rb_num                                                                          | num
--------------------------------------------------------------------------------+------
\x030100000000000000000000003a30000001000000000001001000000001000200             | {1,2}

Functions supporting only RoaringBitmap (32-bit)

Function Input type Output type Description Example Result
rb_build INTEGER[] RoaringBitmap Creates a 32-bit roaring bitmap from an integer array. rb_build('{1,2,3,4,5}') {1,2,3,4,5}
roaringbitmap_in TEXT RoaringBitmap Converts a TEXT-encoded roaring bitmap to RoaringBitmap type. Requires V2.1.33+. See example below.
rb_index RoaringBitmap, INTEGER BIGINT Returns the 0-based index of an element. Returns -1 if the element is not present. Requires V1.3.16+. rb_index(rb_build('{1,2,3}'), 3) 2
rb_and_null2empty RoaringBitmap, RoaringBitmap RoaringBitmap AND operation; treats NULL inputs as empty bitmaps. Requires V1.1.42+. rb_and_null2empty(rb_build(null), rb_build('{3,4,5}')) {}
rb_or_null2empty RoaringBitmap, RoaringBitmap RoaringBitmap OR operation; treats NULL inputs as empty bitmaps. Requires V1.1.42+. rb_or_null2empty(rb_build(null), rb_build('{3,4,5}')) {3,4,5}
rb_andnot_null2empty RoaringBitmap, RoaringBitmap RoaringBitmap ANDNOT operation; treats NULL inputs as empty bitmaps. Requires V1.1.42+. rb_andnot_null2empty(rb_build(null), rb_build('{3,4,5}')) {}
rb_and_null2empty_cardinality RoaringBitmap, RoaringBitmap INTEGER Returns the AND cardinality; treats NULL inputs as empty bitmaps. Requires V1.1.42+. rb_and_null2empty_cardinality(rb_build(null), rb_build('{3,4,5}')) 0
rb_or_null2empty_cardinality RoaringBitmap, RoaringBitmap INTEGER Returns the OR cardinality; treats NULL inputs as empty bitmaps. Requires V1.1.42+. rb_or_null2empty_cardinality(rb_build(null), rb_build('{3,4,5}')) 3
rb_xor_cardinality RoaringBitmap, RoaringBitmap INTEGER Returns the cardinality of the XOR of two roaring bitmaps. rb_xor_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}')) 4
rb_andnot_cardinality RoaringBitmap, RoaringBitmap INTEGER Returns the cardinality of the ANDNOT of two roaring bitmaps. rb_andnot_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}')) 2
rb_andnot_null2empty_cardinality RoaringBitmap, RoaringBitmap INTEGER Returns the ANDNOT cardinality; treats NULL inputs as empty bitmaps. Requires V1.1.42+. rb_andnot_null2empty_cardinality(rb_build(null), rb_build('{3,4,5}')) 0
rb_is_empty RoaringBitmap BOOLEAN Checks whether a roaring bitmap is empty. rb_is_empty(rb_build('{1,2,3,4,5}')) false
rb_fill RoaringBitmap, BIGINT, BIGINT RoaringBitmap Fills offsets in [start, end), excluding the end. Requires V1.3.16+. rb_fill(rb_build('{1,2,3}'), 5, 7) {1,2,3,5,6}
rb_clear RoaringBitmap, BIGINT, BIGINT RoaringBitmap Clears offsets in [start, end), excluding the end. Requires V1.3.16+. rb_clear(rb_build('{1,2,3}'), 2, 3)
rb_contains_bitmap RoaringBitmap, RoaringBitmap BOOLEAN Checks whether the first bitmap contains all elements of the second. rb_contains_bitmap(rb_build('{1,2,3}'), rb_build('{3}')) true
rb_flip RoaringBitmap, INTEGER, INTEGER RoaringBitmap Flips offsets in the specified range. rb_flip(rb_build('{1,2,3}'), 2, 3)
rb_range_cardinality RoaringBitmap, BIGINT, BIGINT BIGINT Returns the cardinality of elements in [start, end), where start is 1-based. Requires V1.3.16+. rb_range_cardinality(rb_build('{1,2,3}'), 2, 3)
rb_rank RoaringBitmap, INTEGER INTEGER Returns the count of elements less than or equal to the specified offset. rb_rank(rb_build('{1,2,3}'), 3) 3
rb_jaccard_dist RoaringBitmap, RoaringBitmap DOUBLE PRECISION Returns the Jaccard distance or the Jaccard similarity coefficient between two roaring bitmaps. Requires V1.3.16+. rb_jaccard_dist(rb_build('{1,2,3}'), rb_build('{3,4}')) 0.75
rb_select RoaringBitmap, bitset_limit BIGINT, bitset_offset BIGINT=0, reverse BOOLEAN=false, range_start BIGINT=-2147483648, range_end BIGINT=2147483647 RoaringBitmap Returns the [bitset_offset, bitset_offset+bitset_limit) subset of the [range_start, range_end) range. rb_select(rb_build('{1,2,3,4,5,6,7,8,9}'), 5, 2)
rb_iterate RoaringBitmap Set of INTEGER Returns each offset in a roaring bitmap as a row. rb_iterate(rb_build('{1,2,3}')) 1, 2, 3

`roaringbitmap_in` example:

-- Create a sample table.
CREATE TABLE rb_text (
    id int,
    a text
);

-- Insert data.
INSERT INTO rb_text
    VALUES (1, '\x3a300000010000000000090010000000010002000300040005000600070008000900c800');

-- Convert to RoaringBitmap and compute AND cardinality.
SELECT
    rb_and_cardinality_agg(roaringbitmap_in(a::cstring))
FROM
    rb_text;

Expected output:

 rb_and_cardinality_agg
------------------------
                     10

Roaring bitmap aggregate functions

Functions supporting both RoaringBitmap and RoaringBitmap64

All examples below use multi-row input to show how aggregation merges multiple bitmaps.

Function Input type Output type Description Example Result
rb_or_agg RoaringBitmap | RoaringBitmap64 Same as input OR aggregate across all input rows. See example below.
rb_and_agg RoaringBitmap | RoaringBitmap64 Same as input AND aggregate across all input rows. See example below.
rb_or_cardinality_agg RoaringBitmap | RoaringBitmap64 INTEGER Returns the cardinality of the OR aggregate. See example below.
rb_and_cardinality_agg RoaringBitmap | RoaringBitmap64 INTEGER Returns the cardinality of the AND aggregate. See example below.
-- OR aggregate: returns the union of all input bitmaps.
SELECT rb_or_agg(bitmap)
FROM (VALUES
    (rb_build('{1,2,3}')),
    (rb_build('{2,3,4}'))
) t(bitmap);
-- Result: {1,2,3,4}

-- AND aggregate: returns the intersection of all input bitmaps.
SELECT rb_and_agg(bitmap)
FROM (VALUES
    (rb_build('{1,2,3}')),
    (rb_build('{2,3,4}'))
) t(bitmap);
-- Result: {2,3}

-- OR cardinality aggregate.
SELECT rb_or_cardinality_agg(bitmap)
FROM (VALUES
    (rb_build('{1,2,3}')),
    (rb_build('{2,3,4}'))
) t(bitmap);
-- Result: 4

-- AND cardinality aggregate.
SELECT rb_and_cardinality_agg(bitmap)
FROM (VALUES
    (rb_build('{1,2,3}')),
    (rb_build('{2,3,4}'))
) t(bitmap);
-- Result: 2

Functions supporting only RoaringBitmap (32-bit)

Function Input type Output type Description Example Result
rb_xor_agg RoaringBitmap RoaringBitmap XOR aggregate across all input rows. See example below.
rb_xor_cardinality_agg RoaringBitmap INTEGER Returns the cardinality of the XOR aggregate. See example below.
-- XOR aggregate: returns elements in exactly one of the two bitmaps.
SELECT rb_xor_agg(bitmap)
FROM (VALUES
    (rb_build('{1,2,3}')),
    (rb_build('{2,3,4}'))
) t(bitmap);
-- Result: {1,4}

-- XOR cardinality aggregate.
SELECT rb_xor_cardinality_agg(bitmap)
FROM (VALUES
    (rb_build('{1,2,3}')),
    (rb_build('{2,3,4}'))
) t(bitmap);
-- Result: 2

Other roaring bitmap functions

The following functions support only the RoaringBitmap (32-bit) type.

Function Input type Output type Description Example Result
roaringbitmap_text TEXT, BOOLEAN RoaringBitmap Deserializes binary RoaringBitmap data from TEXT into a RoaringBitmap structure. The second parameter controls format verification — set it to true to avoid invalid bitmap data. roaringbitmap_text(':0', true)
rb_to_text RoaringBitmap TEXT Converts a RoaringBitmap structure to its binary TEXT representation. rb_to_text(rb_build('{1,2,3}')) \x3a300000...

Examples

The following end-to-end example shows a complete workflow: enable the extension, create a table, insert data, run bitwise operations, and inspect the results.

  1. Enable the extension.

    CREATE EXTENSION roaringbitmap;
  2. Create a table to store roaring bitmap data.

    -- Create table t1.
    CREATE TABLE public.t1 (id integer, bitmap roaringbitmap);
  3. Insert roaring bitmap data.

    -- Build a bitmap from an explicit array.
    INSERT INTO public.t1 SELECT 1, RB_BUILD(ARRAY[1,2,3,4,5,6,7,8,9,200]);
    
    -- Build a bitmap by aggregating a generated series.
    INSERT INTO public.t1 SELECT 2, RB_BUILD_AGG(e) FROM GENERATE_SERIES(1,100) e;
  4. Run bitwise operations.

    -- OR the two bitmaps.
    SELECT RB_OR(a.bitmap, b.bitmap)
    FROM (SELECT bitmap FROM public.t1 WHERE id = 1) AS a,
         (SELECT bitmap FROM public.t1 WHERE id = 2) AS b;

    Expected output (the union of {1..9, 200} and {1..100}):

     rb_or
    -------
     {1,2,3,4,5,6,7,8,9,10,...,100,200}
  5. Run aggregate operations to combine all bitmaps in the table.

    SELECT RB_OR_AGG(bitmap)  FROM public.t1;  -- union of all bitmaps
    SELECT RB_AND_AGG(bitmap) FROM public.t1;  -- intersection of all bitmaps
    SELECT RB_XOR_AGG(bitmap) FROM public.t1;  -- symmetric difference
    SELECT RB_BUILD_AGG(id)   FROM public.t1;  -- build a bitmap from the id column
  6. Calculate cardinality (the count of set bits).

    SELECT RB_CARDINALITY(bitmap) FROM public.t1;

    Expected output:

     id | rb_cardinality
    ----+----------------
      1 |             10
      2 |            100
  7. List all set offsets.

    SELECT RB_ITERATE(bitmap) FROM public.t1 WHERE id = 1;

    Expected output:

     rb_iterate
    ------------
              1
              2
              3
              4
              5
              6
              7
              8
              9
            200
  8. Convert a roaring bitmap to an array.

    SELECT RB_TO_ARRAY(bitmap) FROM public.t1 WHERE id = 1;

    Expected output:

               rb_to_array
    --------------------------------
     {1,2,3,4,5,6,7,8,9,200}