All Products
Search
Document Center

PolarDB:Plan acceleration

Last Updated:Aug 25, 2026

This topic describes the PartitionedTableScan feature, including its background, usage, and performance comparison.

Background

PolarDB for PostgreSQL (Compatible with Oracle)has no limit on the number of partitions in a partitioned table. When partitions exceed 2 levels, the number of partitions increases exponentially.

For example, a partitioned table has two levels of partitions. The first level uses hash partitioning with 100 partitions. The second level also uses hash partitioning, with each first-level partition further divided into 100 subpartitions. The entire partitioned table has 10,000 partitions. When you query this partitioned table, the query plan is as follows:

explain analyze select * from part_hash;
                                 QUERY PLAN                                  
-----------------------------------------------------------------------------
 Append  (cost=0.00..344500.00 rows=16300000 width=22)
   ->  Seq Scan on part_hash_sys0102  (cost=0.00..26.30 rows=1630 width=22)
   ->  Seq Scan on part_hash_sys0103  (cost=0.00..26.30 rows=1630 width=22)
   ->  Seq Scan on part_hash_sys0104  (cost=0.00..26.30 rows=1630 width=22)
  ...
  ...
  ...
   ->  Seq Scan on part_hash_sys10198  (cost=0.00..26.30 rows=1630 width=22)
   ->  Seq Scan on part_hash_sys10199  (cost=0.00..26.30 rows=1630 width=22)
   ->  Seq Scan on part_hash_sys10200  (cost=0.00..26.30 rows=1630 width=22)
 Planning Time: 3183.644 ms
 Execution Time: 633.779 ms
(10003 rows)
Total Memory: 216852KB

As shown in the preceding result, the query process is relatively slow. This is because the principle of partitioned tables in the optimizer can be simply understood as follows: first, an optimal plan is generated for each partition, and then the Append operator combines these plans in parallel as the optimal plan for the partitioned table. If the number of partitions is small, this process is fast and imperceptible to users. However, once the number of partitions reaches a certain scale, this process becomes increasingly noticeable, and users may find that queries on partitioned tables are particularly slow compared to regular tables.

In the preceding SQL example, the table part_hash has 10,000 partitions, and its Planning Time can reach about 3 seconds, while the Planning Time for a regular table query only takes 0.1 milliseconds, a difference of hundreds of times. In addition to the Planning Time gap, the memory usage of the query process is also extremely high, which may cause OOM.

This limitation of partitioned tables is more pronounced in join queries:

create table part_hash2 (a int, b int, c varchar(10))
PARTITION by HASH(a) SUBPARTITION by HASH (b) PARTITIONS 100 SUBPARTITIONS 100;

