Skip to content

Commit f41dde4

Browse files
committed
sql: default to node-level cache for sequences
The session-level cache was implemented first and shared node-level cache was added in #118546. The better behaving node-level cache is not the default option. Changes the default for `CACHE` option to set node-level cache. A new option to allows the session-level cache to be set. Epic: CRDB-42587 Fixes: #131531 Release note (sql change): Changed the basic sequence cache option to set cache at the node-level. The PER SESSION NODE sequence option (syntax is is [ PER SESSION CACHE # ]) provides the previous behavior to set session-level cache.
1 parent fd18c1b commit f41dde4

File tree

50 files changed

+195
-168
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+195
-168
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
alter_sequence_stmt ::=
22
( 'ALTER' 'SEQUENCE' sequence_name 'RENAME' 'TO' sequence_name | 'ALTER' 'SEQUENCE' 'IF' 'EXISTS' sequence_name 'RENAME' 'TO' sequence_name )
3-
| ( 'ALTER' 'SEQUENCE' sequence_name ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) )* ) | 'ALTER' 'SEQUENCE' 'IF' 'EXISTS' sequence_name ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) )* ) )
3+
| ( 'ALTER' 'SEQUENCE' sequence_name ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'PER' 'SESSION' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'PER' 'SESSION' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) )* ) | 'ALTER' 'SEQUENCE' 'IF' 'EXISTS' sequence_name ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'PER' 'SESSION' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'PER' 'SESSION' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) )* ) )
44
| ( 'ALTER' 'SEQUENCE' sequence_name 'SET' 'SCHEMA' schema_name | 'ALTER' 'SEQUENCE' 'IF' 'EXISTS' sequence_name 'SET' 'SCHEMA' schema_name )
55
| ( 'ALTER' 'SEQUENCE' sequence_name 'OWNER' 'TO' role_spec | 'ALTER' 'SEQUENCE' 'IF' 'EXISTS' sequence_name 'OWNER' 'TO' role_spec )
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
create_sequence_stmt ::=
2-
'CREATE' opt_temp 'SEQUENCE' sequence_name ( ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) )* ) | )
3-
| 'CREATE' opt_temp 'SEQUENCE' 'IF' 'NOT' 'EXISTS' sequence_name ( ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) )* ) | )
2+
'CREATE' opt_temp 'SEQUENCE' sequence_name ( ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'PER' 'SESSION' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'PER' 'SESSION' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) )* ) | )
3+
| 'CREATE' opt_temp 'SEQUENCE' 'IF' 'NOT' 'EXISTS' sequence_name ( ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'PER' 'SESSION' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) ( ( ( 'AS' typename | 'NO' 'CYCLE' | 'OWNED' 'BY' 'NONE' | 'OWNED' 'BY' column_name | 'CACHE' integer | 'PER' 'NODE' 'CACHE' integer | 'PER' 'SESSION' 'CACHE' integer | 'INCREMENT' integer | 'INCREMENT' 'BY' integer | 'MINVALUE' integer | 'NO' 'MINVALUE' | 'MAXVALUE' integer | 'NO' 'MAXVALUE' | 'START' integer | 'START' 'WITH' integer | 'RESTART' | 'RESTART' integer | 'RESTART' 'WITH' integer | 'VIRTUAL' ) ) )* ) | )

