-
Notifications
You must be signed in to change notification settings - Fork 154
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
Encountered a subscription error: Error applying subscription data: FOREIGN KEY constraint failed #609
Comments
Thanks for reporting and for the repo to reproduce. We'll take a look. |
To be clear. I'm still unable to reproduce the issue outside of production but the repro repo contains part of the affected DB schema most likely to cause the error. |
Right, yes, got it, thanks for the clarification. |
Our TypeScript client enables foreign keys on SQLite. By default, foreign keys in SQLite are checked immediately so the order in which rows are inserted during a transaction does matter. From the docs:
Perhaps two things to figure out if this is the issue:
The Since you don't have this problem on Postgres could it be that you configured Postgres to defer constraint checks? |
Do you think our initial sync mechanism could somehow be sending rows in the wrong order? It may be hard for @gorbak25 to replicate the order of the rows if not using our sync mechanism. |
@thruflo in the repo I've sent the operations arrive from the electric server in the wrong order:
Here ColumnReference is sent first instead of ColumnReferenceSet, this is also what I've observed on production. Does the electric client reoder those operations so foreign keys are respected?(insert in toposort order). Is this driver specific? It was working for a long time but it suddenly stopped working on production when there's too much data. @kevin-dp We don't use deferred foreign keys in postgres. We always insert entities in an order compatible with the constrains (ColumnReferenceSet first, then ColumnReference, lastly we update Column to point to ColumnReferenceSet). |
cc @icehaunter re initial sync ordering. |
@kevin-dp Is the order supposed to matter when doing the initial sync Postgres -> Sqlite?
|
One important detail is that the data arrives after the timeout #605 🤔 Does the same code path trigger in that case? |
Ok tis is very useful information. @balegas was expecting this to be the cause. So because the transaction arrive in a different order it is essentially inserting a row with a foreign key to a record that does not yet exist... We should avoid this.
EDIT: The Electric client does not insert rows in topological order. As pointed out by @davidmartos96 this is not needed because the client enables the |
You're right, order within a transaction doesn't matter. I had missed this |
I wonder if there is some unspecified behaviour after the timeout. It doesn't look relevant for the problem at hand, except that we have very
@icehaunter made the point that on the server, by the virtue of (right now) shapes requiring that all parent, child (and grandchildren) having to be included in a shape definition, all rows and their FKs have to be delivered in same Txn, therefore the order would be irrelevant because of deferred fk checks.
What we know is brittle is that on the client we may handle incoming transactions out of order, but this would require multiple transactions and not a single transaction (which would not be possible during the initial sync ) |
@balegas Yes there is only 1 shape subscription. I'm sometimes still hitting this FK error when syncing a lot of small updates from the backend(and there is no timeout). I'm currently migrating from 0.6.3 to 0.7.1 |
It's different if the issue happens during the initial sync of the shape or during streaming phase. Could you confirm that you no longer see the issue in the finial sync phase, but only during streaming of 'small updates' |
@balegas Yesterday @hugodutka encountered this again when syncing small updates. This happened on 7.1 |
ok, that's slightly better, because during streaming phase we know where we can improve. We have an issue already for addressing that. |
Closing this as addressed by #696. |
Initial clean sync got broken in electric-sql at one point. It happened when the state of 1 client accumulated a few MB's of data, previously it worked fine.
I've verified all foreign key constrains in the Postgres DB and the data model is consistent on the server side.
For now as a workaround we decided to disable foreign key constrains during the initial sync:
I've tried to isolate the bug in a separate repo but i'm unable to reproduce it besides it happening in our production app.
https://github.com/hocus-dev/electric-relation-order-bug
I think what is happening is that when the data get's big enough(https://github.com/hocus-dev/electric-relation-order-bug/blob/e8b09338c2517277b90afacc0c82ea584a39758f/prisma/schema.prisma#L35) the data gets inserted into the DB in the wrong order.
Can you help point me in the right direction with finding the root cause behind the failure?
The text was updated successfully, but these errors were encountered: