|
| 1 | +// Copyright (c) YugabyteDB, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 4 | +// in compliance with the License. You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software distributed under the License |
| 9 | +// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 10 | +// or implied. See the License for the specific language governing permissions and limitations |
| 11 | +// under the License. |
| 12 | + |
| 13 | +#include <regex> |
| 14 | +#include <string> |
| 15 | + |
| 16 | +#include <gtest/gtest.h> |
| 17 | + |
| 18 | +#include "yb/master/catalog_manager_if.h" |
| 19 | +#include "yb/master/master_ddl.pb.h" |
| 20 | +#include "yb/master/sys_catalog.h" |
| 21 | + |
| 22 | +#include "yb/yql/pgwrapper/pg_mini_test_base.h" |
| 23 | + |
| 24 | +namespace yb::master { |
| 25 | + |
| 26 | +class SysCatalogITest : public pgwrapper::PgMiniTestBase { |
| 27 | + public: |
| 28 | + void SetUp() override { |
| 29 | + TEST_SETUP_SUPER(pgwrapper::PgMiniTestBase); |
| 30 | + |
| 31 | + master::GetNamespaceInfoResponsePB resp; |
| 32 | + ASSERT_OK(client_->GetNamespaceInfo( |
| 33 | + /*namespace_id=*/std::string(), namespace_name, YQL_DATABASE_PGSQL, &resp)); |
| 34 | + namespace_id_ = resp.namespace_().id(); |
| 35 | + |
| 36 | + google::SetVLOGLevel("catalog_manager*", 1); |
| 37 | + } |
| 38 | + |
| 39 | + const std::string namespace_name = "yugabyte"; |
| 40 | + NamespaceId namespace_id_; |
| 41 | +}; |
| 42 | + |
| 43 | +TEST_F(SysCatalogITest, ReadHighestNormalPreservableOid) { |
| 44 | + auto conn = ASSERT_RESULT(ConnectToDB(namespace_name)); |
| 45 | + auto database_oid = ASSERT_RESULT(GetPgsqlDatabaseOid(namespace_id_)); |
| 46 | + auto sys_catalog = ASSERT_RESULT(catalog_manager())->sys_catalog(); |
| 47 | + |
| 48 | + auto original_highest_oid = |
| 49 | + ASSERT_RESULT(sys_catalog->ReadHighestNormalPreservableOid(database_oid)); |
| 50 | + // Make sure all the OIDs we are going to use are higher than any of the starting OIDs. |
| 51 | + ASSERT_LT(original_highest_oid, 20'000); |
| 52 | + |
| 53 | + // Create secondary space OIDs of each of the kinds we preserve. |
| 54 | + { |
| 55 | + // Here @ will be replaced by the lowest secondary space OID. |
| 56 | + std::string command = R"( |
| 57 | + SET yb_binary_restore = true; |
| 58 | + SET yb_ignore_pg_class_oids = false; |
| 59 | + SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid((@)::pg_catalog.oid); |
| 60 | + SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid((@+1)::pg_catalog.oid); |
| 61 | + CREATE TYPE high_enum AS ENUM (); |
| 62 | + SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid((@+2)::pg_catalog.oid); |
| 63 | + ALTER TYPE high_enum ADD VALUE 'red'; |
| 64 | + SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid((@+3)::pg_catalog.oid); |
| 65 | + SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode((@+4)::pg_catalog.oid); |
| 66 | + CREATE SEQUENCE high_sequence; |
| 67 | + )"; |
| 68 | + ASSERT_OK(conn.Execute(std::regex_replace( |
| 69 | + command, std::regex{"@"}, std::to_string(kPgFirstSecondarySpaceObjectId)))); |
| 70 | + auto oid = ASSERT_RESULT(sys_catalog->ReadHighestNormalPreservableOid(database_oid)); |
| 71 | + // The addition of these OIDs should not have changed the highest normal space OID at all. |
| 72 | + ASSERT_EQ(oid, original_highest_oid); |
| 73 | + } |
| 74 | + |
| 75 | + { |
| 76 | + // Run CREATE TYPE new_enum AS ENUM ('red', 'orange'); but using 50000-50001 as pg_enum OIDs. |
| 77 | + ASSERT_OK(conn.Execute(R"( |
| 78 | + SET yb_binary_restore = true; |
| 79 | + SET yb_ignore_pg_class_oids = false; |
| 80 | + SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('30000'::pg_catalog.oid); |
| 81 | + SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('30001'::pg_catalog.oid); |
| 82 | +
|
| 83 | + CREATE TYPE new_enum AS ENUM (); |
| 84 | + SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid('50000'::pg_catalog.oid); |
| 85 | + ALTER TYPE new_enum ADD VALUE 'red'; |
| 86 | + SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid('50001'::pg_catalog.oid); |
| 87 | + ALTER TYPE new_enum ADD VALUE 'orange'; |
| 88 | + )")); |
| 89 | + auto oid = ASSERT_RESULT(sys_catalog->ReadHighestNormalPreservableOid(database_oid)); |
| 90 | + EXPECT_EQ(oid, 50'001); |
| 91 | + } |
| 92 | + |
| 93 | + { |
| 94 | + // Run CREATE SEQUENCE new_sequence; using pg_class OID 60000. |
| 95 | + ASSERT_OK(conn.Execute(R"( |
| 96 | + SET yb_binary_restore = true; |
| 97 | + SET yb_ignore_pg_class_oids = false; |
| 98 | + SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('60000'::pg_catalog.oid); |
| 99 | + SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('30010'::pg_catalog.oid); |
| 100 | +
|
| 101 | + CREATE SEQUENCE new_sequence; |
| 102 | + )")); |
| 103 | + auto oid = ASSERT_RESULT(sys_catalog->ReadHighestNormalPreservableOid(database_oid)); |
| 104 | + EXPECT_EQ(oid, 60000); |
| 105 | + } |
| 106 | + |
| 107 | + { |
| 108 | + // Run CREATE TYPE new_composite AS (x int); using pg_type OID 70000-70001. |
| 109 | + ASSERT_OK(conn.Execute(R"( |
| 110 | + SET yb_binary_restore = true; |
| 111 | + SET yb_ignore_pg_class_oids = false; |
| 112 | + SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('70000'::pg_catalog.oid); |
| 113 | + SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('70001'::pg_catalog.oid); |
| 114 | + SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('30020'::pg_catalog.oid); |
| 115 | +
|
| 116 | + CREATE TYPE new_composite AS (x int); |
| 117 | + )")); |
| 118 | + auto oid = ASSERT_RESULT(sys_catalog->ReadHighestNormalPreservableOid(database_oid)); |
| 119 | + EXPECT_EQ(oid, 70'001); |
| 120 | + } |
| 121 | + |
| 122 | + { |
| 123 | + // Run CREATE TABLE public.my_table (x integer); attempting to use pg_relfilenode 80000. |
| 124 | + ASSERT_OK(conn.Execute(R"( |
| 125 | + SET yb_binary_restore = true; |
| 126 | + SET yb_ignore_pg_class_oids = false; |
| 127 | + SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('30030'::pg_catalog.oid); |
| 128 | + SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('30031'::pg_catalog.oid); |
| 129 | + SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('30032'::pg_catalog.oid); |
| 130 | + SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('80000'::pg_catalog.oid); |
| 131 | +
|
| 132 | + CREATE TABLE my_table (x integer); |
| 133 | + )")); |
| 134 | + |
| 135 | + // TODO(yhaddad): fix Postgres to honor binary_upgrade_set_next_heap_relfilenode directive then |
| 136 | + // update this test to expect 80'000. |
| 137 | + // |
| 138 | + // (Currently this directive is not honored, which prevents this test from testing the |
| 139 | + // relfilenode case.) |
| 140 | + LOG(INFO) << ASSERT_RESULT(conn.FetchAllAsString("SELECT oid, relfilenode FROM pg_class;")); |
| 141 | + auto oid = ASSERT_RESULT(sys_catalog->ReadHighestNormalPreservableOid(database_oid)); |
| 142 | + // EXPECT_EQ(oid, 80'000); |
| 143 | + EXPECT_EQ(oid, 70'001); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +} // namespace yb::master |
0 commit comments