Creating B-tree indexes on large tables can take significant time, blocking maintenance windows and slowing schema migrations. Elastic Parallel Query (EPQ) parallelizes the heap table scan during index creation, reducing build time to approximately one-fifth of the native serial method.
How it works
Without EPQ, PolarDB for PostgreSQL (Compatible with Oracle) scans the heap table sequentially to build index entries, then constructs the B-tree from those entries.
With EPQ enabled, a query coordinator (QC) process distributes the heap scan across parallel workers. Each worker scans a partition of the table and forwards results to the index-creation process, which assembles the final B-tree.
Limitations
| Limitation | Details |
|---|---|
| Column types | EPQ only accelerates index creation on general columns. Expression columns are not supported. |
| Syntax | The CONCURRENTLY and INCLUDE options are not supported with EPQ. |
Parameters
Configure the following parameters before creating indexes with EPQ.
| Parameter | Default | Recommended | Description |
|---|---|---|---|
polar_px_enable_btbuild | off | on | Enables EPQ-accelerated index creation. |
polar_px_dop_per_node | 1 | 8 or 16 | Degree of parallelism (DOP) per node. Also controls DOP for . |
polar_px_enable_replay_wait | (auto) | — | Activates automatically for the current session when polar_px_enable_btbuild=on. Ensures the most recent table entries are indexed. Resets to the database default after index creation completes. |
polar_bt_write_page_buffer_size | 0 | 4096 | Controls write I/O behavior during index creation. Unit: block. Maximum: 8192. Setting this to 4096 reduces index creation time by up to 20%. |
polar_bt_write_page_buffer_size behavior
`0` (default): Flushes index entries to disk block-by-block as each index page fills up.
Non-zero value: Buffers index entries in a kernel buffer of the specified size and flushes them all at once when the buffer is full, reducing frequent I/O scheduling overhead.
Create a B-tree index with EPQ
Prerequisites
Before you begin, ensure that you have:
A PolarDB for PostgreSQL (Compatible with Oracle) cluster
Access to run DDL statements on the target table
Procedure
The following example creates a table named test and builds an index using EPQ.
1. Create the test table.
CREATE TABLE test(id int, id2 int);Verify the table schema:
\d testExpected output:
Table "public.test"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | |
id2 | integer | | |2. Enable EPQ-accelerated index creation.
SET polar_px_enable_btbuild=on;Verify the setting:
SHOW polar_px_enable_btbuild;Expected output:
polar_px_enable_btbuild
-------------------------
on
(1 row)3. Create the index.
Add WITH (px_build=on) to the CREATE INDEX statement to use EPQ for this build:
CREATE INDEX t ON test(id) WITH(px_build=on);polar_px_enable_btbuild=onalone does not activate EPQ for a specific index build. You must includeWITH (px_build=on)in theCREATE INDEXstatement. Without it, PolarDB uses the native (serial) index creation method, and the resulting index shows nopx_buildannotation.
4. Verify the index was created with EPQ.
\d testExpected output:
Table "public.test"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | |
id2 | integer | | |
Indexes:
"t" btree (id) WITH (px_build=finish)The (px_build=finish) field confirms that EPQ was used.
Performance data
EPQ creates indexes for large tables approximately five times faster than the native method.
The following figure compares global index creation performance before and after enabling EPQ, using 130 GB of data.
