#10500·timescaledb

XX000: ORDER/GROUP BY expression not found in list, with caggs

Author: akuzmCreated Aug 25, 2026Updated Sep 8, 2026
LabelsbugContinuous Aggregateplannerinternal error
-- ORDER BY on the aggregate column of a DISTINCT grouped subquery over a
-- continuous aggregate errors out in the planner when the GROUP BY reordering
-- optimization for continuous aggregates is enabled (the default).

create table cloud_cover (observed_at timestamptz not null, cover_percent float);
select create_hypertable('cloud_cover', 'observed_at');

create materialized view daily_cloud_cover with (timescaledb.continuous) as
select time_bucket('1 day', observed_at) as day, max(cover_percent) as max_cover
from cloud_cover group by 1 with no data;

create table stations (region text);

-- baseline: with the reordering optimization disabled the query works
set timescaledb.enable_cagg_reorder_groupby = off;
select * from (
    select distinct s.region, max(c.max_cover) as peak
    from daily_cloud_cover c, stations s
    group by s.region) q
order by peak;

-- the same query with the default setting
reset timescaledb.enable_cagg_reorder_groupby;
select * from (
    select distinct s.region, max(c.max_cover) as peak
    from daily_cloud_cover c, stations s
    group by s.region) q
order by peak;  -- ERROR:  ORDER/GROUP BY expression not found in list

This query only fails on PG18 for me.

It looks that the optimization is meant to handle the old format caggs which are now unspported, so maybe it can be removed altogether.