Skip to content

Commit 218af53

Browse files
committed
[BACKPORT 2024.2][#25929] Docdb: Disable catalog version check when reading sequences as of time
Summary: Original commit: 7fc910d / D41911 Currently, clone is failing when the database has a sequence, and the time we are cloning to is a time before a DDL that increments the last_breaking_version of the source database. Clone is failing in ysql_dump phase when executing the following query to get the sequence data: `SELECT last_value, is_called FROM public.seq_name`. The error is: `ERROR: The catalog snapshot used for this transaction has been invalidated: expected: 2, got: 1: MISMATCHED_SCHEMA`. Generally, ysql_dump sets the GUC `yb_disable_catalog_version_check` to true, which should not set the catalog version in all the read requests that ysql_dump is issuing, and thus no catalog version check is performed. However, as sequences have their own read path, the GUC `yb_disable_catalog_version_check` is not taking effect, and the catalog version check is returning a catalog version mismatch. Fixed by extending the role of `yb_disable_catalog_version_check` to cover the sequences' read path. When set to true, we don't send the catalog version with the read request to the tserver and thus disable the catalog version check for such a sequence read query. This allows ysql_dump to read the sequence as of a time regardless of the catalog version at that time and thus, being able to clone sequences to a time before a DDL happened. Jira: DB-15245 Test Plan: ./yb_build.sh --cxx-test integration-tests_minicluster-snapshot-test --gtest_filter Colocation/PgCloneTestWithColocatedDBParam.CloneWithSequencesAndDdl/0 Reviewers: asrivastava Reviewed By: asrivastava Subscribers: slingam, yql, ybase Differential Revision: https://phorge.dev.yugabyte.com/D42696
1 parent 0522ebf commit 218af53

File tree

9 files changed

+40
-15
lines changed

9 files changed

+40
-15
lines changed

src/postgres/src/backend/utils/misc/pg_yb_utils.c

-1
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,6 @@ bool yb_enable_fkey_catcache = true;
14581458
int yb_insert_on_conflict_read_batch_size = 1024;
14591459
bool yb_enable_inplace_index_update = true;
14601460
bool yb_enable_nop_alter_role_optimization = true;
1461-
bool yb_disable_catalog_version_check = false;
14621461

14631462
YBUpdateOptimizationOptions yb_update_optimization_options = {
14641463
.has_infra = true,

src/postgres/src/include/pg_yb_utils.h

-2
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,6 @@ extern int yb_insert_on_conflict_read_batch_size;
582582
*/
583583
extern bool yb_enable_nop_alter_role_optimization;
584584

585-
extern bool yb_disable_catalog_version_check;
586-
587585
//------------------------------------------------------------------------------
588586
// GUC variables needed by YB via their YB pointers.
589587
extern int StatementTimeout;

src/yb/integration-tests/minicluster-snapshot-test.cc

+17
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,23 @@ TEST_P(PgCloneTestWithColocatedDBParam, YB_DISABLE_TEST_IN_SANITIZERS(CloneWithS
975975
ASSERT_EQ(row, kRows[3]);
976976
}
977977

978+
TEST_P(PgCloneTestWithColocatedDBParam, YB_DISABLE_TEST_IN_SANITIZERS(CloneWithSequencesAndDdl)) {
979+
auto seq_table_name = "table_with_sequence";
980+
ASSERT_OK(
981+
source_conn_->ExecuteFormat("CREATE TABLE $0 (id INT, i2 SERIAL, c1 INT)", seq_table_name));
982+
ASSERT_OK(source_conn_->ExecuteFormat("INSERT INTO $0 (id,c1) VALUES (11,22)", seq_table_name));
983+
auto clone_to_time = ASSERT_RESULT(GetCurrentTime()).ToInt64();
984+
// Run a DDL that increments the last_breaking_version of pg_yb_catalog.
985+
ASSERT_OK(source_conn_->ExecuteFormat("ALTER TABLE $0 RENAME COLUMN c1 TO c2", seq_table_name));
986+
ASSERT_OK(source_conn_->ExecuteFormat(
987+
"CREATE DATABASE $0 TEMPLATE $1 AS OF $2", kTargetNamespaceName1, kSourceNamespaceName,
988+
clone_to_time));
989+
auto target_conn = ASSERT_RESULT(ConnectToDB(kTargetNamespaceName1));
990+
auto row = ASSERT_RESULT(
991+
(target_conn.FetchRow<int64_t>(Format("SELECT count(*) FROM $0", seq_table_name))));
992+
ASSERT_EQ(row, 1);
993+
}
994+
978995
// Test yb_database_clones (ysql function to list clones)
979996
TEST_F(PgCloneTest, YB_DISABLE_TEST_IN_SANITIZERS(YsqlListClonesAPI)) {
980997
std::string list_clones_query =

src/yb/yql/pggate/pg_client.cc

+10-8
Original file line numberDiff line numberDiff line change
@@ -688,17 +688,19 @@ class PgClient::Impl : public BigDataFetcher {
688688
}
689689

690690
Result<std::pair<int64_t, bool>> ReadSequenceTuple(
691-
int64_t db_oid, int64_t seq_oid, uint64_t ysql_catalog_version,
691+
int64_t db_oid, int64_t seq_oid, std::optional<uint64_t> ysql_catalog_version,
692692
bool is_db_catalog_version_mode, std::optional<uint64_t> read_time) {
693693
tserver::PgReadSequenceTupleRequestPB req;
694694
req.set_session_id(session_id_);
695695
req.set_db_oid(db_oid);
696696
req.set_seq_oid(seq_oid);
697-
if (is_db_catalog_version_mode) {
698-
DCHECK(FLAGS_ysql_enable_db_catalog_version_mode);
699-
req.set_ysql_db_catalog_version(ysql_catalog_version);
700-
} else {
701-
req.set_ysql_catalog_version(ysql_catalog_version);
697+
if (ysql_catalog_version) {
698+
if (is_db_catalog_version_mode) {
699+
DCHECK(FLAGS_ysql_enable_db_catalog_version_mode);
700+
req.set_ysql_db_catalog_version(*ysql_catalog_version);
701+
} else {
702+
req.set_ysql_catalog_version(*ysql_catalog_version);
703+
}
702704
}
703705

704706
if (read_time) {
@@ -1644,8 +1646,8 @@ Result<std::pair<int64_t, int64_t>> PgClient::FetchSequenceTuple(int64_t db_oid,
16441646
}
16451647

16461648
Result<std::pair<int64_t, bool>> PgClient::ReadSequenceTuple(
1647-
int64_t db_oid, int64_t seq_oid, uint64_t ysql_catalog_version, bool is_db_catalog_version_mode,
1648-
std::optional<uint64_t> read_time) {
1649+
int64_t db_oid, int64_t seq_oid, std::optional<uint64_t> ysql_catalog_version,
1650+
bool is_db_catalog_version_mode, std::optional<uint64_t> read_time) {
16491651
return impl_->ReadSequenceTuple(
16501652
db_oid, seq_oid, ysql_catalog_version, is_db_catalog_version_mode, read_time);
16511653
}

src/yb/yql/pggate/pg_client.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class PgClient {
211211
bool cycle);
212212

213213
Result<std::pair<int64_t, bool>> ReadSequenceTuple(
214-
int64_t db_oid, int64_t seq_oid, uint64_t ysql_catalog_version,
214+
int64_t db_oid, int64_t seq_oid, std::optional<uint64_t> ysql_catalog_version,
215215
bool is_db_catalog_version_mode, std::optional<uint64_t> read_time = std::nullopt);
216216

217217
Status DeleteSequenceTuple(int64_t db_oid, int64_t seq_oid);

src/yb/yql/pggate/pg_session.cc

+8-3
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,17 @@ Result<std::pair<int64_t, bool>> PgSession::ReadSequenceTuple(int64_t db_oid,
491491
int64_t seq_oid,
492492
uint64_t ysql_catalog_version,
493493
bool is_db_catalog_version_mode) {
494+
std::optional<uint64_t> optional_ysql_catalog_version = std::nullopt;
495+
std::optional<uint64_t> optional_yb_read_time = std::nullopt;
496+
if (!yb_disable_catalog_version_check) {
497+
optional_ysql_catalog_version = ysql_catalog_version;
498+
}
494499
if (yb_read_time != 0) {
495-
return pg_client_.ReadSequenceTuple(
496-
db_oid, seq_oid, ysql_catalog_version, is_db_catalog_version_mode, yb_read_time);
500+
optional_yb_read_time = yb_read_time;
497501
}
498502
return pg_client_.ReadSequenceTuple(
499-
db_oid, seq_oid, ysql_catalog_version, is_db_catalog_version_mode);
503+
db_oid, seq_oid, optional_ysql_catalog_version, is_db_catalog_version_mode,
504+
optional_yb_read_time);
500505
}
501506

502507
//--------------------------------------------------------------------------------------------------

src/yb/yql/pggate/util/yb_guc.cc

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ int yb_explicit_row_locking_batch_size = 1;
8181

8282
uint64_t yb_read_time = 0;
8383
bool yb_is_read_time_ht = false;
84+
bool yb_disable_catalog_version_check = false;
8485

8586
int yb_read_after_commit_visibility = 0;
8687

src/yb/yql/pggate/util/yb_guc.h

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ extern int yb_xcluster_consistency_level;
154154

155155
extern uint64_t yb_read_time;
156156
extern bool yb_is_read_time_ht;
157+
extern bool yb_disable_catalog_version_check;
158+
157159
/*
158160
* Allows for customizing the number of rows to be prefetched.
159161
*/

src/yb/yql/pggate/util/ybc_util.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <stddef.h>
2020
#include <stdint.h>
2121

22+
#include "yb/yql/pggate/util/yb_guc.h"
2223
#include "yb/yql/pggate/ybc_pg_typedefs.h"
2324

2425
#ifdef __cplusplus

0 commit comments

Comments
 (0)