Once in a while I run into "No space left on the machine" after running a lengthy query. I also like to have a reasonable estimation, so that the IT team can purchase a correct cloud server. My question is: Given a query, is there a way to predict how much hard drive space it might cost? Is adding the size of involved tables a good enough estimation?
EDIT: Currently I am using Postgres 10. The query in question is about creating a materialized view
and do some arithmetic, lag
and group by
on a single table with a size of around 750 GB.
Query looks like
CREATE MATERIALIZED VIEW foo AS
WITH change_data AS (
SELECT (
CAST(lagged_data.todays_count AS double precision) / CAST(lagged_data.previous_count AS double precision)
- CAST(1 AS double precision)
) / GREATEST(EXTRACT(
days from (date_belongs_to - previous_date_belongs_to)
), 1) AS change,
lagged_data.date_belongs_to,
lagged_data.user_id
FROM lagged_data
WHERE lagged_data.previous_count IS NOT NULL
),
lagged_change_data AS (
SELECT change_data.change,
LAG(change_data.change) OVER (
PARTITION BY change_data.user_id
ORDER BY change_data.date_belongs_to
) AS previous_change,
change_data.user_id
FROM change_data
),
acceleration AS (
SELECT AVG(ABS(lagged_change_data.change - lagged_change_data.previous_change)) AS fgs,
lagged_change_data.user_id
FROM lagged_change_data
WHERE lagged_change_data.previous_change IS NOT NULL
GROUP BY lagged_change_data.user_id
)