timescale / timescaledb

An open-source time-series SQL database optimized for fast ingest and complex queries. Packaged as a PostgreSQL extension.
https://www.timescale.com/
Other
16.83k stars 852 forks source link

wrong order for direct select from a single partially compressed chunk #7084

Open akuzm opened 5 days ago

akuzm commented 5 days ago

As reported by Sven, using the ht_metrics_partially_compressed table from tsbench:

explain (costs off) select * from _timescaledb_internal._hyper_41_787_chunk order by device, time limit 1;
                                                        QUERY PLAN                                                        
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 Limit
   ->  Append
         ->  Custom Scan (DecompressChunk) on _hyper_41_787_chunk
               ->  Sort
                     Sort Key: compress_hyper_43_801_chunk.device, compress_hyper_43_801_chunk._ts_meta_sequence_num DESC
                     ->  Seq Scan on compress_hyper_43_801_chunk
         ->  Sort
               Sort Key: _hyper_41_787_chunk.device, _hyper_41_787_chunk."time"
               ->  Seq Scan on _hyper_41_787_chunk

The top-level sorting is wrong, because the plan should be MergeAppend. Apparently this never worked, reproduces both on the current 2.16.0-dev and on 2.12.