Skip to content

Commit 24f7c95

Browse files
committed
workload/schemachanger: treat dependency error as potential error in column rename
We previously enforced a strict check to determine if a column being renamed was used by another object. However, this check was incomplete. When the column is part of a hash-sharded primary key, renaming it triggers a rename of the corresponding crdb_internal shard column. If another object (e.g., a trigger) depends on the shard column, the dependency isn’t detected via the original column. As a result, we now treat this as a potential execution-time error instead of statically verifying it. Informs #147514 Epic: none Release note: none
1 parent 9b3b2bc commit 24f7c95

File tree

2 files changed

+5
-42
lines changed

2 files changed

+5
-42
lines changed

pkg/workload/schemachange/error_screening.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -400,43 +400,6 @@ func (og *operationGenerator) colIsRefByComputed(
400400
return colIsRefByGeneratedExpr, nil
401401
}
402402

403-
func (og *operationGenerator) columnIsDependedOnByView(
404-
ctx context.Context, tx pgx.Tx, tableName *tree.TableName, columnName tree.Name,
405-
) (bool, error) {
406-
return og.scanBool(ctx, tx, `SELECT EXISTS(
407-
SELECT source.column_id
408-
FROM (
409-
SELECT DISTINCT column_id
410-
FROM (
411-
SELECT unnest(
412-
string_to_array(
413-
rtrim(
414-
ltrim(
415-
fd.dependedonby_details,
416-
'Columns: ['
417-
),
418-
']'
419-
),
420-
' '
421-
)::INT8[]
422-
) AS column_id
423-
FROM crdb_internal.forward_dependencies
424-
AS fd
425-
WHERE fd.descriptor_id
426-
= $1::REGCLASS
427-
AND fd.dependedonby_type != 'sequence'
428-
)
429-
) AS cons
430-
INNER JOIN (
431-
SELECT ordinal_position AS column_id
432-
FROM information_schema.columns
433-
WHERE table_schema = $2
434-
AND table_name = $3
435-
AND column_name = $4
436-
) AS source ON source.column_id = cons.column_id
437-
)`, tableName.String(), tableName.Schema(), tableName.Object(), columnName)
438-
}
439-
440403
func (og *operationGenerator) colIsPrimaryKey(
441404
ctx context.Context, tx pgx.Tx, tableName *tree.TableName, columnName tree.Name,
442405
) (bool, error) {

pkg/workload/schemachange/operation_generator.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,17 +2200,17 @@ func (og *operationGenerator) renameColumn(ctx context.Context, tx pgx.Tx) (*opS
22002200
if err != nil {
22012201
return nil, err
22022202
}
2203-
columnIsDependedOnByView, err := og.columnIsDependedOnByView(ctx, tx, tableName, srcColumnName)
2204-
if err != nil {
2205-
return nil, err
2206-
}
22072203

22082204
stmt := makeOpStmt(OpStmtDDL)
22092205
stmt.expectedExecErrors.addAll(codesWithConditions{
22102206
{pgcode.UndefinedColumn, !srcColumnExists},
22112207
{pgcode.DuplicateColumn, destColumnExists && srcColumnName != destColumnName},
2212-
{pgcode.DependentObjectsStillExist, columnIsDependedOnByView},
22132208
})
2209+
// The column may be referenced in a view or trigger, which can lead to a
2210+
// dependency error. This is particularly hard to detect in cases where renaming
2211+
// a column that is part of a hash-sharded primary key triggers a cascading rename
2212+
// of the crdb_internal shard column, which might be used indirectly by other objects.
2213+
stmt.potentialExecErrors.add(pgcode.DependentObjectsStillExist)
22142214

22152215
stmt.sql = fmt.Sprintf(`ALTER TABLE %s RENAME COLUMN %s TO %s`,
22162216
tableName.String(), srcColumnName.String(), destColumnName.String())

0 commit comments

Comments
 (0)