Skip to content

Instantly share code, notes, and snippets.

@tmountain
Created September 20, 2024 10:45
Show Gist options
  • Save tmountain/9e03a5ec27335ee638bc42715ce467a0 to your computer and use it in GitHub Desktop.
Save tmountain/9e03a5ec27335ee638bc42715ce467a0 to your computer and use it in GitHub Desktop.
PGMQ insert benchmark
-- SELECT pgmq.create('my_queue');
DO $$
DECLARE
i INT;
batch_size INT := 1000; -- Change this to experiment with different batch sizes
total_messages INT := 1000000; -- Total number of messages to send
msgs JSONB[];
start_time TIMESTAMP;
end_time TIMESTAMP;
queue_name TEXT := 'my_queue';
BEGIN
-- Start the clock
start_time := clock_timestamp();
-- Loop through the total number of messages, sending them in batches
FOR i IN 1..(total_messages / batch_size) LOOP
-- Generate a batch of messages (you can adjust the message content as needed)
msgs := ARRAY(SELECT jsonb_build_object('foo', 'bar' || j::text) FROM generate_series(1, batch_size) j);
-- Call the send_batch function to insert the batch of messages
PERFORM pgmq.send_batch(
queue_name => queue_name,
msgs => msgs,
delay => 5 -- Optional delay
);
END LOOP;
-- End the clock
end_time := clock_timestamp();
-- Output the time taken for the benchmark
RAISE NOTICE 'Time taken to insert % messages in batches of %: %', total_messages, batch_size, end_time - start_time;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment