Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BACKPORT pg15-cherrypicks][#23312] YSQL: Fix backup/restore for colo…
…cated tables without tablespace information. Summary: Previously, backup and restore operations for colocated tables failed when multiple tablespaces were present, and the `--use_tablespaces` option was not used. The issue arose because each tablespace could only contain one tablegroup, leading to conflicts during the restore process. Without tablespace information, we could only restore tables to a single tablegroup (corresponding to the default tablespace). However, since a tablegroup represents a (parent) colocated tablet, restoring multiple colocated tablets required the same number of tablegroups. The revision resolves this issue by enabling multiple tablegroups in the default tablespace during restoration. This is achieved through the following steps: * Originally, the tablegroup’s name is `colocation_<tablespace_oid>`, but since the concept of tablespaces doesn’t apply after restoration, we rename the tablegroup as `colocation_restore_<tablegroup_oid>`. The default tablegroup's name remains unchanged after the restore process. * To retain the original name of the default tablegroup, we track which tables are associated with it during the backup. This allows us to correctly restore the tablegroup's properties. **Implementation** This update enhances the backup and restore functionality for colocated tables with tablespaces by addressing the issue as follows: - **Backup Phase**: The backup process now tags each table as either belonging to the default tablegroup or not, along with the tablegroup ID in YSQLDump. The tablegroup name, originally formatted as `colocation_<tablespace_oid>`, will be renamed during restoration as described above. - **Restore Phase**: During restoration, the tablegroup name and its association with the default tablegroup are retrieved from YSQLDump. If the corresponding tablegroup doesn’t already exist, it is created in the default tablespace, allowing multiple tablegroups to coexist without conflicts. - **ALTER Table**: To move a tablegroup to another tablespace after restoration, the syntax: ```ALTER TABLE ALL IN TABLESPACE pg_default COLOCATED WITH <table_name> SET TABLESPACE <new_tablespace_name> CASCADE;``` should be used, ensuring proper management of tablegroups and tablespaces. DB-12237 Original commit: 0dbe7d6 / D36859 - src/postgres/src/backend/commands/indexcmds.c - DefineIndex, in if (IsYugabyteEnabled()) block after is_colocated assignment: - This YB master commit adjusts indentation of braces. - pg15 initial merge 55782d5 replaced the block with assignment of tablegroupId. - Take pg15 change. Note some diff tools may resolve this automatically because the master change is whitespace-only. - DefineIndex, after declaration of is_colocated_tables_with_tablespace_enabled: - This YB master commit adds logic, and fixes indentation of braces. - pg15 merge commit 4e9241b indents logic around if (is_colocated_tables_with_tablespace_enabled && is_colocated_via_database && !MyColocatedDatabaseLegacy) to be inside IsYugabyteEnabled(). - Take the if (is_colocated_tables_with_tablespace_enabled && is_colocated_via_database && !MyColocatedDatabaseLegacy) condition first. Then pull the entire block beginning char *tablegroup_name = NULL;, adjusting the indentation, up to the end of the block for the OidIsValid(tablespaceId) condition. Then take the master change of the remaining conflict, but indent it to be correctly indented within the IsYugabyteEnabled(). - src/postgres/src/backend/utils/adt/ruleutils.c - pg_get_indexdef_worker - This YB master commit removed piece of code. - YB Pg15 commit 55782d5 changed heap_open (this was present in code which was removed in this master commit) to table_open. - Resolved by accepting incoming master commit hence completely removing piece of code. - src/postgres/src/bin/pg_dump/pg_dump.c - getYbTablePropertiesAndReloptions: - This YB master commit introduces new piece of code. - YB Pg15 commit 55782d5, changed exit_horribly to fatal and introduced #ifdef YB_TODO and #endif - Resolved conflict by accepting a combination of both the changes, by keeping it fatal with ifdef and then introducing new code snippet - yb_ysql_dump_colocated_tables_with_tablespaces.data.sql - Upstream PG commit 9a974cbcba005256a19991203583a94b4f9a21a9 preserves relfilenodes. - Upstream PG commit 9fbc3f318d039c3e1e8614c38e40843cf8fcffde adds dump of pg_stat_statements_info. YB role-related code added in YB master commit 5659b73. - Other minor PG15 changes. - yb_ysql_dump_describe_colocated_tables_with_tablespaces.out - Change is likely due to changes in the execution plan, similar to schema ordering differences found in YB pg15 ab32fa2 Test Plan: == Automated == ./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 --java-test org.yb.pgsql.TestYsqlDump#ysqlDumpColocatedTablesWithTablespaces 1. **YBBackupTestColocatedTablesWithTablespaces.TestBackupColocatedTablesWithoutTablespaces**: This test case increases the number of tablespaces (and consequently the number of tablegroups) to replicate the scenario where multiple tablespaces exist. It validates that backup and restore work correctly when performed without the `--use_tablespaces` flag. This is the primary case that this revision addresses. 2. **YBBackupTestColocatedTablesWithTablespaces.TestBackupColocatedTablesWithTablespaces**: To ensure consistency, this test also increases the number of tablespaces but verifies that backup and restore function correctly when using the `--use_tablespaces` flag. 3. **org.yb.pgsql.TestYsqlDump#ysqlDumpColocatedTablesWithTablespaces**: This test checks that the YsqlDump output matches the expected format. Specifically, in the case of the fix, the dump file should include the line: ```SELECT pg_catalog.binary_upgrade_set_next_tablegroup_default(true);``` Reviewers: jason, tfoucher Reviewed By: tfoucher Subscribers: ybase, yql, mlillibridge, svc_phabricator Differential Revision: https://phorge.dev.yugabyte.com/D38269
- Loading branch information