Skip to content

Commit ef6e2e3

Browse files
craig[bot]rafiss
andcommitted
Merge #145374
145374: ttljob: avoid using different versions of enum; use a desc.Collection tied to the correct txn r=rafiss a=rafiss ### ttl: use a desc.Collection tied to the txn for TTL job metadata Previously, we would pass in a desc.Collection from the flowCtx while gathering metadata for the TTL job, such as column names and types. This was incorrect, since it meant that the lease on any descriptors fetched in those transactions would never be released until the job completed. ### ttljob: make an error message slightly more helpful The new message assures the user that the job will run again. ### ttljob: avoid using different versions of enum This patch changes the SELECT/DELETE queries so the use the logical string representation of enums, rather than the encoded datum. This avoids an issue that was caused by the enum descriptor's version being passed in as part of the query placeholder argument. fixes #145908 Informs: https://github.com/cockroachlabs/support/issues/3246 Release note (bug fix): Fixed a bug that could cause the row level TTL job to fail with a "comparison of two different versions of enum" error if an ENUM type referenced by the table experienced a schema change. Co-authored-by: Rafi Shamim <[email protected]>
2 parents 4f7fed9 + 4ff7e87 commit ef6e2e3

16 files changed

+147
-91
lines changed

pkg/sql/schema_changer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6911,7 +6911,7 @@ CREATE TABLE t.test (id TEXT PRIMARY KEY, expire_at TIMESTAMPTZ) WITH (ttl_expir
69116911
desc: "error during multiple ALTER TABLE x SET ttl_expire_after x during modify row-level-ttl mutation",
69126912
setup: createNonTTLTable,
69136913
schemaChange: `
6914-
ALTER TABLE t.test SET (ttl_cron = '@daily');
6914+
ALTER TABLE t.test SET (ttl_job_cron = '@daily');
69156915
ALTER TABLE t.test SET (ttl_expire_after = '10 hours');
69166916
`,
69176917
runBeforeModifyRowLevelTTL: createFailOnceFunc(),

pkg/sql/sem/tree/datum.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5460,7 +5460,7 @@ func (d *DEnum) ResolvedType() *types.T {
54605460
return d.EnumTyp
54615461
}
54625462

5463-
// PlanCistFromCtx returns the plan gist if it is stored in the context. It is
5463+
// PlanGistFromCtx returns the plan gist if it is stored in the context. It is
54645464
// injected from the sql package to avoid import cycle.
54655465
var PlanGistFromCtx func(context.Context) string
54665466

@@ -5487,11 +5487,11 @@ func (d *DEnum) Compare(ctx context.Context, cmpCtx CompareContext, other Datum)
54875487
// safe string.
54885488
gist = redact.SafeString(PlanGistFromCtx(ctx))
54895489
}
5490-
panic(errors.AssertionFailedf(
5490+
return 0, errors.AssertionFailedf(
54915491
"comparison of two different versions of enum %s oid %d: versions %d and %d, gist %q",
54925492
d.EnumTyp.SQLStringForError(), errors.Safe(d.EnumTyp.Oid()), d.EnumTyp.TypeMeta.Version,
54935493
v.EnumTyp.TypeMeta.Version, gist,
5494-
))
5494+
)
54955495
}
54965496

54975497
res := bytes.Compare(d.PhysicalRep, v.PhysicalRep)

