Skip to content

Commit db05204

Browse files
committed
sql: support making sequences read only
Previously, the only physical table types that were read only were materialized viewed. As a part of the PCR work to support reading from a tenant we added support for replicating catalogs and inserting them w ith external data rows. We apply the same idea to sequences, however we are lacking runtime support. To address this, this patch makes sequence opertions read only if external data rows exists. Release note: None
1 parent f228c7c commit db05204

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

pkg/sql/catalog/descpb/structured.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ func (desc *TableDescriptor) MaterializedView() bool {
230230
return desc.IsMaterializedView
231231
}
232232

233+
// IsReadOnly implements the TableDescriptor interface.
234+
func (desc *TableDescriptor) IsReadOnly() bool {
235+
return desc.IsMaterializedView || desc.GetExternal() != nil
236+
}
237+
233238
// IsPhysicalTable implements the TableDescriptor interface.
234239
func (desc *TableDescriptor) IsPhysicalTable() bool {
235240
return desc.IsSequence() || (desc.IsTable() && !desc.IsVirtualTable()) || desc.MaterializedView()

pkg/sql/catalog/descriptor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ type TableDescriptor interface {
337337
IsPhysicalTable() bool
338338
// MaterializedView returns whether this TableDescriptor is a MaterializedView.
339339
MaterializedView() bool
340+
// IsReadOnly returns if this table descriptor has external data, and cannot
341+
// be written to.
342+
IsReadOnly() bool
340343
// IsAs returns true if the TableDescriptor describes a Table that was created
341344
// with a CREATE TABLE AS command.
342345
IsAs() bool

pkg/sql/catalog/replication/reader_catalog_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ func TestReaderCatalog(t *testing.T) {
9292
compareConn("SELECT * FROM system.role_options")
9393
compareConn("SELECT * FROM system.database_role_settings")
9494

95+
// Validate that sequences can be selected.
96+
compareConn("SELECT * FROM sq1")
97+
98+
// Validate that sequence operations are blocked.
99+
destRunner.ExpectErr(t, "cannot execute nextval\\(\\) in a read-only transaction", "SELECT nextval('sq1')")
100+
destRunner.ExpectErr(t, "cannot execute setval\\(\\) in a read-only transaction", "SELECT setval('sq1', 32)")
95101
// Manipulate the schema first.
96102
ddlToExec = []string{
97103
"ALTER TABLE t1 ADD COLUMN j int default 32",

pkg/sql/sequence.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ func incrementSequenceHelper(
112112
string(descriptor.DescriptorType()), descriptor.GetName())
113113
}
114114

115+
// If the descriptor is read only block any write operations.
116+
if descriptor.IsReadOnly() {
117+
return 0, readOnlyError("nextval()")
118+
}
119+
115120
var err error
116121
seqOpts := descriptor.GetSequenceOpts()
117122

@@ -294,6 +299,10 @@ func (p *planner) SetSequenceValueByID(
294299
if err != nil {
295300
return err
296301
}
302+
// If the descriptor is read only block any write operations.
303+
if descriptor.IsReadOnly() {
304+
return readOnlyError("setval()")
305+
}
297306
seqName, err := p.getQualifiedTableName(ctx, descriptor)
298307
if err != nil {
299308
return err
@@ -410,7 +419,14 @@ func (p *planner) GetSequenceValue(
410419
func getSequenceValueFromDesc(
411420
ctx context.Context, txn *kv.Txn, codec keys.SQLCodec, desc catalog.TableDescriptor,
412421
) (int64, error) {
413-
keyValue, err := txn.Get(ctx, codec.SequenceKey(uint32(desc.GetID())))
422+
targetID := desc.GetID()
423+
// For external row data, adjust the key that we will
424+
// scan.
425+
if ext := desc.ExternalRowData(); ext != nil {
426+
codec = keys.MakeSQLCodec(ext.TenantID)
427+
targetID = desc.ExternalRowData().TableID
428+
}
429+
keyValue, err := txn.Get(ctx, codec.SequenceKey(uint32(targetID)))
414430
if err != nil {
415431
return 0, err
416432
}

0 commit comments

Comments
 (0)