@@ -55,6 +55,8 @@ import (
55
55
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
56
56
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
57
57
"github.com/cockroachdb/cockroach/pkg/sql/isql"
58
+ "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
59
+ "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
58
60
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scbackup"
59
61
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
60
62
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
@@ -2080,6 +2082,12 @@ func (r *restoreResumer) doResume(ctx context.Context, execCtx interface{}) erro
2080
2082
}
2081
2083
}
2082
2084
2085
+ if err := p .ExecCfg ().JobRegistry .CheckPausepoint (
2086
+ "restore.after_cleanup_temp_system_tables" ,
2087
+ ); err != nil {
2088
+ return err
2089
+ }
2090
+
2083
2091
if details .DescriptorCoverage != tree .RequestedDescriptors {
2084
2092
// Bump the version of the role membership table so that the cache is
2085
2093
// invalidated.
@@ -2766,24 +2774,20 @@ func (r *restoreResumer) dropDescriptors(
2766
2774
b := txn .KV ().NewBatch ()
2767
2775
const kvTrace = false
2768
2776
// Collect the tables into mutable versions.
2769
- mutableTables := make ([]* tabledesc.Mutable , len (details .TableDescs ))
2770
- for i := range details .TableDescs {
2771
- var err error
2772
- mutableTables [i ], err = descsCol .MutableByID (txn .KV ()).Table (ctx , details .TableDescs [i ].ID )
2773
- if err != nil {
2774
- return err
2775
- }
2776
- // Ensure that the version matches what we expect. In the case that it
2777
- // doesn't, it's not really clear what to do. Just log and carry on. If the
2778
- // descriptors have already been published, then there's nothing to fuss
2779
- // about so we only do this check if they have not been published.
2780
- if ! details .DescriptorsPublished {
2781
- if got , exp := mutableTables [i ].Version , details .TableDescs [i ].Version ; got != exp {
2782
- log .Errorf (ctx , "version changed for restored descriptor %d before " +
2783
- "drop: got %d, expected %d" , mutableTables [i ].GetID (), got , exp )
2784
- }
2777
+ mutableTables , err := getUndroppedTablesFromRestore (
2778
+ ctx , txn .KV (), details , descsCol ,
2779
+ )
2780
+ if err != nil {
2781
+ return errors .Wrap (err , "getting mutable tables from restore" )
2782
+ }
2783
+ // Ensure that the table versions matches what we expect. In the case that it
2784
+ // doesn't, it's not really clear what to do. Just log and carry on. If the
2785
+ // descriptors have already been published, then there's nothing to fuss
2786
+ // about so we only do this check if they have not been published.
2787
+ if ! details .DescriptorsPublished {
2788
+ if err := checkRestoredTableDescriptorVersions (details , mutableTables ); err != nil {
2789
+ log .Errorf (ctx , "table version mismatch during drop: %v" , err )
2785
2790
}
2786
-
2787
2791
}
2788
2792
2789
2793
// Remove any back references installed from existing types to tables being restored.
@@ -3363,6 +3367,58 @@ func (r *restoreResumer) cleanupTempSystemTables(ctx context.Context) error {
3363
3367
return nil
3364
3368
}
3365
3369
3370
+ // getUndroppedTablesFromRestore retrieves all table descriptors, offline or
3371
+ // online, as listed in the restore details, excluding any tables that have been
3372
+ // dropped or marked as dropped. This helps avoid situations where the temporary
3373
+ // system database tables have already been copied over to the real system
3374
+ // database and dropped but attempting to load those temporary tables again
3375
+ // would result in an error.
3376
+ func getUndroppedTablesFromRestore (
3377
+ ctx context.Context , txn * kv.Txn , details jobspb.RestoreDetails , descCol * descs.Collection ,
3378
+ ) ([]* tabledesc.Mutable , error ) {
3379
+ var tables []* tabledesc.Mutable
3380
+ for _ , desc := range details .TableDescs {
3381
+ mutableTable , err := descCol .MutableByID (txn ).Table (ctx , desc .ID )
3382
+ if err != nil {
3383
+ if pgerror .GetPGCode (err ) == pgcode .UndefinedTable {
3384
+ continue
3385
+ }
3386
+ return nil , err
3387
+ }
3388
+ if mutableTable .Dropped () {
3389
+ continue
3390
+ }
3391
+ tables = append (tables , mutableTable )
3392
+ }
3393
+ return tables , nil
3394
+ }
3395
+
3396
+ // checkeRestoredTableDescriptorVersions compares the versions of descriptors at
3397
+ // the time of restore with the versions of the restored tables. It returns an
3398
+ // error if any of the restored tables have a version that does not match the
3399
+ // version that was recorded at the time of restore in the job details.
3400
+ func checkRestoredTableDescriptorVersions (
3401
+ details jobspb.RestoreDetails , restoredTables []* tabledesc.Mutable ,
3402
+ ) error {
3403
+ versionsAtRestoreTime := make (map [descpb.ID ]descpb.DescriptorVersion )
3404
+ for _ , desc := range details .TableDescs {
3405
+ versionsAtRestoreTime [desc .ID ] = desc .Version
3406
+ }
3407
+ for _ , table := range restoredTables {
3408
+ if expVersion , ok := versionsAtRestoreTime [table .GetID ()]; ok {
3409
+ if table .Version != expVersion {
3410
+ return errors .Errorf (
3411
+ "version mismatch for restored descriptor %d, expected version %d, got %d" ,
3412
+ table .GetID (), expVersion , table .Version ,
3413
+ )
3414
+ }
3415
+ } else {
3416
+ return errors .Errorf ("restored table %d not found in restore details" , table .GetID ())
3417
+ }
3418
+ }
3419
+ return nil
3420
+ }
3421
+
3366
3422
var _ jobs.Resumer = & restoreResumer {}
3367
3423
3368
3424
func init () {
0 commit comments