@@ -132,25 +132,13 @@ func SetupOrAdvanceStandbyReaderCatalog(
132
132
// if a table and sequence depend on each other, then updating one and
133
133
// fetching the other in a mutable way to remove a dependency will hit
134
134
// a validation error.
135
- for _ , mut := range descriptorsToWrite {
136
- if err := txn .Descriptors ().WriteDescToBatch (ctx , true , mut , b ); err != nil {
137
- return errors .Wrapf (err , "unable to create replicated descriptor: %d %T" , mut .GetID (), mut )
138
- }
139
- }
140
- if err := extracted .ForEachNamespaceEntry (func (e nstree.NamespaceEntry ) error {
141
- if ! shouldSetupForReader (e .GetID (), e .GetParentID ()) {
142
- return nil
143
- }
144
- // Do not upsert entries if one already exists.
145
- entry := allExistingDescs .LookupNamespaceEntry (catalog .MakeNameInfo (e ))
146
- if entry != nil && e .GetID () == entry .GetID () {
147
- return nil
148
- }
149
- return errors .Wrapf (txn .Descriptors ().UpsertNamespaceEntryToBatch (ctx , true , e , b ), "namespace entry %v" , e )
150
- }); err != nil {
151
- return err
152
- }
135
+
153
136
// Figure out which descriptors should be deleted.
137
+ //
138
+ // NB: we issue deletes of existing descriptors/namespace entries before
139
+ // we upsert new ones in the batch to ensure that if we need to delete and
140
+ // upsert the same namespace entry but for a different table id, after the
141
+ // txn, the reader will see the upsert.
154
142
if err := allExistingDescs .ForEachDescriptor (func (desc catalog.Descriptor ) error {
155
143
// Skip descriptors that were updated above
156
144
if ! shouldSetupForReader (desc .GetID (), desc .GetParentID ()) ||
@@ -177,6 +165,25 @@ func SetupOrAdvanceStandbyReaderCatalog(
177
165
}); err != nil {
178
166
return err
179
167
}
168
+
169
+ for _ , mut := range descriptorsToWrite {
170
+ if err := txn .Descriptors ().WriteDescToBatch (ctx , true , mut , b ); err != nil {
171
+ return errors .Wrapf (err , "unable to create replicated descriptor: %d %T" , mut .GetID (), mut )
172
+ }
173
+ }
174
+ if err := extracted .ForEachNamespaceEntry (func (e nstree.NamespaceEntry ) error {
175
+ if ! shouldSetupForReader (e .GetID (), e .GetParentID ()) {
176
+ return nil
177
+ }
178
+ // Do not upsert entries if one already exists with the same ID.
179
+ entry := allExistingDescs .LookupNamespaceEntry (catalog .MakeNameInfo (e ))
180
+ if entry != nil && e .GetID () == entry .GetID () {
181
+ return nil
182
+ }
183
+ return errors .Wrapf (txn .Descriptors ().UpsertNamespaceEntryToBatch (ctx , true , e , b ), "namespace entry %v" , e )
184
+ }); err != nil {
185
+ return err
186
+ }
180
187
if err := maybeBlockSchemaChangesOnSystemDatabase (ctx , txn , b ); err != nil {
181
188
return errors .Wrapf (err , "blocking schema changes" )
182
189
}
0 commit comments