All Products
Search
Document Center

ApsaraDB RDS:Use the roaringbitmap extension

Last Updated:Mar 28, 2026

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

FunctionInputOutputDescriptionExample
rb_buildinteger[]roaringbitmapCreates a bitmap from an integer array.rb_build('{1,2,3,4,5}')
rb_androaringbitmap, roaringbitmaproaringbitmapAND operation.rb_and(rb_build('{1,2,3}'), rb_build('{3,4,5}'))
rb_orroaringbitmap, roaringbitmaproaringbitmapOR operation.rb_or(rb_build('{1,2,3}'), rb_build('{3,4,5}'))
rb_xorroaringbitmap, roaringbitmaproaringbitmapXOR operation.rb_xor(rb_build('{1,2,3}'), rb_build('{3,4,5}'))
rb_andnotroaringbitmap, roaringbitmaproaringbitmapANDNOT operation.rb_andnot(rb_build('{1,2,3}'), rb_build('{3,4,5}'))
rb_cardinalityroaringbitmapintegerReturns the cardinality (count of bits set to 1).rb_cardinality(rb_build('{1,2,3,4,5}'))
rb_and_cardinalityroaringbitmap, roaringbitmapintegerCardinality of an AND operation.rb_and_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}'))
rb_or_cardinalityroaringbitmap, roaringbitmapintegerCardinality of an OR operation.rb_or_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}'))
rb_xor_cardinalityroaringbitmap, roaringbitmapintegerCardinality of an XOR operation.rb_xor_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}'))
rb_andnot_cardinalityroaringbitmap, roaringbitmapintegerCardinality of an ANDNOT operation.rb_andnot_cardinality(rb_build('{1,2,3}'), rb_build('{3,4,5}'))
rb_is_emptyroaringbitmapbooleanReturns true if the bitmap is empty.rb_is_empty(rb_build('{1,2,3,4,5}'))
rb_equalsroaringbitmap, roaringbitmapbooleanReturns true if two bitmaps are identical.rb_equals(rb_build('{1,2,3}'), rb_build('{3,4,5}'))
rb_intersectroaringbitmap, roaringbitmapbooleanReturns true if two bitmaps share at least one common element.rb_intersect(rb_build('{1,2,3}'), rb_build('{3,4,5}'))
rb_removeroaringbitmap, integerroaringbitmapRemoves a specific offset from the bitmap.rb_remove(rb_build('{1,2,3}'), 3)
rb_fliproaringbitmap, integer, integerroaringbitmapFlips bits within a specified offset range.rb_flip(rb_build('{1,2,3}'), 2, 3)
rb_minimumroaringbitmapintegerReturns the smallest offset. Returns -1 if the bitmap is empty.rb_minimum(rb_build('{1,2,3}'))
rb_maximumroaringbitmapintegerReturns the largest offset. Returns 0 if the bitmap is empty.rb_maximum(rb_build('{1,2,3}'))
rb_rankroaringbitmap, integerintegerReturns the count of elements less than or equal to a specified offset.rb_rank(rb_build('{1,2,3}'), 3)
rb_iterateroaringbitmapsetof integerReturns all offsets in the bitmap as a set.rb_iterate(rb_build('{1,2,3}'))

Bitmap aggregate functions

FunctionInputOutputDescriptionExample
rb_build_aggintegerroaringbitmapAggregates a set of integers into a bitmap.rb_build_agg(1)
rb_or_aggroaringbitmaproaringbitmapOR aggregate operation.rb_or_agg(rb_build('{1,2,3}'))
rb_and_aggroaringbitmaproaringbitmapAND aggregate operation.rb_and_agg(rb_build('{1,2,3}'))
rb_xor_aggroaringbitmaproaringbitmapXOR aggregate operation.rb_xor_agg(rb_build('{1,2,3}'))
rb_or_cardinality_aggroaringbitmapintegerCardinality from an OR aggregate operation.rb_or_cardinality_agg(rb_build('{1,2,3}'))
rb_and_cardinality_aggroaringbitmapintegerCardinality from an AND aggregate operation.rb_and_cardinality_agg(rb_build('{1,2,3}'))
rb_xor_cardinality_aggroaringbitmapintegerCardinality from an XOR aggregate operation.rb_xor_cardinality_agg(rb_build('{1,2,3}'))