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] Support ALTER TABLE ADD PRIMARY KEY for colocated tables #7534

Closed
m-iancu opened this issue Mar 8, 2021 · 2 comments
Closed

[YSQL] Support ALTER TABLE ADD PRIMARY KEY for colocated tables #7534

m-iancu opened this issue Mar 8, 2021 · 2 comments
Assignees
Labels
area/ysql Yugabyte SQL (YSQL)
Projects
Milestone

Comments

@m-iancu
Copy link
Contributor

m-iancu commented Mar 8, 2021

i.e. for tables in colocated databases or in a tablegroup.
Should be analogous to the regular table case.

See also: https://docs.yugabyte.com/latest/explore/colocated-tables.

One note about the regress tests needed:

  • For colocated databases we cannot get the state from SQL so, for the regress tests, we can just verify that the alter works.
  • For tablegroups though we can verify this by querying the respective system tables (see yb_tablegroup.sql tests). So we should verify in the tests that the new table is in the appropriate tablegroup using something like:
    SELECT s.relname, pg_tablegroup.grpname
        FROM (SELECT relname, unnest(reloptions) AS opts FROM pg_class) s, pg_tablegroup
        WHERE opts LIKE CONCAT('%tablegroup=', CAST(pg_tablegroup.oid AS text), '%');
    

cc @frozenspider

@m-iancu m-iancu added the area/ysql Yugabyte SQL (YSQL) label Mar 8, 2021
@m-iancu m-iancu added this to Backlog in YSQL via automation Mar 8, 2021
@m-iancu m-iancu changed the title [YSQL] Support ALTER TABLE ADD PRIMARY KEY for colocated tables. [YSQL] Support ALTER TABLE ADD PRIMARY KEY for colocated tables Mar 8, 2021
@frozenspider
Copy link
Contributor

See also: #6159

myang2021 added a commit that referenced this issue Apr 22, 2021
Summary:
Currently for a table in a colocated database or in a tablegroup,
altering table to add primary key fails with the following error,

CREATE DATABASE clc WITH colocated = true;
CREATE DATABASE
You are now connected to database "clc" as user "yugabyte".
CREATE TABLE nopk_c (id int);
CREATE TABLE
INSERT INTO nopk_c VALUES (3);
INSERT 0 1
INSERT INTO nopk_c VALUES (4);
INSERT 0 1
clc=# ALTER TABLE nopk_c ADD PRIMARY KEY (id);
ALTER TABLE nopk_c ADD PRIMARY KEY (id);
ERROR:  adding primary key to a colocated table is not yet implemented

Since we now support add primary key, we can remove this error.
The existing tests already have some test code paths for the cases
once the above error is removed and they are now enabled.

Also fixed a potential null pointer SEGV in get_tablegroup_name when
the tuple is not found.

Test Plan:
ybd debug --verbose --java-test org.yb.pgsql.TestPgAlterTableAddPrimaryKey
ybd debug --verbose --java-test org.yb.pgsql.TestPgRegressTablegroup

Reviewers: mihnea, dsrinivasan, alex

Reviewed By: alex

Differential Revision: https://phabricator.dev.yugabyte.com/D11069
@m-iancu
Copy link
Contributor Author

m-iancu commented Apr 27, 2021

Fixed by b318938.

@m-iancu m-iancu closed this as completed Apr 27, 2021
YSQL automation moved this from Backlog to Done Apr 27, 2021
@m-iancu m-iancu added this to the 2.7.x milestone Apr 27, 2021
YintongMa pushed a commit to YintongMa/yugabyte-db that referenced this issue May 26, 2021
…ed tables

Summary:
Currently for a table in a colocated database or in a tablegroup,
altering table to add primary key fails with the following error,

CREATE DATABASE clc WITH colocated = true;
CREATE DATABASE
You are now connected to database "clc" as user "yugabyte".
CREATE TABLE nopk_c (id int);
CREATE TABLE
INSERT INTO nopk_c VALUES (3);
INSERT 0 1
INSERT INTO nopk_c VALUES (4);
INSERT 0 1
clc=# ALTER TABLE nopk_c ADD PRIMARY KEY (id);
ALTER TABLE nopk_c ADD PRIMARY KEY (id);
ERROR:  adding primary key to a colocated table is not yet implemented

Since we now support add primary key, we can remove this error.
The existing tests already have some test code paths for the cases
once the above error is removed and they are now enabled.

Also fixed a potential null pointer SEGV in get_tablegroup_name when
the tuple is not found.

Test Plan:
ybd debug --verbose --java-test org.yb.pgsql.TestPgAlterTableAddPrimaryKey
ybd debug --verbose --java-test org.yb.pgsql.TestPgRegressTablegroup

Reviewers: mihnea, dsrinivasan, alex

Reviewed By: alex

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

No branches or pull requests

3 participants