Skip to content

Commit

Permalink
[YSQL]: #2280 Update catalog manager data while renaming YSQL table, …
Browse files Browse the repository at this point in the history
…DB and columns.

Summary: Update catalog manager maps and data when table, database and columns are renamed.

Test Plan:
run
```
create database foo;
\c test1
create table foo(a int);
alter table foo rename to bar;
\c yugabyte
alter database test1 rename to test2;
```
see the UI updated

```
ctest -R pg_libpq-test
ctest -R master-test
```

Reviewers: mihnea, neha

Reviewed By: neha

Subscribers: yql, bogdan

Differential Revision: https://phabricator.dev.yugabyte.com/D7210
  • Loading branch information
Zhongwei Zhao committed Nov 20, 2019
1 parent 0be6e2a commit 6a8f17c
Show file tree
Hide file tree
Showing 35 changed files with 649 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/postgres/src/backend/commands/dbcommands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,14 @@ RenameDatabase(const char *oldname, const char *newname)
namestrcpy(&(((Form_pg_database) GETSTRUCT(newtup))->datname), newname);
CatalogTupleUpdate(rel, &newtup->t_self, newtup);

if (IsYugaByteEnabled()) {
YBCPgStatement handle = NULL;
HandleYBStatus(YBCPgNewAlterDatabase(ybc_pg_session, oldname, db_id, &handle));
HandleYBStmtStatus(YBCPgAlterDatabaseRenameDatabase(handle, newname), handle);
HandleYBStmtStatus(YBCPgExecAlterDatabase(handle), handle);
HandleYBStatus(YBCPgDeleteStatement(handle));
}

InvokeObjectPostAlterHook(DatabaseRelationId, db_id, 0);

ObjectAddressSet(address, DatabaseRelationId, db_id);
Expand Down
14 changes: 7 additions & 7 deletions src/postgres/src/backend/commands/tablecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -3012,11 +3012,6 @@ renameatt(RenameStmt *stmt)
return InvalidObjectAddress;
}

if (IsYugaByteEnabled())
{
YBCRename(stmt, relid);
}

attnum =
renameatt_internal(relid,
stmt->subname, /* old att name */
Expand All @@ -3026,6 +3021,11 @@ renameatt(RenameStmt *stmt)
0, /* expected inhcount */
stmt->behavior);

if (IsYugaByteEnabled())
{
YBCRename(stmt, relid);
}

ObjectAddressSubSet(address, RelationRelationId, relid, attnum);

return address;
Expand Down Expand Up @@ -3218,14 +3218,14 @@ RenameRelation(RenameStmt *stmt)
return InvalidObjectAddress;
}

RenameRelationInternal(relid, stmt->newname, false);

/* Do the work */
if (IsYugaByteEnabled())
{
YBCRename(stmt, relid);
}

RenameRelationInternal(relid, stmt->newname, false);

ObjectAddressSet(address, RelationRelationId, relid);

return address;
Expand Down
4 changes: 4 additions & 0 deletions src/postgres/src/backend/commands/ybccmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,10 @@ YBCRename(RenameStmt *stmt, Oid relationId)
errmsg("Renaming this object is not yet supported.")));

}

if (IsYBRelationById(relationId)) {
YBCExecAlterTable(handle);
}
}

void
Expand Down
88 changes: 88 additions & 0 deletions src/postgres/src/test/regress/expected/yb_feature_alter_rename.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--
-- ALTER RENAME
-- rename column, table, database
--
create database test_rename;
create database test_rename1;
\c test_rename
create table foo(a int primary key, b int);
insert into foo (a, b) values (1, 2);
\d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | foo | table | yugabyte
(1 row)

\d foo
Table "public.foo"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
a | integer | | not null |
b | integer | | |
Indexes:
"foo_pkey" PRIMARY KEY, lsm (a HASH)

alter table foo rename column b to c;
select a, b from foo; -- fail
ERROR: column "b" does not exist
LINE 1: select a, b from foo;
^
select a, c from foo;
a | c
---+---
1 | 2
(1 row)

insert into foo (a, b) values (2, 3); -- fail
ERROR: column "b" of relation "foo" does not exist
LINE 1: insert into foo (a, b) values (2, 3);
^
insert into foo (a, c) values (3, 4);
\d foo
Table "public.foo"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
a | integer | | not null |
c | integer | | |
Indexes:
"foo_pkey" PRIMARY KEY, lsm (a HASH)

alter table foo rename to bar;
select * from foo; -- fail
ERROR: relation "foo" does not exist
LINE 1: select * from foo;
^
select * from bar;
a | c
---+---
1 | 2
3 | 4
(2 rows)

\d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | bar | table | yugabyte
(1 row)

\c test_rename1;
alter database test_rename rename to test_rename2;
alter database test_rename2 rename to postgres; -- fail
ERROR: database "postgres" already exists
\l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------------+----------+----------+---------+-------------+-----------------------
postgres | postgres | UTF8 | C | en_US.UTF-8 |
system_platform | postgres | UTF8 | C | en_US.UTF-8 |
template0 | postgres | UTF8 | C | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
test_rename1 | yugabyte | UTF8 | C | en_US.UTF-8 |
test_rename2 | yugabyte | UTF8 | C | en_US.UTF-8 |
yugabyte | postgres | UTF8 | C | en_US.UTF-8 |
(7 rows)

31 changes: 31 additions & 0 deletions src/postgres/src/test/regress/sql/yb_feature_alter_rename.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

--
-- ALTER RENAME
-- rename column, table, database
--

create database test_rename;
create database test_rename1;
\c test_rename

create table foo(a int primary key, b int);
insert into foo (a, b) values (1, 2);
\d
\d foo

alter table foo rename column b to c;
select a, b from foo; -- fail
select a, c from foo;
insert into foo (a, b) values (2, 3); -- fail
insert into foo (a, c) values (3, 4);
\d foo

alter table foo rename to bar;
select * from foo; -- fail
select * from bar;
\d

\c test_rename1;
alter database test_rename rename to test_rename2;
alter database test_rename2 rename to postgres; -- fail
\l
1 change: 1 addition & 0 deletions src/postgres/src/test/regress/yb_feature_serial_schedule
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ test: yb_feature_config
test: yb_feature_copy
test: yb_feature_copyselect
test: yb_feature_alter_table
test: yb_feature_alter_rename
test: yb_feature_temp
test: yb_feature_db
1 change: 1 addition & 0 deletions src/yb/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set(CLIENT_SRCS
in_flight_op.cc
meta_cache.cc
meta_data_cache.cc
namespace_alterer.cc
permissions.cc
session.cc
schema.cc
Expand Down
21 changes: 21 additions & 0 deletions src/yb/client/client-internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ YB_CLIENT_SPECIALIZE_SIMPLE(GetTabletLocations);
YB_CLIENT_SPECIALIZE_SIMPLE(ListMasters);
YB_CLIENT_SPECIALIZE_SIMPLE(CreateNamespace);
YB_CLIENT_SPECIALIZE_SIMPLE(DeleteNamespace);
YB_CLIENT_SPECIALIZE_SIMPLE(AlterNamespace);
YB_CLIENT_SPECIALIZE_SIMPLE(ListNamespaces);
YB_CLIENT_SPECIALIZE_SIMPLE(ReservePgsqlOids);
YB_CLIENT_SPECIALIZE_SIMPLE(GetYsqlCatalogConfig);
Expand Down Expand Up @@ -698,6 +699,26 @@ Status YBClient::Data::WaitForTruncateTableToFinish(YBClient* client,
std::bind(&YBClient::Data::IsTruncateTableInProgress, this, client, table_id, _1, _2));
}

Status YBClient::Data::AlterNamespace(YBClient* client,
const AlterNamespaceRequestPB& req,
CoarseTimePoint deadline) {
AlterNamespaceResponsePB resp;
Status s =
SyncLeaderMasterRpc<AlterNamespaceRequestPB, AlterNamespaceResponsePB>(
deadline,
client,
req,
&resp,
nullptr /* num_attempts */,
"AlterNamespace",
&MasterServiceProxy::AlterNamespace);
RETURN_NOT_OK(s);
if (resp.has_error()) {
return StatusFromPB(resp.error().status());
}
return Status::OK();
}

