Updated on 2024-06-07 GMT+08:00

Dynamic PBE Pruning

Dynamic PBE pruning is supported in the following scenarios:
  1. Supported partitioning levels: level-1 partition and level-2 partition
  2. Supported partitioning types: range partitioning, interval partitioning, hash partitioning, and list partitioning.
  3. Supported expression types: comparison expression (<, <=, =, >=, >), logical expression, and array expression.
  4. Supported conversions and functions: some type conversions and the IMMUTABLE function.
  • Dynamic PBE pruning supports expressions, implicit conversions, the IMMUTABLE function, and the STABLE function, but does not support subquery expressions or VOLATILE function. For the STABLE function, type conversion functions such as to_timestamp may be affected by GUC parameters and lead to different pruning results. To ensure performance optimization, you can analyze table to regenerate a Gplan.
  • Dynamic PBE pruning is based on the generic plan. Therefore, when determining whether a statement can be dynamically pruned, you need to set plan_cache_mode to 'force_generic_plan' to eliminate the interference of the custom plan.
  • When dynamic pruning is used, the sparsely partitioned index plan cannot be generated because the partition to be actually executed in the plan generation phase has not been determined.
  • Typical scenarios where dynamic PBE pruning is supported are as follows:
    1. Comparison expressions
      gaussdb=# 
      -- Create a partitioned table.
      CREATE TABLE t1 (c1 int, c2 int)
      PARTITION BY RANGE (c1)
      (
          PARTITION p1 VALUES LESS THAN(10),
          PARTITION p2 VALUES LESS THAN(20),
          PARTITION p3 VALUES LESS THAN(MAXVALUE)
      );
      -- Set parameters.
      gaussdb=# set plan_cache_mode = 'force_generic_plan';
      
      gaussdb=# PREPARE p1(int) AS SELECT * FROM t1 WHERE c1 = $1;
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p1(1);
                     QUERY PLAN                
      -----------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 = $1)
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
      
      gaussdb=# PREPARE p2(int) AS SELECT * FROM t1 WHERE c1 < $1;
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p2(1);
                     QUERY PLAN                
      -----------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 < $1)
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
      
      gaussdb=# PREPARE p3(int) AS SELECT * FROM t1 WHERE c1 > $1;
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p3(1);
                     QUERY PLAN                
      -----------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 > $1)
               Selected Partitions:  1..3 (pbe-pruning)
      (7 rows)
    2. Logical expressions
      gaussdb=# PREPARE p5(INT, INT) AS SELECT * FROM t1 WHERE c1 = $1 AND c2 = $2;
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p5(1, 2);
                         QUERY PLAN                    
      -------------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: ((t1.c1 = $1) AND (t1.c2 = $2))
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
      
      gaussdb=# PREPARE p6(INT, INT) AS SELECT * FROM t1 WHERE c1 = $1 OR c2 = $2;
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p6(1, 2);
                         QUERY PLAN                   
      ------------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: ((t1.c1 = $1) OR (t1.c2 = $2))
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
      gaussdb=# PREPARE p7(INT) AS SELECT * FROM t1 WHERE NOT c1 = $1;
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) execute p7(1);
                     QUERY PLAN                
      -----------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 <> $1)
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
    3. Array expressions
      gaussdb=# PREPARE p8(INT, INT, INT) AS SELECT * FROM t1 WHERE c1 IN ($1, $2, $3);
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p8(1, 2, 3);
                          QUERY PLAN                     
      ---------------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 = ANY (ARRAY[$1, $2, $3]))
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
      gaussdb=# PREPARE p9(INT, INT, INT) AS SELECT * FROM t1 WHERE c1 NOT IN ($1, $2, $3);
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p9(1, 2, 3);
                           QUERY PLAN                     
      ----------------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 <> ALL (ARRAY[$1, $2, $3]))
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
      gaussdb=# PREPARE p10(INT, INT, INT) AS SELECT * FROM t1 WHERE c1 = ALL(ARRAY[$1, $2, $3]);
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p10(1, 2, 3);
                          QUERY PLAN                     
      ---------------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 = ALL (ARRAY[$1, $2, $3]))
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
      gaussdb=# PREPARE p11(INT, INT, INT) AS SELECT * FROM t1 WHERE c1 = ANY(ARRAY[$1, $2, $3]);
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p11(1, 2, 3);
                          QUERY PLAN                     
      ---------------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 = ANY (ARRAY[$1, $2, $3]))
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
      gaussdb=# PREPARE p12(INT, INT, INT) AS SELECT * FROM t1 WHERE c1 = SOME(ARRAY[$1, $2, $3]);
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p12(1, 2, 3);
                          QUERY PLAN                     
      ---------------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 = ANY (ARRAY[$1, $2, $3]))
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
    4. Implicit type conversion
      gaussdb=# set plan_cache_mode = 'force_generic_plan';
      gaussdb=# PREPARE p13(TEXT) AS SELECT * FROM t1 WHERE c1 = $1;
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p13('12');
                          QUERY PLAN
      --------------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 = ($1)::bigint)
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
    5. IMMUTABLE function
      gaussdb=# PREPARE p14(TEXT) AS SELECT * FROM t1 WHERE c1 = LENGTHB($1);
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p14('hello');
                          QUERY PLAN
      --------------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: PART
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: (t1.c1 = lengthb($1))
               Selected Partitions:  1 (pbe-pruning)
      (7 rows)
  • Typical scenarios where dynamic PBE pruning is not supported are as follows:
    1. Subquery expressions
      gaussdb=# PREPARE p15(INT) AS SELECT * FROM t1 WHERE c1 = ALL(SELECT c2 FROM t1 WHERE c1 > $1);
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p15(1);
                               QUERY PLAN                          
      -------------------------------------------------------------
       Partition Iterator
         Output: public.t1.c1, public.t1.c2
         Iterations: 3
         ->  Partitioned Seq Scan on public.t1
               Output: public.t1.c1, public.t1.c2
               Filter: (SubPlan 1)
               Selected Partitions:  1..3
      (7 rows)
    2. Implicit type conversion failure
      gaussdb=# PREPARE p16(name) AS SELECT * FROM t1 WHERE c1 = $1;
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p16('12');
                        QUERY PLAN
      ----------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: 3
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: ((t1.c1)::text = ($1)::text)
               Selected Partitions:  1..3
      (7 rows)
    3. STABLE and VOLATILE functions
      gaussdb=# create sequence seq;
      gaussdb=# PREPARE p17(TEXT) AS SELECT * FROM t1 WHERE c1 = currval($1);-- The VOLATILE function does not support pruning.
      PREPARE
      gaussdb=# EXPLAIN (VERBOSE ON, COSTS OFF) EXECUTE p17('seq');
                                QUERY PLAN
      --------------------------------------------------------------
       Partition Iterator
         Output: c1, c2
         Iterations: 3
         ->  Partitioned Seq Scan on public.t1
               Output: c1, c2
               Filter: ((t1.c1)::numeric = currval(($1)::regclass))
               Selected Partitions:  1..3
      (7 rows)
      
      gaussdb=# DROP TABLE t1;