timescaledb
timescaledb copied to clipboard
[Bug]: Planner chunk exclusion not working in VIEWs
What type of bug is this?
Performance issue
What subsystems and features are affected?
Query planner
What happened?
When querying a view on a hypertable constraints on dimensions are not used for plan time chunk exclusion.
TimescaleDB version affected
main
PostgreSQL version used
14.4
What operating system did you use?
Archlinux 2022.07.01 x64
What installation method did you use?
Source
What platform did you run on?
On prem/Self-hosted
Relevant log output and stack trace
explain SELECT * from v2 where time > now() - '168h'::interval;
QUERY PLAN
-------------------------------------------------------------------------------
HashAggregate (cost=4.10..4.15 rows=4 width=18)
Group Key: metrics."time", metrics.device
-> Custom Scan (ChunkAppend) on metrics (cost=0.00..4.07 rows=4 width=18)
Chunks excluded during startup: 3
-> Seq Scan on _hyper_1_4_chunk (cost=0.00..1.02 rows=1 width=18)
Filter: ("time" > (now() - '168:00:00'::interval))
(6 rows)
How can we reproduce the bug?
create table metrics(time timestamptz,device text, value float);
SELECT create_hypertable('metrics','time');
INSERT INTO metrics select '2000-01-01','a',0.5;
INSERT INTO metrics select '2001-01-01','b',0.5;
INSERT INTO metrics select '2002-01-01','b',0.5;
INSERT INTO metrics select now(),'b',0.5;
CREATE VIEW v2 AS SELECT time, device, avg(value) from metrics group by 1,2;
EXPLAIN SELECT * from v2 where time > now() - '168h'::interval;