PostgreSQL notify in loop

424 Views Asked by At

I have used the pgjdbc-ng for Postgresql listen and notify.

I'd like to perform notify in the loop.

My example code:

FOR idx  IN 0..3 LOOP
   PERFORM pg_notify('q_event','test');
END LOOP;

I expect this using listen:

test test test

But I just receive one text.

I want to know it is possible. If then, teach me please. Thank you for your answer.

1

There are 1 best solutions below

0
On BEST ANSWER

Consecutive notifies with the same payload are treated as a single one. Try this:

FOR idx  IN 0..3 LOOP
   PERFORM pg_notify('q_event', format('test %s', idx));
END LOOP;

A client listening on the channel q_event will receive four messages (from 0 to 3).