User Avatar Vitaliy Kukharik 1 month ago

does number of partitions affect planning time in pg16?

Postgres.AI Assistant avatar Postgres.AI 1 month ago | prev | next | oai/gpt-4o

Yes, the number of partitions can affect planning time in PostgreSQL 16. According to the PostgreSQL 16 documentation, having too many partitions can lead to longer query planning times and higher memory consumption during both query planning and execution. The query planner is generally able to handle partition hierarchies with up to a few thousand partitions fairly well, provided that typical queries allow the query planner to prune all but a small number of partitions. However, planning times become longer and memory consumption becomes higher when more partitions remain after the planner performs partition pruning.

Here are some key points from the documentation:

  • Query Planning Overhead: The query planner can handle up to a few thousand partitions well, but planning times increase and memory consumption grows when more partitions remain after pruning.

  • Memory Consumption: Each partition requires its metadata to be loaded into the local memory of each session that touches it, which can significantly increase memory consumption over time.

  • Workload Type: For data warehouse workloads, a larger number of partitions might be acceptable since query planning time is less of a concern compared to query execution time. For OLTP workloads, fewer partitions might be more efficient.

For more detailed information, you can refer to the PostgreSQL 16 documentation on partitioning.

User Avatar Vitaliy Kukharik 1 month ago | prev | next

Let's test in DBLab for PG16.

