Skip to content

Commit f2c6da9

Browse files
Merge pull request #153700 from spilchen/blathers/backport-release-25.2-153665
release-25.2: workload: detect self-referencing FK for column dependency
2 parents e97c017 + 6f3dea2 commit f2c6da9

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
@@ -289,7 +289,8 @@ func (og *operationGenerator) columnIsDependedOn(
289289
// so performing unions and joins is easier.
290290
//
291291
// To check if any foreign key references exist to this table, we use pg_constraint
292-
// and check if any columns are dependent.
292+
// and check if any columns are dependent. We check both confrelid (table is referenced)
293+
// and self-referential foreign keys (where conrelid = confrelid).
293294
return og.scanBool(ctx, tx, `SELECT EXISTS(
294295
SELECT source.column_id
295296
FROM (
@@ -318,6 +319,12 @@ func (og *operationGenerator) columnIsDependedOn(
318319
FROM pg_catalog.pg_constraint
319320
WHERE confrelid = $1::REGCLASS
320321
)
322+
UNION (
323+
SELECT unnest(conkey) AS column_id
324+
FROM pg_catalog.pg_constraint
325+
WHERE conrelid = $1::REGCLASS
326+
AND confrelid = $1::REGCLASS
327+
)
321328
) AS cons
322329
INNER JOIN (
323330
SELECT ordinal_position AS column_id

0 commit comments

Comments
 (0)