Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscriptions might lose data #982

Open
josephg opened this issue Nov 12, 2024 · 0 comments
Open

Subscriptions might lose data #982

josephg opened this issue Nov 12, 2024 · 0 comments

Comments

@josephg
Copy link

josephg commented Nov 12, 2024

I'm currently implementing CDC in a web application on top of postgres.

I want to:

  • Fetch the users table
  • Be notified whenever the users table changes and update my local copy

But obviously, I also need to be careful of the race condition: What happens if the data is modified between my fetch and subscribe calls?

The "right answer", as far as I can tell, is to get the WAL log location (lsn) along with my fetch query, and then subscribe to all changes from that location onwards. For example, to fetch:

let startLsn
{
  await sql.begin('ISOLATION LEVEL REPEATABLE READ', async sql => {
    const lsnResult = await sql`SELECT pg_current_wal_lsn() as lsn`;
    startLsn = lsnResult[0].lsn

    const result = await sql<User[]>`SELECT * FROM users`;
    result.forEach((user) => {
      users.set(user.id, user);
    });
  })
}

But then to subscribe I need to pass the LSN in. I want to make this query:

sql`START_REPLICATION SLOT users_cache_slot LOGICAL ${startLsn} (proto_version '1', publication_names 'users_pub')`

Unfortunately, the subscription code in postgres can't do this as far as I can tell?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant