supabase / pg_replicate

Build Postgres replication apps in Rust
Apache License 2.0
559 stars 19 forks source link

Error when table does not have a replica identity #22

Closed olirice closed 1 month ago

olirice commented 2 months ago

When a table included in the referenced publication does not have a replica identity, pg_replicate crashes on start

-- Create a table with no replica identity
create table foo(
  id int,
  body text
);

create publication abc for table foo;
drop publication abc;

Test the stdout example

cargo run --example stdout -- \
  --db-host=db.<ref>.supabase.co \
  --db-port=5432 --db-name=postgres \
  --db-username=postgres \
  --db-password=<PW> \
  cdc abc my_pub_slot

Results in

Screenshot 2024-07-30 at 11 15 11 AM

That occurs because

select pg_get_replica_identity_index('foo'::regclass::oid)

returns null

https://github.com/imor/pg_replicate/blob/815e96d36c8a29ada355b8b3737c80ee9b4aa780/pg_replicate/src/clients/postgres.rs#L136-L139

which causes i.indkey to be null, and then is_identity to be null.

Logical replication on tables without a replica identity is support for inserts but not for updates or deletes. If an update or delete is found, an error is thrown.

I think it would make sense to ignore that nuance ^ and enforce that all tables in the publication have a replica identity before startup. If any of them do not have a replica identity, then raise an error message explaining that they need to add a unique key or set the replica identity to full.

olirice commented 1 month ago

dupe of #18