Skip to content

Commit

Permalink
colocation: enforce colocated reloption behavior
Browse files Browse the repository at this point in the history
Summary:
Enforce the behavior of the `colocated` reloption from `CREATE TABLE`.
By default, inherit the `colocated` option from the database.  Then,
allow overrides by the reloption specified by the user _only if the
database is colocated_.  Allow the user to redundantly specify
`colocated = false` for a non-colocated database; error when the user
specifies `colocated = true` for a non-colocated database.

Depends on D8207

Test Plan:
* `./yb_build.sh --cxx-test pgwrapper_pg_libpq-test --gtest_filter
  PgLibPqTest.TableColocation`
* `./yb_build.sh --java-test org.yb.pgsql.TestPgRegressBetaFeatures`

Reviewers: neha

Reviewed By: neha

Subscribers: yql

Differential Revision: https://phabricator.dev.yugabyte.com/D8218
  • Loading branch information
Jason Kim committed Apr 1, 2020
1 parent 99daa74 commit 5acfdde
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/postgres/src/backend/commands/ybccmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,18 +483,29 @@ YBCCreateTable(CreateStmt *stmt, char relkind, TupleDesc desc, Oid relationId, O
}
}

ListCell *opt_cell;
// Set the default option to true so that tables created in a colocated database will be
// colocated by default. For regular database, this argument will be ignored.
bool colocated = true;
/* Scan list to see if colocated was included */
/* By default, inherit the colocated option from the database */
bool colocated = MyDatabaseColocated;

/* Handle user-supplied colocated reloption */
ListCell *opt_cell;
foreach(opt_cell, stmt->options)
{
DefElem *def = (DefElem *) lfirst(opt_cell);

if (strcmp(def->defname, "colocated") == 0)
{
colocated = defGetBoolean(def);
bool colocated_relopt = defGetBoolean(def);
if (MyDatabaseColocated)
colocated = colocated_relopt;
else if (colocated_relopt)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot set colocated true on a non-colocated"
" database")));
/* The following break is fine because there should only be one
* colocated reloption at this point due to checks in
* parseRelOptions */
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
--
-- Colocation
--
-- CREATE TABLE on non-colocated database
CREATE TABLE tab_colo (a INT) WITH (colocated = true);
ERROR: cannot set colocated true on a non-colocated database
CREATE TABLE tab_noco (a INT) WITH (colocated = false);
DROP TABLE tab_noco;
-- CREATE DATABASE colocated
CREATE DATABASE colocation_test colocated = true;
\c colocation_test
-- CREATE TABLE
Expand Down
8 changes: 8 additions & 0 deletions src/postgres/src/test/regress/sql/yb_feature_colocation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
-- Colocation
--

-- CREATE TABLE on non-colocated database

CREATE TABLE tab_colo (a INT) WITH (colocated = true);
CREATE TABLE tab_noco (a INT) WITH (colocated = false);
DROP TABLE tab_noco;

-- CREATE DATABASE colocated

CREATE DATABASE colocation_test colocated = true;
\c colocation_test

Expand Down

0 comments on commit 5acfdde

Please sign in to comment.