Skip to content

Commit

Permalink
Fix regression: not respecting interval in read_with_poll (#343)
Browse files Browse the repository at this point in the history
* Fix regression: not respecting interval in read_with_poll

The regression happened in the port to PL/pgSQL.

Division of integers on postgres yields integer results. Our unit conversion from
milliseconds to seconds didn't consider that, then returning wrong results.
This caused `pgmq.read_with_poll` to sleep 0s instead of `poll_interval_ms`
if `poll_interval_ms` was under 1000, using 100% of CPU.

The fix is simply converting `poll_interval_ms` to `numeric` before doing the
division, which produces a `numeric` result as originally intended.

* Fix tests
  • Loading branch information
v0idpwn authored Nov 15, 2024
1 parent 11488be commit 897f666
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pgmq-extension/sql/pgmq--1.4.4--1.5.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ BEGIN
IF FOUND THEN
RETURN;
ELSE
PERFORM pg_sleep(poll_interval_ms / 1000);
PERFORM pg_sleep(poll_interval_ms::numeric / 1000);
END IF;
END LOOP;
END;
Expand Down
2 changes: 1 addition & 1 deletion pgmq-extension/sql/pgmq.sql
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ BEGIN
IF FOUND THEN
RETURN;
ELSE
PERFORM pg_sleep(poll_interval_ms / 1000);
PERFORM pg_sleep(poll_interval_ms::numeric / 1000);
END IF;
END LOOP;
END;
Expand Down
2 changes: 1 addition & 1 deletion pgmq-extension/test/expected/base.out
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ SELECT ARRAY(

-- Read with poll will poll until the first message is available
SELECT clock_timestamp() AS start \gset
SELECT msg_id = :msg_id1 FROM pgmq.read_with_poll('test_read_queue', 10, 5, 5, 100);
SELECT msg_id = :msg_id1 FROM pgmq.read_with_poll('test_read_queue', 10, 5, 6, 100);
?column?
----------
t
Expand Down
2 changes: 1 addition & 1 deletion pgmq-extension/test/sql/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ SELECT ARRAY(

-- Read with poll will poll until the first message is available
SELECT clock_timestamp() AS start \gset
SELECT msg_id = :msg_id1 FROM pgmq.read_with_poll('test_read_queue', 10, 5, 5, 100);
SELECT msg_id = :msg_id1 FROM pgmq.read_with_poll('test_read_queue', 10, 5, 6, 100);
SELECT clock_timestamp() - :'start' > '3 second'::interval;

-- test_purge_queue
Expand Down

0 comments on commit 897f666

Please sign in to comment.