BEGIN
                                                                                                                                                                                                                                                                       QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=53227.18..53229.20 rows=15 width=132) (actual rows=23 loops=1)
   Buffers: shared hit=276723 read=3816
   ->  Incremental Sort  (cost=53227.18..53229.16 rows=15 width=132) (actual rows=159 loops=1)
         Sort Key: (COALESCE(la.canonical_node_id, upper(btrim(s.node_id)))), (COALESCE(s.uptime_secs, '0'::bigint)) DESC, s."time" DESC
         Presorted Key: (COALESCE(la.canonical_node_id, upper(btrim(s.node_id))))
         Full-sort Groups: 5  Sort Method: quicksort  Average Memory: 33kB  Peak Memory: 33kB
         Buffers: shared hit=276723 read=3816
         ->  Merge Left Join  (cost=53227.08..53228.49 rows=15 width=132) (actual rows=159 loops=1)
               Merge Cond: (COALESCE(la.canonical_node_id, upper(btrim(s.node_id))) = pc.rx_node_id)
               Buffers: shared hit=276717 read=3816
               ->  Merge Left Join  (cost=13150.29..13151.14 rows=15 width=214) (actual rows=159 loops=1)
                     Merge Cond: ((COALESCE(la.canonical_node_id, upper(btrim(s.node_id)))) = n.node_id)
                     Filter: ((n.name IS NULL) OR (n.name !~~ '%🚫%'::text))
                     Buffers: shared hit=9383 read=4
                     ->  Sort  (cost=28.38..28.42 rows=15 width=182) (actual rows=159 loops=1)
                           Sort Key: (COALESCE(la.canonical_node_id, upper(btrim(s.node_id))))
                           Sort Method: quicksort  Memory: 57kB
                           Buffers: shared hit=388 read=4
                           ->  Nested Loop Left Join  (cost=0.56..28.09 rows=15 width=182) (actual rows=159 loops=1)
                                 Buffers: shared hit=388 read=4
                                 ->  Custom Scan (ChunkAppend) on node_status_samples s  (cost=0.29..5.01 rows=15 width=117) (actual rows=159 loops=1)
                                       Chunks excluded during startup: 0
                                       Buffers: shared hit=70 read=4
                                       ->  Index Scan using _hyper_2_316_chunk_node_status_samples_time_idx on _hyper_2_316_chunk s_1  (cost=0.29..5.01 rows=15 width=117) (actual rows=159 loops=1)
                                             Index Cond: ("time" > (now() - '00:15:00'::interval))
                                             Filter: (network = ANY ('{ukmesh,northeast,teesside}'::text[]))
                                             Buffers: shared hit=70 read=4
                                 ->  Index Scan using node_identity_aliases_pkey on node_identity_aliases la  (cost=0.27..1.54 rows=1 width=130) (actual rows=0 loops=159)
                                       Index Cond: (source_node_id = upper(btrim(s.node_id)))
                                       Buffers: shared hit=318
                     ->  Sort  (cost=13121.91..13122.07 rows=64 width=64) (actual rows=8458 loops=1)
                           Sort Key: n.node_id
                           Sort Method: quicksort  Memory: 1270kB
                           Buffers: shared hit=8995
                           ->  Subquery Scan on n  (cost=13118.07..13119.99 rows=64 width=64) (actual rows=8365 loops=1)
                                 Buffers: shared hit=8995
                                 ->  CTE Scan on v  (cost=13118.07..13119.35 rows=64 width=381) (actual rows=8365 loops=1)
                                       Buffers: shared hit=8995
                                       CTE v
                                         ->  Merge Join  (cost=11854.79..13118.07 rows=64 width=381) (actual rows=8365 loops=1)
                                               Merge Cond: (r.canonical_node_id = mapped.canonical_node_id)
                                               Buffers: shared hit=8995
                                               CTE mapped
                                                 ->  Hash Left Join  (cost=30.16..9225.37 rows=12887 width=312) (actual rows=8461 loops=1)
                                                       Hash Cond: (upper(btrim(n_1.node_id)) = la_1.source_node_id)
                                                       Buffers: shared hit=8992
                                                       ->  Seq Scan on nodes n_1  (cost=0.00..9092.87 rows=12887 width=280) (actual rows=8461 loops=1)
                                                             Buffers: shared hit=8964
                                                       ->  Hash  (cost=28.96..28.96 rows=96 width=130) (actual rows=96 loops=1)
                                                             Buckets: 1024  Batches: 1  Memory Usage: 24kB
                                                             Buffers: shared hit=28
                                                             ->  Seq Scan on node_identity_aliases la_1  (cost=0.00..28.96 rows=96 width=130) (actual rows=96 loops=1)
                                                                   Buffers: shared hit=28
                                               ->  Subquery Scan on r  (cost=1491.90..2361.78 rows=64 width=292) (actual rows=8365 loops=1)
                                                     Filter: (r.representative_rank = 1)
                                                     Buffers: shared hit=8995
                                                     ->  WindowAgg  (cost=1491.90..2200.69 rows=12887 width=393) (actual rows=8365 loops=1)
                                                           Run Condition: (row_number() OVER (?) <= 1)
                                                           Buffers: shared hit=8995
                                                           ->  Sort  (cost=1491.90..1524.12 rows=12887 width=332) (actual rows=8461 loops=1)
                                                                 Sort Key: m.canonical_node_id, (CASE WHEN ((m.role = 2) AND (m.lat >= '-90'::double precision) AND (m.lat <= '90'::double precision) AND (m.lon >= '-180'::double precision) AND (m.lon <= '180'::double precision) AND ((abs(m.lat) >= '1e-09'::double precision) OR (abs(m.lon) >= '1e-09'::double precision))) THEN 0 WHEN (m.role = 2) THEN 1 WHEN (COALESCE(m.advert_count, 0) > 0) THEN 2 ELSE 3 END), (COALESCE(m.advert_count, 0)) DESC, m.last_seen DESC NULLS LAST, m.node_id
                                                                 Sort Method: quicksort  Memory: 2925kB
                                                                 Buffers: shared hit=8995
                                                                 ->  CTE Scan on mapped m  (cost=0.00..612.13 rows=12887 width=332) (actual rows=8461 loops=1)
                                                                       Buffers: shared hit=8992
                                               ->  GroupAggregate  (cost=1137.51..1527.62 rows=200 width=129) (actual rows=8365 loops=1)
                                                     Group Key: mapped.canonical_node_id
                                                     ->  Sort  (cost=1137.51..1169.73 rows=12887 width=117) (actual rows=8461 loops=1)
                                                           Sort Key: mapped.canonical_node_id, mapped.node_id
                                                           Sort Method: quicksort  Memory: 1988kB
                                                           ->  CTE Scan on mapped  (cost=0.00..257.74 rows=12887 width=117) (actual rows=8461 loops=1)
               ->  Sort  (cost=40076.78..40076.87 rows=34 width=40) (actual rows=26 loops=1)
                     Sort Key: pc.rx_node_id
                     Sort Method: quicksort  Memory: 27kB
                     Buffers: shared hit=267334 read=3812
                     ->  Subquery Scan on pc  (cost=40066.74..40075.92 rows=34 width=40) (actual rows=26 loops=1)
                           Buffers: shared hit=267334 read=3812
                           ->  Finalize HashAggregate  (cost=40066.74..40075.58 rows=34 width=40) (actual rows=26 loops=1)
                                 Group Key: (meshcore_canonical_node_id(packets.rx_node_id))
                                 Batches: 1  Memory Usage: 24kB
                                 Buffers: shared hit=267334 read=3812
                                 ->  Custom Scan (ChunkAppend) on packets  (cost=0.42..40066.40 rows=68 width=40) (actual rows=51 loops=1)
                                       Chunks excluded during startup: 0
                                       Buffers: shared hit=267334 read=3812
                                       ->  Partial HashAggregate  (cost=21224.41..21233.25 rows=34 width=40) (actual rows=26 loops=1)
                                             Group Key: meshcore_canonical_node_id(_hyper_1_312_chunk.rx_node_id)
                                             Batches: 1  Memory Usage: 24kB
                                             Buffers: shared hit=142296 read=2057
                                             ->  Index Only Scan using _hyper_1_312_chunk_idx_packets_observer_region on _hyper_1_312_chunk  (cost=0.42..20898.73 rows=65136 width=32) (actual rows=67918 loops=1)
                                                   Index Cond: ((network = ANY ('{ukmesh,northeast,teesside}'::text[])) AND ("time" > (now() - '24:00:00'::interval)) AND (rx_node_id IS NOT NULL))
                                                   Heap Fetches: 11874
                                                   Buffers: shared hit=142296 read=2057
                                       ->  Partial HashAggregate  (cost=18824.31..18833.15 rows=34 width=40) (actual rows=25 loops=1)
                                             Group Key: meshcore_canonical_node_id(_hyper_1_314_chunk.rx_node_id)
                                             Batches: 1  Memory Usage: 24kB
                                             Buffers: shared hit=125038 read=1755
                                             ->  Index Only Scan using _hyper_1_314_chunk_idx_packets_observer_region on _hyper_1_314_chunk  (cost=0.42..18515.07 rows=61848 width=32) (actual rows=61837 loops=1)
                                                   Index Cond: ((network = ANY ('{ukmesh,northeast,teesside}'::text[])) AND ("time" > (now() - '24:00:00'::interval)) AND (rx_node_id IS NOT NULL))
                                                   Heap Fetches: 2415
                                                   Buffers: shared hit=125038 read=1755
 Settings: max_parallel_workers_per_gather = '0', work_mem = '10076kB', effective_io_concurrency = '256', random_page_cost = '1.1', effective_cache_size = '1475MB', jit = 'off'
 Planning:
   Buffers: shared hit=2393 read=35
 Planning Time: 50.469 ms
 Execution Time: 6607.320 ms
(105 rows)

ROLLBACK
