Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Big O notation of Postgresql Max with timescaledb index

I am writing some scripts that need to determine the last timestamp a timeseries datastream that can be interupted.

I am currently working out the most efficient way to do this, the simplest would be to look for the largest timestamp using MAX. As the tables in question are timescaledb hypertables they are indexed, so in theory it should be a case of following the index to find the largest and this should be very efficient operation. However, I am not sure if this is actually true and was wondering if anyone knew how max scales if it’s working down an index, I know it’s an O(n) function normally.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If there is an index on the column, max can use the index and will become O(1):

EXPLAIN (COSTS OFF) SELECT max(attrelid) FROM pg_attribute;

                                          QUERY PLAN                                          
══════════════════════════════════════════════════════════════════════════════════════════════
 Result
   InitPlan 1 (returns $0)
     ->  Limit
           ->  Index Only Scan Backward using pg_attribute_relid_attnum_index on pg_attribute
                 Index Cond: (attrelid IS NOT NULL)
(5 rows)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading