Bloom Index Filters Multi-Column Equality
Create a bloom index over equality-filtered columns, inspect its definition, and see why range predicates cannot use it.
Code
-- bloom 扩展:多列等值过滤的概率索引
-- A bloom index filters multi-column equality predicates
CREATE EXTENSION IF NOT EXISTS bloom;
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL,
status text NOT NULL,
region text NOT NULL,
total numeric(12, 2) NOT NULL
);
CREATE INDEX orders_bloom_idx
ON orders USING bloom (customer_id, status, region)
WITH (length = 80, col1 = 2, col2 = 2, col3 = 2);
-- 多列等值条件可以走 bloom 索引
SELECT id, total
FROM orders
WHERE customer_id = 42 AND status = 'paid' AND region = 'eu';
-- 查看索引定义与参数
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = 'orders';
-- bloom 只支持等值比较;范围条件无法使用
SELECT id FROM orders WHERE customer_id > 10;
-- length 是每个签名的位数,colN 是每列的哈希函数个数
SELECT relname, reloptions
FROM pg_class
WHERE relname = 'orders_bloom_idx';How the snippet works
Several statements share this file — SELECT / CREATE, with orders, pg_indexes and pg_class among the objects involved.
Concretely, the SQL combines: a WHERE clause narrowing which rows one statement affects; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity; a GENERATED ALWAYS/GENERATED BY clause that computes the column value.
Applying it
Run the statement against the orders table.
Statements that matter
CREATE INDEX orders_bloom_idx
ON orders USING bloom (customer_id, status, region)
WITH (length = 80, col1 = 2, col2 = 2, col3 = 2);
SELECT id, total
FROM orders
WHERE customer_id = 42 AND status = 'paid' AND region = 'eu';
SELECT relname, reloptions
FROM pg_class
WHERE relname = 'orders_bloom_idx';
Interactive Tools for This Topic
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.CSV Column AnalyzerProfile every CSV column with inferred types, empty-cell counts, unique values, and numeric min and max ranges.
FAQ
- What relations does the SQL touch?
- `orders`, `pg_indexes`, `pg_class` — referenced directly, no join needed.
Report a problem with this exampleOpens a pre-filled issue on GitHub.