Skip to content

Commit 27bf568

Browse files
craig[bot]fqazi
andcommitted
Merge #151127
151127: workload/schemachanger: address drop column flake r=fqazi a=fqazi Previously, the logic used to detect if dropping a column would clean up any indexes backing a foreign key was incorrect. It would only look for exact matching alternative indexes that could back the foreign key, when it should have been looking for precautions. To address this bug, this patch fixes up the CTE query. Fixes: #150539 Release note: None Co-authored-by: Faizan Qazi <[email protected]>
2 parents e670091 + feca636 commit 27bf568

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

pkg/workload/schemachange/error_screening.go

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ func (og *operationGenerator) tableHasDependencies(
140140
}
141141

142142
// columnRemovalWillDropFKBackingIndexes determines if dropping this column
143-
// will lead to no indexes backing a foreign key.
143+
// will lead to no indexes backing a foreign key. CockroachDB will consider
144+
// alternative indexes backing a foreign key if they have the same number of
145+
// key columns as the FK in any order.
144146
func (og *operationGenerator) columnRemovalWillDropFKBackingIndexes(
145147
ctx context.Context, tx pgx.Tx, tableName *tree.TableName, columName tree.Name,
146148
) (bool, error) {
@@ -218,58 +220,51 @@ WITH
218220
NOT LIKE '%%_pkey' -- renames would keep the old table name
219221
)
220222
),
221-
matching_indexes
223+
fk_col_counts
222224
AS (
223225
SELECT
224-
oid,
225-
index_name,
226-
count(base_ordinal) AS count_base_ordinal,
227-
count(seq_in_index) AS count_seq_in_index
226+
oid, count(*) AS num_cols
228227
FROM
229-
fk, valid_indexes
230-
WHERE
231-
storing = 'f'
232-
AND non_unique = 'f'
233-
AND base_col = column_name
228+
fk
234229
GROUP BY
235-
(oid, index_name)
230+
oid
236231
),
237-
valid_index_attrib_count
232+
index_col_counts
238233
AS (
239234
SELECT
240-
index_name,
241-
max(seq_in_index) AS max_seq_in_index
235+
index_name, count(*) AS num_cols
242236
FROM
243237
valid_indexes
244238
WHERE
245239
storing = 'f'
246240
GROUP BY
247241
index_name
248242
),
249-
valid_fk_count
250-
AS (
251-
SELECT
252-
oid, max(base_ordinal) AS max_base_ordinal
253-
FROM
254-
fk
255-
GROUP BY
256-
fk
257-
),
258243
matching_fks
259244
AS (
260245
SELECT
261-
DISTINCT f.oid
246+
fk.oid
262247
FROM
263-
valid_index_attrib_count AS i,
264-
valid_fk_count AS f,
265-
matching_indexes AS m
248+
fk
249+
JOIN valid_indexes
250+
ON
251+
fk.base_col = valid_indexes.column_name
252+
JOIN fk_col_counts
253+
ON
254+
fk.oid = fk_col_counts.oid
255+
JOIN index_col_counts
256+
ON
257+
valid_indexes.index_name = index_col_counts.index_name
266258
WHERE
267-
f.oid = m.oid
268-
AND i.index_name = m.index_name
269-
AND i.max_seq_in_index
270-
= m.count_seq_in_index
271-
AND f.max_base_ordinal
272-
= m.count_base_ordinal
259+
valid_indexes.storing = 'f'
260+
AND valid_indexes.non_unique = 'f'
261+
AND fk_col_counts.num_cols = index_col_counts.num_cols
262+
GROUP BY
263+
fk.oid,
264+
valid_indexes.index_name,
265+
fk_col_counts.num_cols
266+
HAVING
267+
count(*) = fk_col_counts.num_cols
273268
)
274269
SELECT
275270
EXISTS(
@@ -278,7 +273,7 @@ SELECT
278273
FROM
279274
fk
280275
WHERE
281-
oid NOT IN (SELECT oid FROM matching_fks)
276+
oid NOT IN (SELECT DISTINCT oid FROM matching_fks)
282277
);
283278
`, tableName.String(), tableName.String()), tableName.String(), columName)
284279
}

0 commit comments

Comments
 (0)