8000 release-20.1: opt: fix bug where ON CONFLICT DO NOTHING ignores some input by rytaft · Pull Request #59172 · cockroachdb/cockroach · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

release-20.1: opt: fix bug where ON CONFLICT DO NOTHING ignores some input #59172

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

Merged
merged 1 commit into from
Jan 20, 2021

Conversation

rytaft
Copy link
Collaborator
@rytaft rytaft commented Jan 20, 2021

Backport 1/1 commits from #59147.

/cc @cockroachdb/release


Prior to this patch, it was possible for some valid input to an
INSERT ... ON CONFLICT DO NOTHING to be discarded. For example,
consider the following example:

CREATE TABLE uniq (
  k INT PRIMARY KEY,
  v INT UNIQUE,
  w INT UNIQUE,
  x INT,
  y INT DEFAULT 5,
  UNIQUE (x, y)
);

INSERT INTO uniq VALUES (1, 1, 1, 1, 1);

INSERT INTO uniq VALUES (1, 20, 20, 20, 20),
                        (20, 1, 20, 20, 20),
                        (20, 20, 20, 20, 20)
ON CONFLICT DO NOTHING;

Since row (20, 20, 20, 20, 20) does not conflict with the existing
row in uniq, it should be inserted. However, prior to this patch, all
three rows in the second INSERT statement were discarded.

This commit fixes the problem by applying the upsert-distinct-on
operators for each index after all conflicting rows are removed by
left-joins + filters.

Fixes #59125

Release note (bug fix): Fixed a bug in which some non-conflicting rows
provided as input to an INSERT ... ON CONFLICT DO NOTHING statement could
be discarded, and not inserted. This could happen in cases where the
table had one or more unique indexes in addition to the primary index,
and some of the rows in the input conflicted with existing values in one
or more unique index. This scenario could cause the rows that did not
conflict to be erroneously discarded. This has now been fixed.

Prior to this patch, it was possible for some valid input to an
INSERT ... ON CONFLICT DO NOTHING to be discarded. For example,
consider the following example:

CREATE TABLE uniq (
  k INT PRIMARY KEY,
  v INT UNIQUE,
  w INT UNIQUE,
  x INT,
  y INT DEFAULT 5,
  UNIQUE (x, y)
);

INSERT INTO uniq VALUES (1, 1, 1, 1, 1);

INSERT INTO uniq VALUES (1, 20, 20, 20, 20),
                        (20, 1, 20, 20, 20),
                        (20, 20, 20, 20, 20)
ON CONFLICT DO NOTHING;

Since row (20, 20, 20, 20, 20) does not conflict with the existing
row in uniq, it should be inserted. However, prior to this patch, all
three rows in the second INSERT statement were discarded.

This commit fixes the problem by applying the upsert-distinct-on
operators for each index after all conflicting rows are removed by
left-joins + filters.

Fixes cockroachdb#59125

Release note (bug fix): Fixed a bug in which some non-conflicting rows
provided as input to an INSERT ... ON CONFLICT DO NOTHING statement could
be discarded, and not inserted. This could happen in cases where the
table had one or more unique indexes in addition to the primary index,
and some of the rows in the input conflicted with existing values in one
or more unique index. This scenario could cause the rows that did not
conflict to be erroneously discarded. This has now been fixed.
@rytaft rytaft requested a review from a team as a code owner January 20, 2021 01:01
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator
@mgartner mgartner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 5 of 5 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @RaduBerinde)

@rytaft rytaft merged commit 3d762ac into cockroachdb:release-20.1 Jan 20, 2021
@rytaft rytaft deleted the backport20.1-59147 branch January 20, 2021 01:55
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

Successfully merging this pull request may close these issues.

3 participants
0