pkg/sql/ttl/ttlbase/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ go_library(
1313
"//pkg/sql/catalog/catenumpb",
1414
"//pkg/sql/catalog/catpb",
1515
"//pkg/sql/catalog/tabledesc",
16+
"//pkg/sql/types",
1617
"@com_github_cockroachdb_errors//:errors",
1718
],
1819
)
@@ -23,6 +24,7 @@ go_test(
2324
embed = [":ttlbase"],
2425
deps = [
2526
"//pkg/sql/catalog/catenumpb",
27+
"//pkg/sql/types",
2628
"//pkg/util/leaktest",
2729
"//pkg/util/log",
2830
"@com_github_stretchr_testify//require",

pkg/sql/ttl/ttlbase/ttl_helpers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catenumpb"
1717
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
1818
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
19+
"github.com/cockroachdb/cockroach/pkg/sql/types"
1920
"github.com/cockroachdb/errors"
2021
)
2122

@@ -169,6 +170,7 @@ func BuildSelectQuery(
169170
relationName string,
170171
pkColNames []string,
171172
pkColDirs []catenumpb.IndexColumn_Direction,
173+
pkColTypes []*types.T,
172174
aostDuration time.Duration,
173175
ttlExpr catpb.Expression,
174176
numStartQueryBounds, numEndQueryBounds int,
@@ -217,6 +219,8 @@ func BuildSelectQuery(
217219
buf.WriteString(pkColNames[j])
218220
buf.WriteString(" = $")
219221
buf.WriteString(strconv.Itoa(j + placeholderOffset))
222+
buf.WriteString("::")
223+
buf.WriteString(pkColTypes[j].SQLStringFullyQualified())
220224
buf.WriteString(" AND ")
221225
}
222226
buf.WriteString(pkColNames[i])
@@ -227,6 +231,8 @@ func BuildSelectQuery(
227231
}
228232
buf.WriteString(" $")
229233
buf.WriteString(strconv.Itoa(i + placeholderOffset))
234+
buf.WriteString("::")
235+
buf.WriteString(pkColTypes[i].SQLStringFullyQualified())
230236
buf.WriteString(")")
231237
if !isLast {
232238
buf.WriteString(" OR")

pkg/sql/ttl/ttlbase/ttl_helpers_test.go

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catenumpb"
12+
"github.com/cockroachdb/cockroach/pkg/sql/types"
1213
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
1314
"github.com/cockroachdb/cockroach/pkg/util/log"
1415
"github.com/stretchr/testify/require"
@@ -43,10 +44,10 @@ FROM relation_name
4344
AS OF SYSTEM TIME INTERVAL '-30 seconds'
4445
WHERE ((expire_at) <= $1)
4546
AND (
46-
(col0 > $3)
47+
(col0 > $3::INT8)
4748
)
4849
AND (
49-
(col0 <= $2)
50+
(col0 <= $2::INT8)
5051
)
5152
ORDER BY col0 ASC
5253
LIMIT 2`,
@@ -63,10 +64,10 @@ FROM relation_name
6364
AS OF SYSTEM TIME INTERVAL '-30 seconds'
6465
WHERE ((expire_at) <= $1)
6566
AND (
66-
(col0 < $3)
67+
(col0 < $3::INT8)
6768
)
6869
AND (
69-
(col0 >= $2)
70+
(col0 >= $2::INT8)
7071
)
7172
ORDER BY col0 DESC
7273
LIMIT 2`,
@@ -113,10 +114,10 @@ FROM relation_name
113114
AS OF SYSTEM TIME INTERVAL '-30 seconds'
114115
WHERE ((expire_at) <= $1)
115116
AND (
116-
(col0 >= $3)
117+
(col0 >= $3::INT8)
117118
)
118119
AND (
119-
(col0 <= $2)
120+
(col0 <= $2::INT8)
120121
)
121122
ORDER BY col0 ASC
122123
LIMIT 2`,
@@ -134,10 +135,10 @@ FROM relation_name
134135
AS OF SYSTEM TIME INTERVAL '-30 seconds'
135136
WHERE ((expire_at) <= $1)
136137
AND (
137-
(col0 <= $3)
138+
(col0 <= $3::INT8)
138139
)
139140
AND (
140-
(col0 >= $2)
141+
(col0 >= $2::INT8)
141142
)
142143
ORDER BY col0 DESC
143144
LIMIT 2`,
@@ -155,12 +156,12 @@ FROM relation_name
155156
AS OF SYSTEM TIME INTERVAL '-30 seconds'
156157
WHERE ((expire_at) <= $1)
157158
AND (
158-
(col0 > $4) OR
159-
(col0 = $4 AND col1 > $5)
159+
(col0 > $4::INT8) OR
160+
(col0 = $4::INT8 AND col1 > $5::INT8)
160161
)
161162
AND (
162-
(col0 < $2) OR
163-
(col0 = $2 AND col1 <= $3)
163+
(col0 < $2::INT8) OR
164+
(col0 = $2::INT8 AND col1 <= $3::INT8)
164165
)
165166
ORDER BY col0 ASC, col1 ASC
166167
LIMIT 2`,
@@ -178,11 +179,11 @@ FROM relation_name
178179
AS OF SYSTEM TIME INTERVAL '-30 seconds'
179180
WHERE ((expire_at) <= $1)
180181
AND (
181-
(col0 > $4)
182+
(col0 > $4::INT8)
182183
)
183184
AND (
184-
(col0 < $2) OR
185-
(col0 = $2 AND col1 <= $3)
185+
(col0 < $2::INT8) OR
186+
(col0 = $2::INT8 AND col1 <= $3::INT8)
186187
)
187188
ORDER BY col0 ASC, col1 ASC
188189
LIMIT 2`,
@@ -200,11 +201,11 @@ FROM relation_name
200201
AS OF SYSTEM TIME INTERVAL '-30 seconds'
201202
WHERE ((expire_at) <= $1)
202203
AND (
203-
(col0 > $3) OR
204-
(col0 = $3 AND col1 > $4)
204+
(col0 > $3::INT8) OR
205+
(col0 = $3::INT8 AND col1 > $4::INT8)
205206
)
206207
AND (
207-
(col0 <= $2)
208+
(col0 <= $2::INT8)
208209
)
209210
ORDER BY col0 ASC, col1 ASC
210211
LIMIT 2`,
@@ -222,12 +223,12 @@ FROM relation_name
222223
AS OF SYSTEM TIME INTERVAL '-30 seconds'
223224
WHERE ((expire_at) <= $1)
224225
AND (
225-
(col0 < $4) OR
226-
(col0 = $4 AND col1 < $5)
226+
(col0 < $4::INT8) OR
227+
(col0 = $4::INT8 AND col1 < $5::INT8)
227228
)
228229
AND (
229-
(col0 > $2) OR
230-
(col0 = $2 AND col1 >= $3)
230+
(col0 > $2::INT8) OR
231+
(col0 = $2::INT8 AND col1 >= $3::INT8)
231232
)
232233
ORDER BY col0 DESC, col1 DESC
233234
LIMIT 2`,
@@ -245,11 +246,11 @@ FROM relation_name
245246
AS OF SYSTEM TIME INTERVAL '-30 seconds'
246247
WHERE ((expire_at) <= $1)
247248
AND (
248-
(col0 < $4)
249+
(col0 < $4::INT8)
249250
)
250251
AND (
251-
(col0 > $2) OR
252-
(col0 = $2 AND col1 >= $3)
252+
(col0 > $2::INT8) OR
253+
(col0 = $2::INT8 AND col1 >= $3::INT8)
253254
)
254255
ORDER BY col0 DESC, col1 DESC
255256
LIMIT 2`,
@@ -267,11 +268,11 @@ FROM relation_name
267268
AS OF SYSTEM TIME INTERVAL '-30 seconds'
268269
WHERE ((expire_at) <= $1)
269270
AND (
270-
(col0 < $3) OR
271-
(col0 = $3 AND col1 < $4)
271+
(col0 < $3::INT8) OR
272+
(col0 = $3::INT8 AND col1 < $4::INT8)
272273
)
273274
AND (
274-
(col0 >= $2)
275+
(col0 >= $2::INT8)
275276
)
276277
ORDER BY col0 DESC, col1 DESC
277278
LIMIT 2`,
@@ -290,14 +291,14 @@ FROM relation_name
290291
AS OF SYSTEM TIME INTERVAL '-30 seconds'
291292
WHERE ((expire_at) <= $1)
292293
AND (
293-
(col0 > $5) OR
294-
(col0 = $5 AND col1 < $6) OR
295-
(col0 = $5 AND col1 = $6 AND col2 > $7)
294+
(col0 > $5::INT8) OR
295+
(col0 = $5::INT8 AND col1 < $6::INT8) OR
296+
(col0 = $5::INT8 AND col1 = $6::INT8 AND col2 > $7::INT8)
296297
)
297298
AND (
298-
(col0 < $2) OR
299-
(col0 = $2 AND col1 > $3) OR
300-
(col0 = $2 AND col1 = $3 AND col2 <= $4)
299+
(col0 < $2::INT8) OR
300+
(col0 = $2::INT8 AND col1 > $3::INT8) OR
301+
(col0 = $2::INT8 AND col1 = $3::INT8 AND col2 <= $4::INT8)
301302
)
302303
ORDER BY col0 ASC, col1 DESC, col2 ASC
303304
LIMIT 2`,
@@ -316,14 +317,14 @@ FROM relation_name
316317
AS OF SYSTEM TIME INTERVAL '-30 seconds'
317318
WHERE ((expire_at) <= $1)
318319
AND (
319-
(col0 < $5) OR
320-
(col0 = $5 AND col1 > $6) OR
321-
(col0 = $5 AND col1 = $6 AND col2 < $7)
320+
(col0 < $5::INT8) OR
321+
(col0 = $5::INT8 AND col1 > $6::INT8) OR
322+
(col0 = $5::INT8 AND col1 = $6::INT8 AND col2 < $7::INT8)
322323
)
323324
AND (
324-
(col0 > $2) OR
325-
(col0 = $2 AND col1 < $3) OR
326-
(col0 = $2 AND col1 = $3 AND col2 >= $4)
325+
(col0 > $2::INT8) OR
326+
(col0 = $2::INT8 AND col1 < $3::INT8) OR
327+
(col0 = $2::INT8 AND col1 = $3::INT8 AND col2 >= $4::INT8)
327328
)
328329
ORDER BY col0 DESC, col1 ASC, col2 DESC
329330
LIMIT 2`,
@@ -334,10 +335,15 @@ LIMIT 2`,
334335
t.Run(tc.desc, func(t *testing.T) {
335336
pkColDirs := tc.pkColDirs
336337
pkColNames := GenPKColNames(len(pkColDirs))
338+
pkColTypes := make([]*types.T, len(pkColDirs))
339+
for i := range pkColDirs {
340+
pkColTypes[i] = types.Int
341+
}
337342
actualQuery := BuildSelectQuery(
338343
relationName,
339344
pkColNames,
340345
pkColDirs,
346+
pkColTypes,
341347
DefaultAOSTDuration,
342348
ttlExpr,
343349
tc.numStartQueryBounds,

pkg/sql/ttl/ttljob/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ go_test(
9191
"//pkg/sql/catalog",
9292
"//pkg/sql/catalog/catenumpb",
9393
"//pkg/sql/catalog/catpb",
94-
"//pkg/sql/catalog/catsessiondata",
9594
"//pkg/sql/catalog/colinfo",
9695
"//pkg/sql/catalog/descpb",
9796
"//pkg/sql/catalog/descs",
@@ -105,7 +104,6 @@ go_test(
105104
"//pkg/sql/rowenc",
106105
"//pkg/sql/sem/eval",
107106
"//pkg/sql/sem/tree",
108-
"//pkg/sql/sessiondata",
109107
"//pkg/sql/ttl/ttlbase",
110108
"//pkg/sql/types",
111109
"//pkg/testutils",

pkg/sql/ttl/ttljob/testdata/ttljob_plans

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ FROM defaultdb.public.p@"p_pkey"
1818
AS OF SYSTEM TIME INTERVAL ''0 seconds''
1919
WHERE ((crdb_internal_expiration) <= ''2024-01-01 00:00:00'')
2020
AND (
21-
(id >= 0)
21+
(id >= 0::INT8)
2222
)
2323
AND (
24-
(id <= 100)
24+
(id <= 100::INT8)
2525
)
2626
ORDER BY id ASC
2727
LIMIT 500

pkg/sql/ttl/ttljob/ttljob.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/cockroachdb/cockroach/pkg/jobs"
1414
"github.com/cockroachdb/cockroach/pkg/jobs/joberror"
1515
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
16-
"github.com/cockroachdb/cockroach/pkg/kv"
1716
"github.com/cockroachdb/cockroach/pkg/roachpb"
1817
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
1918
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
@@ -59,8 +58,7 @@ func (t rowLevelTTLResumer) Resume(ctx context.Context, execCtx interface{}) (re
5958

6059
jobExecCtx := execCtx.(sql.JobExecContext)
6160
execCfg := jobExecCtx.ExecCfg()
62-
db := execCfg.DB
63-
descsCol := jobExecCtx.ExtendedEvalContext().Descs
61+
db := execCfg.InternalDB
6462

6563
settingsValues := execCfg.SV()
6664
if err := ttlbase.CheckJobEnabled(settingsValues); err != nil {
@@ -84,8 +82,8 @@ func (t rowLevelTTLResumer) Resume(ctx context.Context, execCtx interface{}) (re
8482
var rowLevelTTL *catpb.RowLevelTTL
8583
var relationName string
8684
var entirePKSpan roachpb.Span
87-
if err := db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
88-
desc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, details.TableID)
85+
if err := db.DescsTxn(ctx, func(ctx context.Context, txn descs.Txn) error {
86+
desc, err := txn.Descriptors().ByIDWithLeased(txn.KV()).WithoutNonPublic().Get().Table(ctx, details.TableID)
8987
if err != nil {
9088
return err
9189
}
@@ -96,7 +94,7 @@ func (t rowLevelTTLResumer) Resume(ctx context.Context, execCtx interface{}) (re
9694
if modificationTime.After(aost) {
9795
return pgerror.Newf(
9896
pgcode.ObjectNotInPrerequisiteState,
99-
"found a recent schema change on the table at %s, aborting",
97+
"found a recent schema change on the table at %s, job will run at the next scheduled time",
10098
modificationTime.Format(time.RFC3339),
10199
)
102100
}
@@ -111,7 +109,7 @@ func (t rowLevelTTLResumer) Resume(ctx context.Context, execCtx interface{}) (re
111109
return pgerror.Newf(pgcode.OperatorIntervention, "ttl jobs on table %s are currently paused", tree.Name(desc.GetName()))
112110
}
113111

114-
tn, err := descs.GetObjectName(ctx, txn, descsCol, desc)
112+
tn, err := descs.GetObjectName(ctx, txn.KV(), txn.Descriptors(), desc)
115113
if err != nil {
116114
return errors.Wrapf(err, "error fetching table relation name for TTL")
117115
}

0 commit comments

Comments
 (0)