docs/generated/sql/bnf/stmt_block.bnf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3743,6 +3743,7 @@ sequence_option_elem ::=
37433743
| 'OWNED' 'BY' column_path
37443744
| 'CACHE' signed_iconst64
37453745
| 'PER' 'NODE' 'CACHE' signed_iconst64
3746+
| 'PER' 'SESSION' 'CACHE' signed_iconst64
37463747
| 'INCREMENT' signed_iconst64
37473748
| 'INCREMENT' 'BY' signed_iconst64
37483749
| 'MINVALUE' signed_iconst64
@@ -4939,6 +4940,8 @@ col_qual_list ::=
49394940
identity_option_elem ::=
49404941
'SET' 'NO' 'CYCLE'
49414942
| 'SET' 'CACHE' signed_iconst64
4943+
| 'SET' 'PER' 'NODE' 'CACHE' signed_iconst64
4944+
| 'SET' 'PER' 'SESSION' 'CACHE' signed_iconst64
49424945
| 'SET' 'INCREMENT' signed_iconst64
49434946
| 'SET' 'INCREMENT' 'BY' signed_iconst64
49444947
| 'SET' 'MINVALUE' signed_iconst64

pkg/sql/alter_table.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,8 +1252,11 @@ func applyColumnMutation(
12521252

12531253
opts := seqDesc.GetSequenceOpts()
12541254
optsNode := tree.SequenceOptions{}
1255-
if opts.CacheSize > 1 {
1256-
optsNode = append(optsNode, tree.SequenceOption{Name: tree.SeqOptCache, IntVal: &opts.CacheSize})
1255+
if opts.SessionCacheSize > 1 {
1256+
optsNode = append(optsNode, tree.SequenceOption{Name: tree.SeqOptCacheSession, IntVal: &opts.SessionCacheSize})
1257+
}
1258+
if opts.NodeCacheSize > 1 {
1259+
optsNode = append(optsNode, tree.SequenceOption{Name: tree.SeqOptCacheNode, IntVal: &opts.NodeCacheSize})
12571260
}
12581261
optsNode = append(optsNode, tree.SequenceOption{Name: tree.SeqOptMinValue, IntVal: &opts.MinValue})
12591262
optsNode = append(optsNode, tree.SequenceOption{Name: tree.SeqOptMaxValue, IntVal: &opts.MaxValue})

pkg/sql/catalog/descpb/structured.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (opts *TableDescriptor_SequenceOpts) HasOwner() bool {
302302
//
303303
// Note: An unset cache size is considered disabled.
304304
func (opts *TableDescriptor_SequenceOpts) EffectiveCacheSize() int64 {
305-
switch sessionCacheSize := opts.CacheSize; sessionCacheSize {
305+
switch sessionCacheSize := opts.SessionCacheSize; sessionCacheSize {
306306
case 0, 1:
307307
default:
308308
return sessionCacheSize

pkg/sql/catalog/descpb/structured.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,8 +1194,8 @@ message TableDescriptor {
11941194
optional SequenceOwner sequence_owner = 6 [(gogoproto.nullable) = false];
11951195

11961196
// The number of values (which have already been created in KV)
1197-
// that a node can cache locally.
1198-
optional int64 cache_size = 7 [(gogoproto.nullable) = false];
1197+
// that a session can cache locally.
1198+
optional int64 session_cache_size = 7 [(gogoproto.nullable) = false];
11991199
// AS option value for CREATE SEQUENCE, which specifies the default
12001200
// min and max values a sequence can take on.
12011201
optional string as_integer_type = 8 [(gogoproto.nullable) = false];

pkg/sql/catalog/descpb/structured_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ func TestEffectiveCacheSize(t *testing.T) {
132132
for _, tt := range testCases {
133133
t.Run(tt.name, func(t *testing.T) {
134134
opts := TableDescriptor_SequenceOpts{
135-
CacheSize: tt.session,
136-
NodeCacheSize: tt.node,
135+
SessionCacheSize: tt.session,
136+
NodeCacheSize: tt.node,
137137
}
138138
result := opts.EffectiveCacheSize()
139139
if result != tt.expectedResult {

pkg/sql/catalog/schemaexpr/sequence_options.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func AssignSequenceOptions(
111111
opts.MaxValue = -1
112112
opts.Start = opts.MaxValue
113113
}
114-
opts.CacheSize = 1
114+
opts.SessionCacheSize = 1
115115
}
116116

117117
// Set Minvalue and Maxvalue to new types bounds if at current bounds.
@@ -148,19 +148,19 @@ func AssignSequenceOptions(
148148
"CYCLE option is not supported")
149149
case tree.SeqOptNoCycle:
150150
// Do nothing; this is the default.
151-
case tree.SeqOptCache:
151+
case tree.SeqOptCacheNode:
152152
if v := *option.IntVal; v >= 1 {
153-
opts.CacheSize = v
153+
opts.NodeCacheSize = v
154154
} else {
155155
return errors.Newf(
156-
"CACHE (%d) must be greater than zero", v)
156+
"PER NODE CACHE (%d) must be greater than zero", v)
157157
}
158-
case tree.SeqOptCacheNode:
158+
case tree.SeqOptCacheSession:
159159
if v := *option.IntVal; v >= 1 {
160-
opts.NodeCacheSize = v
160+
opts.SessionCacheSize = v
161161
} else {
162162
return errors.Newf(
163-
"PER NODE CACHE (%d) must be greater than zero", v)
163+
"PER SESSION CACHE (%d) must be greater than zero", v)
164164
}
165165
case tree.SeqOptIncrement:
166166
// Do nothing; this has already been set.

pkg/sql/catalog/serial_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func SequenceOptionsFromNormalizationMode(
6161
case sessiondatapb.SerialUsesCachedSQLSequences:
6262
cacheValue := sqlclustersettings.CachedSequencesCacheSizeSetting.Get(&st.SV)
6363
seqOpts = append(seqOpts, tree.SequenceOption{
64-
Name: tree.SeqOptCache,
64+
Name: tree.SeqOptCacheSession,
6565
IntVal: &cacheValue,
6666
})
6767
case sessiondatapb.SerialUsesCachedNodeSQLSequences:

pkg/sql/catalog/systemschema/system.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,11 +1737,11 @@ var (
17371737
),
17381738
func(tbl *descpb.TableDescriptor) {
17391739
tbl.SequenceOpts = &descpb.TableDescriptor_SequenceOpts{
1740-
Increment: 1,
1741-
MinValue: 1,
1742-
MaxValue: math.MaxInt64,
1743-
Start: 1,
1744-
CacheSize: 1,
1740+
Increment: 1,
1741+
MinValue: 1,
1742+
MaxValue: math.MaxInt64,
1743+
Start: 1,
1744+
SessionCacheSize: 1,
17451745
}
17461746
tbl.NextColumnID = 0
17471747
tbl.NextFamilyID = 0
@@ -1781,11 +1781,11 @@ var (
17811781
),
17821782
func(tbl *descpb.TableDescriptor) {
17831783
opts := &descpb.TableDescriptor_SequenceOpts{
1784-
Increment: 1,
1785-
MinValue: 100,
1786-
MaxValue: math.MaxInt32,
1787-
Start: 100,
1788-
CacheSize: 1,
1784+
Increment: 1,
1785+
MinValue: 100,
1786+
MaxValue: math.MaxInt32,
1787+
Start: 100,
1788+
SessionCacheSize: 1,
17891789
}
17901790
tbl.SequenceOpts = opts
17911791
tbl.NextColumnID = 0
@@ -1826,11 +1826,11 @@ var (
18261826
),
18271827
func(tbl *descpb.TableDescriptor) {
18281828
tbl.SequenceOpts = &descpb.TableDescriptor_SequenceOpts{
1829-
Increment: 1,
1830-
MinValue: 1,
1831-
MaxValue: math.MaxInt64,
1832-
Start: 1,
1833-
CacheSize: 1,
1829+
Increment: 1,
1830+
MinValue: 1,
1831+
MaxValue: math.MaxInt64,
1832+
Start: 1,
1833+
SessionCacheSize: 1,
18341834
}
18351835
tbl.NextColumnID = 0
18361836
tbl.NextFamilyID = 0

0 commit comments

Comments
 (0)