We have just upgraded our RDS instance to v12.2 from v11.22, and we are seeing some of the queries are performing much slower than v11.6 instance. Would highly appreciate if can get help with this.
new version: PostgreSQL 12.20 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-17), 64-bit
old version: PostgreSQL 11.22 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-12), 64-bit
For example, with the query:
explain (buffers, analyze)
select
"partner_id" as "partnerId",
"instance_id" as "instanceId",
"advertiser_id" as "advertiserId",
"campaign_id" as "campaignId",
"platform",
coalesce(SUM("total_budget_spent"), 0) as "totalBudgetSpent",
coalesce(SUM("total_budget_spent_advertiser_currency"), 0) as "totalBudgetSpentAdvertiserCurrency"
from
"dsp_hourly_campaign_analytics" as "dsp_hourly_campaign_analytics"
where
("dsp_hourly_campaign_analytics"."time" >= 1717596340 -- 1609372800
and "dsp_hourly_campaign_analytics"."time" < 1724256000) and
"dsp_hourly_campaign_analytics"."platform" in ('KRTB') and
"dsp_hourly_campaign_analytics"."campaign_id" in ('247257', '247259', '249282', '266304', '272557', '247258', '247256', '255462', '255461', '246096', '275592', '275593', '275668', '275681', '270524', '270557', '269423', '270838')
group by
"partner_id",
"instance_id",
"advertiser_id",
"campaign_id",
"platform"
limit 2147483647 offset 0;
on v11, output is:
Limit (cost=8516.52..8526.36 rows=656 width=104) (actual time=878.502..878.514 rows=13 loops=1)
Buffers: shared hit=718 read=8521
I/O Timings: read=851.931
-> HashAggregate (cost=8516.52..8526.36 rows=656 width=104) (actual time=878.501..878.511 rows=13 loops=1)
Group Key: dsp_hourly_campaign_analytics.partner_id, dsp_hourly_campaign_analytics.instance_id, dsp_hourly_campaign_analytics.advertiser_id, dsp_hourly_campaign_analytics.campaign_id, dsp_hourly_campaign_analytics.platform
Buffers: shared hit=718 read=8521
I/O Timings: read=851.931
-> Append (cost=0.00..8401.74 rows=6559 width=56) (actual time=0.087..875.336 rows=6542 loops=1)
Buffers: shared hit=718 read=8521
I/O Timings: read=851.931
-> Seq Scan on dsp_hourly_campaign_analytics (cost=0.00..0.00 rows=1 width=2596) (actual time=0.003..0.003 rows=0 loops=1)
Filter: (("time" >= 1717596340) AND ("time" < 1724256000) AND ((platform)::text = 'KRTB'::text) AND ((campaign_id)::text = ANY ('{247257,247259,249282,266304,272557,247258,247256,255462,255461,246096,275592,275593,275668,275681,270524,270557,269423,270838}'::text[])))
-> Index Scan using dsp_hourly_campaign_analytics_202406_campaign_index_202406 on dsp_hourly_campaign_analytics_202406 dsp_hourly_campaign_analytics_1 (cost=0.43..2559.39 rows=1828 width=56) (actual time=0.084..37.129 rows=3536 loops=1)
Index Cond: ((campaign_id)::text = ANY ('{247257,247259,249282,266304,272557,247258,247256,255462,255461,246096,275592,275593,275668,275681,270524,270557,269423,270838}'::text[]))
Filter: (("time" >= 1717596340) AND ("time" < 1724256000) AND ((platform)::text = 'KRTB'::text))
Rows Removed by Filter: 892
Buffers: shared hit=377 read=5493
I/O Timings: read=24.067
-> Index Scan using dsp_hourly_campaign_analytics_202407_campaign_index_202407 on dsp_hourly_campaign_analytics_202407 dsp_hourly_campaign_analytics_2 (cost=0.43..3682.56 rows=3229 width=56) (actual time=0.541..824.004 rows=2853 loops=1)
Index Cond: ((campaign_id)::text = ANY ('{247257,247259,249282,266304,272557,247258,247256,255462,255461,246096,275592,275593,275668,275681,270524,270557,269423,270838}'::text[]))
Filter: (("time" >= 1717596340) AND ("time" < 1724256000) AND ((platform)::text = 'KRTB'::text))
Buffers: shared hit=170 read=2979
I/O Timings: read=814.945
-> Index Scan using dsp_hourly_campaign_analytics_202408_campaign_index_202408 on dsp_hourly_campaign_analytics_202408 dsp_hourly_campaign_analytics_3 (cost=0.42..2126.99 rows=1501 width=56) (actual time=0.050..13.398 rows=153 loops=1)
Index Cond: ((campaign_id)::text = ANY ('{247257,247259,249282,266304,272557,247258,247256,255462,255461,246096,275592,275593,275668,275681,270524,270557,269423,270838}'::text[]))
Filter: (("time" >= 1717596340) AND ("time" < 1724256000) AND ((platform)::text = 'KRTB'::text))
Rows Removed by Filter: 13
Buffers: shared hit=171 read=49
I/O Timings: read=12.919
Planning Time: 28.975 ms
Execution Time: 878.663 ms
but on v12, plan seems not as performant, output below:
Limit (cost=8413.26..8422.96 rows=647 width=104) (actual time=8997.791..8997.807 rows=11 loops=1)
Buffers: shared hit=180 read=6096
I/O Timings: read=8953.020
-> HashAggregate (cost=8413.26..8422.96 rows=647 width=104) (actual time=8997.789..8997.804 rows=11 loops=1)
Group Key: dsp_hourly_campaign_analytics.partner_id, dsp_hourly_campaign_analytics.instance_id, dsp_hourly_campaign_analytics.advertiser_id, dsp_hourly_campaign_analytics.campaign_id, dsp_hourly_campaign_analytics.platform
Buffers: shared hit=180 read=6096
I/O Timings: read=8953.020
-> Append (cost=0.00..8300.00 rows=6472 width=56) (actual time=1.994..8990.356 rows=5656 loops=1)
Buffers: shared hit=180 read=6096
I/O Timings: read=8953.020
-> Seq Scan on dsp_hourly_campaign_analytics (cost=0.00..0.00 rows=1 width=2596) (actual time=0.008..0.008 rows=0 loops=1)
Filter: (("time" >= 1717596340) AND ("time" < 1724256000) AND ((platform)::text = 'KRTB'::text) AND ((campaign_id)::text = ANY ('{247257,247259,249282,266304,272557,247258,247256,255462,255461,246096,275592,275593,275668,275681,270524,270557,269423,270838}'::text[])))
-> Index Scan using dsp_hourly_campaign_analytics_202406_campaign_index_202406 on dsp_hourly_campaign_analytics_202406 dsp_hourly_campaign_analytics_1 (cost=0.43..3421.11 rows=2471 width=56) (actual time=1.985..3781.430 rows=2666 loops=1)
Index Cond: ((campaign_id)::text = ANY ('{247257,247259,249282,266304,272557,247258,247256,255462,255461,246096,275592,275593,275668,275681,270524,270557,269423,270838}'::text[]))
Filter: (("time" >= 1717596340) AND ("time" < 1724256000) AND ((platform)::text = 'KRTB'::text))
Rows Removed by Filter: 440
Buffers: shared hit=75 read=3088
I/O Timings: read=3761.303
-> Index Scan using dsp_hourly_campaign_analytics_202407_campaign_index_202407 on dsp_hourly_campaign_analytics_202407 dsp_hourly_campaign_analytics_2 (cost=0.43..2730.11 rows=2386 width=56) (actual time=2.510..4979.345 rows=2837 loops=1)
Index Cond: ((campaign_id)::text = ANY ('{247257,247259,249282,266304,272557,247258,247256,255462,255461,246096,275592,275593,275668,275681,270524,270557,269423,270838}'::text[]))
Filter: (("time" >= 1717596340) AND ("time" < 1724256000) AND ((platform)::text = 'KRTB'::text))
Buffers: shared hit=62 read=2835
I/O Timings: read=4964.716
-> Index Scan using dsp_hourly_campaign_analytics_202408_campaign_index_202408 on dsp_hourly_campaign_analytics_202408 dsp_hourly_campaign_analytics_3 (cost=0.42..2116.42 rows=1614 width=56) (actual time=21.175..227.975 rows=153 loops=1)
Index Cond: ((campaign_id)::text = ANY ('{247257,247259,249282,266304,272557,247258,247256,255462,255461,246096,275592,275593,275668,275681,270524,270557,269423,270838}'::text[]))
Filter: (("time" >= 1717596340) AND ("time" < 1724256000) AND ((platform)::text = 'KRTB'::text))
Rows Removed by Filter: 9
Buffers: shared hit=43 read=173
I/O Timings: read=227.000
Planning Time: 7.668 ms
Execution Time: 8997.911 ms
The tables are organized into partitions based on inherit like below:
CREATE TABLE public.dsp_hourly_campaign_analytics_202402 (
CONSTRAINT dsp_hourly_campaign_analytics_202402_time_check CHECK ((("time" >= 1706745600) AND ("time" < 1709251200)))
)
INHERITS (public.dsp_hourly_campaign_analytics);
CREATE INDEX dsp_hourly_campaign_analytics_202402_advertiser_index_202402 ON public.dsp_hourly_campaign_analytics_202402 USING btree (advertiser_id);
CREATE INDEX dsp_hourly_campaign_analytics_202402_campaign_index_202402 ON public.dsp_hourly_campaign_analytics_202402 USING btree (campaign_id);
CREATE INDEX dsp_hourly_campaign_analytics_202402_instance_index_202402 ON public.dsp_hourly_campaign_analytics_202402 USING btree (instance_id);
CREATE INDEX dsp_hourly_campaign_analytics_202402_partner_index_202402 ON public.dsp_hourly_campaign_analytics_202402 USING btree (partner_id);
CREATE UNIQUE INDEX dsp_hourly_campaign_analytics_202402_pkey_202402 ON public.dsp_hourly_campaign_analytics_202402 USING btree ("time", advertiser_id, campaign_id, platform, publisher_channel, publisher_platform);
CREATE INDEX dsp_hourly_campaign_analytics_202402_platform_index_202402 ON public.dsp_hourly_campaign_analytics_202402 USING btree (platform);
CREATE INDEX idx_dsp_hourly_campaign_analytics_202402_time ON public.dsp_hourly_campaign_analytics_202402 USING btree ("time");