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

[YSQL][Colocation] TRUNCATE TABLE breaks index consistency #10912

Closed
frozenspider opened this issue Dec 20, 2021 · 1 comment
Closed

[YSQL][Colocation] TRUNCATE TABLE breaks index consistency #10912

frozenspider opened this issue Dec 20, 2021 · 1 comment
Assignees
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug

Comments

@frozenspider
Copy link
Contributor

Description

When using colocated database and tablegroup, TRUNCATE TABLE seem to only truncate the table, leaving index content intact.

Examples:

With colocated database:

CREATE DATABASE test_clc COLOCATED TRUE;
\c test_clc

CREATE TABLE tg1_table1(
  v1 int,
  v2 int UNIQUE,
  v3 text
);

INSERT INTO tg1_table1 VALUES (1, 1, 'v2'),  (1, 2, 'v2'), (3, 3, 'v2'), (4, 4, 'v2');
CREATE INDEX tg1_table1_v1_idx ON tg1_table1 (v1);

\d tg1_table1
             Table "public.tg1_table1"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 v1     | integer |           |          |
 v2     | integer |           |          |
 v3     | text    |           |          |
Indexes:
    "tg1_table1_v2_key" UNIQUE CONSTRAINT, lsm (v2 ASC)
    "tg1_table1_v1_idx" lsm (v1 ASC)

SELECT v1 FROM tg1_table1 WHERE v1 IN (1, 2, 4);
 v1
----
  1
  1
  4
(3 rows)

SELECT v2 FROM tg1_table1 WHERE v2 IN (1, 2, 4);
 v2
----
  1
  2
  4
(3 rows)

SELECT * FROM tg1_table1 WHERE v2 IN (1, 2, 4);
 v1 | v2 | v3
----+----+----
  1 |  1 | v2
  1 |  2 | v2
  4 |  4 | v2
(3 rows)

TRUNCATE TABLE tg1_table1;

SELECT v1 FROM tg1_table1 WHERE v1 IN (1, 2, 4);
 v1
----
  1
  1
  4
(3 rows)

SELECT v2 FROM tg1_table1 WHERE v2 IN (1, 2, 4);
 v2
----
  1
  2
  4
(3 rows)

SELECT * FROM tg1_table1 WHERE v2 IN (1, 2, 4);
ERROR:  Query error: ybctid DocKey([], ["l\x07S{\xcb\xaeF_\xb6\x93,\xbd\xd3\x0c\x15\xeb"]) not found in indexed table

With tablegroup:

CREATE TABLEGROUP tg1;
CREATE TABLE tg1_table1(
  v1 int,
  v2 int UNIQUE,
  v3 text
) TABLEGROUP tg1;

INSERT INTO tg1_table1 VALUES (1, 1, 'v2'),  (1, 2, 'v2'), (3, 3, 'v2'), (4, 4, 'v2');
CREATE INDEX tg1_table1_v1_idx ON tg1_table1 (v1);

SELECT v1 FROM tg1_table1 WHERE v1 IN (1, 2, 4);
 v1
----
  1
  1
  4
(3 rows)

SELECT v2 FROM tg1_table1 WHERE v2 IN (1, 2, 4);
 v2
----
  1
  2
  4
(3 rows)

SELECT * FROM tg1_table1 WHERE v2 IN (1, 2, 4);
 v1 | v2 | v3
----+----+----
  1 |  1 | v2
  1 |  2 | v2
  4 |  4 | v2
(3 rows)

TRUNCATE TABLE tg1_table1;

SELECT v1 FROM tg1_table1 WHERE v1 IN (1, 2, 4);
 v1
----
  1
  1
  4
(3 rows)

SELECT v2 FROM tg1_table1 WHERE v2 IN (1, 2, 4);
 v2
----
  1
  2
  4
(3 rows)

SELECT * FROM tg1_table1 WHERE v2 IN (1, 2, 4);
ERROR:  Query error: ybctid DocKey([], ["\x99_U?\x85\xfaO\x7f\xabz\xe2\x8e\x0b:uM"]) not found in indexed table
@frozenspider frozenspider added kind/bug This issue is a bug area/ysql Yugabyte SQL (YSQL) labels Dec 20, 2021
@frozenspider frozenspider added this to Backlog in YSQL via automation Dec 20, 2021
@frozenspider frozenspider added this to To do in Colocation via automation Dec 20, 2021
@lnguyen-yugabyte lnguyen-yugabyte self-assigned this Jan 11, 2022
lnguyen-yugabyte added a commit that referenced this issue Jan 18, 2022
…with the table

