I have the following procedure:
CREATE OR REPLACE PROCEDURE delete_events(job_id int, config jsonb)
LANGUAGE plpgsql
AS
$$
DECLARE
l_rec record;
decompress_sql text;
policy_id integer;
delete_sql text;
run_compression_sql text;
BEGIN
FOR l_rec IN SELECT table_schema, TABLE_NAME
FROM information_schema.tables
WHERE TABLE_NAME = 'log_win'
AND table_schema LIKE '%org_%'
LOOP
EXECUTE format('SELECT job_id
FROM timescaledb_information.jobs
WHERE proc_name = ''policy_compression''
AND hypertable_schema = %L
AND hypertable_name = ''log_win''', l_rec.table_schema)
INTO policy_id;
decompress_sql := format('SELECT public.decompress_chunk(c, true)
FROM public.show_chunks(''%I.log_win'') c', l_rec.table_schema);
EXECUTE decompress_sql;
delete_sql := format('WITH summary AS (
SELECT time, device_id, ROW_NUMBER() OVER (PARTITION BY device_id
ORDER BY time DESC) AS rank
FROM %I.log_win
JOIN %I.device USING (device_id)
)
DELETE FROM %I.log_win
USING summary
WHERE summary.device_id = log_win.device_id
AND summary.rank = 2000
AND log_win.time < summary.time', l_rec.table_schema, l_rec.table_schema, l_rec.table_schema);
EXECUTE delete_sql;
run_compression_sql := format('CALL public.run_job(%L)', policy_id);
EXECUTE run_compression_sql;
END LOOP;
END
$$;
This procedure runs through all my schemas, decompresses the log_win
table, trims it and then it calls a compression_policy job in the end that basically compresses the log_win tables back again.
When I run this procedure I get the following error:
ERROR: portal snapshots (1) did not account for all active snapshots (2)
CONTEXT: PL/pgSQL function _timescaledb_internal.policy_compression_execute(integer,integer,anyelement,integer,boolean,boolean) line 45 at COMMIT
SQL statement "CALL _timescaledb_internal.policy_compression_execute(
job_id, htid, lag_value::INTERVAL,
maxchunks, verbose_log, recompress_enabled
)"
PL/pgSQL function _timescaledb_internal.policy_compression(integer,jsonb) line 51 at CALL
SQL statement "CALL public.run_job('1085')"
PL/pgSQL function delete_events(integer,jsonb) line 37 at EXECUTE
SQL state: XX000
I have no clue what it means, but the records is never trimmed… All I know is that it has something to do with the last step:
run_compression_sql := format('CALL public.run_job(%L)', policy_id);
EXECUTE run_compression_sql;
When I remove it, everything works fine. Also it is only the job_id 1085, it can run jobs for other schemas on identical tables without any issues.
How do I solve this?