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

sql: INSERT with self-referencing FK check issues #20041

Closed
BramGruneir opened this issue Nov 14, 2017 · 6 comments
Closed

sql: INSERT with self-referencing FK check issues #20041

BramGruneir opened this issue Nov 14, 2017 · 6 comments
Labels
A-sql-execution Relating to SQL execution. A-sql-fks A-sql-pgcompat Semantic compatibility with PostgreSQL C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.

Comments

@BramGruneir
Copy link
Member

BramGruneir commented Nov 14, 2017

When inserting in a self-referential table, you can't insert multiple values in a single statement if they rely on a value earlier in the statement.

CREATE TABLE self (
  id INT PRIMARY KEY
 ,other_id INT REFERENCES self
);
CREATE TABLE
Time: 9.248944ms
INSERT INTO self VALUES (1,NULL), (2, 1);
pq: foreign key violation: value [1] not found in self@primary [id]

But this works.

INSERT INTO self VALUES (1,NULL);
INSERT INTO self VALUES (2, 1);

This is due to each individual insert statement using a single batch and batches can't read previous writes.
However, if we were to use a single batch per inserted element, it would drastically slow down the inserts overall.

Another, slightly different version of this problem:

CREATE TABLE bah (
  id INT NOT NULL,
  id2 INT NULL,
  CONSTRAINT "primary" PRIMARY KEY (id ASC),
  CONSTRAINT aaa FOREIGN KEY (id2) REFERENCES bah (id),
  INDEX bah_auto_index_aaa (id2 ASC),
  FAMILY "primary" (id, id2)
)

INSERT INTO bah VALUES (1,1);
pq: foreign key violation: value [1] not found in bah@primary [id]

This fails because we eagerly check the FKs instead of waiting to the end of the statement. But even if we did wait to check at the end of the statement, then we would not yet have the batch committed and the FK check would still fail.
That being said, it might be possible to elide this FK check if the values match in the mutation path.

@BramGruneir
Copy link
Member Author

Oh, and to add to this, it does work on postgres.

@knz knz added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. A-sql-pgcompat Semantic compatibility with PostgreSQL labels Apr 27, 2018
@knz
Copy link
Contributor

knz commented Apr 27, 2018

This may have been fixed on #23373 . testing.

@knz
Copy link
Contributor

knz commented Apr 27, 2018

nope, not fixed.

@knz knz changed the title sql: Inserts can't see their earlier writes sql: Insert FK checks can't see their earlier writes Apr 27, 2018
@knz knz added the A-sql-execution Relating to SQL execution. label Apr 27, 2018
@BramGruneir BramGruneir changed the title sql: Insert FK checks can't see their earlier writes sql: INSERT with self-referencing FK check issues Nov 27, 2018
@BramGruneir
Copy link
Member Author

Updated this based on #32610 and merging that issue into this.

@knz
Copy link
Contributor

knz commented Dec 4, 2019

Solution is described (and will be implemented by fixing) #33475.

@asubiotto
Copy link
Contributor

#33475 is not closed so assuming this can be closed as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-execution Relating to SQL execution. A-sql-fks A-sql-pgcompat Semantic compatibility with PostgreSQL C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.
Projects
None yet
Development

No branches or pull requests

4 participants