Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: use internal executor for FK validation checks #37339

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 11 additions & 32 deletions pkg/sql/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,27 +177,17 @@ func (p *planner) validateForeignKey(
query,
)

plan, err := p.delegateQuery(ctx, "ALTER TABLE VALIDATE", query, nil, nil)
if err != nil {
return err
}

plan, err = p.optimizePlan(ctx, plan, allColumns(plan))
if err != nil {
return err
}
defer plan.Close(ctx)

rows, err := p.runWithDistSQL(ctx, plan)
rows, err := p.ExtendedEvalContext().ExecCfg.InternalExecutor.Query(
ctx, "alter-table-validate-fk-full", p.txn, query,
)
if err != nil {
return err
}
defer rows.Close(ctx)

if rows.Len() > 0 {
if len(rows) > 0 {
return pgerror.Newf(pgerror.CodeForeignKeyViolationError,
"foreign key violation: MATCH FULL does not allow mixing of null and nonnull values %s for %s",
rows.At(0), srcIdx.ForeignKey.Name,
rows[0], srcIdx.ForeignKey.Name,
)
}
}
Expand All @@ -209,34 +199,23 @@ func (p *planner) validateForeignKey(
query,
)

plan, err := p.delegateQuery(ctx, "ALTER TABLE VALIDATE", query, nil, nil)
if err != nil {
return err
}

plan, err = p.optimizePlan(ctx, plan, allColumns(plan))
if err != nil {
return err
}
defer plan.Close(ctx)

rows, err := p.runWithDistSQL(ctx, plan)
rows, err := p.ExtendedEvalContext().ExecCfg.InternalExecutor.Query(
ctx, "alter-table-validate-fk", p.txn, query,
)
if err != nil {
return err
}
defer rows.Close(ctx)

if rows.Len() == 0 {
if len(rows) == 0 {
return nil
}

values := rows.At(0)
var pairs bytes.Buffer
for i := range values {
for i, d := range rows[0] {
if i > 0 {
pairs.WriteString(", ")
}
pairs.WriteString(fmt.Sprintf("%s=%v", srcIdx.ColumnNames[i], values[i]))
pairs.WriteString(fmt.Sprintf("%s=%v", srcIdx.ColumnNames[i], d))
}
return pgerror.Newf(pgerror.CodeForeignKeyViolationError,
"foreign key violation: %q row %s has no match in %q",
Expand Down