From 46316ee3ace0da96cb27710e70cf3bab27063ebb Mon Sep 17 00:00:00 2001 From: Radu Berinde Date: Tue, 30 Apr 2019 10:08:04 -0400 Subject: [PATCH] sql: use internal executor for FK validation checks Switching to the internal executor instead of `delegateQuery` for FK validation queries. Release note: None --- pkg/sql/check.go | 43 +++++++++++-------------------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/pkg/sql/check.go b/pkg/sql/check.go index e7c5dba71799..42e47765ae2a 100644 --- a/pkg/sql/check.go +++ b/pkg/sql/check.go @@ -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, ) } } @@ -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",