Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#24809] YSQL: Fix Default Tablespace Backup/Restore For Colocated Ta…
…bles Summary: In commit 0dbe7d6, support was introduced for backup and restore of colocated tables with tablespaces, allowing for backup/restore without explicit tablespace information (without `--use_tablespaces`). This approach included: 1. Adding a binary upgrade option in YSQLDump (`pg_catalog.binary_upgrade_set_next_tablegroup_default`), which was set true when (1) a backup was done without `--use_tablespaces`, and (2) the table originally belonged to the default tablespace/tablegroup. 2. Utilizing the existing `pg_catalog.binary_upgrade_set_next_tablegroup_oid` to retain tablegroup IDs across backup and restore, a feature introduced in commit 254b726. During restoration, tablegroups are named: - With `--use_tablespaces`: `colocation_<new_tablespace_oid>`, achieved by utilizing the tablespace data in YSQLDump. - Without `--use_tablespaces`: During restoration without `--use_tablespaces`, tablegroups are named as `colocation_restore_<tablegroup_oid>` based on the following conditions - If `pg_catalog.binary_upgrade_set_next_tablegroup_oid` has a valid `tablegroup_oid` and `pg_catalog.binary_upgrade_set_next_tablegroup_default` is set to false, then the tablegroup is named `colocation_restore_<tablegroup_oid>`. - If `pg_catalog.binary_upgrade_set_next_tablegroup_default` is set to true or if tablegroup_oid is invalid, the tablegroup is named as "default." - In both cases, the original "default" tablegroup retains its name after restoration. **Problem** Previously, for backups made with `--use_tablespaces`, the YSQLDump structure led to inconsistencies between tables sharing the same colocation tablet (in default tablespace) but assigned different tablegroups on restore. For example, consider the following tables: ``` CREATE DATABASE db WITH COLOCATION=true; CREATE TABLE t1(col INT) TABLESPACE tsp1; CREATE TABLE t2(col INT); CREATE TABLE t3(col INT) TABLESPACE tsp1; CREATE TABLE t4(col INT); ``` The YSQLDump output looked like this: ``` SET default_tablespace = "tsp1"; SELECT pg_catalog.binary_upgrade_set_next_tablegroup_oid(<tablegroup_oid1>); CREATE TABLE t1(col INT); SET default_tablespace = ""; -- default tablespace SELECT pg_catalog.binary_upgrade_set_next_tablegroup_oid(<tablegroup_oid2>); CREATE TABLE t2(col INT); SET default_tablespace = "tsp1"; CREATE TABLE t3(col INT); -- no oid logged again for already touched tablespace SET default_tablespace = ""; -- default tablespace CREATE TABLE t4(col INT); -- no oid logged again for already touched tablespace ``` This inconsistency resulted in tables like `t2` and `t4`, which are part of the same colocation tablet in default tablespace, receiving different tablegroups after restoration: - Table t2 was assigned to the tablegroup `colocation_restore_<tablegroup_oid2>`, as it had a valid tablegroup OID and an invalid tablespace OID (indicating the default tablespace). - Table t4 was assigned to the default tablegroup, as it had both an invalid tablespace and tablegroup OID. This discrepancy in assigned tablegroups was due to inconsistencies in the logged tablegroup information during YSQLDump creation. **Fix** To address this, the revision modifies YSQLDump to now log `pg_catalog.binary_upgrade_set_next_tablegroup_oid` and `pg_catalog.binary_upgrade_set_next_tablegroup_default` for every table creation, in `--use_tablespaces` . This approach ensures that `t2` and `t4` are consistently assigned the same default tablegroup after restoration. ``` SET default_tablespace = "tsp1"; SELECT pg_catalog.binary_upgrade_set_next_tablegroup_oid(<tablegroup_oid1>); CREATE TABLE t1(col INT); SET default_tablespace = ""; -- default tablespace SELECT pg_catalog.binary_upgrade_set_next_tablegroup_oid(<tablegroup_oid2>); SELECT pg_catalog.binary_upgrade_set_next_tablegroup_default(true); -- added CREATE TABLE t2(col INT); SET default_tablespace = "tsp1"; SELECT pg_catalog.binary_upgrade_set_next_tablegroup_oid(<tablegroup_oid1>); -- added CREATE TABLE t3(col INT); SET default_tablespace = ""; SELECT pg_catalog.binary_upgrade_set_next_tablegroup_oid(<tablegroup_oid2>); -- added SELECT pg_catalog.binary_upgrade_set_next_tablegroup_default(true); -- added CREATE TABLE t4(col INT); ``` This revision also resolves unrelated lint errors in the pg code. JIRA: DB-13915 Test Plan: ./yb_build.sh --cxx-test yb-backup-cross-feature-test --gtest_filter YBBackupTestColocatedTablesWithTablespaces.TestBackupColocatedTablesWithTablespaces ./yb_build.sh --cxx-test yb-backup-cross-feature-test --gtest_filter YBBackupTestColocatedTablesWithTablespaces.TestBackupColocatedTablesWithoutTablespaces **./yb_build.sh --cxx-test yb-backup-cross-feature-test --gtest_filter YBBackupTestColocatedTablesWithTablespaces.TestBackupGeoPartitionedColocatedTablesWithTablespaces**: This is a newly introduced test, which reproduces the bug. ./yb_build.sh --cxx-test yb-backup-cross-feature-test --gtest_filter YBBackupTestColocatedTablesWithTablespaces.TestBackupGeoPartitionedColocatedTablesWithoutTablespaces ./yb_build.sh --java-test org.yb.pgsql.TestYsqlDump#ysqlDumpColocatedTablesWithTablespaces ./yb_build.sh --java-test org.yb.pgsql.TestPgRegressColocatedTablesWithTablespaces#testPgRegressColocatedTablesWithTablespaces **./yb_build.sh --java-test 'org.yb.pgsql.TestYsqlDump#ysqlDumpColocatedDB'** : Modified this test to accommodate the changes in the revision ./yb_build.sh --java-test 'org.yb.pgsql.TestYsqlDump#ysqlDumpLegacyColocatedDB' ./yb_build.sh --cxx_test yb-backup-test_ent --gtest-filter YBBackupTest.TestColocationDuplication ./yb_build.sh --cxx_test yb-backup-test_ent --gtest-filter YBBackupTest.TestLegacyColocatedDBColocationDuplication ./yb_build.sh --java-test 'org.yb.pgsql.TestYbBackup#testAlteredTableInColocatedDB' ./yb_build.sh --java-test 'org.yb.pgsql.TestYbBackup#testAlteredTableInLegacyColocatedDB' ./yb_build.sh --java-test 'org.yb.pgsql.TestYbBackup#testMixedColocatedDatabase' ./yb_build.sh --java-test 'org.yb.pgsql.TestYbBackup#testMixedLegacyColocatedDatabase' ./yb_build.sh --java-test 'org.yb.pgsql.TestYbBackup#testColocatedDBWithColocationIdAlreadySet' ./yb_build.sh --java-test 'org.yb.pgsql.TestYbBackup#testLegacyColocatedDBWithColocationIdAlreadySet' ./yb_build.sh --java-test 'org.yb.pgsql.TestYbBackup#testColocatedDatabaseRestoreToOriginalDB' ./yb_build.sh --java-test 'org.yb.pgsql.TestYbBackup#testLegacyColocatedDatabaseRestoreToOriginalDB' ./yb_build.sh --java-test 'org.yb.pgsql.TestYbBackup#testColocatedMateralizedViewBackup' ./yb_build.sh --java-test 'org.yb.pgsql.TestYbBackup#testLegacyColocatedMateralizedViewBackup' Reviewers: skumar, yguan, aagrawal Reviewed By: yguan, aagrawal Subscribers: yql, ybase Differential Revision: https://phorge.dev.yugabyte.com/D39798
- Loading branch information