realtime icon indicating copy to clipboard operation
realtime copied to clipboard

`postgres_changes` event type does not scope data sent to client

Open acupofjose opened this issue 2 years ago • 1 comments

Bug report

  • [x] I confirm this is a bug with Supabase, not with my own application.
  • [x] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

Documentation leads me to believe that setting a listener type ("UPDATE" or "INSERT") on postgres_changes would scope the data sent to the client to the event listener type.

However, when specifying a listen type it seems that ALL events on the table are sent via websocket to the client. Note: The callback produces the correctly scoped result.

To Reproduce

Given the following table (with realtime enabled and RLS turned off):

create table
  public.test_realtime (
    id bigint generated by default as identity not null,
    data text null,
    created_at timestamp with time zone null default now(),
    constraint test_realtime_pkey primary key (id)
  ) tablespace pg_default;

And using the following code:

const url = "......."
const publicKey = "......"
const supabase = createClient(url, publicKey)

const channel = supabase.channel("testing_postgres")

channel.on("postgres_changes", { event: "UPDATE", schema: "public", table: "test_realtime" }, (data) => {
  console.log(`Called from UPDATE: ${JSON.stringify(data, null, 2)}`)
})

await channel.subscribe()

setTimeout(async () => {
  const table = supabase.from("test_realtime")
  const { data } = await table.insert({ data: `New Data inserted at ${new Date().toLocaleTimeString()}` }).select()

  if (data) {
    const row = data[0]
    await table.update({ data: `New Data updated at: ${new Date().toLocaleTimeString()}` }).eq("id", row.id)
    await table.delete().eq("id", row.id)
  }
}, 3000)

Produces the following websocket logs:

{"topic":"realtime:testing_postgres","event":"phx_join","payload":{"config":{"broadcast":{"ack":false,"self":false},"presence":{"key":""},"postgres_changes":[{"event":"UPDATE","schema":"public","table":"test_realtime"}]}},"ref":"1","join_ref":"1"}
{"event":"phx_reply","payload":{"response":{"postgres_changes":[{"id":46046838,"event":"UPDATE","schema":"public","table":"test_realtime"}]},"status":"ok"},"ref":"1","topic":"realtime:testing_postgres"}
{"event":"presence_state","payload":{},"ref":null,"topic":"realtime:testing_postgres"}
{"event":"system","payload":{"channel":"testing_postgres","extension":"postgres_changes","message":"Subscribed to PostgreSQL","status":"ok"},"ref":null,"topic":"realtime:testing_postgres"}
{"event":"postgres_changes","payload":{"data":{"columns":[{"name":"id","type":"int8"},{"name":"data","type":"text"},{"name":"created_at","type":"timestamptz"}],"commit_timestamp":"2023-04-28T02:20:36.979Z","errors":null,"record":{"created_at":"2023-04-28T02:20:36.978014+00:00","data":"New Data inserted at 9:20:36 PM","id":62},"schema":"public","table":"test_realtime","type":"INSERT"},"ids":[46046838]},"ref":null,"topic":"realtime:testing_postgres"}
{"event":"postgres_changes","payload":{"data":{"columns":[{"name":"id","type":"int8"},{"name":"data","type":"text"},{"name":"created_at","type":"timestamptz"}],"commit_timestamp":"2023-04-28T02:20:37.175Z","errors":null,"old_record":{"id":62},"record":{"created_at":"2023-04-28T02:20:36.978014+00:00","data":"New Data updated at: 9:20:37 PM","id":62},"schema":"public","table":"test_realtime","type":"UPDATE"},"ids":[46046838]},"ref":null,"topic":"realtime:testing_postgres"}
{"event":"postgres_changes","payload":{"data":{"columns":[{"name":"id","type":"int8"},{"name":"data","type":"text"},{"name":"created_at","type":"timestamptz"}],"commit_timestamp":"2023-04-28T02:20:37.301Z","errors":null,"old_record":{"id":62},"schema":"public","table":"test_realtime","type":"DELETE"},"ids":[46046838]},"ref":null,"topic":"realtime:testing_postgres"}

And the following console logs:

Called from UPDATE: {
  "schema": "public",
  "table": "test_realtime",
  "commit_timestamp": "2023-04-28T02:20:37.175Z",
  "eventType": "UPDATE",
  "new": {
    "created_at": "2023-04-28T02:20:36.978014+00:00",
    "data": "New Data updated at: 9:20:37 PM",
    "id": 62
  },
  "old": {
    "id": 62
  },
  "errors": null
}

Expected behavior

Websocket logs should not show events of type DELETE or INSERT when subscribed only to UPDATEs

System information

  • OS: Windows/Mac
  • Browser (if applies): Chrome
  • Version of supabase-js: 2.21.0
  • Version of Node.js: v18.14.0

Additional context

Also in reference to supabase-community/realtime-csharp#27

acupofjose avatar Apr 28 '23 02:04 acupofjose

I wasn't able to replicate the issue, can you check if this behaviour has changed?

Screenshot 2023-11-17 at 19 13 57

filipecabaco avatar Nov 17 '23 19:11 filipecabaco