Skip to content

Commit 48d0797

Browse files
committed
workload: detect self-referencing FK for column dependency
In the schemachanger workload we have a function to detect dependencies on a given column. It would look up and check for foreign keys, but it didn't account for self-referencing foreign keys. This updates the query to detect that. Fixes #153547 Epic: none Release note: none
1 parent 7d65e38 commit 48d0797

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

pkg/workload/schemachange/error_screening.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ func (og *operationGenerator) columnIsDependedOn(
303303
// so performing unions and joins is easier.
304304
//
305305
// To check if any foreign key references exist to this table, we use pg_constraint
306-
// and check if any columns are dependent.
306+
// and check if any columns are dependent. We check both confrelid (table is referenced)
307+
// and self-referential foreign keys (where conrelid = confrelid).
307308
return og.scanBool(ctx, tx, `SELECT EXISTS(
308309
SELECT source.column_id
309310
FROM (
@@ -332,6 +333,12 @@ func (og *operationGenerator) columnIsDependedOn(
332333
FROM pg_catalog.pg_constraint
333334
WHERE confrelid = $1::REGCLASS
334335
)
336+
UNION (
337+
SELECT unnest(conkey) AS column_id
338+
FROM pg_catalog.pg_constraint
339+
WHERE conrelid = $1::REGCLASS
340+
AND confrelid = $1::REGCLASS
341+
)
335342
) AS cons
336343
INNER JOIN (
337344
SELECT ordinal_position AS column_id

0 commit comments

Comments
 (0)