Skip to content

Commit d2acf29

Browse files
craig[bot]rafissmiraradeva
committed
143538: sql: fix drop cascade for nested procedures in legacy schema changer r=rafiss a=rafiss fixes #143282 Release note (bug fix): Fixed a bug that could cause a function reference to be left behind if a procedure referred to another procedure that depends on a a table, and that table is dropped with CASCADE. 143574: kvserver: skip TestTxnReadWithinUncertaintyIntervalAfterRangeMerge under duress r=tbg a=miraradeva This test has always been flaky under deadlock since its introduction. Due to its complexity, we are not going to spend time on improving it now. Informs: #143563 Release note: None 143590: logictest: deflake zone_config_system_tenant r=rafiss a=rafiss This assertion was accidentally rewritten in 24ef7d5. Add it back, and add an additional assertion to prevent further accidental rewrites. fixes #143493 Release note: None Co-authored-by: Rafi Shamim <[email protected]> Co-authored-by: Mira Radeva <[email protected]>
4 parents cc910e9 + 326a04a + 1b3f335 + bde5d52 commit d2acf29

File tree

9 files changed

+99
-9
lines changed

9 files changed

+99
-9
lines changed

pkg/kv/kvserver/client_replica_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,11 @@ func TestTxnReadWithinUncertaintyIntervalAfterLeaseTransfer(t *testing.T) {
930930
func TestTxnReadWithinUncertaintyIntervalAfterRangeMerge(t *testing.T) {
931931
defer leaktest.AfterTest(t)()
932932
defer log.Scope(t).Close(t)
933+
934+
// This test has always been flaky under deadlock since its introduction. Due
935+
// to its complexity, we are not going to spend time on improving it now.
936+
skip.UnderDuress(t)
937+
933938
run := func(t *testing.T, alignLeaseholders bool, alsoSplit bool) {
934939

935940
// The stores 0 and 1 are the "LHS", and the stores 2 and 3 are the RHS.

pkg/sql/drop_cascade.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ func (d *dropCascadeState) dropAllCollectedObjects(ctx context.Context, p *plann
243243
if err := p.canDropFunction(ctx, fn); err != nil {
244244
return err
245245
}
246-
if err := p.dropFunctionImpl(ctx, fn); err != nil {
246+
// We use DropRestrict here since we've already collected all the functions
247+
// in the schema so they will be dropped explicitly.
248+
if err := p.dropFunctionImpl(ctx, fn, tree.DropRestrict); err != nil {
247249
return err
248250
}
249251
}

pkg/sql/drop_function.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (p *planner) DropFunction(ctx context.Context, n *tree.DropRoutine) (ret pl
102102

103103
func (n *dropFunctionNode) startExec(params runParams) error {
104104
for _, fnMutable := range n.toDrop {
105-
if err := params.p.dropFunctionImpl(params.ctx, fnMutable); err != nil {
105+
if err := params.p.dropFunctionImpl(params.ctx, fnMutable, n.dropBehavior); err != nil {
106106
return err
107107
}
108108
}
@@ -192,7 +192,9 @@ func (p *planner) canDropFunction(ctx context.Context, fnDesc catalog.FunctionDe
192192
return nil
193193
}
194194

195-
func (p *planner) dropFunctionImpl(ctx context.Context, fnMutable *funcdesc.Mutable) error {
195+
func (p *planner) dropFunctionImpl(
196+
ctx context.Context, fnMutable *funcdesc.Mutable, dropBehavior tree.DropBehavior,
197+
) error {
196198
if fnMutable.Dropped() {
197199
return errors.Errorf("function %q is already being dropped", fnMutable.Name)
198200
}
@@ -204,6 +206,23 @@ func (p *planner) dropFunctionImpl(ctx context.Context, fnMutable *funcdesc.Muta
204206
return scerrors.ConcurrentSchemaChangeError(fnMutable)
205207
}
206208

209+
// Drop dependent functions first if cascade is specified.
210+
if dropBehavior == tree.DropCascade {
211+
for _, ref := range fnMutable.DependedOnBy {
212+
depFuncMutable, err := p.Descriptors().MutableByID(p.txn).Function(ctx, ref.ID)
213+
if err != nil {
214+
return err
215+
}
216+
// Skip functions that are already being dropped.
217+
if depFuncMutable.Dropped() {
218+
continue
219+
}
220+
if err := p.dropFunctionImpl(ctx, depFuncMutable, dropBehavior); err != nil {
221+
return err
222+
}
223+
}
224+
}
225+
207226
// Remove backreference from tables/views/sequences referenced by this UDF.
208227
for _, id := range fnMutable.DependsOn {
209228
refMutable, err := p.Descriptors().MutableByID(p.txn).Table(ctx, id)
@@ -310,11 +329,11 @@ func (p *planner) writeDropFuncSchemaChange(ctx context.Context, funcDesc *funcd
310329
}
311330

312331
func (p *planner) removeDependentFunction(
313-
ctx context.Context, tbl *tabledesc.Mutable, fn *funcdesc.Mutable,
332+
ctx context.Context, tbl *tabledesc.Mutable, fn *funcdesc.Mutable, dropBehavior tree.DropBehavior,
314333
) error {
315334
// In the table whose index is being removed, filter out all back-references
316335
// that refer to the view that's being removed.
317336
tbl.DependedOnBy = removeMatchingReferences(tbl.DependedOnBy, fn.ID)
318337
// Then proceed to actually drop the view and log an event for it.
319-
return p.dropFunctionImpl(ctx, fn)
338+
return p.dropFunctionImpl(ctx, fn, dropBehavior)
320339
}

pkg/sql/drop_index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ func (p *planner) removeDependents(
547547
droppedViews = append(droppedViews, cascadedViews...)
548548
droppedViews = append(droppedViews, qualifiedView.FQString())
549549
case *funcdesc.Mutable:
550-
if err := p.removeDependentFunction(ctx, tableDesc, t); err != nil {
550+
if err := p.removeDependentFunction(ctx, tableDesc, t, dropBehavior); err != nil {
551551
return nil, err
552552
}
553553
}

pkg/sql/drop_sequence.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func dropDependentOnSequence(ctx context.Context, p *planner, seqDesc *tabledesc
270270
numDependedOnByTablesToSkip++
271271
}
272272
case *funcdesc.Mutable:
273-
err = p.dropFunctionImpl(ctx, t)
273+
err = p.dropFunctionImpl(ctx, t, tree.DropCascade)
274274
default:
275275
err = errors.AssertionFailedf(
276276
"unexpected dependent %s %s on sequence %s",

pkg/sql/drop_table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func (p *planner) dropTableImpl(
324324
droppedViews = append(droppedViews, cascadedViews...)
325325
droppedViews = append(droppedViews, qualifiedView.FQString())
326326
case *funcdesc.Mutable:
327-
if err := p.dropFunctionImpl(ctx, t); err != nil {
327+
if err := p.dropFunctionImpl(ctx, t, behavior); err != nil {
328328
return droppedViews, err
329329
}
330330
}

pkg/sql/drop_view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func (p *planner) dropViewImpl(
313313
cascadeDroppedViews = append(cascadeDroppedViews, cascadedViews...)
314314
cascadeDroppedViews = append(cascadeDroppedViews, qualifiedView.FQString())
315315
case *funcdesc.Mutable:
316-
if err := p.dropFunctionImpl(ctx, t); err != nil {
316+
if err := p.dropFunctionImpl(ctx, t, behavior); err != nil {
317317
return cascadeDroppedViews, err
318318
}
319319
}

pkg/sql/logictest/testdata/logic_test/procedure

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,55 @@ CREATE PROCEDURE p142886(p VARCHAR(100)) LANGUAGE SQL AS $$ SELECT 0; $$;
554554

555555
statement ok
556556
DROP PROCEDURE p142886;
557+
558+
subtest nested_procedure_drop_cascade
559+
560+
statement ok
561+
CREATE TABLE xy (x INT, y INT);
562+
563+
statement ok
564+
CREATE PROCEDURE p1_143282() LANGUAGE PLpgSQL AS $$
565+
BEGIN
566+
INSERT INTO xy VALUES (1, 2) RETURNING x;
567+
END;
568+
$$;
569+
570+
statement ok
571+
CREATE PROCEDURE p2_143282() LANGUAGE PLpgSQL AS $$
572+
DECLARE foo INT;
573+
BEGIN
574+
CALL p1_143282();
575+
END;
576+
$$;
577+
578+
statement ok
579+
CREATE PROCEDURE p3_143282() LANGUAGE PLpgSQL AS $$
580+
BEGIN
581+
CALL p1_143282();
582+
CALL p2_143282();
583+
END;
584+
$$;
585+
586+
statement ok
587+
CALL p3_143282();
588+
589+
# Verify data was inserted twice (once from p1 directly, once via p2)
590+
query II
591+
SELECT * FROM xy ORDER BY x
592+
----
593+
1 2
594+
1 2
595+
596+
statement ok
597+
DROP TABLE xy CASCADE;
598+
599+
statement error procedure p3_143282 does not exist
600+
CALL p3_143282()
601+
602+
statement error procedure p2_143282 does not exist
603+
CALL p2_143282()
604+
605+
statement error procedure p1_143282 does not exist
606+
CALL p1_143282()
607+
608+
subtest end

pkg/sql/logictest/testdata/logic_test/zone_config_system_tenant

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ FROM system.span_configurations
141141
WHERE end_key > (SELECT crdb_internal.table_span($t_id)[1])
142142
ORDER BY start_key
143143
----
144+
/Table/110 {"gcPolicy": {"ttlSeconds": 90001}, "numReplicas": 3, "rangeMaxBytes": "67108864", "rangeMinBytes": "1048576"}
145+
/Table/111 {"gcPolicy": {"ttlSeconds": 90001}, "numReplicas": 3, "rangeMaxBytes": "1073741824", "rangeMinBytes": "67108864"}
146+
147+
# Run the same query again and make sure there are 2 rows. This assertion is
148+
# only here to prevent the previous test case from being rewritten accidentally.
149+
statement count 2
150+
SELECT
151+
crdb_internal.pretty_key(start_key, -1),
152+
crdb_internal.pb_to_json('cockroach.roachpb.SpanConfig', config)
153+
FROM system.span_configurations
154+
WHERE end_key > (SELECT crdb_internal.table_span($t_id)[1])
155+
ORDER BY start_key
144156

145157
subtest transactional_schemachanges
146158

0 commit comments

Comments
 (0)