Skip to content

Commit

Permalink
[BACKPORT 2.6][#6470] YSQL: Enable ALTER SCHEMA RENAME
Browse files Browse the repository at this point in the history
Summary:
DocDB layer knows nothing about postgres schemas. This information in stored only in postgres sys catalog.
It is enough to allow the `ALTER SCHEMA RENAME` command on parser level to support schema reaming.

Original commit: D12387 / 39b2b6b

Test Plan:
Jenkins: rebase: 2.6
postgres's regress test `namespace.sql` was integrated as `yb_pg_namespace.sql`

```
./yb_build.sh --java-test 'org.yb.pgsql.TestPgRegressPgMiscIndependent'
```

Reviewers: alex, mihnea

Reviewed By: mihnea

Subscribers: yql

Differential Revision: https://phabricator.dev.yugabyte.com/D12471
  • Loading branch information
d-uspenskiy committed Aug 6, 2021
1 parent 5db005b commit d6ad691
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/postgres/src/backend/parser/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,8 @@ CreateSchemaStmt:
/* One can omit the schema name or the authorization id. */
n->schemaname = $3;
n->authrole = $5;
if ($6 != NIL)
parser_ybc_not_support(@6, "CREATE SCHEMA with elements");
n->schemaElts = $6;
n->if_not_exists = false;
$$ = (Node *)n;
Expand All @@ -1402,6 +1404,8 @@ CreateSchemaStmt:
/* ...but not both */
n->schemaname = $3;
n->authrole = NULL;
if ($4 != NIL)
parser_ybc_not_support(@4, "CREATE SCHEMA with elements");
n->schemaElts = $4;
n->if_not_exists = false;
$$ = (Node *)n;
Expand Down Expand Up @@ -1446,7 +1450,6 @@ OptSchemaName:
OptSchemaEltList:
OptSchemaEltList schema_stmt
{
parser_ybc_not_support(@2, "CREATE SCHEMA with elements");
if (@$ < 0) /* see comments for YYLLOC_DEFAULT */
@$ = @2;
$$ = lappend($1, $2);
Expand Down Expand Up @@ -9413,7 +9416,6 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
}
| ALTER SCHEMA name RENAME TO name
{
parser_ybc_not_support(@1, "ALTER SCHEMA");
RenameStmt *n = makeNode(RenameStmt);
n->renameType = OBJECT_SCHEMA;
n->subname = $3;
Expand Down
80 changes: 80 additions & 0 deletions src/postgres/src/test/regress/expected/yb_pg_namespace.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
--
-- Regression tests for schemas (namespaces)
--
CREATE SCHEMA test_ns_schema_1
CREATE UNIQUE INDEX abc_a_idx ON abc (a)
CREATE VIEW abc_view AS
SELECT a+1 AS a, b+1 AS b FROM abc
CREATE TABLE abc (
a serial,
b int UNIQUE
);
ERROR: CREATE SCHEMA with elements not supported yet
LINE 2: CREATE UNIQUE INDEX abc_a_idx ON abc (a)
^
HINT: Please report the issue on https://github.com/YugaByte/yugabyte-db/issues
-- TODO(dmitry): Remove separate statements for creation each element in schema after
-- `schema creation with elements` command will be supported.
CREATE SCHEMA test_ns_schema_1;
CREATE TABLE test_ns_schema_1.abc (a serial, b int UNIQUE);
CREATE UNIQUE INDEX abc_a_idx ON test_ns_schema_1.abc (a);
CREATE VIEW test_ns_schema_1.abc_view AS SELECT a+1 AS a, b+1 AS b FROM test_ns_schema_1.abc;
-- verify that the objects were created
SELECT COUNT(*) FROM pg_class WHERE relnamespace =
(SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_1');
count
-------
5
(1 row)

INSERT INTO test_ns_schema_1.abc DEFAULT VALUES;
INSERT INTO test_ns_schema_1.abc DEFAULT VALUES;
INSERT INTO test_ns_schema_1.abc DEFAULT VALUES;
SELECT * FROM test_ns_schema_1.abc ORDER BY a;
a | b
---+---
1 |
2 |
3 |
(3 rows)

SELECT * FROM test_ns_schema_1.abc_view ORDER BY a;
a | b
---+---
2 |
3 |
4 |
(3 rows)

ALTER SCHEMA test_ns_schema_1 RENAME TO test_ns_schema_renamed;
SELECT COUNT(*) FROM pg_class WHERE relnamespace =
(SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_1');
count
-------
0
(1 row)

-- test IF NOT EXISTS cases
CREATE SCHEMA test_ns_schema_renamed; -- fail, already exists
ERROR: schema "test_ns_schema_renamed" already exists
CREATE SCHEMA IF NOT EXISTS test_ns_schema_renamed; -- ok with notice
NOTICE: schema "test_ns_schema_renamed" already exists, skipping
CREATE SCHEMA IF NOT EXISTS test_ns_schema_renamed -- fail, disallowed
CREATE TABLE abc (
a serial,
b int UNIQUE
);
ERROR: CREATE SCHEMA IF NOT EXISTS cannot include schema elements
LINE 2: CREATE TABLE abc (
^
DROP SCHEMA test_ns_schema_renamed CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to table test_ns_schema_renamed.abc
drop cascades to view test_ns_schema_renamed.abc_view
-- verify that the objects were dropped
SELECT COUNT(*) FROM pg_class WHERE relnamespace =
(SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_renamed');
count
-------
0
(1 row)
51 changes: 51 additions & 0 deletions src/postgres/src/test/regress/sql/yb_pg_namespace.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--
-- Regression tests for schemas (namespaces)
--

CREATE SCHEMA test_ns_schema_1
CREATE UNIQUE INDEX abc_a_idx ON abc (a)

CREATE VIEW abc_view AS
SELECT a+1 AS a, b+1 AS b FROM abc

CREATE TABLE abc (
a serial,
b int UNIQUE
);

-- TODO(dmitry): Remove separate statements for creation each element in schema after
-- `schema creation with elements` command will be supported.
CREATE SCHEMA test_ns_schema_1;
CREATE TABLE test_ns_schema_1.abc (a serial, b int UNIQUE);
CREATE UNIQUE INDEX abc_a_idx ON test_ns_schema_1.abc (a);
CREATE VIEW test_ns_schema_1.abc_view AS SELECT a+1 AS a, b+1 AS b FROM test_ns_schema_1.abc;

-- verify that the objects were created
SELECT COUNT(*) FROM pg_class WHERE relnamespace =
(SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_1');

INSERT INTO test_ns_schema_1.abc DEFAULT VALUES;
INSERT INTO test_ns_schema_1.abc DEFAULT VALUES;
INSERT INTO test_ns_schema_1.abc DEFAULT VALUES;

SELECT * FROM test_ns_schema_1.abc ORDER BY a;
SELECT * FROM test_ns_schema_1.abc_view ORDER BY a;

ALTER SCHEMA test_ns_schema_1 RENAME TO test_ns_schema_renamed;
SELECT COUNT(*) FROM pg_class WHERE relnamespace =
(SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_1');

-- test IF NOT EXISTS cases
CREATE SCHEMA test_ns_schema_renamed; -- fail, already exists
CREATE SCHEMA IF NOT EXISTS test_ns_schema_renamed; -- ok with notice
CREATE SCHEMA IF NOT EXISTS test_ns_schema_renamed -- fail, disallowed
CREATE TABLE abc (
a serial,
b int UNIQUE
);

DROP SCHEMA test_ns_schema_renamed CASCADE;

-- verify that the objects were dropped
SELECT COUNT(*) FROM pg_class WHERE relnamespace =
(SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_renamed');
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test: yb_pg_drop_operator
test: yb_prepare
test: yb_sequence
test: yb_pg_identity
test: yb_pg_namespace

# Features that are explicitly disabled and therefore should not work
test: yb_disabled

0 comments on commit d6ad691

Please sign in to comment.