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

[Colocation][YSQL] Fix ADD PRIMARY KEY setting colocated when it was unset #9812

Closed
jaki opened this issue Aug 24, 2021 · 0 comments
Closed
Assignees
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug pgcm priority/medium Medium priority issue

Comments

@jaki
Copy link
Contributor

jaki commented Aug 24, 2021

Jira Link: [DB-384](https://yugabyte.atlassian.net/browse/DB-384)
ALTER TABLE ADD PRIMARY KEY or ALTER TABLE ADD CONSTRAINT ... PRIMARY KEY may fail with error message

ERROR:  cannot set colocated true on a non-colocated database

even when the user does nothing related to colocation. It happens when the table has any reloptions set (see SELECT reloptions FROM pg_class WHERE relname = 'mytabname'). Since pk is tied to the main table, adding a primary key is done by creating a new table (YBCloneRelationSetPrimaryKey). One part of it does

	} else if ((*mutable_rel)->rd_options) {
		const bool colocated = RelationGetColocated(*mutable_rel);
		create_stmt->options = lappend(create_stmt->options,
			makeDefElem("colocated", (Node *) makeInteger(colocated), -1));

meaning that, if any reloptions are set, the colocated reloptions is copied over. However, the colocated reloption could be unset while some other reloption, like fillfactor, was set, so this could end up explicitly setting colocated to the default value (true). There is a check later on that makes sure colocation is unset or false when operating on a noncolocated database (the default), so the error is thrown.

Repro:

-- in noncolocated database
CREATE TABLE t (i int) WITH (autovacuum_enabled = true);
ALTER TABLE t ADD PRIMARY KEY (i);

Issue #9129 could have reduced the scope of this bug.

Credits to Suranjan Kumar for the initial bug report and Ted Yu for tracing to the root cause.

@jaki jaki added kind/bug This issue is a bug area/ysql Yugabyte SQL (YSQL) labels Aug 24, 2021
@jaki jaki added this to Backlog in YSQL via automation Aug 24, 2021
tedyu added a commit that referenced this issue Aug 25, 2021
…ion for Alter Table

Summary:
Currently YBCloneRelationSetPrimaryKey() may add colocated option for the new table without checking that the database is colocated.
This would result in the following error:
```
ERROR:  cannot set colocated true on a non-colocated database
```
This revision adds checking that the underlying database is colocated before adding the option.

Test Plan: mvn test -Dtest=yb.pgsql.TestPgAlterTableAddPrimaryKey#withFillFactor

Reviewers: myang, jason

Reviewed By: myang, jason

Subscribers: yql

Differential Revision: https://phabricator.dev.yugabyte.com/D12715
tedyu added a commit that referenced this issue Sep 1, 2021
…g colocated option for Alter Table

Summary:
Currently YBCloneRelationSetPrimaryKey() may add colocated option for the new table without checking that the database is colocated.
This would result in the following error:
```
ERROR:  cannot set colocated true on a non-colocated database
```
This revision adds checking that the underlying database is colocated before adding the option.

Test Plan:
Jenkins: rebase: 2.6
mvn test -Dtest=yb.pgsql.TestPgAlterTableAddPrimaryKey#withFillFactor

Reviewers: myang

Reviewed By: myang

Subscribers: yql

Differential Revision: https://phabricator.dev.yugabyte.com/D12794
@frozenspider frozenspider added this to To do in Colocation via automation Apr 20, 2022
YSQL automation moved this from Backlog to Done May 17, 2022
Colocation automation moved this from To do to Done May 17, 2022
@yugabyte-ci yugabyte-ci added the priority/medium Medium priority issue label May 17, 2022
@frozenspider frozenspider changed the title ysql: fix ADD PRIMARY KEY setting colocated when it was unset [Colocation][YSQL] Fix ADD PRIMARY KEY setting colocated when it was unset May 18, 2022
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 pgcm priority/medium Medium priority issue
Projects
Status: Done
YSQL
  
Done
Development

No branches or pull requests

5 participants