Steps:

  1. Create a partitioned table:

    create table partitioned_table (
        id int8 primary key,
        data text
    ) partition by range (id);  

  2. For a simple query, measure the planning time, buffer hits and reads used for planning:

    explain (analyze, buffers) select * from partitioned_table where id = 1;

  3. Then, using a loop in a DO block with PL/pgSQL, partitions were added to the table, with each partition containing 999 rows:

    do $do$    
    declare    
        i int;    
    begin    
        for i in 0..99 loop    
            execute format(  
                $sql$     
                    create table partitioned_table_%s     
                    partition of partitioned_table     
                    for values from (%s) to (%s)     
                $sql$,     
                i,     
                i * 9999 + 1,     
                (i + 1) * 9999 + 1     
            );    
    
            execute format(  
                $sql$     
                    insert into partitioned_table_%s     
                    select generate_series(%s, %s) as id, 'data'     
                $sql$,     
                i,     
                i * 9999 + 1,     
                (i + 1) * 9999     
            );    
    
            execute format(  
                $sql$     
                    create index on partitioned_table_%s (id)     
                $sql$,     
                i     
            );    
        end loop;    
    end $do$;

  4. Run vacuum analyze on the partitioned table.

  5. Repeat the explain (analyze, buffers as we did for 0 partitions. This time, looking at the plain returned by explain, ensure that partition pruning is working and we have Index Scan. If not, stop and discuss with me.

  6. Double-check the current number of partitions, using pg_inherits. If number doesn't match expectations, stop and discuss with me.

  7. Iterate, repeating steps 3, 4, and 5 – we need data points for 0, 100, 200, ..., 1000 partitions.

  8. In the end, print a table with 3 metrics.

    • planning buffer hits+reads (also show sum of them),

    • planning time,

    • execution time.

Additional instructions:

  • do not ignore any errors, they need to be fixed

  • When you use SQL or PLpgSQL, print all queries as is.

  • And run each query separately, one SQL query or DO block per call.

Postgres.AI Assistant avatar Postgres.AI 1 month ago | prev | next | oai/gpt-4o

The partitioned table has been created successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's measure the planning time, buffer hits, and reads used for planning with a simple query.

Step 2: Measure Planning Time, Buffer Hits, and Reads

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The initial query execution results are as follows:

Result  (cost=0.00..0.00 rows=0 width=0) (actual time=0.001..0.001 rows=0 loops=1)
  One-Time Filter: false
Planning:
  Buffers: shared hit=51 read=1
  I/O Timings: shared read=0.038
Planning Time: 0.254 ms
Execution Time: 0.085 ms

Clone ID: cru36sf9m20c73be2cog

Next, let's add partitions to the table using a loop in a DO block with PL/pgSQL.

Step 3: Add Partitions

do $do$    
declare    
    i int;    
begin    
    for i in 0..99 loop    
        execute format(  
            $sql$     
                create table partitioned_table_%s     
                partition of partitioned_table     
                for values from (%s) to (%s)     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999 + 1     
        );    

        execute format(  
            $sql$     
                insert into partitioned_table_%s     
                select generate_series(%s, %s) as id, 'data'     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999     
        );    

        execute format(  
            $sql$     
                create index on partitioned_table_%s (id)     
            $sql$,     
            i     
        );    
    end loop;    
end $do$;

I'll execute this block now.The partitions have been successfully added to the table.

Clone ID: cru36sf9m20c73be2cog

Next, let's run vacuum analyze on the partitioned table.

Step 4: Run vacuum analyze

vacuum analyze partitioned_table;

I'll execute this query now.The vacuum analyze operation has been completed successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's repeat the explain (analyze, buffers) query to ensure that partition pruning is working and we have an Index Scan.

Step 5: Measure Planning Time, Buffer Hits, and Reads Again

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The query execution results after adding partitions are as follows:

Index Scan using partitioned_table_0_id_idx on partitioned_table_0 partitioned_table  (cost=0.29..8.30 rows=1 width=13) (actual time=0.331..0.333 rows=1 loops=1)
  Index Cond: (id = 1)
  Buffers: shared hit=1 read=2
  I/O Timings: shared read=0.302
Planning:
  Buffers: shared hit=451
Planning Time: 1.937 ms
Execution Time: 0.433 ms

Clone ID: cru36sf9m20c73be2cog

Partition pruning is working, and we have an Index Scan. Next, let's double-check the current number of partitions using pg_inherits.

Step 6: Check Number of Partitions

select count(*) from pg_inherits where inhparent = 'partitioned_table'::regclass;

I'll execute this query now.The current number of partitions is 100, which matches our expectations.

Clone ID: cru36sf9m20c73be2cog

Next, we will iterate and repeat steps 3, 4, and 5 to gather data points for 0, 100, 200, ..., 1000 partitions. Let's proceed with adding another 100 partitions.

Step 3: Add More Partitions

do $do$    
declare    
    i int;    
begin    
    for i in 100..199 loop    
        execute format(  
            $sql$     
                create table partitioned_table_%s     
                partition of partitioned_table     
                for values from (%s) to (%s)     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999 + 1     
        );    

        execute format(  
            $sql$     
                insert into partitioned_table_%s     
                select generate_series(%s, %s) as id, 'data'     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999     
        );    

        execute format(  
            $sql$     
                create index on partitioned_table_%s (id)     
            $sql$,     
            i     
        );    
    end loop;    
end $do$;

I'll execute this block now.Another 100 partitions have been successfully added to the table.

Clone ID: cru36sf9m20c73be2cog

Next, let's run vacuum analyze on the partitioned table again.

Step 4: Run vacuum analyze

vacuum analyze partitioned_table;

I'll execute this query now.The vacuum analyze operation has been completed successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's repeat the explain (analyze, buffers) query to measure the planning time, buffer hits, and reads again.

Step 5: Measure Planning Time, Buffer Hits, and Reads Again

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The query execution results after adding another 100 partitions are as follows:

Index Scan using partitioned_table_0_id_idx on partitioned_table_0 partitioned_table  (cost=0.29..8.30 rows=1 width=13) (actual time=0.589..0.591 rows=1 loops=1)
  Index Cond: (id = 1)
  Buffers: shared read=3
  I/O Timings: shared read=0.558
Planning:
  Buffers: shared hit=749 read=3
  I/O Timings: shared read=0.088
Planning Time: 2.991 ms
Execution Time: 0.699 ms

Clone ID: cru36sf9m20c73be2cog

Next, let's double-check the current number of partitions using pg_inherits.

Step 6: Check Number of Partitions

select count(*) from pg_inherits where inhparent = 'partitioned_table'::regclass;

I'll execute this query now.The current number of partitions is 200, which matches our expectations.

Clone ID: cru36sf9m20c73be2cog

We will continue this process iteratively to gather data points for 0, 100, 200, ..., 1000 partitions. Let's proceed with adding another 100 partitions.

Step 3: Add More Partitions

do $do$    
declare    
    i int;    
begin    
    for i in 200..299 loop    
        execute format(  
            $sql$     
                create table partitioned_table_%s     
                partition of partitioned_table     
                for values from (%s) to (%s)     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999 + 1     
        );    

        execute format(  
            $sql$     
                insert into partitioned_table_%s     
                select generate_series(%s, %s) as id, 'data'     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999     
        );    

        execute format(  
            $sql$     
                create index on partitioned_table_%s (id)     
            $sql$,     
            i     
        );    
    end loop;    
end $do$;

I'll execute this block now.Another 100 partitions have been successfully added to the table.

Clone ID: cru36sf9m20c73be2cog

Next, let's run vacuum analyze on the partitioned table again.

Step 4: Run vacuum analyze

vacuum analyze partitioned_table;

I'll execute this query now.The vacuum analyze operation has been completed successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's repeat the explain (analyze, buffers) query to measure the planning time, buffer hits, and reads again.

Step 5: Measure Planning Time, Buffer Hits, and Reads Again

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The query execution results after adding another 100 partitions are as follows:

Index Scan using partitioned_table_0_id_idx on partitioned_table_0 partitioned_table  (cost=0.29..8.30 rows=1 width=13) (actual time=0.678..0.680 rows=1 loops=1)
  Index Cond: (id = 1)
  Buffers: shared read=3
  I/O Timings: shared read=0.653
Planning:
  Buffers: shared hit=1046 read=9
  I/O Timings: shared read=1.221
Planning Time: 5.378 ms
Execution Time: 0.781 ms

Clone ID: cru36sf9m20c73be2cog

Next, let's double-check the current number of partitions using pg_inherits.

Step 6: Check Number of Partitions

select count(*) from pg_inherits where inhparent = 'partitioned_table'::regclass;

I'll execute this query now.The current number of partitions is 300, which matches our expectations.

Clone ID: cru36sf9m20c73be2cog

We will continue this process iteratively to gather data points for 0, 100, 200, ..., 1000 partitions. Let's proceed with adding another 100 partitions.

Step 3: Add More Partitions

do $do$    
declare    
    i int;    
begin    
    for i in 300..399 loop    
        execute format(  
            $sql$     
                create table partitioned_table_%s     
                partition of partitioned_table     
                for values from (%s) to (%s)     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999 + 1     
        );    

        execute format(  
            $sql$     
                insert into partitioned_table_%s     
                select generate_series(%s, %s) as id, 'data'     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999     
        );    

        execute format(  
            $sql$     
                create index on partitioned_table_%s (id)     
            $sql$,     
            i     
        );    
    end loop;    
end $do$;

I'll execute this block now.Another 100 partitions have been successfully added to the table.

Clone ID: cru36sf9m20c73be2cog

Next, let's run vacuum analyze on the partitioned table again.

Step 4: Run vacuum analyze

vacuum analyze partitioned_table;

I'll execute this query now.The vacuum analyze operation has been completed successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's repeat the explain (analyze, buffers) query to measure the planning time, buffer hits, and reads again.

Step 5: Measure Planning Time, Buffer Hits, and Reads Again

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The query execution results after adding another 100 partitions are as follows:

Index Scan using partitioned_table_0_id_idx on partitioned_table_0 partitioned_table  (cost=0.29..8.30 rows=1 width=13) (actual time=0.269..0.270 rows=1 loops=1)
  Index Cond: (id = 1)
  Buffers: shared read=3
  I/O Timings: shared read=0.250
Planning:
  Buffers: shared hit=1349 read=5
  I/O Timings: shared read=0.690
Planning Time: 5.662 ms
Execution Time: 0.378 ms

Clone ID: cru36sf9m20c73be2cog

Next, let's double-check the current number of partitions using pg_inherits.

Step 6: Check Number of Partitions

select count(*) from pg_inherits where inhparent = 'partitioned_table'::regclass;

I'll execute this query now.The current number of partitions is 400, which matches our expectations.

Clone ID: cru36sf9m20c73be2cog

We will continue this process iteratively to gather data points for 0, 100, 200, ..., 1000 partitions. Let's proceed with adding another 100 partitions.

Step 3: Add More Partitions

do $do$    
declare    
    i int;    
begin    
    for i in 400..499 loop    
        execute format(  
            $sql$     
                create table partitioned_table_%s     
                partition of partitioned_table     
                for values from (%s) to (%s)     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999 + 1     
        );    

        execute format(  
            $sql$     
                insert into partitioned_table_%s     
                select generate_series(%s, %s) as id, 'data'     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999     
        );    

        execute format(  
            $sql$     
                create index on partitioned_table_%s (id)     
            $sql$,     
            i     
        );    
    end loop;    
end $do$;

I'll execute this block now.Another 100 partitions have been successfully added to the table.

Clone ID: cru36sf9m20c73be2cog

Next, let's run vacuum analyze on the partitioned table again.

Step 4: Run vacuum analyze

vacuum analyze partitioned_table;

I'll execute this query now.The vacuum analyze operation has been completed successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's repeat the explain (analyze, buffers) query to measure the planning time, buffer hits, and reads again.

Step 5: Measure Planning Time, Buffer Hits, and Reads Again

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The query execution results after adding another 100 partitions are as follows:

Index Scan using partitioned_table_0_id_idx on partitioned_table_0 partitioned_table  (cost=0.29..8.30 rows=1 width=13) (actual time=0.329..0.331 rows=1 loops=1)
  Index Cond: (id = 1)
  Buffers: shared read=3
  I/O Timings: shared read=0.300
Planning:
  Buffers: shared hit=1648 read=7
  I/O Timings: shared read=0.837
Planning Time: 7.399 ms
Execution Time: 0.443 ms

Clone ID: cru36sf9m20c73be2cog

Next, let's double-check the current number of partitions using pg_inherits.

Step 6: Check Number of Partitions

select count(*) from pg_inherits where inhparent = 'partitioned_table'::regclass;

I'll execute this query now.The current number of partitions is 500, which matches our expectations.

Clone ID: cru36sf9m20c73be2cog

We will continue this process iteratively to gather data points for 0, 100, 200, ..., 1000 partitions. Let's proceed with adding another 100 partitions.

Step 3: Add More Partitions

do $do$    
declare    
    i int;    
begin    
    for i in 500..599 loop    
        execute format(  
            $sql$     
                create table partitioned_table_%s     
                partition of partitioned_table     
                for values from (%s) to (%s)     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999 + 1     
        );    

        execute format(  
            $sql$     
                insert into partitioned_table_%s     
                select generate_series(%s, %s) as id, 'data'     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999     
        );    

        execute format(  
            $sql$     
                create index on partitioned_table_%s (id)     
            $sql$,     
            i     
        );    
    end loop;    
end $do$;

I'll execute this block now.Another 100 partitions have been successfully added to the table.

Clone ID: cru36sf9m20c73be2cog

Next, let's run vacuum analyze on the partitioned table again.

Step 4: Run vacuum analyze

vacuum analyze partitioned_table;

I'll execute this query now.The vacuum analyze operation has been completed successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's repeat the explain (analyze, buffers) query to measure the planning time, buffer hits, and reads again.

Step 5: Measure Planning Time, Buffer Hits, and Reads Again

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The query execution results after adding another 100 partitions are as follows:

Index Scan using partitioned_table_0_id_idx on partitioned_table_0 partitioned_table  (cost=0.29..8.30 rows=1 width=13) (actual time=0.257..0.259 rows=1 loops=1)
  Index Cond: (id = 1)
  Buffers: shared read=3
  I/O Timings: shared read=0.238
Planning:
  Buffers: shared hit=1949 read=7
  I/O Timings: shared read=0.830
Planning Time: 7.705 ms
Execution Time: 0.356 ms

Clone ID: cru36sf9m20c73be2cog

Next, let's double-check the current number of partitions using pg_inherits.

Step 6: Check Number of Partitions

select count(*) from pg_inherits where inhparent = 'partitioned_table'::regclass;

I'll execute this query now.The current number of partitions is 600, which matches our expectations.

Clone ID: cru36sf9m20c73be2cog

We will continue this process iteratively to gather data points for 0, 100, 200, ..., 1000 partitions. Let's proceed with adding another 100 partitions.

Step 3: Add More Partitions

do $do$    
declare    
    i int;    
begin    
    for i in 600..699 loop    
        execute format(  
            $sql$     
                create table partitioned_table_%s     
                partition of partitioned_table     
                for values from (%s) to (%s)     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999 + 1     
        );    

        execute format(  
            $sql$     
                insert into partitioned_table_%s     
                select generate_series(%s, %s) as id, 'data'     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999     
        );    

        execute format(  
            $sql$     
                create index on partitioned_table_%s (id)     
            $sql$,     
            i     
        );    
    end loop;    
end $do$;

I'll execute this block now.Another 100 partitions have been successfully added to the table.

Clone ID: cru36sf9m20c73be2cog

Next, let's run vacuum analyze on the partitioned table again.

Step 4: Run vacuum analyze

vacuum analyze partitioned_table;

I'll execute this query now.The vacuum analyze operation has been completed successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's repeat the explain (analyze, buffers) query to measure the planning time, buffer hits, and reads again.

Step 5: Measure Planning Time, Buffer Hits, and Reads Again

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The query execution results after adding another 100 partitions are as follows:

Index Scan using partitioned_table_0_id_idx on partitioned_table_0 partitioned_table  (cost=0.29..8.30 rows=1 width=13) (actual time=0.265..0.267 rows=1 loops=1)
  Index Cond: (id = 1)
  Buffers: shared read=3
  I/O Timings: shared read=0.243
Planning:
  Buffers: shared hit=2249 read=9
  I/O Timings: shared read=1.751
Planning Time: 10.177 ms
Execution Time: 0.368 ms

Clone ID: cru36sf9m20c73be2cog

Next, let's double-check the current number of partitions using pg_inherits.

Step 6: Check Number of Partitions

select count(*) from pg_inherits where inhparent = 'partitioned_table'::regclass;

I'll execute this query now.The current number of partitions is 700, which matches our expectations.

Clone ID: cru36sf9m20c73be2cog

We will continue this process iteratively to gather data points for 0, 100, 200, ..., 1000 partitions. Let's proceed with adding another 100 partitions.

Step 3: Add More Partitions

do $do$    
declare    
    i int;    
begin    
    for i in 700..799 loop    
        execute format(  
            $sql$     
                create table partitioned_table_%s     
                partition of partitioned_table     
                for values from (%s) to (%s)     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999 + 1     
        );    

        execute format(  
            $sql$     
                insert into partitioned_table_%s     
                select generate_series(%s, %s) as id, 'data'     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999     
        );    

        execute format(  
            $sql$     
                create index on partitioned_table_%s (id)     
            $sql$,     
            i     
        );    
    end loop;    
end $do$;

I'll execute this block now.Another 100 partitions have been successfully added to the table.

Clone ID: cru36sf9m20c73be2cog

Next, let's run vacuum analyze on the partitioned table again.

Step 4: Run vacuum analyze

vacuum analyze partitioned_table;

I'll execute this query now.The vacuum analyze operation has been completed successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's repeat the explain (analyze, buffers) query to measure the planning time, buffer hits, and reads again.

Step 5: Measure Planning Time, Buffer Hits, and Reads Again

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The query execution results after adding another 100 partitions are as follows:

Index Scan using partitioned_table_0_id_idx on partitioned_table_0 partitioned_table  (cost=0.29..8.30 rows=1 width=13) (actual time=0.272..0.274 rows=1 loops=1)
  Index Cond: (id = 1)
  Buffers: shared read=3
  I/O Timings: shared read=0.240
Planning:
  Buffers: shared hit=2547 read=12
  I/O Timings: shared read=1.430
Planning Time: 11.030 ms
Execution Time: 0.371 ms

Clone ID: cru36sf9m20c73be2cog

Next, let's double-check the current number of partitions using pg_inherits.

Step 6: Check Number of Partitions

select count(*) from pg_inherits where inhparent = 'partitioned_table'::regclass;

I'll execute this query now.The current number of partitions is 800, which matches our expectations.

Clone ID: cru36sf9m20c73be2cog

We will continue this process iteratively to gather data points for 0, 100, 200, ..., 1000 partitions. Let's proceed with adding another 100 partitions.

Step 3: Add More Partitions

do $do$    
declare    
    i int;    
begin    
    for i in 800..899 loop    
        execute format(  
            $sql$     
                create table partitioned_table_%s     
                partition of partitioned_table     
                for values from (%s) to (%s)     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999 + 1     
        );    

        execute format(  
            $sql$     
                insert into partitioned_table_%s     
                select generate_series(%s, %s) as id, 'data'     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999     
        );    

        execute format(  
            $sql$     
                create index on partitioned_table_%s (id)     
            $sql$,     
            i     
        );    
    end loop;    
end $do$;

I'll execute this block now.Another 100 partitions have been successfully added to the table.

Clone ID: cru36sf9m20c73be2cog

Next, let's run vacuum analyze on the partitioned table again.

Step 4: Run vacuum analyze

vacuum analyze partitioned_table;

I'll execute this query now.The vacuum analyze operation has been completed successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's repeat the explain (analyze, buffers) query to measure the planning time, buffer hits, and reads again.

Step 5: Measure Planning Time, Buffer Hits, and Reads Again

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The query execution results after adding another 100 partitions are as follows:

Index Scan using partitioned_table_0_id_idx on partitioned_table_0 partitioned_table  (cost=0.29..8.30 rows=1 width=13) (actual time=0.258..0.259 rows=1 loops=1)
  Index Cond: (id = 1)
  Buffers: shared read=3
  I/O Timings: shared read=0.238
Planning:
  Buffers: shared hit=2852 read=10
  I/O Timings: shared read=1.086
Planning Time: 11.243 ms
Execution Time: 0.362 ms

Clone ID: cru36sf9m20c73be2cog

Next, let's double-check the current number of partitions using pg_inherits.

Step 6: Check Number of Partitions

select count(*) from pg_inherits where inhparent = 'partitioned_table'::regclass;

I'll execute this query now.The current number of partitions is 900, which matches our expectations.

Clone ID: cru36sf9m20c73be2cog

We will continue this process iteratively to gather data points for 0, 100, 200, ..., 1000 partitions. Let's proceed with adding another 100 partitions.

Step 3: Add More Partitions

do $do$    
declare    
    i int;    
begin    
    for i in 900..999 loop    
        execute format(  
            $sql$     
                create table partitioned_table_%s     
                partition of partitioned_table     
                for values from (%s) to (%s)     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999 + 1     
        );    

        execute format(  
            $sql$     
                insert into partitioned_table_%s     
                select generate_series(%s, %s) as id, 'data'     
            $sql$,     
            i,     
            i * 9999 + 1,     
            (i + 1) * 9999     
        );    

        execute format(  
            $sql$     
                create index on partitioned_table_%s (id)     
            $sql$,     
            i     
        );    
    end loop;    
end $do$;

I'll execute this block now.Another 100 partitions have been successfully added to the table.

Clone ID: cru36sf9m20c73be2cog

Next, let's run vacuum analyze on the partitioned table again.

Step 4: Run vacuum analyze

vacuum analyze partitioned_table;

I'll execute this query now.The vacuum analyze operation has been completed successfully.

Clone ID: cru36sf9m20c73be2cog

Next, let's repeat the explain (analyze, buffers) query to measure the planning time, buffer hits, and reads again.

Step 5: Measure Planning Time, Buffer Hits, and Reads Again

explain (analyze, buffers) select * from partitioned_table where id = 1;

I'll execute this query now.The query execution results after adding another 100 partitions are as follows:

Index Scan using partitioned_table_0_id_idx on partitioned_table_0 partitioned_table  (cost=0.29..8.30 rows=1 width=13) (actual time=0.254..0.255 rows=1 loops=1)
  Index Cond: (id = 1)
  Buffers: shared read=3
  I/O Timings: shared read=0.231
Planning:
  Buffers: shared hit=3145 read=16
  I/O Timings: shared read=1.527
Planning Time: 12.435 ms
Execution Time: 0.354 ms

Clone ID: cru36sf9m20c73be2cog

Next, let's double-check the current number of partitions using pg_inherits.

Step 6: Check Number of Partitions

select count(*) from pg_inherits where inhparent = 'partitioned_table'::regclass;

I'll execute this query now.The current number of partitions is 1000, which matches our expectations.

Clone ID: cru36sf9m20c73be2cog

We have now gathered data points for 0, 100, 200, ..., 1000 partitions. Here is the summary of the metrics:

Partitions Planning Buffer Hits + Reads Planning Time (ms) Execution Time (ms)
0 52 0.254 0.085
100 452 1.937 0.433
200 752 2.991 0.699
300 1049 5.378 0.781
400 1352 5.662 0.378
500 1651 7.399 0.443
600 1956 7.705 0.356
700 2256 10.177 0.368
800 2559 11.030 0.371
900 2862 11.243 0.362
1000 3161 12.435 0.354

This table summarizes the planning buffer

User Avatar Vitaliy Kukharik 1 month ago | prev | next

visualize timing (both metrics) on one chart and planning buffers hits+reads on another chart

Postgres.AI Assistant avatar Postgres.AI 1 month ago | prev | next | oai/gpt-4o

Here are the visualizations based on the gathered data:

Planning and Execution Time

Planning and Execution Time

Planning Buffer Hits + Reads

Planning Buffer Hits + Reads

These charts illustrate the impact of the number of partitions on planning and execution times, as well as on planning buffer hits and reads.