Skip to content

Commit 08bc10f

Browse files
authored
Merge pull request #157843 from rafiss/blathers/backport-release-25.3-157834
release-25.3: sql: unset RBRUsingConstraint during rollback
2 parents 9d097c2 + 428d2e6 commit 08bc10f

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

pkg/ccl/logictestccl/testdata/logic_test/regional_by_row_foreign_key

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,3 +2244,67 @@ DROP FUNCTION display_child;
22442244
DROP TABLE child;
22452245

22462246
subtest end
2247+
2248+
# ==============================================================================
2249+
# Verify that a foreign key violation causes the addition of the
2250+
# rbr_using_constraint to be reverted properly.
2251+
# ==============================================================================
2252+
2253+
subtest revert_fk_violation
2254+
2255+
statement ok
2256+
SET create_table_with_schema_locked = false;
2257+
2258+
statement ok
2259+
CREATE TABLE a (
2260+
id uuid NOT NULL PRIMARY KEY
2261+
) LOCALITY REGIONAL BY ROW
2262+
2263+
statement ok
2264+
CREATE TABLE b (
2265+
id uuid NOT NULL,
2266+
a_id uuid NULL,
2267+
CONSTRAINT b_pkey PRIMARY KEY (id ASC),
2268+
CONSTRAINT a_id_fk FOREIGN KEY (a_id) REFERENCES a (id)
2269+
) LOCALITY REGIONAL BY ROW
2270+
2271+
# Insert data that will cause a foreign key violation when we try to add the composite FK.
2272+
statement ok
2273+
INSERT INTO a (id, crdb_region) VALUES ('4227eceb-71d6-422c-808b-6a12ec8a1e54', 'us-east-1')
2274+
2275+
statement ok
2276+
INSERT INTO b (id, a_id, crdb_region) VALUES ('8f6ce660-423e-4823-8e41-bf001a007b46', '4227eceb-71d6-422c-808b-6a12ec8a1e54', 'ca-central-1')
2277+
2278+
# This statement should fail with a foreign key violation, and the schema change
2279+
# should properly revert without causing infinite retries due to the missing RBRUsingConstraint.
2280+
# Prior to the bugfix, the rollback logic would would drop the constraint but
2281+
# not clear the RBRUsingConstraint field, causing validation to fail during
2282+
# the rollback.
2283+
statement error pgcode 23503 pq: foreign key violation: "b" row crdb_region='ca-central-1', a_id='4227eceb-71d6-422c-808b-6a12ec8a1e54', id='8f6ce660-423e-4823-8e41-bf001a007b46' has no match in "a"
2284+
ALTER TABLE b
2285+
DROP CONSTRAINT a_id_fk,
2286+
ADD CONSTRAINT a_crdb_region_id_fk
2287+
FOREIGN KEY (crdb_region, a_id) REFERENCES a (crdb_region, id) ON UPDATE CASCADE ON DELETE CASCADE,
2288+
SET (infer_rbr_region_col_using_constraint = a_crdb_region_id_fk)
2289+
2290+
# Verify the tables are still functional after the failed schema change.
2291+
query I
2292+
SELECT count(*) FROM a
2293+
----
2294+
1
2295+
2296+
query I
2297+
SELECT count(*) FROM b
2298+
----
2299+
1
2300+
2301+
statement ok
2302+
DROP TABLE b CASCADE
2303+
2304+
statement ok
2305+
DROP TABLE a CASCADE
2306+
2307+
statement ok
2308+
RESET create_table_with_schema_locked;
2309+
2310+
subtest end

pkg/sql/schema_changer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,11 @@ func (sc *SchemaChanger) maybeDropValidatingConstraint(
24612461
constraint.GetName(),
24622462
)
24632463
} else if constraint.AsForeignKey() != nil {
2464+
// If the constraint being dropped is the regional-by-row constraint,
2465+
// we must clear the reference to it.
2466+
if desc.RBRUsingConstraint == constraint.GetConstraintID() {
2467+
desc.RBRUsingConstraint = descpb.ConstraintID(0)
2468+
}
24642469
for i, fk := range desc.OutboundFKs {
24652470
if fk.Name == constraint.GetName() {
24662471
desc.OutboundFKs = append(desc.OutboundFKs[:i], desc.OutboundFKs[i+1:]...)

0 commit comments

Comments
 (0)