|
| 1 | + |
| 2 | +DROP PROCEDURE IF EXISTS _timescaledb_additional.task_refresh_continuous_aggregate_incremental_runner; |
| 3 | +CREATE PROCEDURE _timescaledb_additional.task_refresh_continuous_aggregate_incremental_runner ( |
| 4 | + job_id int, |
| 5 | + config jsonb |
| 6 | +) LANGUAGE plpgsql AS $BODY$ |
| 7 | +DECLARE |
| 8 | + max_runtime interval := (config->>'max_runtime')::interval; |
| 9 | + global_start_time timestamptz := pg_catalog.clock_timestamp(); |
| 10 | + global_end_time timestamptz; |
| 11 | + app_name text; |
| 12 | +BEGIN |
| 13 | + max_runtime := coalesce(max_runtime, interval '6 hours'); |
| 14 | + global_end_time := global_start_time + max_runtime; |
| 15 | + |
| 16 | + WHILE pg_catalog.clock_timestamp() < global_end_time LOOP |
| 17 | + SET search_path TO 'pg_catalog,pg_temp'; |
| 18 | + SET lock_timeout TO '3s'; |
| 19 | + SET application_name TO 'cagg incremental refresh consumer - idle'; |
| 20 | + |
| 21 | + -- Prevent a hot loop |
| 22 | + PERFORM pg_catalog.pg_sleep(1.0); |
| 23 | + |
| 24 | + SET application_name TO 'cagg incremental refresh consumer - retrieving new task'; |
| 25 | + |
| 26 | + DECLARE |
| 27 | + p_id bigint; |
| 28 | + p_cagg regclass; |
| 29 | + p_window_start timestamptz; |
| 30 | + p_window_end timestamptz; |
| 31 | + p_start_time timestamptz; |
| 32 | + p_end_time timestamptz; |
| 33 | + p_mat_hypertable_id int; |
| 34 | + p_job_id int; |
| 35 | + BEGIN |
| 36 | + SELECT |
| 37 | + q.id, |
| 38 | + q.continuous_aggregate, |
| 39 | + q.window_start, |
| 40 | + q.window_end, |
| 41 | + cagg.mat_hypertable_id, |
| 42 | + coalesce(jobs.job_id, -1) |
| 43 | + INTO |
| 44 | + p_id, |
| 45 | + p_cagg, |
| 46 | + p_window_start, |
| 47 | + p_window_end, |
| 48 | + p_mat_hypertable_id, |
| 49 | + p_job_id |
| 50 | + FROM |
| 51 | + _timescaledb_additional.incremental_continuous_aggregate_refreshes AS q |
| 52 | + JOIN |
| 53 | + pg_catalog.pg_class AS pc ON (q.continuous_aggregate=oid) |
| 54 | + JOIN |
| 55 | + pg_catalog.pg_namespace AS pn ON (relnamespace=pn.oid) |
| 56 | + JOIN |
| 57 | + _timescaledb_catalog.continuous_agg AS cagg ON (cagg.user_view_schema=nspname AND cagg.user_view_name=pc.relname) |
| 58 | + JOIN |
| 59 | + _timescaledb_catalog.hypertable AS h ON (cagg.mat_hypertable_id=h.id) |
| 60 | + LEFT JOIN |
| 61 | + timescaledb_information.jobs ON (proc_name='policy_refresh_continuous_aggregate' AND proc_schema='_timescaledb_functions' AND jobs.config->>'mat_hypertable_id' = cagg.mat_hypertable_id::text) |
| 62 | + WHERE |
| 63 | + q.worker_pid IS NULL AND q.finished IS NULL |
| 64 | + -- We don't want multiple workers to be active on the same CAgg, |
| 65 | + AND NOT EXISTS ( |
| 66 | + SELECT |
| 67 | + FROM |
| 68 | + _timescaledb_additional.incremental_continuous_aggregate_refreshes AS a |
| 69 | + JOIN |
| 70 | + pg_catalog.pg_stat_activity ON (pid=worker_pid) |
| 71 | + WHERE |
| 72 | + a.finished IS NULL |
| 73 | + -- If pids ever get recycled (container/machine restart), |
| 74 | + -- this filter ensures we ignore the old ones |
| 75 | + AND started > backend_start |
| 76 | + AND q.continuous_aggregate = a.continuous_aggregate |
| 77 | + ) |
| 78 | + ORDER BY |
| 79 | + q.priority ASC, |
| 80 | + q.scheduled ASC |
| 81 | + FOR NO KEY UPDATE OF q SKIP LOCKED |
| 82 | + LIMIT |
| 83 | + 1; |
| 84 | + |
| 85 | + IF p_cagg IS NULL THEN |
| 86 | + COMMIT; |
| 87 | + -- There are no items in the queue that we can currently process. We therefore |
| 88 | + -- sleep a while longer before attempting to try again. |
| 89 | + IF global_end_time - interval '30 seconds' < now () THEN |
| 90 | + EXIT; |
| 91 | + ELSE |
| 92 | + SET application_name TO 'cagg incremental refresh consumer - waiting for next task'; |
| 93 | + PERFORM pg_catalog.pg_sleep(0.1); |
| 94 | + CONTINUE; |
| 95 | + END IF; |
| 96 | + END IF; |
| 97 | + |
| 98 | + UPDATE |
| 99 | + _timescaledb_additional.incremental_continuous_aggregate_refreshes |
| 100 | + SET |
| 101 | + worker_pid = pg_backend_pid(), |
| 102 | + started = clock_timestamp() |
| 103 | + WHERE |
| 104 | + id = p_id; |
| 105 | + |
| 106 | + -- Inform others of what we are doing. |
| 107 | + app_name := ' refresh ' || p_window_start::date; |
| 108 | + IF p_window_end::date != p_window_start::date THEN |
| 109 | + app_name := app_name || ' ' || p_window_end::date; |
| 110 | + ELSE |
| 111 | + app_name := app_name || to_char(p_window_start, 'THH24:MI'); |
| 112 | + END IF; |
| 113 | + IF length(app_name) + length(p_cagg::text) > 63 THEN |
| 114 | + app_name := '...' || right(p_cagg::text, 60 - length(app_name)) || app_name; |
| 115 | + ELSE |
| 116 | + app_name := p_cagg::text || app_name; |
| 117 | + END IF; |
| 118 | + PERFORM pg_catalog.set_config( |
| 119 | + 'application_name', |
| 120 | + app_name, |
| 121 | + false |
| 122 | + ); |
| 123 | + |
| 124 | + RAISE NOTICE |
| 125 | + '% - Processing %, (% - %)', |
| 126 | + pg_catalog.to_char(pg_catalog.clock_timestamp(), 'YYYY-MM-DD HH24:MI:SS.FF3OF'), |
| 127 | + p_cagg, |
| 128 | + p_window_start, |
| 129 | + p_window_end; |
| 130 | + |
| 131 | + -- We need to ensure that all other workers now know we are working on this |
| 132 | + -- task. We therefore need to commit once now. This also releases our |
| 133 | + -- access exclusive lock on the queue table. |
| 134 | + COMMIT; |
| 135 | + |
| 136 | + -- We take out a row-level-lock to signal to concurrent workers that *we* |
| 137 | + -- are working on it. By taking this type of lock, we can clean up |
| 138 | + -- this table from different tasks: They can update/delete these rows |
| 139 | + -- if no active worker is working on them, and no lock is established. |
| 140 | + PERFORM |
| 141 | + FROM |
| 142 | + _timescaledb_additional.incremental_continuous_aggregate_refreshes |
| 143 | + WHERE |
| 144 | + id = p_id |
| 145 | + FOR NO KEY UPDATE; |
| 146 | + |
| 147 | + CALL _timescaledb_functions.policy_refresh_continuous_aggregate( |
| 148 | + -1, |
| 149 | + config => jsonb_build_object( |
| 150 | + 'end_offset', (clock_timestamp() - p_window_end)::interval(0), |
| 151 | + 'start_offset', (clock_timestamp() - p_window_start)::interval(0), |
| 152 | + 'mat_hypertable_id', p_mat_hypertable_id |
| 153 | + ) |
| 154 | + ); |
| 155 | + |
| 156 | + UPDATE |
| 157 | + _timescaledb_additional.incremental_continuous_aggregate_refreshes |
| 158 | + SET |
| 159 | + finished = clock_timestamp() |
| 160 | + WHERE |
| 161 | + id = p_id; |
| 162 | + COMMIT; |
| 163 | + |
| 164 | + SET application_name TO 'cagg incremental refresh consumer - idle'; |
| 165 | + END; |
| 166 | + END LOOP; |
| 167 | + |
| 168 | + RAISE NOTICE 'Shutting down worker, as we exceeded our maximum runtime (%)', max_runtime; |
| 169 | +END; |
| 170 | +$BODY$; |
| 171 | + |
| 172 | +GRANT EXECUTE ON PROCEDURE _timescaledb_additional.task_refresh_continuous_aggregate_incremental_runner TO pg_database_owner; |
0 commit comments