postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Logical replication, need to get only the changed column values, not all column values, on update event row

Open uasan opened this issue 1 year ago • 1 comments

Hello.

When an update event occurs, all columns are sent to the subscription handler, even those that were not updated. In my business logic, I need to understand which columns have changed their values. How can this be done?

Thanks.

uasan avatar Dec 17 '24 03:12 uasan

YOu need to enable full identity:

await sql`ALTER TABLE my_table REPLICA IDENTITY FULL`

Then the old argument of the subscribe's callback, will be set and a custom diff function must compare row with old:

  const { unsubscribe } = await subscriptionSql.subscribe(
    '*:my_table',
    (row, { command, relation, key, old }) => {
      // diff row vs old
    },
    () => {
      // Callback on initial connect and potential reconnects
    }
  )```

Eomm avatar Jun 16 '25 13:06 Eomm