In PostgreSQL, partitioned tables are an effective way to handle continuously growing data, and partition pruning speeds up queries.PolarDB for PostgreSQL (Compatible with Oracle) columnstore indexes also support partitioned tables and further address their statistics and analysis needs.
Background
As business systems keep running, large amounts of historical data accumulate and tables grow larger. A common practice is to partition data by dimensions such as time or user_id so that each partition holds only a subset of the data. When you query the data, native PostgreSQL also uses partition pruning to avoid reading unrelated data.
PolarDB for PostgreSQL (Compatible with Oracle) columnstore indexes also accelerate analysis on partitioned tables. You use them the same way that you use existing partitioned table indexes.
Results
At a degree of parallelism of 4, the columnstore index is more than 35 times faster than native PostgreSQL parallel execution for all three queries.
|
Query |
Native PostgreSQL parallel execution |
Columnstore index |
|
Q1 |
2.13 s |
0.05 s |
|
Q2 |
6.42 s |
0.18 s |
|
Q3 |
10.51 s |
0.30 s |
Procedure
Step 1: Prepare the environment
-
Verify that your cluster version and configuration meet the following requirements:
-
Cluster versions:
Oracle syntax compatibility 2.0 (minor engine version 2.0.14.10.20.0 or later)
NoteYou can view the minor engine version in the console or by running the
SHOW polardb_version;statement. If the minor engine version does not meet the requirements, upgrade the minor engine version。 -
The
wal_levelparameter must be set tological. This setting adds the required information for logical decoding to the write-ahead logging (WAL).NoteYou can set the wal_level parameter in the console. Modifying this parameter restarts the cluster. Plan your business operations accordingly and proceed with caution.
-
The source table must have a primary key, and the primary key column must be included when you create the columnstore index. Using a
SERIALorBIGSERIALdata type for the primary key is recommended, as it significantly improves data synchronization efficiency. -
You can create only one columnstore index for each table.
-
-
Enable the columnstore index feature.
The method for enabling IMCI varies depending on the minor engine version of your PolarDB for PostgreSQL (Compatible with Oracle) cluster:
Step 2: Prepare the data
This example creates a multi-level partitioned table, inserts 320 million rows of simulated data (about 16 GB), and then runs statistical analysis based on partition conditions.
The test partitioned table has the following schema:
-
sales: the primary table. -
sales_2023: partitioned by year.-
sales_2023_a: partitioned by month; months 1 through 6 are defined as partition a. -
sales_2023_b: partitioned by month; months 7 through 12 are defined as partition b.
-
-
sales_2024: partitioned by year.-
sales_2024_a: partitioned by month; months 1 through 6 are defined as partition a. -
sales_2024_b: partitioned by month; months 7 through 12 are defined as partition b.
-
-
Create a multi-level partitioned table named
sales, with the time columnsale_dateas the partition key. The definition is as follows:CREATE TABLE sales ( sale_id serial, product_id int NOT NULL, sale_date date NOT NULL, amount numeric(10,2) NOT NULL, primary key(sale_id, sale_date) ) PARTITION BY RANGE (sale_date); CREATE TABLE sales_2023 PARTITION OF sales FOR VALUES FROM ('2023-1-1') TO ('2024-1-1') PARTITION BY RANGE (sale_date); CREATE TABLE sales_2023_a PARTITION OF sales_2023 FOR VALUES FROM ('2023-1-1') TO ('2023-7-1'); CREATE TABLE sales_2023_b PARTITION OF sales_2023 FOR VALUES FROM ('2023-7-1') TO ('2024-1-1'); CREATE TABLE sales_2024 PARTITION OF sales FOR VALUES FROM ('2024-1-1') TO ('2025-1-1') PARTITION BY RANGE (sale_date); CREATE TABLE sales_2024_a PARTITION OF sales_2024 FOR VALUES FROM ('2024-1-1') TO ('2024-7-1'); CREATE TABLE sales_2024_b PARTITION OF sales_2024 FOR VALUES FROM ('2024-7-1') TO ('2025-1-1'); -
Generate the data and insert it into the partitioned table (about 16 GB).
INSERT INTO sales (product_id, sale_date, amount) SELECT (random()*100)::int AS product_id, '2023-01-1'::date + i/3200000*7 AS sale_date, (random()*1000)::numeric(10,2) AS amount FROM generate_series(1, 320000000) i; -
Create a columnstore index on the table and add the
sale_id,product_id,sale_date, andamountcolumns to the index.CREATE INDEX ON sales USING CSI(sale_id, product_id, sale_date, amount);
Step 3: Run the queries
Run queries by using different execution engines. Three queries (Q1, Q2, and Q3) are generated based on different partition conditions.
-
Use the columnstore index.
--- Enable the columnstore index and set the query degree of parallelism to 4 SET polar_csi.enable_query to on; // Later kernel versions SET polar_csi.max_parallel_workers to 4; // Earlier kernel versions SET polar_csi.exec_parallel to 4; --- Q1 EXPLAIN ANALYZE SELECT sale_date, COUNT(*) FROM sales WHERE sale_date BETWEEN '2023-1-1' and '2023-3-1' AND amount > 100 GROUP BY sale_date; --- Q2 EXPLAIN ANALYZE SELECT sale_date, COUNT(*) FROM sales WHERE sale_date BETWEEN '2023-1-1' and '2023-9-1' AND amount > 100 GROUP BY sale_date; --- Q3 EXPLAIN ANALYZE SELECT sale_date, COUNT(*) FROM sales WHERE sale_date BETWEEN '2023-1-1' and '2024-3-1' AND amount > 100 GROUP BY sale_date;Notepolar_csi.max_parallel_workerswas namedpolar_csi.exec_parallelin early engine versions. For older engine versions that do not supportpolar_csi.max_parallel_workers, usepolar_csi.exec_parallel.Oracle syntax compatibility 2.0:
For version 2.0.14.20.42.0 and earlier, use
polar_csi.exec_parallel.For version 2.0.14.20.43.0 and later, use
polar_csi.max_parallel_workers.
-
Disable the columnstore index and use the row store engine.
--- Disable the columnstore index, use the row store engine, and set the query degree of parallelism to 4 SET polar_csi.enable_query to off; SET max_parallel_workers_per_gather to 4; --- Q1 EXPLAIN ANALYZE SELECT sale_date, COUNT(*) FROM sales WHERE sale_date BETWEEN '2023-1-1' and '2023-3-1' AND amount > 100 GROUP BY sale_date; --- Q2 EXPLAIN ANALYZE SELECT sale_date, COUNT(*) FROM sales WHERE sale_date BETWEEN '2023-1-1' and '2023-9-1' AND amount > 100 GROUP BY sale_date; --- Q3 EXPLAIN ANALYZE SELECT sale_date, COUNT(*) FROM sales WHERE sale_date BETWEEN '2023-1-1' and '2024-3-1' AND amount > 100 GROUP BY sale_date;