add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
version
PostgreSQL 12.8 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.4.1 20200928 (Red Hat 8.4.1-1), 64-bit
23 rows affected
23 rows affected
id name date1
1 BMW 2017-01-01
2 AUDI 2017-12-01
3 BMW 2017-12-01
4 SEAT 2017-12-01
5 BMW 2017-12-05
6 BMW 2017-12-29
7 BMW 2018-01-01
8 BMW 2018-01-01
9 BMW 2018-01-01
10 MERCEDES 2018-01-01
11 MERCEDES 2018-01-01
12 null 2018-01-01
13 MERCEDES 2018-01-05
14 MERCEDES 2018-01-05
15 MERCEDES 2018-01-09
16 SEAT 2018-02-01
17 MERCEDES 2018-03-01
18 null 2018-03-01
19 MERCEDES 2018-04-01
20 SEAT 2018-04-01
21 MERCEDES 2018-04-09
22 SEAT 2018-05-01
23 SEAT 2018-07-01
name count min max
BMW 5 2017-12-05 2018-01-01
MERCEDES 2 2018-01-01 2018-01-01
MERCEDES 3 2018-01-05 2018-01-09
SEAT 2 2018-05-01 2018-07-01
QUERY PLAN
Sort (cost=285.04..285.05 rows=6 width=56) (actual time=0.270..0.273 rows=4 loops=1)
  Output: c.name, (count(*)), (min(temp1.date1)), (max(l.date1)), temp1.date1, l.date1
  Sort Key: temp1.date1, (count(*)) DESC, c.name
  Sort Method: quicksort Memory: 25kB
  Buffers: shared hit=3
  -> GroupAggregate (cost=284.79..284.96 rows=6 width=56) (actual time=0.258..0.264 rows=4 loops=1)
        Output: c.name, count(*), min(temp1.date1), max(l.date1), temp1.date1, l.date1
        Group Key: c.name, temp1.date1, l.date1
        Buffers: shared hit=3
        -> Sort (cost=284.79..284.81 rows=6 width=40) (actual time=0.252..0.255 rows=12 loops=1)
              Output: c.name, temp1.date1, l.date1
              Sort Key: c.name, temp1.date1, l.date1
              Sort Method: quicksort Memory: 25kB
              Buffers: shared hit=3
              -> Hash Right Join (cost=257.68..284.72 rows=6 width=40) (actual time=0.227..0.242 rows=12 loops=1)
                    Output: c.name, temp1.date1, l.date1
                    Hash Cond: (c.name = temp1.name)
                    Join Filter: ((c.id >= temp1.id) AND (c.id <= l.id))
                    Rows Removed by Join Filter: 16
                    Buffers: shared hit=3
                    -> Seq Scan on public.cars c (cost=0.00..22.00 rows=1200 width=36) (actual time=0.006..0.008 rows=23 loops=1)
                          Output: c.id, c.name, c.date1
                          Buffers: shared hit=1
                    -> Hash (cost=257.60..257.60 rows=6 width=48) (actual time=0.207..0.209 rows=4 loops=1)
                          Output: temp1.date1, temp1.name, temp1.id, l.date1, l.id
                          Buckets: 1024 Batches: 1 Memory Usage: 9kB
                          Buffers: shared hit=2
                          -> Nested Loop Left Join (cost=166.75..257.60 rows=6 width=48) (actual time=0.158..0.202 rows=4 loops=1)
                                Output: temp1.date1, temp1.name, temp1.id, l.date1, l.id
                                Join Filter: ((row_number() OVER (?)) = l.rn)
                                Rows Removed by Join Filter: 12
                                Buffers: shared hit=2
                                -> WindowAgg (cost=83.37..128.46 rows=6 width=52) (actual time=0.047..0.066 rows=4 loops=1)
                                      Output: temp1.id, temp1.name, temp1.date1, NULL::integer, row_number() OVER (?)
                                      Buffers: shared hit=1
                                      -> Subquery Scan on temp1 (cost=83.37..128.37 rows=6 width=40) (actual time=0.033..0.055 rows=4 loops=1)
                                            Output: temp1.id, temp1.name, temp1.date1
                                            Filter: (temp1.is_first = 1)
                                            Rows Removed by Filter: 19
                                            Buffers: shared hit=1
                                            -> WindowAgg (cost=83.37..113.37 rows=1200 width=44) (actual time=0.027..0.051 rows=23 loops=1)
                                                  Output: cars.id, cars.name, cars.date1, CASE WHEN (((lag(cars.name, 1) OVER (?) <> cars.name) OR (lag(cars.name, 1) OVER (?) IS NULL)) AND (lead(cars.name, 1) OVER (?) = cars.name)) THEN 1 ELSE 0 END
                                                  Buffers: shared hit=1
                                                  -> Sort (cost=83.37..86.37 rows=1200 width=40) (actual time=0.017..0.019 rows=23 loops=1)
                                                        Output: cars.id, cars.name, cars.date1
                                                        Sort Key: cars.id
                                                        Sort Method: quicksort Memory: 26kB
                                                        Buffers: shared hit=1
                                                        -> Seq Scan on public.cars (cost=0.00..22.00 rows=1200 width=40) (actual time=0.007..0.009 rows=23 loops=1)
                                                              Output: cars.id, cars.name, cars.date1
                                                              Buffers: shared hit=1
                                -> Materialize (cost=83.37..128.55 rows=6 width=16) (actual time=0.027..0.033 rows=4 loops=4)
                                      Output: l.date1, l.rn, l.id
                                      Buffers: shared hit=1
                                      -> Subquery Scan on l (cost=83.37..128.52 rows=6 width=16) (actual time=0.057..0.077 rows=4 loops=1)
                                            Output: l.date1, l.rn, l.id
                                            Buffers: shared hit=1
                                            -> WindowAgg (cost=83.37..128.46 rows=6 width=52) (actual time=0.057..0.075 rows=4 loops=1)
                                                  Output: temp2.id, NULL::text, temp2.date1, NULL::integer, row_number() OVER (?)
                                                  Buffers: shared hit=1
                                                  -> Subquery Scan on temp2 (cost=83.37..128.37 rows=6 width=8) (actual time=0.041..0.064 rows=4 loops=1)
                                                        Output: temp2.id, temp2.date1
                                                        Filter: (temp2.is_last = 1)
                                                        Rows Removed by Filter: 19
                                                        Buffers: shared hit=1
                                                        -> WindowAgg (cost=83.37..113.37 rows=1200 width=44) (actual time=0.031..0.061 rows=23 loops=1)
                                                              Output: cars_1.id, NULL::text, cars_1.date1, CASE WHEN (((lead(cars_1.name, 1) OVER (?) <> cars_1.name) OR (lead(cars_1.name, 1) OVER (?) IS NULL)) AND (lag(cars_1.name, 1) OVER (?) = cars_1.name)) THEN 1 ELSE 0 END
                                                              Buffers: shared hit=1
                                                              -> Sort (cost=83.37..86.37 rows=1200 width=40) (actual time=0.022..0.024 rows=23 loops=1)
                                                                    Output: cars_1.id, cars_1.date1, cars_1.name
                                                                    Sort Key: cars_1.id
                                                                    Sort Method: quicksort Memory: 26kB
                                                                    Buffers: shared hit=1
                                                                    -> Seq Scan on public.cars cars_1 (cost=0.00..22.00 rows=1200 width=40) (actual time=0.007..0.010 rows=23 loops=1)
                                                                          Output: cars_1.id, cars_1.date1, cars_1.name
                                                                          Buffers: shared hit=1
Planning Time: 0.292 ms
Execution Time: 0.428 ms