Skip to content

Commit 37cba31

Browse files
committed
sql: display notice with job ID when waiting for a job
This will allow users to easily see the job ID in case they'd like to look at more details about the job in other introspection interfaces. Release note: None
1 parent 2a1771e commit 37cba31

File tree

9 files changed

+51
-17
lines changed

9 files changed

+51
-17
lines changed

pkg/backup/datadriven_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ func (d *datadrivenTestState) getSQLDBForVC(
283283
t.Fatal(err)
284284
}
285285
connector := pq.ConnectorWithNoticeHandler(base, func(notice *pq.Error) {
286+
// Skip all "waiting for job(s) to complete" notices, since they include
287+
// non-deterministic jobIDs.
288+
if strings.HasPrefix(notice.Message, "waiting for job") {
289+
return
290+
}
286291
d.noticeBuffer = append(d.noticeBuffer, notice.Severity+": "+notice.Message)
287292
if notice.Detail != "" {
288293
d.noticeBuffer = append(d.noticeBuffer, "DETAIL: "+notice.Detail)

pkg/cli/clisqlexec/format_table_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ thenshort`,
5858
// not much" int, "very very long
5959
// thenshort" int, "κόσμε" int, "a|b" int, ܈85 int)
6060
// CREATE DATABASE
61-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
6261
// CREATE TABLE
6362
// sql -e insert into t.u values (0, 0, 0, 0, 0, 0, 0, 0)
6463
// INSERT 0 1
@@ -213,11 +212,8 @@ func Example_sql_empty_table() {
213212
// Output:
214213
// sql -e create database t;create table t.norows(x int);create table t.nocolsnorows();create table t.nocols(); insert into t.nocols(rowid) values (1),(2),(3);
215214
// CREATE DATABASE
216-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
217215
// CREATE TABLE
218-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
219216
// CREATE TABLE
220-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
221217
// CREATE TABLE
222218
// INSERT 0 3
223219
// sql --format=tsv -e select * from t.norows
@@ -549,7 +545,6 @@ func Example_sql_table() {
549545
// Output:
550546
// sql -e create database t; create table t.t (s string, d string);
551547
// CREATE DATABASE
552-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
553548
// CREATE TABLE
554549
// sql -e insert into t.t values (e'foo', 'printable ASCII')
555550
// INSERT 0 1

pkg/cli/clisqlexec/format_value_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func Example_sql_format() {
4646
// Output:
4747
// sql -e create database t; create table t.times (bare timestamp, withtz timestamptz)
4848
// CREATE DATABASE
49-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
5049
// CREATE TABLE
5150
// sql -e insert into t.times values ('2016-01-25 10:10:10', '2016-01-25 10:10:10-05:00')
5251
// INSERT 0 1

pkg/cli/clisqlshell/sql_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ func Example_sql() {
6767
// $ cockroach sql
6868
// sql -e create database t; create table t.f (x int, y int); insert into t.f values (42, 69)
6969
// CREATE DATABASE
70-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
7170
// CREATE TABLE
7271
// INSERT 0 1
7372
// sql -e select 3 as "3" -e select * from t.f
@@ -103,13 +102,11 @@ func Example_sql() {
103102
// sql -e create table t.g1 (x int)
104103
// CREATE TABLE
105104
// sql -e create table t.g2 as select * from generate_series(1,10)
106-
// NOTICE: CREATE TABLE ... AS does not copy over indexes, default expressions, or constraints; the new table has a hidden rowid primary key column
107105
// CREATE TABLE AS
108106
// sql -d nonexistent -e select count(*) from "".information_schema.tables limit 0
109107
// count
110108
// sql -d nonexistent -e create database nonexistent; create table foo(x int); select * from foo
111109
// CREATE DATABASE
112-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
113110
// CREATE TABLE
114111
// x
115112
// sql -e copy t.f from stdin
@@ -211,7 +208,6 @@ func Example_misc_table() {
211208
// Output:
212209
// sql -e create database t; create table t.t (s string, d string);
213210
// CREATE DATABASE
214-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
215211
// CREATE TABLE
216212
// sql --format=table -e select ' hai' as x
217213
// x
@@ -250,7 +246,6 @@ func Example_in_memory() {
250246
// Output:
251247
// sql -e create database t; create table t.f (x int, y int); insert into t.f values (42, 69)
252248
// CREATE DATABASE
253-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
254249
// CREATE TABLE
255250
// INSERT 0 1
256251
// node ls
@@ -274,7 +269,6 @@ func Example_pretty_print_numerical_strings() {
274269
// Output:
275270
// sql -e create database t; create table t.t (s string, d string);
276271
// CREATE DATABASE
277-
// NOTICE: auto-committing transaction before processing DDL due to autocommit_before_ddl setting
278272
// CREATE TABLE
279273
// sql -e insert into t.t values (e'0', 'positive numerical string')
280274
// INSERT 0 1

pkg/cli/testutils.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/cockroachdb/cockroach/pkg/security/certnames"
3030
"github.com/cockroachdb/cockroach/pkg/security/securitytest"
3131
"github.com/cockroachdb/cockroach/pkg/security/username"
32+
"github.com/cockroachdb/cockroach/pkg/server/pgurl"
3233
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
3334
"github.com/cockroachdb/cockroach/pkg/sql/stats"
3435
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
@@ -413,16 +414,31 @@ func (c TestCLI) RunWithArgs(origArgs []string) {
413414
args := append([]string(nil), origArgs[:1]...)
414415
if c.Server != nil {
415416
addr := c.getRPCAddr()
416-
if isSQL, err := isSQLCommand(origArgs); err != nil {
417+
isSQL, err := isSQLCommand(origArgs)
418+
if err != nil {
417419
return err
418-
} else if isSQL {
420+
}
421+
if isSQL {
419422
addr = c.getSQLAddr()
420423
}
424+
421425
h, p, err := net.SplitHostPort(addr)
422426
if err != nil {
423427
return err
424428
}
425-
args = append(args, fmt.Sprintf("--host=%s", net.JoinHostPort(h, p)))
429+
430+
if isSQL {
431+
// Create a connection string URL with client_min_messages = 'warning'.
432+
// This avoids showing SQL notices, which can be non-deterministic.
433+
u := pgurl.New().WithNet(pgurl.NetTCP(h, p))
434+
if err := u.SetOption("client_min_messages", "warning"); err != nil {
435+
return err
436+
}
437+
args = append(args, fmt.Sprintf("--url=%s", u.String()))
438+
} else {
439+
args = append(args, fmt.Sprintf("--host=%s", net.JoinHostPort(h, p)))
440+
}
441+
426442
if c.Insecure {
427443
args = append(args, "--insecure=true")
428444
} else {

pkg/sql/conn_executor.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4237,6 +4237,20 @@ func (ex *connExecutor) waitForTxnJobs() error {
42374237
}
42384238
}
42394239
if !queryTimedout.Load() && len(ex.extraTxnState.jobs.created) > 0 {
4240+
jobIDs := strings.Builder{}
4241+
for i, jobID := range ex.extraTxnState.jobs.created {
4242+
if i > 0 {
4243+
jobIDs.WriteString(", ")
4244+
}
4245+
jobIDs.WriteString(jobID.String())
4246+
}
4247+
if err := ex.planner.SendClientNotice(ex.Ctx(),
4248+
pgnotice.Newf("waiting for job(s) to complete: %s\nIf the statement is canceled, jobs will continue in the background.", redact.SafeString(jobIDs.String())),
4249+
true, /* immediateFlush */
4250+
); err != nil {
4251+
return err
4252+
}
4253+
42404254
if err := ex.server.cfg.JobRegistry.WaitForJobs(jobWaitCtx,
42414255
ex.extraTxnState.jobs.created); err != nil {
42424256
if errors.Is(err, context.Canceled) && queryTimedout.Load() {

pkg/sql/logictest/logic.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,11 @@ func (t *logicTest) openDB(pgURL url.URL) *gosql.DB {
13031303
}
13041304

13051305
connector := pq.ConnectorWithNoticeHandler(base, func(notice *pq.Error) {
1306+
// Skip all "waiting for job(s) to complete" notices, since they include
1307+
// non-deterministic jobIDs.
1308+
if strings.HasPrefix(notice.Message, "waiting for job") {
1309+
return
1310+
}
13061311
t.noticeBuffer = append(t.noticeBuffer, notice.Severity+": "+notice.Message)
13071312
if notice.Detail != "" {
13081313
t.noticeBuffer = append(t.noticeBuffer, "DETAIL: "+notice.Detail)

pkg/sql/run_control_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,10 +1153,11 @@ func TestStatementTimeoutForSchemaChangeCommit(t *testing.T) {
11531153
if implicitTxn {
11541154
_, err := conn.DB.ExecContext(ctx, "ALTER TABLE t1 ADD COLUMN j INT DEFAULT 32")
11551155
require.ErrorContains(t, err, sqlerrors.QueryTimeoutError.Error())
1156-
require.Equal(t, 1, len(actualNotices))
1156+
require.Equal(t, 2, len(actualNotices))
1157+
require.Regexp(t, "waiting for job\\(s\\) to complete: \\d+", actualNotices[0])
11571158
require.Regexp(t,
11581159
"The statement has timed out, but the following background jobs have been created and will continue running: \\d+",
1159-
actualNotices[0])
1160+
actualNotices[1])
11601161
} else {
11611162
txn := conn.Begin(t)
11621163
_, err := txn.Exec("SET LOCAL autocommit_before_ddl=off")

pkg/testutils/pgtest/pgtest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ func (p *PGTest) Until(
239239
}
240240
msg := x.Interface().(pgproto3.BackendMessage)
241241
if notice, ok := msg.(*pgproto3.NoticeResponse); ok {
242+
// Skip all "waiting for job(s) to complete" notices, since they include
243+
// non-deterministic jobIDs.
244+
if strings.HasPrefix(notice.Message, "waiting for job") {
245+
continue
246+
}
242247
// The line number can change frequently, so to reduce churn, we always
243248
// ignore it.
244249
notice.Line = 0

0 commit comments

Comments
 (0)