Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: support sequence for postgres query span (#12274)

boojack01c856ef8e4e
diff --git a/backend/plugin/parser/base/span.go b/backend/plugin/parser/base/span.go
index 87166588f1..513f30385d 100644
--- a/backend/plugin/parser/base/span.go
+++ b/backend/plugin/parser/base/span.go
@@ -57,6 +57,27 @@ type ColumnResource struct {
Column string
}
+// String returns the string format of the column resource.
+func (c ColumnResource) String() string {
+ var list []string
+ if c.Server != "" {
+ list = append(list, c.Server)
+ }
+ if c.Database != "" {
+ list = append(list, c.Database)
+ }
+ if c.Schema != "" {
+ list = append(list, c.Schema)
+ }
+ if c.Table != "" {
+ list = append(list, c.Table)
+ }
+ if c.Column != "" {
+ list = append(list, c.Column)
+ }
+ return strings.Join(list, ".")
+}
+
type TableSource interface {
// Interface guard to forbid other types outside this package to implement this interface.
isTableSource()
@@ -203,25 +224,55 @@ func (p *PhysicalTable) GetQuerySpanResult() []QuerySpanResult {
return result
}
-// String returns the string format of the column resource.
-func (c ColumnResource) String() string {
- var list []string
- if c.Server != "" {
- list = append(list, c.Server)
- }
- if c.Database != "" {
- list = append(list, c.Database)
- }
- if c.Schema != "" {
- list = append(list, c.Schema)
- }
- if c.Table != "" {
- list = append(list, c.Table)
- }
- if c.Column != "" {
- list = append(list, c.Column)
+// Sequence is the resource of a sequence.
+type Sequence struct {
+ baseTableSource
+
+ // Server is the normalized server name, it's empty if the column comes from the connected server.
+ Server string
+ // Database is the normalized database name, it should not be empty.
+ Database string
+ // Schema is the normalized schema name, it should not be empty for the engines that support schema, and should be empty for the engines that don't support schema.
+ Schema string
+ // Name is the normalized sequence name, it should not be empty.
+ Name string
+ // Columns are the columns of the sequence.
+ Columns []string
+}
+
+func (p *Sequence) GetTableName() string {
+ return p.Name
+}
+
+func (p *Sequence) GetSchemaName() string {
+ return p.Schema
+}
+
+func (p *Sequence) GetDatabaseName() string {
+ return p.Database
+}
+
+func (p *Sequence) GetServerName() string {
+ return p.Server
+}
+
+func (p *Sequence) GetQuerySpanResult() []QuerySpanResult {
+ result := make([]QuerySpanResult, 0, len(p.Columns))
+ for _, column := range p.Columns {
+ sourceColumnSet := make(SourceColumnSet, 1)
+ sourceColumnSet[ColumnResource{
+ Server: p.Server,
+ Database: p.Database,
+ Schema: p.Schema,
+ Table: p.Name,
+ Column: column,
+ }] = true
+ result = append(result, QuerySpanResult{
+ Name: column,
+ SourceColumns: sourceColumnSet,
+ })
}
- return strings.Join(list, ".")
+ return result
}
type GetQuerySpanContext struct {
diff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.go
index 416060063a..bebf8289a5 100644
--- a/backend/plugin/parser/pg/query_span_extractor.go
+++ b/backend/plugin/parser/pg/query_span_extractor.go
@@ -1276,7 +1276,8 @@ func (q *querySpanExtractor) findTableSchema(schemaName string, tableName string
view := schema.GetView(tableName)
materializedView := schema.GetMaterializedView(tableName)
foreignTable := schema.GetExternalTable(tableName)
- if table == nil && view == nil && foreignTable == nil && materializedView == nil {
+ sequence := schema.GetSequence(tableName)
+ if table == nil && view == nil && foreignTable == nil && materializedView == nil && sequence == nil {
return nil, &parsererror.ResourceNotFoundError{
Database: &q.connectedDB,
Schema: &schemaName,
@@ -1339,6 +1340,18 @@ func (q *querySpanExtractor) findTableSchema(schemaName string, tableName string
Columns: columns,
}, nil
}
+
+ if sequence != nil {
+ // The default columns for sequence in PostgreSQL.
+ columns := []string{"last_value", "log_cnt", "is_called"}
+ return &base.Sequence{
+ Server: "",
+ Database: q.connectedDB,
+ Schema: schemaName,
+ Name: tableName,
+ Columns: columns,
+ }, nil
+ }
return nil, nil
}
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index 4ae0a2edf3..d23aa15147 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -320,6 +320,7 @@ func NewDatabaseMetadata(metadata *storepb.DatabaseSchemaMetadata) *DatabaseMeta
internalMaterializedView: make(map[string]*MaterializedViewMetadata),
internalFunctions: make(map[string]*FunctionMetadata),
internalProcedures: make(map[string]*ProcedureMetadata),
+ internalSequences: make(map[string]*SequenceMetadata),
}
for _, table := range schema.Tables {
tables, names := buildTablesMetadata(table)
@@ -360,6 +361,11 @@ func NewDatabaseMetadata(metadata *storepb.DatabaseSchemaMetadata) *DatabaseMeta
proto: procedure,
}
}
+ for _, sequence := range schema.Sequences {
+ schemaMetadata.internalSequences[sequence.Name] = &SequenceMetadata{
+ proto: sequence,
+ }
+ }
databaseMetadata.internal[schema.Name] = schemaMetadata
}
for _, dbLink := range metadata.LinkedDatabases {
@@ -417,6 +423,7 @@ type SchemaMetadata struct {
internalMaterializedView map[string]*MaterializedViewMetadata
internalFunctions map[string]*FunctionMetadata
internalProcedures map[string]*ProcedureMetadata
+ internalSequences map[string]*SequenceMetadata
}
// GetTable gets the schema by name.
@@ -448,6 +455,11 @@ func (s *SchemaMetadata) GetFunction(name string) *FunctionMetadata {
return s.internalFunctions[name]
}
+// GetSequence gets the sequence by name.
+func (s *SchemaMetadata) GetSequence(name string) *SequenceMetadata {
+ return s.internalSequences[name]
+}
+
// ListTableNames lists the table names.
func (s *SchemaMetadata) ListTableNames() []string {
var result []string
@@ -624,3 +636,11 @@ type ProcedureMetadata struct {
func (p *ProcedureMetadata) GetProto() *storepb.ProcedureMetadata {
return p.proto
}
+
+type SequenceMetadata struct {
+ proto *storepb.SequenceMetadata
+}
+
+func (p *SequenceMetadata) GetProto() *storepb.SequenceMetadata {
+ return p.proto
+}