Summary:
We fix the issue that, on a colocated table with a primary / unique index, one sends a TRUNCATE TABLE request, which deletes the table's content. However, the corresponding indexes are not truncated, resulting to subsequent inserts failing due to duplicate key.

The fix is to send truncate colocated requests for the indexes associated with the table. The previous implementation has a typo which didn't truncate the indexes.

Test Plan: Unit tests added for SQL test and java test.

Reviewers: rskannan, smishra, jason

Reviewed By: jason

Subscribers: smishra, rskannan, jason, lnguyen, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D14761
YSQL automation moved this from Backlog to Done Jan 18, 2022
Colocation automation moved this from To do to Done Jan 18, 2022
lnguyen-yugabyte added a commit that referenced this issue Jan 20, 2022
Summary: For some reasons the previous diff (https://phabricator.dev.yugabyte.com/D14761) has a redundant line in the ysql_colocation_test. We remove that extra line in this diff.

Test Plan: check jenkin builds https://detective.dev.yugabyte.com/D14896.html

Reviewers: jason

Reviewed By: jason

Subscribers: yql

Differential Revision: https://phabricator.dev.yugabyte.com/D14896
lnguyen-yugabyte added a commit that referenced this issue Jan 21, 2022
… indexes associated with the table

Summary:
We fix the issue that, on a colocated table with a primary / unique index, one sends a TRUNCATE TABLE request, which deletes the table's content. However, the corresponding indexes are not truncated, resulting to subsequent inserts failing due to duplicate key.

The fix is to send truncate colocated requests for the indexes associated with the table. The previous implementation has a typo which didn't truncate the indexes.

Original commits / diffs:
ae984b6 / D14761
74158cb / D14896

Test Plan: Unit tests added for SQL test and java test.

Reviewers: rskannan, smishra, jason

Reviewed By: jason

Subscribers: yql, lnguyen, jason, rskannan, smishra

Differential Revision: https://phabricator.dev.yugabyte.com/D14900
lnguyen-yugabyte added a commit that referenced this issue Jan 21, 2022
… indexes associated with the table

Summary:
UPDATE for 2.6: we fix the merge conflicts.

We fix the issue that, on a colocated table with a primary / unique index, one sends a TRUNCATE TABLE request, which deletes the table's content. However, the corresponding indexes are not truncated, resulting to subsequent inserts failing due to duplicate key.

The fix is to send truncate colocated requests for the indexes associated with the table. The previous implementation has a typo which didn't truncate the indexes.

Original commits / diffs:
ae984b6 / D14761
74158cb / D14896

Test Plan: Unit tests added for SQL test and java test.

Reviewers: rskannan, smishra, jason

Reviewed By: jason

Subscribers: yql, lnguyen, jason, rskannan, smishra

Differential Revision: https://phabricator.dev.yugabyte.com/D14904
@kripasreenivasan
Copy link
Contributor

@lnguyen-yugabyte
Please backport this fix to 2.12

lnguyen-yugabyte added a commit that referenced this issue Feb 1, 2022
…xes associated with the table

Summary:
We fix the issue that, on a colocated table with a primary / unique index, one sends a TRUNCATE TABLE request, which deletes the table's content. However, the corresponding indexes are not truncated, resulting to subsequent inserts failing due to duplicate key.

The fix is to send truncate colocated requests for the indexes associated with the table. The previous implementation has a typo which didn't truncate the indexes.

Original commits / diffs:
ae984b6 / D14761
74158cb / D14896

Test Plan: Unit tests added for SQL test and java test.

Reviewers: rskannan, smishra, jason

Reviewed By: jason

Subscribers: yql, lnguyen, jason, rskannan, smishra

Differential Revision: https://phabricator.dev.yugabyte.com/D15181
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug
Projects
YSQL
  
Done
Development

No branches or pull requests

4 participants