postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Duplicate notifications when subscribing to a publication with postgresjs

Open ali-shabani opened this issue 2 years ago • 2 comments

I have created a publication in PostgreSQL and I'm using subscribe to listen to changes. I'm using this code:

const { unsubscribe } = await sql.subscribe(
  'update:users',
  (row, { command, relation, key, old }) => {
    console.log(row, key, old)
  }
)

The problem is that my callback is called twice when I'm updating a record on the users table. Also, I get false for key and null for old in both callbacks. Is there any bug in the implementation or did I do something wrong?

Expected behavior:

I expect the callback to be called only once when I update a record on the users table. I also expect to get the correct values for key and old in the callback.

Actual behavior:

The callback is called twice when I update a record on the users table. I get false for key and null for old in both callbacks.

Environment:

• postgres version: ^3.4.3

• PostgreSQL version: docker image: postgres:15.1

• Node.js version: v18.18.2

ali-shabani avatar Dec 06 '23 15:12 ali-shabani

I have created the publication using below command:

CREATE PUBLICATION alltables FOR ALL TABLES;

and I also tried to initialize the postgres with and without providing the publications options. the result was unchanged.

ali-shabani avatar Dec 06 '23 15:12 ali-shabani

I think you will need to post more of your code for anyone to help you. I have been using the subscribe and never experienced any issue of that sort. It most likely to do with you other setups rather than postgres.js.

Louis-Tian avatar Jan 19 '24 02:01 Louis-Tian