diff --git a/src/postgres/src/bin/pg_dump/pg_dump.c b/src/postgres/src/bin/pg_dump/pg_dump.c index 7eac3e4e0998..f6dc931b756a 100644 --- a/src/postgres/src/bin/pg_dump/pg_dump.c +++ b/src/postgres/src/bin/pg_dump/pg_dump.c @@ -868,7 +868,7 @@ main(int argc, char **argv) pg_yb_tablegroup_exists = catalogTableExists(fout, "pg_yb_tablegroup"); pg_tablegroup_exists = catalogTableExists(fout, "pg_tablegroup"); - /* + /* * Cache (1) whether the dumped database is a colocated database and * (2) whether the dumped database is a legacy colocated database * in global variables. @@ -17279,47 +17279,6 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo) appendPQExpBufferChar(q, ')'); } - /* Get the table and index properties from YB, if relevant. */ - YbTableProperties yb_table_properties = NULL; - YbTableProperties yb_index_properties = NULL; - if (dopt->include_yb_metadata && - (coninfo->contype == 'u')) - { - yb_table_properties = (YbTableProperties) pg_malloc(sizeof(YbTablePropertiesData)); - yb_index_properties = (YbTableProperties) pg_malloc(sizeof(YbTablePropertiesData)); - } - PQExpBuffer yb_table_reloptions = createPQExpBuffer(); - PQExpBuffer yb_index_reloptions = createPQExpBuffer(); - getYbTablePropertiesAndReloptions(fout, yb_table_properties, yb_table_reloptions, - tbinfo->dobj.catId.oid, tbinfo->dobj.name, tbinfo->relkind); - getYbTablePropertiesAndReloptions(fout, yb_index_properties, yb_index_reloptions, - indxinfo->dobj.catId.oid, indxinfo->dobj.name, tbinfo->relkind); - - /* - * Issue #11600: if tablegroups mismatch between the table and its - * constraint, we cannot currently replicate that. - * We have to fail to prevent inconsistency upon yb_backup restore. - */ - if (dopt->include_yb_metadata && - OidIsValid(yb_table_properties->tablegroup_oid) && - yb_index_properties->tablegroup_oid != yb_table_properties->tablegroup_oid) - { - exit_horribly(NULL, - "table %s and its constraint %s have mismatching tablegroups!\n" - "This case cannot currently be handled, see issue " - "https://github.com/yugabyte/yugabyte-db/issues/11600\n", - tbinfo->dobj.name, - coninfo->dobj.name); - } - - YbAppendReloptions2(q, false /* newline_before*/, - indxinfo->indreloptions, "", - yb_index_reloptions->data, "", - fout); - - destroyPQExpBuffer(yb_table_reloptions); - destroyPQExpBuffer(yb_index_reloptions); - if (coninfo->condeferrable) { appendPQExpBufferStr(q, " DEFERRABLE"); diff --git a/src/postgres/src/test/regress/data/yb_ysql_dump_colocated_database.data.sql b/src/postgres/src/test/regress/data/yb_ysql_dump_colocated_database.data.sql index 2a6faded7996..8eca407bd498 100644 --- a/src/postgres/src/test/regress/data/yb_ysql_dump_colocated_database.data.sql +++ b/src/postgres/src/test/regress/data/yb_ysql_dump_colocated_database.data.sql @@ -227,6 +227,33 @@ SPLIT INTO 3 TABLETS; ALTER TABLE public.tbl4 OWNER TO yugabyte_test; \endif +-- +-- Name: tbl5; Type: TABLE; Schema: public; Owner: yugabyte_test +-- + + +-- For binary upgrade, must preserve pg_type oid +SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('16418'::pg_catalog.oid); + + +-- For binary upgrade, must preserve pg_type array oid +SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('16417'::pg_catalog.oid); + + +-- For binary upgrade, must preserve pg_class oids +SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('16416'::pg_catalog.oid); + +CREATE TABLE public.tbl5 ( + k integer, + v integer +) +WITH (colocation_id='20005'); + + +\if :use_roles + ALTER TABLE public.tbl5 OWNER TO yugabyte_test; +\endif + -- -- Data for Name: htest_1; Type: TABLE DATA; Schema: public; Owner: yugabyte_test -- @@ -267,6 +294,28 @@ COPY public.tbl4 (k, v, v2) FROM stdin; \. +-- +-- Data for Name: tbl5; Type: TABLE DATA; Schema: public; Owner: yugabyte_test +-- + +COPY public.tbl5 (k, v) FROM stdin; +\. + + +-- +-- Name: tbl5 tbl5_v_key; Type: CONSTRAINT; Schema: public; Owner: yugabyte_test +-- + + +-- For binary upgrade, must preserve pg_class oids +SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('16419'::pg_catalog.oid); + +CREATE UNIQUE INDEX NONCONCURRENTLY tbl5_v_key ON public.tbl5 USING lsm (v ASC) WITH (colocation_id=20006); + +ALTER TABLE ONLY public.tbl5 + ADD CONSTRAINT tbl5_v_key UNIQUE USING INDEX tbl5_v_key; + + -- -- Name: partial_idx; Type: INDEX; Schema: public; Owner: yugabyte_test -- diff --git a/src/postgres/src/test/regress/data/yb_ysql_dump_legacy_colocated_database.data.sql b/src/postgres/src/test/regress/data/yb_ysql_dump_legacy_colocated_database.data.sql index a2768716eeac..db5c5b3f692e 100644 --- a/src/postgres/src/test/regress/data/yb_ysql_dump_legacy_colocated_database.data.sql +++ b/src/postgres/src/test/regress/data/yb_ysql_dump_legacy_colocated_database.data.sql @@ -224,6 +224,33 @@ SPLIT INTO 3 TABLETS; ALTER TABLE public.tbl4 OWNER TO yugabyte_test; \endif +-- +-- Name: tbl5; Type: TABLE; Schema: public; Owner: yugabyte_test +-- + + +-- For binary upgrade, must preserve pg_type oid +SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('16417'::pg_catalog.oid); + + +-- For binary upgrade, must preserve pg_type array oid +SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('16416'::pg_catalog.oid); + + +-- For binary upgrade, must preserve pg_class oids +SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('16415'::pg_catalog.oid); + +CREATE TABLE public.tbl5 ( + k integer, + v integer +) +WITH (colocation_id='20005'); + + +\if :use_roles + ALTER TABLE public.tbl5 OWNER TO yugabyte_test; +\endif + -- -- Data for Name: htest_1; Type: TABLE DATA; Schema: public; Owner: yugabyte_test -- @@ -264,6 +291,28 @@ COPY public.tbl4 (k, v, v2) FROM stdin; \. +-- +-- Data for Name: tbl5; Type: TABLE DATA; Schema: public; Owner: yugabyte_test +-- + +COPY public.tbl5 (k, v) FROM stdin; +\. + + +-- +-- Name: tbl5 tbl5_v_key; Type: CONSTRAINT; Schema: public; Owner: yugabyte_test +-- + + +-- For binary upgrade, must preserve pg_class oids +SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('16418'::pg_catalog.oid); + +CREATE UNIQUE INDEX NONCONCURRENTLY tbl5_v_key ON public.tbl5 USING lsm (v ASC) WITH (colocation_id=20006); + +ALTER TABLE ONLY public.tbl5 + ADD CONSTRAINT tbl5_v_key UNIQUE USING INDEX tbl5_v_key; + + -- -- Name: partial_idx; Type: INDEX; Schema: public; Owner: yugabyte_test -- diff --git a/src/postgres/src/test/regress/expected/yb_ysql_dump_describe_colocated_database.out b/src/postgres/src/test/regress/expected/yb_ysql_dump_describe_colocated_database.out index 1f4401f9f36f..2f477b6a4521 100644 --- a/src/postgres/src/test/regress/expected/yb_ysql_dump_describe_colocated_database.out +++ b/src/postgres/src/test/regress/expected/yb_ysql_dump_describe_colocated_database.out @@ -7,7 +7,8 @@ public | tbl2 | table | yugabyte_test public | tbl3 | table | yugabyte_test public | tbl4 | table | yugabyte_test -(6 rows) + public | tbl5 | table | yugabyte_test +(7 rows) List of tablegroups Name | Owner | Access privileges | Description | Tablespace | Options @@ -26,5 +27,7 @@ default | postgres | | | | | partial_idx | index | yugabyte_test | | default | postgres | | | | | htest | table | yugabyte_test | | default | postgres | | | | | htest_1 | table | yugabyte_test | | -(8 rows) + default | postgres | | | | | tbl5 | table | yugabyte_test | | + default | postgres | | | | | tbl5_v_key | index | yugabyte_test | | +(10 rows) diff --git a/src/postgres/src/test/regress/expected/yb_ysql_dump_describe_legacy_colocated_database.out b/src/postgres/src/test/regress/expected/yb_ysql_dump_describe_legacy_colocated_database.out index 30ac38e7463d..53a4f53dc362 100644 --- a/src/postgres/src/test/regress/expected/yb_ysql_dump_describe_legacy_colocated_database.out +++ b/src/postgres/src/test/regress/expected/yb_ysql_dump_describe_legacy_colocated_database.out @@ -7,7 +7,8 @@ public | tbl2 | table | yugabyte_test public | tbl3 | table | yugabyte_test public | tbl4 | table | yugabyte_test -(6 rows) + public | tbl5 | table | yugabyte_test +(7 rows) List of tablegroups Name | Owner | Access privileges | Description | Tablespace | Options diff --git a/src/postgres/src/test/regress/sql/yb_ysql_dump_colocated_database.sql b/src/postgres/src/test/regress/sql/yb_ysql_dump_colocated_database.sql index fd7f5f869df0..1d14e8ec78ab 100644 --- a/src/postgres/src/test/regress/sql/yb_ysql_dump_colocated_database.sql +++ b/src/postgres/src/test/regress/sql/yb_ysql_dump_colocated_database.sql @@ -34,3 +34,6 @@ WITH (colocation_id='123456'); CREATE TABLE htest_1 PARTITION OF htest FOR VALUES WITH (modulus 2, remainder 0) WITH (colocation_id='234567'); + +-- UNIQUE constraint +CREATE TABLE tbl5 (k INT, v INT UNIQUE); diff --git a/src/yb/tools/yb-backup/yb-backup-test.cc b/src/yb/tools/yb-backup/yb-backup-test.cc index ddf6b8ddd7fc..05649a977200 100644 --- a/src/yb/tools/yb-backup/yb-backup-test.cc +++ b/src/yb/tools/yb-backup/yb-backup-test.cc @@ -933,5 +933,42 @@ TEST_F(YBBackupTest, LOG(INFO) << "Test finished: " << CURRENT_TEST_CASE_AND_TEST_NAME_STR(); } +TEST_F(YBBackupTest, + YB_DISABLE_TEST_IN_SANITIZERS(TestColocatedTableUniqueConstraint)) { + const string table_name = "mytbl"; + // Create colocated database. + ASSERT_RESULT(RunPsqlCommand("CREATE DATABASE demo WITH colocation=true")); + + // Create table. + SetDbName("demo"); + ASSERT_NO_FATALS(CreateTable(Format("CREATE TABLE $0 (k INT, v INT UNIQUE)", table_name))); + ASSERT_NO_FATALS(InsertRows( + Format("INSERT INTO $0 SELECT i, i FROM generate_series(1, 1000) i", table_name), 1000)); + + const string backup_dir = GetTempDir("backup"); + ASSERT_OK(RunBackupCommand( + {"--backup_location", backup_dir, "--keyspace", "ysql.demo", "create"})); + + ASSERT_OK(RunBackupCommand( + {"--backup_location", backup_dir, "--keyspace", "ysql.demo_new", "restore"})); + + SetDbName("demo_new"); + ASSERT_NO_FATALS(RunPsqlCommand(Format( + "/*+IndexOnlyScan($0)*/ SELECT v FROM $1 WHERE v <= 5 ORDER BY v", table_name, table_name), + R"#( + v + --- + 1 + 2 + 3 + 4 + 5 + (5 rows) + )#" + )); + + LOG(INFO) << "Test finished: " << CURRENT_TEST_CASE_AND_TEST_NAME_STR(); +} + } // namespace tools } // namespace yb