Created
September 20, 2024 10:45
-
-
Save tmountain/9e03a5ec27335ee638bc42715ce467a0 to your computer and use it in GitHub Desktop.
PGMQ insert benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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