warehouse=# explain (analyze, buffers) with recursive t as ( select min("VBAK_MANDT") as "VBAK_MANDT" from staging.pdw00002_vbak union all select (select min("VBAK_MANDT") as "VBAK_MANDT" from staging.pdw00002_vbak where "VBAK_MANDT" > t."VBAK_MANDT") from t where t."VBAK_MANDT" is not null ) select "VBAK_MANDT" from t; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- CTE Scan on t (cost=98.31..100.33 rows=101 width=32) (actual time=0.031..0.054 rows=2 loops=1) Buffers: shared hit=9 CTE t -> Recursive Union (cost=0.73..98.31 rows=101 width=32) (actual time=0.029..0.052 rows=2 loops=1) Buffers: shared hit=9 -> Result (cost=0.73..0.74 rows=1 width=32) (actual time=0.029..0.029 rows=1 loops=1) Buffers: shared hit=5 InitPlan 3 (returns $1) -> Limit (cost=0.56..0.73 rows=1 width=32) (actual time=0.026..0.027 rows=1 loops=1) Buffers: shared hit=5 -> Index Only Scan using pdw00002_vbak_pkey on pdw00002_vbak pdw00002_vbak_1 (cost=0.56..2375293.75 rows=14214332 width=32) (actual time=0.024..0.024 rows=1 loops=1) Index Cond: ("VBAK_MANDT" IS NOT NULL) Heap Fetches: 1 Buffers: shared hit=5 -> WorkTable Scan on t t_1 (cost=0.00..9.56 rows=10 width=32) (actual time=0.009..0.010 rows=0 loops=2) Filter: ("VBAK_MANDT" IS NOT NULL) Rows Removed by Filter: 0 Buffers: shared hit=4 SubPlan 2 -> Result (cost=0.93..0.94 rows=1 width=32) (actual time=0.015..0.015 rows=1 loops=1) Buffers: shared hit=4 InitPlan 1 (returns $3) -> Limit (cost=0.56..0.93 rows=1 width=32) (actual time=0.013..0.013 rows=0 loops=1) Buffers: shared hit=4 -> Index Only Scan using pdw00002_vbak_pkey on pdw00002_vbak (cost=0.56..1732075.91 rows=4738111 width=32) (actual time=0.012..0.012 rows=0 loops=1) Index Cond: (("VBAK_MANDT" IS NOT NULL) AND ("VBAK_MANDT" > t_1."VBAK_MANDT")) Heap Fetches: 0 Buffers: shared hit=4 Planning time: 0.346 ms Execution time: 0.100 ms (30 rows)