Status YBClient::Data::AlterTable(YBClient* client,
const AlterTableRequestPB& req,
CoarseTimePoint deadline) {
Expand Down
4 changes: 4 additions & 0 deletions src/yb/client/client-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class YBClient::Data {
std::vector<internal::RemoteTabletServer*>* candidates,
internal::RemoteTabletServer** ts);

CHECKED_STATUS AlterNamespace(YBClient* client,
const master::AlterNamespaceRequestPB& req,
CoarseTimePoint deadline);

CHECKED_STATUS CreateTable(YBClient* client,
const master::CreateTableRequestPB& req,
const YBSchema& schema,
Expand Down
8 changes: 8 additions & 0 deletions src/yb/client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "yb/client/meta_cache.h"
#include "yb/client/session.h"
#include "yb/client/table_alterer.h"
#include "yb/client/namespace_alterer.h"
#include "yb/client/table_creator.h"
#include "yb/client/tablet_server.h"

Expand Down Expand Up @@ -98,6 +99,8 @@ using yb::master::ListTabletServersResponsePB;
using yb::master::ListTabletServersResponsePB_Entry;
using yb::master::CreateNamespaceRequestPB;
using yb::master::CreateNamespaceResponsePB;
using yb::master::AlterNamespaceRequestPB;
using yb::master::AlterNamespaceResponsePB;
using yb::master::DeleteNamespaceRequestPB;
using yb::master::DeleteNamespaceResponsePB;
using yb::master::ListNamespacesRequestPB;
Expand Down Expand Up @@ -578,6 +581,11 @@ Status YBClient::DeleteNamespace(const std::string& namespace_name,
return Status::OK();
}

YBNamespaceAlterer* YBClient::NewNamespaceAlterer(
const string& namespace_name, const std::string& namespace_id) {
return new YBNamespaceAlterer(this, namespace_name, namespace_id);
}

Result<vector<master::NamespaceIdentifierPB>> YBClient::ListNamespaces(
const boost::optional<YQLDatabase>& database_type) {
ListNamespacesRequestPB req;
Expand Down
5 changes: 5 additions & 0 deletions src/yb/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#endif
#include "yb/client/permissions.h"
#include "yb/client/yb_table_name.h"
#include "yb/client/namespace_alterer.h"

#include "yb/common/partition.h"
#include "yb/common/roles_permissions.h"
Expand Down Expand Up @@ -329,6 +330,9 @@ class YBClient {
const boost::optional<YQLDatabase>& database_type = boost::none,
const std::string& namespace_id = "");

YBNamespaceAlterer* NewNamespaceAlterer(const string& namespace_name,
const std::string& namespace_id);

// For Postgres: reserve oids for a Postgres database.
CHECKED_STATUS ReservePgsqlOids(const std::string& namespace_id,
uint32_t next_oid, uint32_t count,
Expand Down Expand Up @@ -599,6 +603,7 @@ class YBClient {
friend class YBNoOp;
friend class YBTable;
friend class YBTableAlterer;
friend class YBNamespaceAlterer;
friend class YBTableCreator;
friend class internal::Batcher;
friend class internal::GetTableSchemaRpc;
Expand Down
60 changes: 60 additions & 0 deletions src/yb/client/namespace_alterer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) YugaByte, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations
// under the License.
//

#include "yb/client/namespace_alterer.h"

#include "yb/client/client-internal.h"

namespace yb {
namespace client {

YBNamespaceAlterer::YBNamespaceAlterer(
YBClient* client, const std::string& namespace_name, const std::string& namespace_id)
: client_(client), namespace_name_(namespace_name), namespace_id_(namespace_id) {}

YBNamespaceAlterer::~YBNamespaceAlterer() {}

Status YBNamespaceAlterer::Alter() {
master::AlterNamespaceRequestPB req;
RETURN_NOT_OK(ToRequest(&req));
return client_->data_->AlterNamespace(
client_, req, CoarseMonoClock::Now() + client_->default_admin_operation_timeout());
}

YBNamespaceAlterer* YBNamespaceAlterer::RenameTo(const std::string& new_name) {
rename_to_ = new_name;
return this;
}

YBNamespaceAlterer* YBNamespaceAlterer::SetDatabaseType(YQLDatabase type) {
database_type_ = type;
return this;
}

Status YBNamespaceAlterer::ToRequest(master::AlterNamespaceRequestPB* req) {
req->mutable_namespace_()->set_name(namespace_name_);
if (!namespace_id_.empty()) {
req->mutable_namespace_()->set_id(namespace_id_);
}
if (database_type_) {
req->mutable_namespace_()->set_database_type(*database_type_);
}
if (!rename_to_) {
return STATUS(InvalidArgument, "Cannot alter namespace without specifying new name");
}
req->set_new_name(*rename_to_);
return Status::OK();
}

} // namespace client
} // namespace yb
Loading

0 comments on commit 6a8f17c

Please sign in to comment.