Skip to content

Commit a4f34e7

Browse files
committed
sql: add internal function to detect keys in system tables
Previously, we had no easy way of knowing if a key belonged to a system table. This was inadequate because we can improve our redacted debug zips by including contention information involving these tables. This patch will add a new builtin crdb_internal.is_system_table_key which can be used for conditional redaction in SQL. Release note: None
1 parent 4c34b48 commit a4f34e7

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

pkg/sql/faketreeeval/evalctx.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,11 @@ func (ep *DummyPrivilegedAccessor) LookupZoneConfigByNamespaceID(
535535
return "", false, errors.WithStack(errEvalPrivileged)
536536
}
537537

538+
// IsSystemTable is part of the tree.PrivilegedAccessor interface.
539+
func (ep *DummyPrivilegedAccessor) IsSystemTable(ctx context.Context, id int64) (bool, error) {
540+
return false, errors.WithStack(errEvalPrivileged)
541+
}
542+
538543
// DummySessionAccessor implements the eval.SessionAccessor interface by returning errors.
539544
type DummySessionAccessor struct{}
540545

pkg/sql/privileged_accessor.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
2020
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
2121
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
22+
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
2223
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
2324
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
2425
"github.com/cockroachdb/errors"
@@ -76,6 +77,15 @@ func (p *planner) LookupZoneConfigByNamespaceID(
7677
return tree.DBytes(zc.GetRawBytesInStorage()), true, nil
7778
}
7879

80+
// IsSystemTable implements tree.PrivilegedAccessor.
81+
func (p *planner) IsSystemTable(ctx context.Context, id int64) (bool, error) {
82+
tbl, err := p.Descriptors().ByID(p.Txn()).Get().Table(ctx, catid.DescID(id))
83+
if err != nil {
84+
return false, err
85+
}
86+
return catalog.IsSystemDescriptor(tbl), nil
87+
}
88+
7989
// checkDescriptorPermissions returns nil if the executing user has permissions
8090
// to check the permissions of a descriptor given its ID, or the id given
8191
// is not a descriptor of a table or database.

pkg/sql/sem/builtins/builtins.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5728,6 +5728,38 @@ SELECT
57285728
Volatility: volatility.Immutable,
57295729
},
57305730
),
5731+
// Return if a key belongs to a system table, which should make it to print
5732+
// within redacted output.
5733+
"crdb_internal.is_system_table_key": makeBuiltin(
5734+
tree.FunctionProperties{
5735+
Category: builtinconstants.CategorySystemInfo,
5736+
Undocumented: true,
5737+
},
5738+
tree.Overload{
5739+
Types: tree.ParamTypes{
5740+
{Name: "raw_key", Typ: types.Bytes},
5741+
},
5742+
ReturnType: tree.FixedReturnType(types.Bool),
5743+
Fn: func(ctx context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
5744+
_, tableID, err := evalCtx.Codec.DecodeTablePrefix(roachpb.Key(tree.MustBeDBytes(args[0])))
5745+
if err != nil {
5746+
// If a key isn't prefixed with a table ID ignore.
5747+
//nolint:returnerrcheck
5748+
return tree.DBoolFalse, nil
5749+
}
5750+
isSystemTable, err := evalCtx.PrivilegedAccessor.IsSystemTable(ctx, int64(tableID))
5751+
if err != nil {
5752+
// If we can't find the descriptor or its not the right type then its
5753+
// not a system table.
5754+
//nolint:returnerrcheck
5755+
return tree.DBoolFalse, nil
5756+
}
5757+
return tree.MakeDBool(tree.DBool(isSystemTable)), nil
5758+
},
5759+
Info: "This function is used only by CockroachDB's developers for testing purposes.",
5760+
Volatility: volatility.Stable,
5761+
},
5762+
),
57315763

57325764
// Return a pretty string for a given span, skipping the specified number of
57335765
// fields.

pkg/sql/sem/builtins/fixed_oids.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,6 +2439,7 @@ var builtinOidsArray = []string{
24392439
2466: `crdb_internal.setup_span_configs_stream(tenant_name: string) -> bytes`,
24402440
2467: `crdb_internal.request_statement_bundle(stmtFingerprint: string, planGist: string, samplingProbability: float, minExecutionLatency: interval, expiresAfter: interval) -> bool`,
24412441
2468: `crdb_internal.request_statement_bundle(stmtFingerprint: string, planGist: string, antiPlanGist: bool, samplingProbability: float, minExecutionLatency: interval, expiresAfter: interval) -> bool`,
2442+
2469: `crdb_internal.is_system_table_key(raw_key: bytes) -> bool`,
24422443
}
24432444

24442445
var builtinOidsBySignature map[string]oid.Oid

pkg/sql/sem/eval/deps.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ type PrivilegedAccessor interface {
534534
// Returns the config byte array, a bool representing whether the namespace exists,
535535
// and an error if there is one.
536536
LookupZoneConfigByNamespaceID(ctx context.Context, id int64) (tree.DBytes, bool, error)
537+
538+
// IsSystemTable returns if a given descriptor ID is a system table.s
539+
IsSystemTable(ctx context.Context, id int64) (bool, error)
537540
}
538541

539542
// RegionOperator gives access to the current region, validation for all

0 commit comments

Comments
 (0)