Skip to content

Commit 02e10f8

Browse files
committed
[yugabyte#27315] DocDB: Remove code to change the table namespace via AlterTable API
Summary: Some DocDB handlers (like `CatalogManager::AlterTable()`, `YbTableAlterer`, etc.) have ability to change the table namespace. This functionality is NOT used by YCQL & YSQL. So, it can be safely deleted to clean & simplify the source code. Related variables in PBs are also removed (used entry IDs are reserved). Upgrade/Downgrade sefaty: The change is safe when the Master is on new version and TServers are on old version because the removed value `AlterTableRequestPB::new_namespace` is ignored now. NOTE: The Masters must be upgraded before TServers. Test Plan: Unit tests: ./yb_build.sh --cxx-test master-test --gtest_filter MasterTest.TestTablesWithNamespace ./yb_build.sh --cxx-test master-test --gtest_filter MasterTest.TestFullTableName YSQL: ./yb_build.sh --java-test org.yb.pgsql.TestPgAlterTable#testRenameTableIfExists ./yb_build.sh --java-test org.yb.pgsql.TestPgAlterTable YCQL: ./yb_build.sh --java-test org.yb.cql.TestAlterTable Reviewers: mihnea, hsunder Reviewed By: hsunder Subscribers: hsunder, yql Differential Revision: https://phorge.dev.yugabyte.com/D44149
1 parent d4c0b7a commit 02e10f8

File tree

13 files changed

+48
-143
lines changed

13 files changed

+48
-143
lines changed

src/postgres/src/backend/commands/yb_cmds.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,6 @@ YBCRename(Oid relationId, ObjectType renameType, const char *relname,
18911891
{
18921892
YbcPgStatement handle = NULL;
18931893
Oid databaseId = YBCGetDatabaseOidByRelid(relationId);
1894-
char *db_name = get_database_name(databaseId);
18951894

18961895
switch (renameType)
18971896
{
@@ -1900,7 +1899,7 @@ YBCRename(Oid relationId, ObjectType renameType, const char *relname,
19001899
case OBJECT_INDEX:
19011900
HandleYBStatus(YBCPgNewAlterTable(databaseId,
19021901
YbGetRelfileNodeIdFromRelId(relationId), &handle));
1903-
HandleYBStatus(YBCPgAlterTableRenameTable(handle, db_name, relname));
1902+
HandleYBStatus(YBCPgAlterTableRenameTable(handle, relname));
19041903
break;
19051904
case OBJECT_COLUMN:
19061905
case OBJECT_ATTRIBUTE:

src/yb/client/table_alterer.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Status YBTableAlterer::ToRequest(master::AlterTableRequestPB* req) {
134134

135135
req->Clear();
136136

137-
if (table_name_.has_table()) {
137+
if (table_name_.has_table() || table_name_.has_table_id()) {
138138
table_name_.SetIntoTableIdentifierPB(req->mutable_table());
139139
}
140140

@@ -145,10 +145,6 @@ Status YBTableAlterer::ToRequest(master::AlterTableRequestPB* req) {
145145
if (rename_to_) {
146146
if (rename_to_->has_table()) {
147147
req->set_new_table_name(rename_to_->table_name());
148-
149-
if (rename_to_->has_namespace()) {
150-
req->mutable_new_namespace()->set_name(rename_to_->namespace_name());
151-
}
152148
}
153149

154150
if (rename_to_->has_pgschema_name()) {

src/yb/master/catalog_manager.cc

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7407,33 +7407,31 @@ Status CatalogManager::AlterTable(const AlterTableRequestPB* req,
74077407
LOG_WITH_PREFIX(INFO) << "PG table OID for AlterTable request: " << table->GetPgTableOid();
74087408
}
74097409

7410-
NamespaceId new_namespace_id;
7411-
if (req->has_new_namespace()) {
7412-
// Lookup the new namespace and verify if it exists.
7413-
TRACE("Looking up new namespace");
7414-
scoped_refptr<NamespaceInfo> ns;
7415-
NamespaceIdentifierPB namespace_identifier = req->new_namespace();
7416-
// Use original namespace_id as new_namespace_id for YSQL tables.
7417-
if (table->GetTableType() == PGSQL_TABLE_TYPE && !namespace_identifier.has_id()) {
7418-
namespace_identifier.set_id(table->namespace_id());
7419-
}
7420-
ns = VERIFY_NAMESPACE_FOUND(FindNamespace(namespace_identifier), resp);
7410+
// Lookup the namespace and verify if it exists.
7411+
TRACE("Looking up namespace");
7412+
auto namespace_identifier = req->table().namespace_();
7413+
// Use original namespace_id if it's not provided in the request.
7414+
if (!namespace_identifier.has_id()) {
7415+
namespace_identifier.set_id(table->namespace_id());
7416+
}
74217417

7418+
const auto ns = VERIFY_NAMESPACE_FOUND(FindNamespace(namespace_identifier), resp);
7419+
NamespaceId namespace_id;
7420+
{
74227421
auto ns_lock = ns->LockForRead();
7423-
new_namespace_id = ns->id();
7422+
namespace_id = ns->id();
74247423
// Don't use Namespaces that aren't running.
74257424
if (ns->state() != SysNamespaceEntryPB::RUNNING) {
74267425
Status s = STATUS_SUBSTITUTE(TryAgain,
7427-
"Namespace not running (State=$0). Cannot create $1.$2",
7428-
SysNamespaceEntryPB::State_Name(ns->state()), ns->name(), table->name() );
7426+
"Namespace not running (State=$0). Cannot alter table $1.$2",
7427+
SysNamespaceEntryPB::State_Name(ns->state()), ns->name(), table->name());
74297428
return SetupError(resp->mutable_error(), NamespaceMasterError(ns->state()), s);
74307429
}
74317430
}
7432-
if (req->has_new_namespace() || req->has_new_table_name()) {
7433-
if (new_namespace_id.empty()) {
7434-
const Status s = STATUS(InvalidArgument, "No namespace used");
7435-
return SetupError(resp->mutable_error(), MasterErrorPB::NO_NAMESPACE_USED, s);
7436-
}
7431+
7432+
if (namespace_id.empty()) {
7433+
return SetupError(resp->mutable_error(), MasterErrorPB::NO_NAMESPACE_USED,
7434+
STATUS(InvalidArgument, "No namespace used"));
74377435
}
74387436

74397437
if (!FLAGS_ysql_yb_enable_replica_identity &&
@@ -7491,10 +7489,11 @@ Status CatalogManager::AlterTable(const AlterTableRequestPB* req,
74917489
}
74927490
}
74937491

7492+
DCHECK_EQ(namespace_id, l->namespace_id());
7493+
74947494
bool has_changes = false;
74957495
auto& table_pb = l.mutable_data()->pb;
74967496
const TableName table_name = l->name();
7497-
const NamespaceId namespace_id = l->namespace_id();
74987497
const TableName new_table_name = req->has_new_table_name() ? req->new_table_name() : table_name;
74997498

75007499
// Calculate new schema for the on-disk state, not persisted yet.
@@ -7526,30 +7525,28 @@ Status CatalogManager::AlterTable(const AlterTableRequestPB* req,
75267525
}
75277526

75287527
// Try to acquire the new table name.
7529-
if (req->has_new_namespace() || req->has_new_table_name()) {
7528+
if (req->has_new_table_name()) {
75307529

75317530
// Postgres handles name uniqueness constraints in it's own layer.
75327531
if (l->table_type() != PGSQL_TABLE_TYPE) {
75337532
// Verify that the table does not exist.
75347533
scoped_refptr<TableInfo> other_table = FindPtrOrNull(
7535-
table_names_map_, {new_namespace_id, new_table_name});
7534+
table_names_map_, {namespace_id, new_table_name});
75367535
if (other_table != nullptr) {
75377536
Status s = STATUS_SUBSTITUTE(AlreadyPresent,
75387537
"Object '$0.$1' already exists",
7539-
GetNamespaceNameUnlocked(new_namespace_id), other_table->name());
7538+
GetNamespaceNameUnlocked(namespace_id), other_table->name());
75407539
LOG(WARNING) << "Found table: " << other_table->ToStringWithState()
75417540
<< ". Failed alterring table with error: "
75427541
<< s.ToString() << " Request:\n" << req->DebugString();
75437542
return SetupError(resp->mutable_error(), MasterErrorPB::OBJECT_ALREADY_PRESENT, s);
75447543
}
75457544

75467545
// Acquire the new table name (now we have 2 name for the same table).
7547-
table_names_map_[{new_namespace_id, new_table_name}] = table;
7546+
table_names_map_[{namespace_id, new_table_name}] = table;
75487547
}
75497548

7550-
table_pb.set_namespace_id(new_namespace_id);
75517549
table_pb.set_name(new_table_name);
7552-
75537550
has_changes = true;
75547551
}
75557552

@@ -7615,11 +7612,10 @@ Status CatalogManager::AlterTable(const AlterTableRequestPB* req,
76157612
Substitute("Alter table version=$0 ts=$1", table_pb.version(), LocalTimeAsString()));
76167613

76177614
RETURN_NOT_OK(UpdateSysCatalogWithNewSchema(
7618-
table, ddl_log_entries, new_namespace_id, new_table_name, epoch, resp));
7615+
table, ddl_log_entries, namespace_id, new_table_name, epoch, resp));
76197616

76207617
// Remove the old name. Not present if PGSQL.
7621-
if (table->GetTableType() != PGSQL_TABLE_TYPE &&
7622-
(req->has_new_namespace() || req->has_new_table_name())) {
7618+
if (table->GetTableType() != PGSQL_TABLE_TYPE && req->has_new_table_name()) {
76237619
TRACE("Removing (namespace, table) combination ($0, $1) from by-name map",
76247620
namespace_id, table_name);
76257621
table_names_map_.erase({namespace_id, table_name});

src/yb/master/master-test.cc

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,72 +1782,8 @@ TEST_F(MasterTest, TestTablesWithNamespace) {
17821782
EXPECTED_SYSTEM_TABLES
17831783
}, tables);
17841784

1785-
// Alter table: try to change the table namespace name into an invalid one.
1786-
{
1787-
AlterTableRequestPB req;
1788-
AlterTableResponsePB resp;
1789-
req.mutable_table()->set_table_name(kTableName);
1790-
req.mutable_table()->mutable_namespace_()->set_name(other_ns_name);
1791-
req.mutable_new_namespace()->set_name("nonexistingns");
1792-
ASSERT_OK(proxy_ddl_->AlterTable(req, &resp, ResetAndGetController()));
1793-
SCOPED_TRACE(resp.DebugString());
1794-
ASSERT_TRUE(resp.has_error());
1795-
ASSERT_EQ(resp.error().code(), MasterErrorPB::NAMESPACE_NOT_FOUND);
1796-
ASSERT_EQ(resp.error().status().code(), AppStatusPB::NOT_FOUND);
1797-
ASSERT_STR_CONTAINS(resp.error().status().ShortDebugString(), "YCQL keyspace name not found");
1798-
}
1799-
ASSERT_NO_FATALS(DoListAllTables(&tables));
1800-
ASSERT_EQ(1 + kNumSystemTables, tables.tables_size());
1801-
CheckTables(
1802-
{
1803-
std::make_tuple(kTableName, other_ns_name, other_ns_id, USER_TABLE_RELATION),
1804-
EXPECTED_SYSTEM_TABLES
1805-
}, tables);
1806-
1807-
// Alter table: try to change the table namespace id into an invalid one.
1808-
{
1809-
AlterTableRequestPB req;
1810-
AlterTableResponsePB resp;
1811-
req.mutable_table()->set_table_name(kTableName);
1812-
req.mutable_table()->mutable_namespace_()->set_name(other_ns_name);
1813-
req.mutable_new_namespace()->set_id("deadbeafdeadbeafdeadbeafdeadbeaf");
1814-
ASSERT_OK(proxy_ddl_->AlterTable(req, &resp, ResetAndGetController()));
1815-
SCOPED_TRACE(resp.DebugString());
1816-
ASSERT_TRUE(resp.has_error());
1817-
ASSERT_EQ(resp.error().code(), MasterErrorPB::NAMESPACE_NOT_FOUND);
1818-
ASSERT_EQ(resp.error().status().code(), AppStatusPB::NOT_FOUND);
1819-
ASSERT_STR_CONTAINS(resp.error().status().ShortDebugString(), "Keyspace identifier not found");
1820-
}
1821-
ASSERT_NO_FATALS(DoListAllTables(&tables));
1822-
ASSERT_EQ(1 + kNumSystemTables, tables.tables_size());
1823-
CheckTables(
1824-
{
1825-
std::make_tuple(kTableName, other_ns_name, other_ns_id, USER_TABLE_RELATION),
1826-
EXPECTED_SYSTEM_TABLES
1827-
}, tables);
1828-
1829-
// Alter table: change namespace name into the default one.
1830-
{
1831-
AlterTableRequestPB req;
1832-
AlterTableResponsePB resp;
1833-
req.mutable_table()->set_table_name(kTableName);
1834-
req.mutable_table()->mutable_namespace_()->set_name(other_ns_name);
1835-
req.mutable_new_namespace()->set_name(default_namespace_name);
1836-
ASSERT_OK(proxy_ddl_->AlterTable(req, &resp, ResetAndGetController()));
1837-
SCOPED_TRACE(resp.DebugString());
1838-
ASSERT_FALSE(resp.has_error());
1839-
}
1840-
ASSERT_NO_FATALS(DoListAllTables(&tables));
1841-
ASSERT_EQ(1 + kNumSystemTables, tables.tables_size());
1842-
CheckTables(
1843-
{
1844-
std::make_tuple(kTableName, default_namespace_name, default_namespace_id,
1845-
USER_TABLE_RELATION),
1846-
EXPECTED_SYSTEM_TABLES
1847-
}, tables);
1848-
18491785
// Delete the table.
1850-
ASSERT_OK(DeleteTable(default_namespace_name, kTableName));
1786+
ASSERT_OK(DeleteTable(other_ns_name, kTableName));
18511787

18521788
// List tables, should show 1 table.
18531789
ASSERT_NO_FATALS(DoListAllTables(&tables));
@@ -2319,23 +2255,6 @@ TEST_F(MasterTest, TestFullTableName) {
23192255
std::make_tuple(kTableName, other_ns_name, other_ns_id, USER_TABLE_RELATION)
23202256
}, tables);
23212257

2322-
// Try to alter table: change namespace name into the default one.
2323-
// Try to change 'testns::testtb' into 'default_namespace::testtb', but the target table exists,
2324-
// so it must fail.
2325-
{
2326-
AlterTableRequestPB req;
2327-
AlterTableResponsePB resp;
2328-
req.mutable_table()->set_table_name(kTableName);
2329-
req.mutable_table()->mutable_namespace_()->set_name(other_ns_name);
2330-
req.mutable_new_namespace()->set_name(default_namespace_name);
2331-
ASSERT_OK(proxy_ddl_->AlterTable(req, &resp, ResetAndGetController()));
2332-
SCOPED_TRACE(resp.DebugString());
2333-
ASSERT_TRUE(resp.has_error());
2334-
ASSERT_EQ(resp.error().code(), MasterErrorPB::OBJECT_ALREADY_PRESENT);
2335-
ASSERT_EQ(resp.error().status().code(), AppStatusPB::ALREADY_PRESENT);
2336-
ASSERT_STR_CONTAINS(resp.error().status().ShortDebugString(),
2337-
" already exists");
2338-
}
23392258
// Check that nothing's changed (still have 3 tables).
23402259
ASSERT_NO_FATALS(DoListAllTables(&tables));
23412260
ASSERT_EQ(2 + kNumSystemTables, tables.tables_size());

src/yb/master/master_ddl.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ message AlterTableRequestPB {
460460
required TableIdentifierPB table = 1;
461461
repeated Step alter_schema_steps = 2;
462462
optional string new_table_name = 3;
463-
optional NamespaceIdentifierPB new_namespace = 4;
463+
reserved 4; // Deprecated new_namespace
464464
optional TablePropertiesPB alter_properties = 5;
465465
optional uint32 wal_retention_secs = 6;
466466

src/yb/tserver/pg_client.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ message PgRenameColumnPB {
206206
}
207207

208208
message PgRenameTablePB {
209-
string database_name = 1;
209+
reserved 1; // Deprecated database_name
210210
string table_name = 2;
211211
string schema_name = 3;
212212
}

src/yb/tserver/pg_client_session.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,19 +1509,19 @@ class PgClientSession::Impl {
15091509
alterer->DropColumn(drop_column);
15101510
}
15111511

1512-
if (!req.rename_table().table_name().empty()) {
1513-
client::YBTableName new_table_name(
1514-
YQL_DATABASE_PGSQL, req.rename_table().database_name(), req.rename_table().table_name());
1515-
if (!req.rename_table().schema_name().empty()) {
1516-
new_table_name.set_pgschema_name(req.rename_table().schema_name());
1517-
}
1518-
alterer->RenameTo(new_table_name);
1519-
} else if (!req.rename_table().schema_name().empty()) {
1512+
if (!req.rename_table().table_name().empty() ||
1513+
!req.rename_table().schema_name().empty()) {
1514+
const auto ns_id = PgObjectId::GetYbNamespaceIdFromPB(req.table_id());
1515+
// Change table name and/or schema name. DB name cannot be changed.
15201516
client::YBTableName new_table_name(YQL_DATABASE_PGSQL);
1521-
new_table_name.set_pgschema_name(req.rename_table().schema_name());
15221517
new_table_name.set_table_id(table_id);
1523-
const auto ns_id = PgObjectId::GetYbNamespaceIdFromPB(req.table_id());
15241518
new_table_name.set_namespace_id(ns_id);
1519+
if (!req.rename_table().table_name().empty()) {
1520+
new_table_name.set_table_name(req.rename_table().table_name());
1521+
}
1522+
if (!req.rename_table().schema_name().empty()) {
1523+
new_table_name.set_pgschema_name(req.rename_table().schema_name());
1524+
}
15251525
alterer->RenameTo(new_table_name);
15261526
}
15271527

src/yb/yql/pggate/pg_ddl.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,8 @@ Status PgAlterTable::SetReplicaIdentity(const char identity_type) {
474474
return Status::OK();
475475
}
476476

477-
Status PgAlterTable::RenameTable(const char *db_name, const char *newname) {
477+
Status PgAlterTable::RenameTable(const char *newname) {
478478
auto& rename = *req_.mutable_rename_table();
479-
rename.set_database_name(db_name);
480479
rename.set_table_name(newname);
481480
return Status::OK();
482481
}

src/yb/yql/pggate/pg_ddl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class PgAlterTable final : public PgStatementLeafBase<PgDdl, StmtOp::kAlterTable
257257

258258
Status DropColumn(const char *name);
259259

260-
Status RenameTable(const char *db_name, const char *newname);
260+
Status RenameTable(const char *newname);
261261

262262
Status IncrementSchemaVersion();
263263

src/yb/yql/pggate/pggate.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -993,9 +993,8 @@ Status PgApiImpl::AlterTableSetReplicaIdentity(PgStatement* handle, char identit
993993
return VERIFY_RESULT_REF(GetStatementAs<PgAlterTable>(handle)).SetReplicaIdentity(identity_type);
994994
}
995995

996-
Status PgApiImpl::AlterTableRenameTable(
997-
PgStatement* handle, const char* db_name, const char* newname) {
998-
return VERIFY_RESULT_REF(GetStatementAs<PgAlterTable>(handle)).RenameTable(db_name, newname);
996+
Status PgApiImpl::AlterTableRenameTable(PgStatement* handle, const char* newname) {
997+
return VERIFY_RESULT_REF(GetStatementAs<PgAlterTable>(handle)).RenameTable(newname);
999998
}
1000999

10011000
Status PgApiImpl::AlterTableIncrementSchemaVersion(PgStatement* handle) {

0 commit comments

Comments
 (0)