Skip to content

Commit f5ae7cb

Browse files
authored
Merge pull request #153804 from spilchen/blathers/backport-release-25.3-153787
release-25.3: sql/schemachanger: fix lost dependencies in ALTER POLICY expressions
2 parents 5f41232 + 5e513d4 commit f5ae7cb

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

pkg/sql/logictest/testdata/logic_test/row_level_security

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5521,6 +5521,64 @@ REVOKE SYSTEM CREATEDB FROM can_createdb_global;
55215521
statement ok
55225522
DROP ROLE can_createdb_global;
55235523

5524+
# Test that dependencies are preserved when altering only the USING expression
5525+
# of a policy that has both USING and WITH CHECK expressions. This is a repro
5526+
# for GH issue 153191.
5527+
subtest bug_fix_lost_dependencies_alter_policy_#153191
5528+
5529+
statement ok
5530+
CREATE TABLE dep_test (id INT PRIMARY KEY, value INT);
5531+
5532+
statement ok
5533+
CREATE SEQUENCE seq_with_check;
5534+
5535+
statement ok
5536+
CREATE FUNCTION func_using_old(n INT) RETURNS BOOL AS $$ SELECT n > 0; $$ LANGUAGE SQL;
5537+
5538+
statement ok
5539+
CREATE FUNCTION func_using_new(n INT) RETURNS BOOL AS $$ SELECT n >= 0; $$ LANGUAGE SQL;
5540+
5541+
# Create a olicy that have different dependencies for USING and WITH CHECK
5542+
# expressions.
5543+
statement ok
5544+
CREATE POLICY test_policy ON dep_test
5545+
FOR ALL
5546+
USING (func_using_old(value))
5547+
WITH CHECK (nextval('seq_with_check') < 1000);
5548+
5549+
# Verify initial dependencies are tracked.
5550+
statement error pq: cannot drop function "func_using_old" because other objects
5551+
DROP FUNCTION func_using_old;
5552+
5553+
statement error pq: cannot drop sequence seq_with_check because other objects depend on it
5554+
DROP SEQUENCE seq_with_check;
5555+
5556+
# Alter only the USING expression, which should preserve the WITH CHECK
5557+
# dependency.
5558+
statement ok
5559+
ALTER POLICY test_policy ON dep_test USING (func_using_new(value));
5560+
5561+
# Ensure dependency on WITH CHECK still exists.
5562+
statement error pq: cannot drop sequence seq_with_check because other objects depend on it
5563+
DROP SEQUENCE seq_with_check;
5564+
5565+
# Ensure old dependency on USING doesn't exist anymore.
5566+
statement ok
5567+
DROP FUNCTION func_using_old;
5568+
5569+
# And the new USING function should be protected.
5570+
statement error pq: cannot drop function "func_using_new" because other objects
5571+
DROP FUNCTION func_using_new;
5572+
5573+
statement ok
5574+
DROP TABLE dep_test CASCADE;
5575+
5576+
statement ok
5577+
DROP SEQUENCE seq_with_check;
5578+
5579+
statement ok
5580+
DROP FUNCTION func_using_new;
5581+
55245582
subtest end
55255583

55265584
subtest filter_pushdown_leaks

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/create_policy.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ func upsertPolicyExpressions(
204204
} else {
205205
// If we aren't dropping the old expression, we need to keep track of
206206
// the dependencies in case we replace PolicyDeps.
207-
usesTypeIDs = catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...)
208-
usesRelationIDs = catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...)
209-
usesFunctionIDs = catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...)
207+
usesTypeIDs = usesTypeIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...))
208+
usesRelationIDs = usesRelationIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...))
209+
usesFunctionIDs = usesFunctionIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...))
210210
}
211211
}
212212
})
@@ -217,9 +217,9 @@ func upsertPolicyExpressions(
217217
PolicyID: policyID,
218218
Expression: *expr,
219219
})
220-
usesTypeIDs = catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...)
221-
usesRelationIDs = catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...)
222-
usesFunctionIDs = catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...)
220+
usesTypeIDs = usesTypeIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...))
221+
usesRelationIDs = usesRelationIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...))
222+
usesFunctionIDs = usesFunctionIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...))
223223
}
224224

225225
policyElems.ForEach(func(current scpb.Status, target scpb.TargetStatus, e scpb.Element) {
@@ -230,9 +230,9 @@ func upsertPolicyExpressions(
230230
} else {
231231
// If we aren't dropping the old expression, we need to keep track of
232232
// the dependencies in case we replace PolicyDeps.
233-
usesTypeIDs = catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...)
234-
usesRelationIDs = catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...)
235-
usesFunctionIDs = catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...)
233+
usesTypeIDs = usesTypeIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...))
234+
usesRelationIDs = usesRelationIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...))
235+
usesFunctionIDs = usesFunctionIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...))
236236
}
237237
}
238238
})

0 commit comments

Comments
 (0)