This is really cool!
I'm curious why CTEs don't seem to be getting used here?
SELECT
(
SELECT
(
t.data,
t.created_at
)::jsonb_x_timestamp
FROM
telemetries t
WHERE
server_id = 'a0dcc312-0623-af60-4dc0-238301cc9bf8'
AND data ? 'load_avg'
AND t.created_at >= series.target
AND t.created_at <= (series.target + INTERVAL '2592 seconds')
LIMIT 1
)
FROM
(
SELECT
target
FROM
GENERATE_SERIES(
'2020-09-01 00:00:00'::timestamp,
'2020-09-15 23:59:59'::timestamp,
INTERVAL '2592 seconds'
) target
) series
I'm curious on the performance of: WITH series AS SELECT
target
FROM
GENERATE_SERIES(
'2020-09-01 00:00:00'::timestamp,
'2020-09-15 23:59:59'::timestamp,
INTERVAL '2592 seconds'
) target
SELECT
(
t.data,
t.created_at
)::jsonb_x_timestamp
FROM
telemetries t
WHERE
server_id = 'a0dcc312-0623-af60-4dc0-238301cc9bf8'
AND data ? 'load_avg'
AND t.created_at >= series.target
AND t.created_at <= (series.target + INTERVAL '2592 seconds')
LIMIT 1
or even shuffling some of the other selects and chaining CTEs together. I'm even curious if you could somehow use a combination of CTEs to generate something more suitable for use with SYSTEM_ROWS (TIL!! will keep this in my pocket for the future!).CTEs are never a guarantee for performance improvements (they make things worse at times) but I see them getting ignored a lot when at times they are a huge boost.
Edit: wow the formatting is really bad, hopefully the code stuff doesn't kill it for mobile users