explain analyze select count(*) from part_hash a join part_hash2 b on a.a=b.b where b.c = '0001';
                                                                            QUERY PLAN                                                                             
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Finalize Aggregate  (cost=48970442.90..48970442.91 rows=1 width=8) (actual time=6466.854..6859.935 rows=1 loops=1)
   ->  Gather  (cost=48970442.68..48970442.89 rows=2 width=8) (actual time=397.780..6859.902 rows=3 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Partial Aggregate  (cost=48969442.68..48969442.69 rows=1 width=8) (actual time=4.748..11.768 rows=1 loops=3)
               ->  Merge Join  (cost=1403826.01..42177776.01 rows=2716666667 width=0) (actual time=4.736..11.756 rows=0 loops=3)
                     Merge Cond: (a.a = b.b)
                     ->  Sort  (cost=1093160.93..1110135.93 rows=6790000 width=4) (actual time=4.734..8.588 rows=0 loops=3)
                           Sort Key: a.a
                           Sort Method: quicksort  Memory: 25kB
                           Worker 0:  Sort Method: quicksort  Memory: 25kB
                           Worker 1:  Sort Method: quicksort  Memory: 25kB
                           ->  Parallel Append  (cost=0.00..229832.35 rows=6790000 width=4) (actual time=4.665..8.518 rows=0 loops=3)
                                 ->  Parallel Seq Scan on part_hash_sys0102 a  (cost=0.00..19.59 rows=959 width=4) (actual time=0.001..0.001 rows=0 loops=1)
                                 ->  Parallel Seq Scan on part_hash_sys0103 a_1  (cost=0.00..19.59 rows=959 width=4) (actual time=0.001..0.001 rows=0 loops=1)
                                 ->  Parallel Seq Scan on part_hash_sys0104 a_2  (cost=0.00..19.59 rows=959 width=4) (actual time=0.001..0.001 rows=0 loops=1)
                                  ...
                     ->  Sort  (cost=310665.08..310865.08 rows=80000 width=4) (never executed)
                           Sort Key: b.b
                           ->  Append  (cost=0.00..304150.00 rows=80000 width=4) (never executed)
                                 ->  Seq Scan on part_hash2_sys0102 b  (cost=0.00..30.38 rows=8 width=4) (never executed)
                                       Filter: ((c)::text = '0001'::text)
                                 ->  Seq Scan on part_hash2_sys0103 b_1  (cost=0.00..30.38 rows=8 width=4) (never executed)
                                       Filter: ((c)::text = '0001'::text)
                                 ->  Seq Scan on part_hash2_sys0104 b_2  (cost=0.00..30.38 rows=8 width=4) (never executed)
                                       Filter: ((c)::text = '0001'::text)
                                  ... 

 Planning Time: 221082.616 ms
 Execution Time: 9500.148 ms
(30018 rows)
Total Memory: 679540KB

Therefore, we can see that when performing a full table scan on a partitioned table, because no filter conditions are specified, the query cannot be focused on a specific partition. This means that in full table scan scenarios, all advantages of partitioned tables no longer exist, and they are even less efficient than regular tables. Although partition pruning can focus queries on a small number of partitions, for some OLAP scenarios, a full table scan of the entire partitioned table is required.

Overview

To address this issue, PolarDB for PostgreSQL (Compatible with Oracle)provides the PartitionedTableScan operator. It is a query operator for partitioned tables that is more efficient than Append. It can significantly reduce Planning Time and use less memory, effectively preventing OOM. This operator addresses the slow query performance issue when a partitioned table has too many partitions.

The following shows the Planning Time and memory usage when using the PartitionedTableScan operator to query these two SQL statements.

explain analyze select * from part_hash;
                                 QUERY PLAN                                  
----------------------------------------------------------------------------------------------------------------------------------------------------
PartitionedTableScan on part_hash  (cost=0.00..1.00 rows=1 width=22) (actual time=134.348..134.352 rows=0 loops=1)(Iteration partition number 10000)
   Scan Partitions: part_hash_sys0102, part_hash_sys0103, ...part_hash_sys10198, part_hash_sys10199, part_hash_sys10200
   ->  Seq Scan on part_hash  (cost=0.00..1.00 rows=1 width=22)
 Planning Time: 293.778 ms
 Execution Time: 384.202 ms
(5 rows)
Total Memory: 40276KB

explain analyze select count(*) from part_hash a join part_hash2 b on a.a=b.b where b.c = '0001';
                                                                            QUERY PLAN                                                                             
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate  (cost=2.02..2.03 rows=1 width=8) (actual time=152.322..152.326 rows=1 loops=1)
   ->  Nested Loop  (cost=0.00..2.02 rows=1 width=0) (actual time=152.308..152.311 rows=0 loops=1)
         Join Filter: (a.a = b.b)
         ->  PartitionedTableScan on part_hash a  (cost=0.00..1.00 rows=1 width=4) (actual time=152.305..152.306 rows=0 loops=1)(Iteration partition number 10000)
               Scan Partitions: part_hash_sys0102, part_hash_sys0103,, part_hash_sys10198, part_hash_sys10199, part_hash_sys10200
               ->  Seq Scan on part_hash a  (cost=0.00..1.00 rows=1 width=4)
         ->  PartitionedTableScan on part_hash2 b  (cost=0.00..1.00 rows=1 width=4) (never executed)
               ->  Seq Scan on part_hash2 b  (cost=0.00..1.00 rows=1 width=4)
                     Filter: ((c)::text = '0001'::text)
 Planning Time: 732.952 ms
 Execution Time: 436.927 ms
(11 rows)
Total Memory: 68104KB

As shown in this example, both Planning Time and Total Memory decrease significantly after using the PartitionedTableScan operator. The following table shows the detailed comparison:

Type

Append

PartitionedTableScan

Single Query Planning Time

3183.644ms

293.778ms

Single Total Memory

216852 KB

40276 KB

Join Query Planning Time

221082.616ms

732.952ms

Join Total Memory

679540 KB

68104 KB

Limits

  • PolarDB for PostgreSQL (compatible with Oracle) 1.0 and 2.0 both support the PartitionedTableScan operator, and the minor kernel version must be V1.1.32 or later.

  • PartitionedTableScan currently only supports Select and does not support DML statements.

  • PartitionedTableScan does not support Partition-wise join. If you enable partition-wise join, the PartitionedTableScan plan will not be generated.

Note

PartitionedTable Scan applies only to clusters with a minor kernel version of V1.1.32 or later. For existing clusters on earlier minor kernel versions that need to use this feature, run Contact us to enable it.

Usage notes

To better illustrate the PartitionedTableScan feature, we use the following example.

First, create a partitioned table.

CREATE TABLE prt1 (a int, b int, c varchar) PARTITION BY Hash(a) partitions 16;

Enable PartitionedTableScan through parameters

You can enable or disable PartitionedTableScan by adjusting the polar_num_parts_for_partitionedscan parameter.

Parameter

Value range

Default value

Description

polar_num_parts_for_partitionedscan

-1 to INT_MAX

64

The threshold for the number of subpartitions in a partitioned table under the PartitionedTableScan feature. When the number of subpartitions exceeds the value of polar_num_parts_for_partitionedscan, PartitionedTableScan is automatically enabled.

For example, the default value means that when the number of subpartitions in a partitioned table is greater than 64, PartitionedTableScan is automatically enabled. Otherwise, it is not enabled.

This parameter has the following two special values:

  • When polar_num_parts_for_partitionedscan is set to -1, PartitionedTableScan is enabled regardless of the number of subpartitions.

  • When polar_num_parts_for_partitionedscan is set to 0, PartitionedTableScan is not enabled regardless of the number of subpartitions.

Example:

SET polar_num_parts_for_partitionedscan to -1;
explain select * from prt1;
                           QUERY PLAN                            
-----------------------------------------------------------------
 PartitionedTableScan on prt1  (cost=0.00..1.00 rows=1 width=40)
   ->  Seq Scan on prt1  (cost=0.00..1.00 rows=1 width=40)
(2 rows)

Use HINT

Use the HINT syntax PARTEDSCAN(table alias),Example:

EXPLAIN select /*+PARTEDSCAN(prt1) */ * from prt1;
                           QUERY PLAN                            
-----------------------------------------------------------------
 PartitionedTableScan on prt1  (cost=0.00..1.00 rows=1 width=40)
   ->  Seq Scan on prt1  (cost=0.00..1.00 rows=1 width=40)
(2 rows)

parallel query

PolarDB for PostgreSQL (Compatible with Oracle)supports Parallel query for partitioned tables, which handles large-scale data queries well. Similar to Append, PartitionedTableScan also supports parallel query. However, unlike Append, the parallel query of PartitionedTableScan is called PartitionedTableScan Append, and there are only two parallelism modes: inter-partition parallelism and hybrid parallelism.

image.png

Inter-partition parallelism

Inter-partition parallelism means each worker queries one partition to enable multiple workers to perform parallel query on the entire partitioned table.

EXPLAIN (COSTS OFF) select /*+PARTEDSCAN(prt1) */ * from prt1;
                 QUERY PLAN                  
---------------------------------------------
 Gather
   Workers Planned: 4
   ->  Parallel PartitionedTableScan on prt1
         ->  Seq Scan on prt1
(4 rows)

As shown above, the entire partitioned table starts 4 parallel workers (Workers Planned: 4), and each worker is responsible for querying one partition. The obvious indicator is an operator named Parallel PartitionedTableScan.

Hybrid parallelism

Hybrid parallelism means that both inter-partition and intra-partition operations can be executed in parallel, achieving overall parallel execution of the partitioned table. This is the highest degree of parallelism.

EXPLAIN (COSTS OFF) select /*+PARTEDSCAN(prt1) */ * from prt1;
                 QUERY PLAN                  
---------------------------------------------
 Gather
   Workers Planned: 8
   ->  Parallel PartitionedTableScan on prt1
         ->  Parallel Seq Scan on prt1
(4 rows)

As shown above, the entire query uses 8 workers for parallel query (Workers Planned: 8). Both inter-partition and intra-partition operations can be performed in parallel. The obvious indicator is an operator named Parallel PartitionedTableScan.

Both parallelism modes have their own cost models, and the optimizer selects the optimal one.

Partition pruning

PartitionedTableScan, similar to Append, supports partition pruning in three phases. For details about partition pruning, see Partition pruning.

Performance comparison

PartitionedTableScan is more efficient than Append. The following test data shows the performance comparison between PartitionedTableScan and Append.

Note

The following test data is from a development environment and is not standard benchmark data. Results may vary with different configurations and conditions. The purpose of this test is to compare the performance differences between Append and PartitionedTableScan under the same environment configuration based on the single-variable principle.

The test SQL is as follows:

explain select  * from prt1 where b = 10; 

explain select /*+PARTEDSCAN(prt1) */ * from prt1 where b = 10;  

Planning Time of a single SQL statement

Number of partitions

Append Planning Time

PartitionedTableScan Planning Time

16

0.266ms

0.067ms

32

1.820ms

0.258ms

64

3.654ms

0.402ms

128

7.010ms

0.664ms

256

14.095ms

1.247ms

512

27.697ms

2.328ms

1024

73.176ms

4.165ms

Memory (memory usage of a single SQL statement)

Number of partitions

Append Memory

PartitionedTableScan Memory

16

1,170 KB

1,044 KB

32

1,240 KB

1,044 KB

64

2,120 KB

1,624 KB

128

2,244 KB

1,524 KB

256

2,888 KB

2,072 KB

512

4,720 KB

3,012 KB

1024

8,236 KB

5,280 KB

QPS(Query per Second)

pgbench -i --scale=10
pgbench -c 64 -j 64 -n -T60
Query:
	explain select  * from prt1 where b = 10; 
	explain select /*+PARTEDSCAN(prt1) */ * from prt1 where b = 10;  

Number of partitions

Append QPS

PartitionedTableScan QPS

16

25,318

93,950

32

10,906

61,879

64

5,281

30,839

128

2,195

16,684

256

920

8,372

512

92

3,708

1024

21

1,190

Conclusion

From the comparison between PartitionedTableScan and Append, it can be seen that PartitionedTableScan shows significant performance improvement over Append as the number of partitions increases. Therefore, if your partitioned table has a large number of partitions and the Planning Time is high, we recommend that you use PartitionedTableScan for optimization.