Skip to content

Commit 2358d75

Browse files
committed
Fix locks on bgw_job_stats
We're updating the `bgw_job_stats` table without properly lock the row to prevent concurrent updates from other background workers. Fixed it by properly lock it using SELECT ... FOR UPDATE and issue a COMMIT after each function call;
1 parent d243727 commit 2358d75

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

backfill.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ BEGIN
9898
IF compression_job_id IS NULL THEN
9999
old_time = NULL::timestamptz;
100100
ELSE
101-
SELECT next_start INTO old_time FROM _timescaledb_internal.bgw_job_stat WHERE job_id = compression_job_id;
101+
SELECT next_start INTO old_time FROM _timescaledb_internal.bgw_job_stat WHERE job_id = compression_job_id FOR UPDATE;
102102

103103
IF version = 1 THEN
104104
PERFORM alter_job_schedule(compression_job_id, next_start=> new_time);
@@ -171,6 +171,7 @@ BEGIN
171171
-- Push the compression job out for some period of time so we don't end up compressing a decompressed chunk
172172
-- Don't disable completely because at least then if we fail and fail to move it back things won't get completely weird
173173
SELECT move_compression_job(hypertable_row.id, hypertable_row.schema_name, hypertable_row.table_name, now() + compression_job_push_interval) INTO old_compression_job_time;
174+
COMMIT;
174175

175176
--Get the min and max times in timescale internal format from the source table, this will tell us which chunks we need to decompress
176177
EXECUTE FORMAT($$SELECT _timescaledb_internal.time_to_internal(min(%1$I)) ,
@@ -284,9 +285,10 @@ BEGIN
284285
GET DIAGNOSTICS affected = ROW_COUNT;
285286
RAISE NOTICE '% rows moved in range % to %', affected, r_start, r_end ;
286287
COMMIT;
287-
--Move our job back to where it was
288-
SELECT move_compression_job(hypertable_row.id, hypertable_row.schema_name, hypertable_row.table_name, old_compression_job_time) INTO old_compression_job_time;
289-
COMMIT;
288+
289+
--Move our job back to where it was
290+
SELECT move_compression_job(hypertable_row.id, hypertable_row.schema_name, hypertable_row.table_name, old_compression_job_time) INTO old_compression_job_time;
291+
COMMIT;
290292
END;
291293

292294
$proc$

0 commit comments

Comments
 (0)