Original query:

WindowAgg  (cost=2512646.05..2514256.68 rows=13148 width=484)
  ->  GroupAggregate  (cost=2512646.05..2514092.33 rows=13148 width=476)
        Group Key: aod.as_of_date, aod.calendar_month, aod.calendar_year, aod.calendar_quarter, aod.market_id, aod.dim_market_id, aod.enterprise_market_name, aod.station_id, aod.dim_station_id, aod.enterprise_station_name, aod.revenue_type_id_dim, aod.revenue_type_name_dim, aod.revenue_type_id, aod.revenue_type_name, aod.base_revenue_type_id, aod.base_revenue_type_name, aod.sales_region_id, aod.sales_region_name, aod.local_revenue_type_id, aod.local_revenue_type_name, aod.sub_revenue_type_id_dim, aod.sub_revenue_type_name_dim, aod.dim_revenue_category_id, aod.enterprise_revenue_category_name, aod.evp, aod.rvp, aod.inv_enterprise_sales_channel_name
        ->  Sort  (cost=2512646.05..2512678.92 rows=13148 width=311)
              Sort Key: aod.as_of_date, aod.calendar_month, aod.calendar_year, aod.calendar_quarter, aod.market_id, aod.dim_market_id, aod.enterprise_market_name, aod.station_id, aod.dim_station_id, aod.enterprise_station_name, aod.revenue_type_id_dim, aod.revenue_type_name_dim, aod.revenue_type_id, aod.revenue_type_name, aod.base_revenue_type_id, aod.base_revenue_type_name, aod.sales_region_id, aod.sales_region_name, aod.local_revenue_type_id, aod.local_revenue_type_name, aod.sub_revenue_type_id_dim, aod.sub_revenue_type_name_dim, aod.dim_revenue_category_id, aod.enterprise_revenue_category_name, aod.evp, aod.rvp, aod.inv_enterprise_sales_channel_name
              ->  Merge Right Join  (cost=2431747.35..2509856.56 rows=13148 width=311)
                    Merge Cond: ((ly.station_id = aod.station_id) AND (ly.calendar_month = aod.calendar_month) AND (ly.calendar_year = ((aod.calendar_year - 1))) AND (ly.market_id = aod.market_id) AND ((COALESCE(ly.revenue_type_id, 0)) = (COALESCE(aod.revenue_type_id, 0))) AND ((COALESCE(ly.base_revenue_type_id, 0)) = (COALESCE(aod.base_revenue_type_id, 0))) AND ((COALESCE(ly.sales_region_id, 0)) = (COALESCE(aod.sales_region_id, 0))) AND ((COALESCE(ly.local_revenue_type_id, 0)) = (COALESCE(aod.local_revenue_type_id, 0))))
                    ->  Sort  (cost=896579.34..904418.06 rows=3135490 width=41)
                          Sort Key: ly.station_id, ly.calendar_month, ly.calendar_year, ly.market_id, (COALESCE(ly.revenue_type_id, 0)), (COALESCE(ly.base_revenue_type_id, 0)), (COALESCE(ly.sales_region_id, 0)), (COALESCE(ly.local_revenue_type_id, 0))
                          ->  Seq Scan on revenue_pacing_mv_5 ly  (cost=0.00..365342.90 rows=3135490 width=41)
                    ->  Materialize  (cost=1535162.56..1535228.30 rows=13148 width=302)
                          ->  Sort  (cost=1535162.56..1535195.43 rows=13148 width=302)
                                Sort Key: aod.station_id, aod.calendar_month, ((aod.calendar_year - 1)), aod.market_id, (COALESCE(aod.revenue_type_id, 0)), (COALESCE(aod.base_revenue_type_id, 0)), (COALESCE(aod.sales_region_id, 0)), (COALESCE(aod.local_revenue_type_id, 0))
                                ->  Subquery Scan on aod  (cost=1362156.75..1532418.57 rows=13148 width=302)
                                      ->  Hash Left Join  (cost=1362156.75..1532287.09 rows=13148 width=379)
                                            Hash Cond: (dmrc.revenue_category_id = rc.dim_revenue_category_id)
                                            CTE default_revenue_category
                                              ->  Seq Scan on dim_revenue_category  (cost=0.00..1.14 rows=1 width=15)
                                                    Filter: (lower(enterprise_revenue_category_name) = 'other'::text)
                                            CTE default_revenue_type
                                              ->  Seq Scan on dim_revenue_type  (cost=0.00..3.15 rows=1 width=33)
                                                    Filter: (enterprise_revenue_type_name = 'Other Revenue - 44000'::text)
                                            CTE trade_revenue_category
                                              ->  Seq Scan on dim_revenue_category rc_1  (cost=0.00..1.14 rows=1 width=15)
                                                    Filter: (lower(enterprise_revenue_category_name) = 'trade'::text)
                                            CTE default_wo_trade_revenue_type
                                              ->  Seq Scan on dim_revenue_type dim_revenue_type_1  (cost=0.00..3.15 rows=1 width=33)
                                                    Filter: (enterprise_revenue_type_name = 'Trade / Trade Agency - 47000'::text)
                                            InitPlan 5 (returns $4)
                                              ->  CTE Scan on default_wo_trade_revenue_type  (cost=0.00..0.02 rows=1 width=4)
                                            InitPlan 6 (returns $5)
                                              ->  CTE Scan on default_revenue_type def_rt  (cost=0.00..0.02 rows=1 width=4)
                                            InitPlan 7 (returns $6)
                                              ->  CTE Scan on default_wo_trade_revenue_type default_wo_trade_revenue_type_1  (cost=0.00..0.02 rows=1 width=32)
                                            InitPlan 8 (returns $7)
                                              ->  CTE Scan on default_revenue_type def_rt_1  (cost=0.00..0.02 rows=1 width=32)
                                            InitPlan 9 (returns $8)
                                              ->  CTE Scan on trade_revenue_category trade  (cost=0.00..0.02 rows=1 width=4)
                                            InitPlan 10 (returns $9)
                                              ->  CTE Scan on default_revenue_category def_rc  (cost=0.00..0.02 rows=1 width=4)
                                            InitPlan 11 (returns $10)
                                              ->  CTE Scan on trade_revenue_category trade_1  (cost=0.00..0.02 rows=1 width=32)
                                            InitPlan 12 (returns $11)
                                              ->  CTE Scan on default_revenue_category def_rc_1  (cost=0.00..0.02 rows=1 width=32)
                                            ->  Hash Left Join  (cost=1362146.82..1532160.65 rows=13148 width=316)
                                                  Hash Cond: ((oj.revenue_type_id = dmrc.revenue_type_id) AND (oj.source_system = dmrc.source_system))
                                                  ->  Hash Left Join  (cost=1361899.17..1531843.97 rows=13148 width=312)
                                                        Hash Cond: (dmcrt.dim_revenue_category_id = drc2.dim_revenue_category_id)
                                                        ->  Hash Anti Join  (cost=1361897.97..1531792.00 rows=13148 width=301)
                                                              Hash Cond: (ds.dim_station_id = e.dim_station_id)
                                                              ->  Hash Left Join  (cost=1361896.65..1531624.02 rows=13327 width=301)
                                                                    Hash Cond: (dmrt2.dim_revenue_type_id = drt2.dim_revenue_type_id)
                                                                    ->  Hash Left Join  (cost=1361892.58..1531583.36 rows=13327 width=272)
                                                                          Hash Cond: ((oj.local_revenue_type_id = dmrt2.revenue_type_id) AND (oj.source_system = dmrt2.source_system))
                                                                          ->  Hash Left Join  (cost=1361644.88..1531265.69 rows=13327 width=268)
                                                                                Hash Cond: (drtw.dim_sales_channel_id = dsc.dim_sales_channel_id)
                                                                                ->  Hash Left Join  (cost=1361643.70..1531210.99 rows=13327 width=261)
                                                                                      Hash Cond: (dmcrt.dim_revenue_type_id = drtw.dim_revenue_type_id)
                                                                                      ->  Hash Left Join  (cost=1361639.63..1531170.33 rows=13327 width=228)
                                                                                            Hash Cond: ((oj.revenue_type_id = dmcrt.revenue_type_id) AND (oj.base_revenue_type_id = dmcrt.base_revenue_type_id) AND (oj.sales_region_id = dmcrt.sales_region_id) AND (oj.local_revenue_type_id = dmcrt.local_revenue_type_id))
                                                                                            Join Filter: (oj.source_system ~~ 'wideorbit%'::text)
                                                                                            ->  Hash Left Join  (cost=1360919.26..1530310.02 rows=13327 width=220)
                                                                                                  Hash Cond: (drt.dim_sales_channel_id = dscs.dim_sales_channel_id)
                                                                                                  ->  Hash Left Join  (cost=1360918.08..1530255.33 rows=13327 width=213)
                                                                                                        Hash Cond: (dmrt.dim_revenue_type_id = drt.dim_revenue_type_id)
                                                                                                        Join Filter: (oj.source_system !~~ 'wideorbit%'::text)
                                                                                                        ->  Hash Left Join  (cost=1360914.01..1530214.42 rows=13327 width=180)
                                                                                                              Hash Cond: ((oj.revenue_type_id = dmrt.revenue_type_id) AND (oj.source_system = dmrt.source_system))
                                                                                                              ->  Hash Left Join  (cost=1360666.31..1529896.75 rows=13327 width=176)
                                                                                                                    Hash Cond: (dm.dim_market_id = vp.dim_market_id)
                                                                                                                    ->  Hash Join  (cost=1360663.27..1529857.11 rows=13327 width=148)
                                                                                                                          Hash Cond: (dmm.dim_market_id = dm.dim_market_id)
                                                                                                                          ->  Merge Right Join  (cost=1360660.20..1529817.44 rows=13327 width=138)
                                                                                                                                Merge Cond: ((oj_ly.as_of_date = ((oj.as_of_date - '364 days'::interval))) AND (oj_ly.calendar_month = oj.calendar_month) AND (oj_ly.calendar_year = oj.calendar_year) AND (oj_ly.calendar_week_start_date = oj.calendar_week_start_date) AND (oj_ly.market_id = oj.market_id) AND (oj_ly.station_id = oj.station_id) AND ((COALESCE(oj_ly.revenue_type_id, 0)) = (COALESCE(oj.revenue_type_id, 0))) AND ((COALESCE(oj_ly.base_revenue_type_id, 0)) = (COALESCE(oj.base_revenue_type_id, 0))) AND ((COALESCE(oj_ly.sales_region_id, 0)) = (COALESCE(oj.sales_region_id, 0))) AND ((COALESCE(oj_ly.local_revenue_type_id, 0)) = (COALESCE(oj.local_revenue_type_id, 0))) AND (oj_ly.order_is_cash = oj.order_is_cash))
                                                                                                                                ->  Sort  (cost=1136966.06..1149944.84 rows=5191511 width=46)
                                                                                                                                      Sort Key: oj_ly.as_of_date, oj_ly.calendar_month, oj_ly.calendar_year, oj_ly.calendar_week_start_date, oj_ly.market_id, oj_ly.station_id, (COALESCE(oj_ly.revenue_type_id, 0)), (COALESCE(oj_ly.base_revenue_type_id, 0)), (COALESCE(oj_ly.sales_region_id, 0)), (COALESCE(oj_ly.local_revenue_type_id, 0)), oj_ly.order_is_cash
                                                                                                                                      ->  Seq Scan on rsg_revenue_aod oj_ly  (cost=0.00..238509.11 rows=5191511 width=46)
                                                                                                                                ->  Sort  (cost=223694.14..223727.46 rows=13327 width=133)
                                                                                                                                      Sort Key: ((oj.as_of_date - '364 days'::interval)), oj.calendar_month, oj.calendar_year, oj.calendar_week_start_date, oj.market_id, oj.station_id, (COALESCE(oj.revenue_type_id, 0)), (COALESCE(oj.base_revenue_type_id, 0)), (COALESCE(oj.sales_region_id, 0)), (COALESCE(oj.local_revenue_type_id, 0)), oj.order_is_cash
                                                                                                                                      ->  Hash Join  (cost=1066.83..222781.10 rows=13327 width=133)
                                                                                                                                            Hash Cond: (dms.dim_station_id = ds.dim_station_id)
                                                                                                                                            Join Filter: ((SubPlan 13) OR (NOT (alternatives: SubPlan 14 or hashed SubPlan 15)))
                                                                                                                                            ->  Gather  (cost=1052.12..222650.31 rows=17769 width=125)
                                                                                                                                                  Workers Planned: 2
                                                                                                                                                  ->  Hash Join  (cost=52.12..219873.41 rows=7404 width=125)
                                                                                                                                                        Hash Cond: ((oj.station_id = dms.station_id) AND (dmm.source_system = dms.source_system))
                                                                                                                                                        ->  Hash Join  (cost=6.70..219591.37 rows=45058 width=136)
                                                                                                                                                              Hash Cond: ((oj.market_id = dmm.market_id) AND (oj.source_system = dmm.source_system))
                                                                                                                                                              ->  Parallel Seq Scan on rsg_revenue_aod oj  (cost=0.00..208225.30 rows=2163130 width=117)
                                                                                                                                                              ->  Hash  (cost=3.88..3.88 rows=188 width=23)
                                                                                                                                                                    ->  Seq Scan on dim_mapping_market dmm  (cost=0.00..3.88 rows=188 width=23)
                                                                                                                                                        ->  Hash  (cost=24.17..24.17 rows=1417 width=25)
                                                                                                                                                              ->  Seq Scan on dim_mapping_station dms  (cost=0.00..24.17 rows=1417 width=25)
                                                                                                                                            ->  Hash  (cost=8.20..8.20 rows=520 width=12)
                                                                                                                                                  ->  Seq Scan on dim_station ds  (cost=0.00..8.20 rows=520 width=12)
                                                                                                                                            SubPlan 13
                                                                                                                                              ->  Seq Scan on rsg_station_sunset e_1  (cost=0.00..1.51 rows=1 width=0)
                                                                                                                                                    Filter: ((dim_station_id = ds.dim_station_id) AND ((date_part('year'::text, (sunset_date)::timestamp without time zone) > (oj.calendar_year)::double precision) OR ((date_part('year'::text, (sunset_date)::timestamp without time zone) = (oj.calendar_year)::double precision) AND (date_part('month'::text, (sunset_date)::timestamp without time zone) >= (oj.calendar_month)::double precision))))
                                                                                                                                            SubPlan 14
                                                                                                                                              ->  Seq Scan on rsg_station_sunset e_2  (cost=0.00..1.15 rows=1 width=0)
                                                                                                                                                    Filter: (dim_station_id = ds.dim_station_id)
                                                                                                                                            SubPlan 15
                                                                                                                                              ->  Seq Scan on rsg_station_sunset e_3  (cost=0.00..1.12 rows=12 width=4)
                                                                                                                          ->  Hash  (cost=1.92..1.92 rows=92 width=14)
                                                                                                                                ->  Seq Scan on dim_market dm  (cost=0.00..1.92 rows=92 width=14)
                                                                                                                    ->  Hash  (cost=1.91..1.91 rows=91 width=32)
                                                                                                                          ->  Seq Scan on rsg_vp_mapping vp  (cost=0.00..1.91 rows=91 width=32)
                                                                                                              ->  Hash  (cost=128.48..128.48 rows=7948 width=17)
                                                                                                                    ->  Seq Scan on dim_mapping_revenue_type dmrt  (cost=0.00..128.48 rows=7948 width=17)
                                                                                                        ->  Hash  (cost=2.92..2.92 rows=92 width=37)
                                                                                                              ->  Seq Scan on dim_revenue_type drt  (cost=0.00..2.92 rows=92 width=37)
                                                                                                  ->  Hash  (cost=1.08..1.08 rows=8 width=15)
                                                                                                        ->  Seq Scan on dim_sales_channel dscs  (cost=0.00..1.08 rows=8 width=15)
                                                                                            ->  Hash  (cost=334.79..334.79 rows=19279 width=24)
                                                                                                  ->  Seq Scan on dim_mapping_sales_channel dmcrt  (cost=0.00..334.79 rows=19279 width=24)
                                                                                      ->  Hash  (cost=2.92..2.92 rows=92 width=37)
                                                                                            ->  Seq Scan on dim_revenue_type drtw  (cost=0.00..2.92 rows=92 width=37)
                                                                                ->  Hash  (cost=1.08..1.08 rows=8 width=15)
                                                                                      ->  Seq Scan on dim_sales_channel dsc  (cost=0.00..1.08 rows=8 width=15)
                                                                          ->  Hash  (cost=128.48..128.48 rows=7948 width=17)
                                                                                ->  Seq Scan on dim_mapping_revenue_type dmrt2  (cost=0.00..128.48 rows=7948 width=17)
                                                                    ->  Hash  (cost=2.92..2.92 rows=92 width=33)
                                                                          ->  Seq Scan on dim_revenue_type drt2  (cost=0.00..2.92 rows=92 width=33)
                                                              ->  Hash  (cost=1.23..1.23 rows=7 width=4)
                                                                    ->  Seq Scan on rsg_station_effective_date e  (cost=0.00..1.23 rows=7 width=4)
                                                                          Filter: (effective_date >= (now())::date)
                                                        ->  Hash  (cost=1.09..1.09 rows=9 width=15)
                                                              ->  Seq Scan on dim_revenue_category drc2  (cost=0.00..1.09 rows=9 width=15)
                                                  ->  Hash  (cost=128.46..128.46 rows=7946 width=17)
                                                        ->  Seq Scan on dim_mapping_revenue_category dmrc  (cost=0.00..128.46 rows=7946 width=17)
                                            ->  Hash  (cost=1.09..1.09 rows=9 width=15)
                                                  ->  Seq Scan on dim_revenue_category rc  (cost=0.00..1.09 rows=9 width=15)