# Postgres Listen/Notify -Listeners increase per every flow deploy

**URL:** https://discourse.nodered.org/t/postgres-listen-notify-listeners-increase-per-every-flow-deploy/12198
**Category:** General
**Created:** [13 June 2019 21:28 UTC](https://discourse.nodered.org/t/postgres-listen-notify-listeners-increase-per-every-flow-deploy/12198 "2019-06-13T21:28:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![novak](https://avatars.discourse-cdn.com/v4/letter/n/6de8d8/32.png) [@novak](https://discourse.nodered.org/u/novak)
#### Post date: [13 June 2019 21:28 UTC](https://discourse.nodered.org/t/postgres-listen-notify-listeners-increase-per-every-flow-deploy/12198/1 "2019-06-13T21:28:34Z")

</div>

I'm using pg.Client.Query(LISTEN [Channel]) in a function node.

My flow starts once with inject node.

I had noticed that per every deploy of the flow the number of listeners increases, meaning per every notified postgress event I'm getting event X number of deployments.

I'm a newbie with the node-red and can't find documentation on how the flow deployment process works.

Is there an option to subscribe to a deployment event in the flow and make cleaning, like closing the connection against pg.

Other suggestions?

Thanks

---

<div class="post-metadata">

### Author: ![novak](https://avatars.discourse-cdn.com/v4/letter/n/6de8d8/32.png) [@novak](https://discourse.nodered.org/u/novak)
#### Post date: [13 June 2019 21:38 UTC](https://discourse.nodered.org/t/postgres-listen-notify-listeners-increase-per-every-flow-deploy/12198/2 "2019-06-13T21:38:34Z")

</div>

my test flow

```auto
[{"id":"9ae31d61.392cb","type":"function","z":"de517d38.f7c6c","name":"CreateUpdateCustomerEvent","func":"var pg = global.get('pgModule');\n\nconst client = new pg.Client({\n user: 'rednode',\n password: 'p6a03a3528dac87de9fe865902dfe1f66bfff3ff551a0d7cd1ef4af3db09b91b5',\n host: 'ec2-54-171-82-109.eu-west-1.compute.amazonaws.com',\n port: '5432',\n database: 'dvh1c9c6mcjrb',\n ssl: true\n})\n\ntry {\n client.connect(function(err) {\n if (err) {\n node.error(err)\n }\n else \n {\n // Listen for all pg_notify channel messages\n client.on('notification', function(msg) {\n \n node.send(msg);\n \n });\n \n client.on('error', (err) => {\n console.error('something bad has happened!', err.stack)\n });\n \n client.on('end', (err) => {\n console.error('session ended', err.stack);\n client.on('notification', function(msg) {\n \n node.send(msg);\n \n });\n client.query('LISTEN customer_changed');\n });\n \n \n \n // Designate which channels we are listening on. Add additional channels with multiple lines.\n client.query('LISTEN customer_changed');\n \n \n \n /* client.query(\"select * from newdev.customer__c where name = 'workdude3'\", \n function (err,res) {\n \n //console.log(res);\n client.end();// closing the connection;\n \n if(err){\n console.log(err);\n node.error(err);\n }\n else {\n \n msg.payload = [res.rows[0]];\n console.log(' ***** payload' + msg.payload)\n }\n });\n */\n }\n \n }\n);\n}\ncatch(err) \n{\n console.log(err);\n node.error(err);\n}\n \nreturn null;","outputs":1,"noerr":0,"x":410,"y":320,"wires":[["b1ba81ef.822b6"]]}]

```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 June 2019 21:40 UTC](https://discourse.nodered.org/t/postgres-listen-notify-listeners-increase-per-every-flow-deploy/12198/3 "2019-06-13T21:40:48Z")

</div>

If you are creating resources that need cleaning up in a function node, then you need to add a handler for the `close` event as described in this section of the docs: [https://nodered.org/docs/writing-functions#sending-messages-asynchronously](https://nodered.org/docs/writing-functions#sending-messages-asynchronously)
