Skip to content

Commit 6556498

Browse files
committed
[#23897] xClusterDDLRepl: Support create table with partition by primary key
Summary: When creating a table with a partition by the primary key, the primary index created is classified as RELKIND_PARTITIONED_INDEX instead of RELKIND_INDEX. Adding support in the ddlrepl extension to handle this type of index as well - since this index is the same table as the parent table, we don't need to search for / replicate this index. Fixes #23897. Jira: DB-12801 Test Plan: ``` ybd --java-test "org.yb.pgsql.TestPgRegressYbExtensionsYbXclusterDdlReplication" ``` Reviewers: xCluster, yyan Reviewed By: yyan Subscribers: ybase, yql Differential Revision: https://phorge.dev.yugabyte.com/D38033
1 parent f0a5db7 commit 6556498

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

src/postgres/yb-extensions/yb_xcluster_ddl_replication/expected/create_drop_index.out

+10-10
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ SET ROLE NONE;
2828
SELECT yb_data FROM yb_xcluster_ddl_replication.ddl_queue ORDER BY start_time;
2929
yb_data
3030
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
31-
{"user": "yugabyte", "query": "CREATE TABLE foo(i int PRIMARY KEY, a int, b text, c int);", "schema": "create_index", "version": 1, "command_tag": "CREATE TABLE", "new_rel_map": [{"rel_name": "foo", "relfile_oid": 16451}]}
32-
{"user": "yugabyte", "query": "CREATE INDEX foo_idx_simple ON foo(a);", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_simple", "relfile_oid": 16456}]}
33-
{"user": "yugabyte", "query": "CREATE UNIQUE INDEX foo_idx_unique ON foo(b);", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_unique", "relfile_oid": 16457}]}
34-
{"user": "yugabyte", "query": "CREATE INDEX foo_idx_filtered ON foo(c ASC, a) WHERE a > c;", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_filtered", "relfile_oid": 16458}]}
35-
{"user": "new_role", "query": "CREATE INDEX foo_idx_include ON foo(lower(b)) INCLUDE (a) SPLIT INTO 2 TABLETS;", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_include", "relfile_oid": 16459}]}
31+
{"user": "yugabyte", "query": "CREATE TABLE foo(i int PRIMARY KEY, a int, b text, c int);", "schema": "create_index", "version": 1, "command_tag": "CREATE TABLE", "new_rel_map": [{"rel_name": "foo", "relfile_oid": 16459}]}
32+
{"user": "yugabyte", "query": "CREATE INDEX foo_idx_simple ON foo(a);", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_simple", "relfile_oid": 16464}]}
33+
{"user": "yugabyte", "query": "CREATE UNIQUE INDEX foo_idx_unique ON foo(b);", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_unique", "relfile_oid": 16465}]}
34+
{"user": "yugabyte", "query": "CREATE INDEX foo_idx_filtered ON foo(c ASC, a) WHERE a > c;", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_filtered", "relfile_oid": 16466}]}
35+
{"user": "new_role", "query": "CREATE INDEX foo_idx_include ON foo(lower(b)) INCLUDE (a) SPLIT INTO 2 TABLETS;", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_include", "relfile_oid": 16467}]}
3636
(5 rows)
3737

3838
SELECT * FROM yb_xcluster_ddl_replication.replicated_ddls ORDER BY start_time;
@@ -54,11 +54,11 @@ DROP TABLE foo;
5454
SELECT yb_data FROM yb_xcluster_ddl_replication.ddl_queue ORDER BY start_time;
5555
yb_data
5656
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
57-
{"user": "yugabyte", "query": "CREATE TABLE foo(i int PRIMARY KEY, a int, b text, c int);", "schema": "create_index", "version": 1, "command_tag": "CREATE TABLE", "new_rel_map": [{"rel_name": "foo", "relfile_oid": 16451}]}
58-
{"user": "yugabyte", "query": "CREATE INDEX foo_idx_simple ON foo(a);", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_simple", "relfile_oid": 16456}]}
59-
{"user": "yugabyte", "query": "CREATE UNIQUE INDEX foo_idx_unique ON foo(b);", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_unique", "relfile_oid": 16457}]}
60-
{"user": "yugabyte", "query": "CREATE INDEX foo_idx_filtered ON foo(c ASC, a) WHERE a > c;", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_filtered", "relfile_oid": 16458}]}
61-
{"user": "new_role", "query": "CREATE INDEX foo_idx_include ON foo(lower(b)) INCLUDE (a) SPLIT INTO 2 TABLETS;", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_include", "relfile_oid": 16459}]}
57+
{"user": "yugabyte", "query": "CREATE TABLE foo(i int PRIMARY KEY, a int, b text, c int);", "schema": "create_index", "version": 1, "command_tag": "CREATE TABLE", "new_rel_map": [{"rel_name": "foo", "relfile_oid": 16459}]}
58+
{"user": "yugabyte", "query": "CREATE INDEX foo_idx_simple ON foo(a);", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_simple", "relfile_oid": 16464}]}
59+
{"user": "yugabyte", "query": "CREATE UNIQUE INDEX foo_idx_unique ON foo(b);", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_unique", "relfile_oid": 16465}]}
60+
{"user": "yugabyte", "query": "CREATE INDEX foo_idx_filtered ON foo(c ASC, a) WHERE a > c;", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_filtered", "relfile_oid": 16466}]}
61+
{"user": "new_role", "query": "CREATE INDEX foo_idx_include ON foo(lower(b)) INCLUDE (a) SPLIT INTO 2 TABLETS;", "schema": "create_index", "version": 1, "command_tag": "CREATE INDEX", "new_rel_map": [{"rel_name": "foo_idx_include", "relfile_oid": 16467}]}
6262
{"user": "yugabyte", "query": "DROP INDEX foo_idx_unique;", "schema": "create_index", "version": 1, "command_tag": "DROP INDEX"}
6363
{"user": "yugabyte", "query": "DROP INDEX foo_idx_filtered;", "schema": "create_index", "version": 1, "command_tag": "DROP INDEX"}
6464
{"user": "yugabyte", "query": "DROP TABLE foo;", "schema": "create_index", "version": 1, "command_tag": "DROP TABLE"}

src/postgres/yb-extensions/yb_xcluster_ddl_replication/expected/create_drop_table.out

+18-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ SELECT * FROM yb_xcluster_ddl_replication.replicated_ddls ORDER BY start_time;
3636
3 | 1 | {"query": "CREATE TABLE unique_foo(i int PRIMARY KEY, u text UNIQUE);"}
3737
(3 rows)
3838

39+
-- Test tables partitioned by their primary key or a column.
40+
CREATE TABLE foo_partitioned_by_pkey(id int, PRIMARY KEY (id)) PARTITION BY RANGE (id);
41+
CREATE TABLE foo_partitioned_by_col(id int) PARTITION BY RANGE (id);
3942
-- Now test dropping these tables.
4043
DROP TABLE foo;
4144
-- Check with manual replication flags enabled, ddl string is captured with flag.
@@ -44,29 +47,39 @@ DROP TABLE manual_foo;
4447
SET yb_xcluster_ddl_replication.enable_manual_ddl_replication = 0;
4548
DROP TABLE extra_foo;
4649
DROP TABLE unique_foo;
50+
DROP TABLE foo_partitioned_by_pkey;
51+
DROP TABLE foo_partitioned_by_col;
4752
SELECT yb_data FROM yb_xcluster_ddl_replication.ddl_queue ORDER BY start_time;
4853
yb_data
4954
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5055
{"user": "yugabyte", "query": "CREATE TABLE foo(i int PRIMARY KEY);", "schema": "public", "version": 1, "command_tag": "CREATE TABLE", "new_rel_map": [{"rel_name": "foo", "relfile_oid": 16412}]}
5156
{"user": "yugabyte", "query": "CREATE TABLE manual_foo(i int PRIMARY KEY);", "schema": "public", "version": 1, "command_tag": "CREATE TABLE", "manual_replication": true}
5257
{"user": "yugabyte", "query": "CREATE TABLE extra_foo(i int PRIMARY KEY) WITH (COLOCATION = false) SPLIT INTO 1 TABLETS;", "schema": "public", "version": 1, "command_tag": "CREATE TABLE", "new_rel_map": [{"rel_name": "extra_foo", "relfile_oid": 16422}]}
5358
{"user": "yugabyte", "query": "CREATE TABLE unique_foo(i int PRIMARY KEY, u text UNIQUE);", "schema": "public", "version": 1, "command_tag": "CREATE TABLE", "new_rel_map": [{"rel_name": "unique_foo", "relfile_oid": 16427}, {"rel_name": "unique_foo_u_key", "relfile_oid": 16432}]}
59+
{"user": "yugabyte", "query": "CREATE TABLE foo_partitioned_by_pkey(id int, PRIMARY KEY (id)) PARTITION BY RANGE (id);", "schema": "public", "version": 1, "command_tag": "CREATE TABLE", "new_rel_map": [{"rel_name": "foo_partitioned_by_pkey", "relfile_oid": 16434}]}
60+
{"user": "yugabyte", "query": "CREATE TABLE foo_partitioned_by_col(id int) PARTITION BY RANGE (id);", "schema": "public", "version": 1, "command_tag": "CREATE TABLE", "new_rel_map": [{"rel_name": "foo_partitioned_by_col", "relfile_oid": 16439}]}
5461
{"user": "yugabyte", "query": "DROP TABLE foo;", "schema": "public", "version": 1, "command_tag": "DROP TABLE"}
5562
{"user": "yugabyte", "query": "DROP TABLE manual_foo;", "schema": "public", "version": 1, "command_tag": "DROP TABLE", "manual_replication": true}
5663
{"user": "yugabyte", "query": "DROP TABLE extra_foo;", "schema": "public", "version": 1, "command_tag": "DROP TABLE"}
5764
{"user": "yugabyte", "query": "DROP TABLE unique_foo;", "schema": "public", "version": 1, "command_tag": "DROP TABLE"}
58-
(8 rows)
65+
{"user": "yugabyte", "query": "DROP TABLE foo_partitioned_by_pkey;", "schema": "public", "version": 1, "command_tag": "DROP TABLE"}
66+
{"user": "yugabyte", "query": "DROP TABLE foo_partitioned_by_col;", "schema": "public", "version": 1, "command_tag": "DROP TABLE"}
67+
(12 rows)
5968

6069
SELECT * FROM yb_xcluster_ddl_replication.replicated_ddls ORDER BY start_time;
6170
start_time | query_id | yb_data
6271
------------+----------+--------------------------------------------------------------------------------------------------------
6372
1 | 1 | {"query": "CREATE TABLE foo(i int PRIMARY KEY);"}
6473
2 | 1 | {"query": "CREATE TABLE extra_foo(i int PRIMARY KEY) WITH (COLOCATION = false) SPLIT INTO 1 TABLETS;"}
6574
3 | 1 | {"query": "CREATE TABLE unique_foo(i int PRIMARY KEY, u text UNIQUE);"}
66-
4 | 1 | {"query": "DROP TABLE foo;"}
67-
5 | 1 | {"query": "DROP TABLE extra_foo;"}
68-
6 | 1 | {"query": "DROP TABLE unique_foo;"}
69-
(6 rows)
75+
4 | 1 | {"query": "CREATE TABLE foo_partitioned_by_pkey(id int, PRIMARY KEY (id)) PARTITION BY RANGE (id);"}
76+
5 | 1 | {"query": "CREATE TABLE foo_partitioned_by_col(id int) PARTITION BY RANGE (id);"}
77+
6 | 1 | {"query": "DROP TABLE foo;"}
78+
7 | 1 | {"query": "DROP TABLE extra_foo;"}
79+
8 | 1 | {"query": "DROP TABLE unique_foo;"}
80+
9 | 1 | {"query": "DROP TABLE foo_partitioned_by_pkey;"}
81+
10 | 1 | {"query": "DROP TABLE foo_partitioned_by_col;"}
82+
(10 rows)
7083

7184
-- Test mix of temp and regular tables.
7285
SET yb_xcluster_ddl_replication.replication_role = SOURCE;

src/postgres/yb-extensions/yb_xcluster_ddl_replication/source_ddl_end_handler.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ ShouldReplicateCreateRelation(Oid rel_oid, List **new_rel_list)
8888

8989
// Ignore temporary tables and primary indexes (same as main table).
9090
if (!IsYBBackedRelation(rel) ||
91-
(rel->rd_rel->relkind == RELKIND_INDEX && rel->rd_index->indisprimary))
91+
((rel->rd_rel->relkind == RELKIND_INDEX ||
92+
rel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
93+
rel->rd_index->indisprimary))
9294
{
9395
RelationClose(rel);
9496
return false;

src/postgres/yb-extensions/yb_xcluster_ddl_replication/sql/create_drop_table.sql

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ CREATE TABLE unique_foo(i int PRIMARY KEY, u text UNIQUE);
2525
SELECT yb_data FROM yb_xcluster_ddl_replication.ddl_queue ORDER BY start_time;
2626
SELECT * FROM yb_xcluster_ddl_replication.replicated_ddls ORDER BY start_time;
2727

28+
-- Test tables partitioned by their primary key or a column.
29+
CREATE TABLE foo_partitioned_by_pkey(id int, PRIMARY KEY (id)) PARTITION BY RANGE (id);
30+
CREATE TABLE foo_partitioned_by_col(id int) PARTITION BY RANGE (id);
31+
2832
-- Now test dropping these tables.
2933
DROP TABLE foo;
3034

@@ -35,6 +39,8 @@ SET yb_xcluster_ddl_replication.enable_manual_ddl_replication = 0;
3539

3640
DROP TABLE extra_foo;
3741
DROP TABLE unique_foo;
42+
DROP TABLE foo_partitioned_by_pkey;
43+
DROP TABLE foo_partitioned_by_col;
3844

3945
SELECT yb_data FROM yb_xcluster_ddl_replication.ddl_queue ORDER BY start_time;
4046
SELECT * FROM yb_xcluster_ddl_replication.replicated_ddls ORDER BY start_time;

0 commit comments

Comments
 (0)