I have trigger of after update / after insert and have python listener using pg_notify. Trigger:
DECLARE
channel text :='test_channel';
BEGIN
RAISE NOTICE 'channel % % ',id;
PERFORM pg_notify(channel,json_build_object('id',new.id,'col1',col1,'col2',col2);
RETURN NEW;
END;
Python code to listen :
while True:
conn_psycopg.poll()
while conn_psycopg.notifies:
notify = conn_psycopg.notifies.pop(0)
json_payload = json.loads(notify.payload)
id = json_payload.get('id')
prepare_payload_and_make_api_call(json_payload, id)
It is working fine with less no of records but there is bulk update (10k) happens taking 1 hour to process all records. Currently if bulk update happens in data receives one by one, so one by one processing is taking time.
{id:1, name: abc} {id:2, name: xyz} {id:3, name: pqr}
Because of this behavior we are also processing these records one by one, but we receive bulk records in one go like this:
[{id:1, name: abc}, {id:2, name: xyz}, {id:3, name: pqr}]
then processing will improve.
Please suggest if there are other approaches also which will help to improve speed.