Skip to content

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

refactor: move pg system object

d-bytebasebd29954c4f51
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.go
index 6154169296..45ce94751b 100644
--- a/backend/api/v1/instance_service.go
+++ b/backend/api/v1/instance_service.go
@@ -20,8 +20,8 @@ import (
enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"
api "github.com/bytebase/bytebase/backend/legacyapi"
metricAPI "github.com/bytebase/bytebase/backend/metric"
- "github.com/bytebase/bytebase/backend/plugin/db/pg"
"github.com/bytebase/bytebase/backend/plugin/metric"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
"github.com/bytebase/bytebase/backend/runner/metricreport"
"github.com/bytebase/bytebase/backend/runner/schemasync"
"github.com/bytebase/bytebase/backend/store"
@@ -330,7 +330,7 @@ func (s *InstanceService) syncSlowQueriesImpl(ctx context.Context, project *stor
if database.SyncState != api.OK {
continue
}
- if pg.IsSystemDatabase(database.DatabaseName) {
+ if pgparser.IsSystemDatabase(database.DatabaseName) {
continue
}
if err := func() error {
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index c58831d6dc..1d52ed1682 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -35,8 +35,8 @@ import (
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/catalog"
"github.com/bytebase/bytebase/backend/plugin/db"
- "github.com/bytebase/bytebase/backend/plugin/db/pg"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/transform"
"github.com/bytebase/bytebase/backend/runner/schemasync"
"github.com/bytebase/bytebase/backend/store"
@@ -1330,12 +1330,12 @@ func allPostgresSystemObjects(statement string) bool {
return false
}
for _, resource := range resources {
- if pg.IsSystemSchema(resource.Schema) {
+ if pgparser.IsSystemSchema(resource.Schema) {
continue
}
// If schema is not specified, user can access the pg_catalog schema if the table is pg_catalog's system table.
// So we need to check this case.
- if resource.Schema == "" && pg.IsSystemTable(resource.Table) {
+ if resource.Schema == "" && pgparser.IsSystemTable(resource.Table) {
continue
}
return false
diff --git a/backend/plugin/db/pg/dump.go b/backend/plugin/db/pg/dump.go
index 8cfcdd207f..58f86d66dc 100644
--- a/backend/plugin/db/pg/dump.go
+++ b/backend/plugin/db/pg/dump.go
@@ -49,7 +49,7 @@ func (driver *Driver) Dump(ctx context.Context, out io.Writer, schemaOnly bool)
dumpableDbNames = []string{driver.databaseName}
} else {
for _, n := range databases {
- if IsSystemDatabase(n.Name) {
+ if pgparser.IsSystemDatabase(n.Name) {
continue
}
dumpableDbNames = append(dumpableDbNames, n.Name)
diff --git a/backend/plugin/db/pg/role.go b/backend/plugin/db/pg/role.go
index d6b09cada4..5e4df79ab4 100644
--- a/backend/plugin/db/pg/role.go
+++ b/backend/plugin/db/pg/role.go
@@ -9,6 +9,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/db"
"github.com/bytebase/bytebase/backend/plugin/db/util"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -362,7 +363,7 @@ func (driver *Driver) getInstanceRoles(ctx context.Context) ([]*storepb.Instance
); err != nil {
return nil, err
}
- if IsSystemUser(role) {
+ if pgparser.IsSystemUser(role) {
continue
}
diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.go
index 77c36272b0..3cbd26e9fa 100644
--- a/backend/plugin/db/pg/sync.go
+++ b/backend/plugin/db/pg/sync.go
@@ -17,6 +17,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/db"
"github.com/bytebase/bytebase/backend/plugin/db/util"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/sql/engine/pg"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -43,7 +44,7 @@ func (driver *Driver) SyncInstance(ctx context.Context) (*db.InstanceMetadata, e
var filteredDatabases []*storepb.DatabaseSchemaMetadata
for _, database := range databases {
// Skip all system databases
- if IsSystemDatabase(database.Name) {
+ if pgparser.IsSystemDatabase(database.Name) {
continue
}
filteredDatabases = append(filteredDatabases, database)
@@ -150,7 +151,7 @@ FROM
WHERE
n.nspname NOT IN(%s)
AND c.contype = 'f'
-ORDER BY fk_schema, fk_table, fk_name;`, systemSchemaWhereClause)
+ORDER BY fk_schema, fk_table, fk_name;`, pgparser.SystemSchemaWhereClause)
func getForeignKeys(txn *sql.Tx) (map[db.TableKey][]*storepb.ForeignKeyMetadata, error) {
foreignKeysMap := make(map[db.TableKey][]*storepb.ForeignKeyMetadata)
@@ -268,7 +269,7 @@ var listSchemaQuery = fmt.Sprintf(`
SELECT nspname
FROM pg_catalog.pg_namespace
WHERE nspname NOT IN (%s);
-`, systemSchemaWhereClause)
+`, pgparser.SystemSchemaWhereClause)
func getSchemas(txn *sql.Tx) ([]string, error) {
rows, err := txn.Query(listSchemaQuery)
@@ -283,7 +284,7 @@ func getSchemas(txn *sql.Tx) ([]string, error) {
if err := rows.Scan(&schemaName); err != nil {
return nil, err
}
- if IsSystemSchema(schemaName) {
+ if pgparser.IsSystemSchema(schemaName) {
continue
}
result = append(result, schemaName)
@@ -304,7 +305,7 @@ SELECT tbl.schemaname, tbl.tablename,
FROM pg_catalog.pg_tables tbl
LEFT JOIN pg_class as pc ON pc.oid = format('%s.%s', quote_ident(tbl.schemaname), quote_ident(tbl.tablename))::regclass` + fmt.Sprintf(`
WHERE tbl.schemaname NOT IN (%s)
-ORDER BY tbl.schemaname, tbl.tablename;`, systemSchemaWhereClause)
+ORDER BY tbl.schemaname, tbl.tablename;`, pgparser.SystemSchemaWhereClause)
// getTables gets all tables of a database.
func getTables(txn *sql.Tx) (map[string][]*storepb.TableMetadata, error) {
@@ -335,7 +336,7 @@ func getTables(txn *sql.Tx) (map[string][]*storepb.TableMetadata, error) {
if err := rows.Scan(&schemaName, &table.Name, &table.DataSize, &table.IndexSize, &table.RowCount, &comment); err != nil {
return nil, err
}
- if IsSystemTable(table.Name) {
+ if pgparser.IsSystemTable(table.Name) {
continue
}
if comment.Valid {
@@ -370,7 +371,7 @@ SELECT
pg_catalog.col_description(format('%s.%s', quote_ident(table_schema), quote_ident(table_name))::regclass, cols.ordinal_position::int) as column_comment
FROM INFORMATION_SCHEMA.COLUMNS AS cols` + fmt.Sprintf(`
WHERE cols.table_schema NOT IN (%s)
-ORDER BY cols.table_schema, cols.table_name, cols.ordinal_position;`, systemSchemaWhereClause)
+ORDER BY cols.table_schema, cols.table_name, cols.ordinal_position;`, pgparser.SystemSchemaWhereClause)
// getTableColumns gets the columns of a table.
func getTableColumns(txn *sql.Tx) (map[db.TableKey][]*storepb.ColumnMetadata, error) {
@@ -416,7 +417,7 @@ func getTableColumns(txn *sql.Tx) (map[db.TableKey][]*storepb.ColumnMetadata, er
var listViewQuery = `
SELECT schemaname, viewname, definition, obj_description(format('%s.%s', quote_ident(schemaname), quote_ident(viewname))::regclass) FROM pg_catalog.pg_views` + fmt.Sprintf(`
-WHERE schemaname NOT IN (%s);`, systemSchemaWhereClause)
+WHERE schemaname NOT IN (%s);`, pgparser.SystemSchemaWhereClause)
// getViews gets all views of a database.
func getViews(txn *sql.Tx) (map[string][]*storepb.ViewMetadata, error) {
@@ -435,7 +436,7 @@ func getViews(txn *sql.Tx) (map[string][]*storepb.ViewMetadata, error) {
return nil, err
}
// Skip system views.
- if IsSystemView(view.Name) {
+ if pgparser.IsSystemView(view.Name) {
continue
}
@@ -553,7 +554,7 @@ SELECT idx.schemaname, idx.tablename, idx.indexname, idx.indexdef, (SELECT 1
AND constraint_type = 'PRIMARY KEY') AS primary,
obj_description(format('%s.%s', quote_ident(idx.schemaname), quote_ident(idx.indexname))::regclass) AS comment` + fmt.Sprintf(`
FROM pg_indexes AS idx WHERE idx.schemaname NOT IN (%s)
-ORDER BY idx.schemaname, idx.tablename, idx.indexname;`, systemSchemaWhereClause)
+ORDER BY idx.schemaname, idx.tablename, idx.indexname;`, pgparser.SystemSchemaWhereClause)
// getIndexes gets all indices of a database.
func getIndexes(txn *sql.Tx) (map[db.TableKey][]*storepb.IndexMetadata, error) {
@@ -625,7 +626,7 @@ left join pg_namespace n on p.pronamespace = n.oid
left join pg_language l on p.prolang = l.oid
left join pg_type t on t.oid = p.prorettype ` + fmt.Sprintf(`
where n.nspname not in (%s)
-order by function_schema, function_name;`, systemSchemaWhereClause)
+order by function_schema, function_name;`, pgparser.SystemSchemaWhereClause)
// getFunctions gets all functions of a database.
func getFunctions(txn *sql.Tx) (map[string][]*storepb.FunctionMetadata, error) {
@@ -643,7 +644,7 @@ func getFunctions(txn *sql.Tx) (map[string][]*storepb.FunctionMetadata, error) {
return nil, err
}
// Skip internal functions.
- if IsSystemFunctions(function.Name, function.Definition) {
+ if pgparser.IsSystemFunctions(function.Name, function.Definition) {
continue
}
diff --git a/backend/plugin/parser/pg/masking.go b/backend/plugin/parser/pg/masking.go
index 88abacb39b..320c9981e9 100644
--- a/backend/plugin/parser/pg/masking.go
+++ b/backend/plugin/parser/pg/masking.go
@@ -70,7 +70,7 @@ func (extractor *fieldExtractor) extractSensitiveFields(statement string) ([]db.
if err != nil {
tableNotFound := regexp.MustCompile("^Table \"(.*)\\.(.*)\" not found$")
content := tableNotFound.FindStringSubmatch(err.Error())
- if len(content) == 3 && isSystemSchema(content[1]) {
+ if len(content) == 3 && IsSystemSchema(content[1]) {
// skip for system schema
return nil, nil
}
@@ -87,14 +87,6 @@ func (extractor *fieldExtractor) extractSensitiveFields(statement string) ([]db.
return result, nil
}
-func isSystemSchema(schema string) bool {
- switch schema {
- case "information_schema", "pg_catalog", "rw_catalog":
- return true
- }
- return false
-}
-
func (extractor *fieldExtractor) pgExtractNode(in *pgquery.Node) ([]base.FieldInfo, error) {
if in == nil {
return nil, nil
diff --git a/backend/plugin/db/pg/system_objects.go b/backend/plugin/parser/pg/system_objects.go
similarity index 98%
rename from backend/plugin/db/pg/system_objects.go
rename to backend/plugin/parser/pg/system_objects.go
index 6f2d62fce2..3fbf87cc89 100644
--- a/backend/plugin/db/pg/system_objects.go
+++ b/backend/plugin/parser/pg/system_objects.go
@@ -28,12 +28,13 @@ var (
"information_schema": true,
"pg_catalog": true,
"pg_toast": true,
+ "rw_catalog": true,
+ "timescaledb_information": true,
+ "timescaledb_experimental": true,
"_timescaledb_cache": true,
"_timescaledb_catalog": true,
"_timescaledb_internal": true,
"_timescaledb_config": true,
- "timescaledb_information": true,
- "timescaledb_experimental": true,
}
// systemTables is the list of system tables that we will exclude from the schema sync.
@@ -125,7 +126,7 @@ var (
"pg_stat_slru": true,
}
- systemSchemaWhereClause = func() string {
+ SystemSchemaWhereClause = func() string {
var schemas []string
for schema := range systemSchemas {
schemas = append(schemas, fmt.Sprintf("'%s'", schema))
@@ -147,6 +148,7 @@ func IsSystemSchema(schema string) bool {
_, ok := systemSchemas[schema]
return ok
}
+
func IsSystemTable(table string) bool {
_, ok := systemTables[table]
return ok
diff --git a/backend/plugin/parser/sql/differ/pg/differ.go b/backend/plugin/parser/sql/differ/pg/differ.go
index 8990a636f4..a885d4b09e 100644
--- a/backend/plugin/parser/sql/differ/pg/differ.go
+++ b/backend/plugin/parser/sql/differ/pg/differ.go
@@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
- "github.com/bytebase/bytebase/backend/plugin/db/pg"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/differ"
pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/sql/engine/pg"
@@ -233,7 +233,7 @@ func newTypeInfo(id int, createType *ast.CreateTypeStmt) *typeInfo {
}
func (m schemaMap) addTable(id int, table *ast.CreateTableStmt) error {
- if pg.IsSystemSchema(table.Name.Schema) {
+ if pgparser.IsSystemSchema(table.Name.Schema) {
return nil
}
schema, exists := m[table.Name.Schema]
@@ -253,7 +253,7 @@ func (m schemaMap) getTable(schemaName string, tableName string) *tableInfo {
}
func (m schemaMap) addConstraint(id int, addConstraint *ast.AddConstraintStmt) error {
- if pg.IsSystemSchema(addConstraint.Table.Schema) {
+ if pgparser.IsSystemSchema(addConstraint.Table.Schema) {
return nil
}
schema, exists := m[addConstraint.Table.Schema]
@@ -285,7 +285,7 @@ func (m schemaMap) getConstraint(schemaName string, tableName string, constraint
}
func (m schemaMap) addExtension(id int, extension *ast.CreateExtensionStmt) error {
- if pg.IsSystemSchema(extension.Schema) {
+ if pgparser.IsSystemSchema(extension.Schema) {
return nil
}
schema, exists := m[extension.Schema]
@@ -305,7 +305,7 @@ func (m schemaMap) getExtension(schemaName string, extensionName string) *extens
}
func (m schemaMap) addFunction(id int, function *ast.CreateFunctionStmt) error {
- if pg.IsSystemSchema(function.Function.Schema) {
+ if pgparser.IsSystemSchema(function.Function.Schema) {
return nil
}
schema, exists := m[function.Function.Schema]
@@ -329,7 +329,7 @@ func (m schemaMap) getFunction(schemaName string, signature string) *functionInf
}
func (m schemaMap) addIndex(id int, index *ast.CreateIndexStmt) error {
- if pg.IsSystemSchema(index.Index.Table.Schema) {
+ if pgparser.IsSystemSchema(index.Index.Table.Schema) {
return nil
}
schema, exists := m[index.Index.Table.Schema]
@@ -349,7 +349,7 @@ func (m schemaMap) getIndex(schemaName string, indexName string) *indexInfo {
}
func (m schemaMap) addSequence(id int, sequence *ast.CreateSequenceStmt) error {
- if pg.IsSystemSchema(sequence.SequenceDef.SequenceName.Schema) {
+ if pgparser.IsSystemSchema(sequence.SequenceDef.SequenceName.Schema) {
return nil
}
schema, exists := m[sequence.SequenceDef.SequenceName.Schema]
@@ -369,7 +369,7 @@ func (m schemaMap) getSequence(schemaName string, sequenceName string) *sequence
}
func (m schemaMap) addTrigger(id int, trigger *ast.CreateTriggerStmt) error {
- if pg.IsSystemSchema(trigger.Trigger.Table.Schema) {
+ if pgparser.IsSystemSchema(trigger.Trigger.Table.Schema) {
return nil
}
schema, exists := m[trigger.Trigger.Table.Schema]
@@ -397,7 +397,7 @@ func (m schemaMap) getTrigger(schemaName string, tableName string, triggerName s
}
func (m schemaMap) addType(id int, createType *ast.CreateTypeStmt) error {
- if pg.IsSystemSchema(createType.Type.TypeName().Schema) {
+ if pgparser.IsSystemSchema(createType.Type.TypeName().Schema) {
return nil
}
schema, exists := m[createType.Type.TypeName().Schema]
@@ -438,7 +438,7 @@ func (m schemaMap) addSequenceOwnedBy(id int, alterStmt *ast.AlterSequenceStmt)
return errors.Errorf("expect OwnedBy only, but found %v", alterStmt)
}
- if pg.IsSystemSchema(alterStmt.Name.Schema) {
+ if pgparser.IsSystemSchema(alterStmt.Name.Schema) {
return nil
}
schema, exists := m[alterStmt.Name.Schema]
@@ -639,7 +639,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
if _, exists := newPartitionMap[fmt.Sprintf("%s.%s", stmt.Name.Schema, stmt.Name.Name)]; exists {
continue
}
- if pg.IsSystemSchema(stmt.Name.Schema) {
+ if pgparser.IsSystemSchema(stmt.Name.Schema) {
continue
}
oldTable := oldSchemaMap.getTable(stmt.Name.Schema, stmt.Name.Name)
@@ -654,7 +654,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateSchemaStmt:
- if pg.IsSystemSchema(stmt.Name) {
+ if pgparser.IsSystemSchema(stmt.Name) {
continue
}
schema, hasSchema := oldSchemaMap[stmt.Name]
@@ -668,7 +668,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
if _, exists := newPartitionMap[fmt.Sprintf("%s.%s", stmt.Table.Schema, stmt.Table.Name)]; exists {
continue
}
- if pg.IsSystemSchema(stmt.Table.Schema) {
+ if pgparser.IsSystemSchema(stmt.Table.Schema) {
continue
}
for _, alterItem := range stmt.AlterItemList {
@@ -708,7 +708,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
if _, exists := newPartitionMap[fmt.Sprintf("%s.%s", stmt.Index.Table.Schema, stmt.Index.Table.Name)]; exists {
continue
}
- if pg.IsSystemSchema(stmt.Index.Table.Schema) {
+ if pgparser.IsSystemSchema(stmt.Index.Table.Schema) {
continue
}
oldIndex := oldSchemaMap.getIndex(stmt.Index.Table.Schema, stmt.Index.Name)
@@ -723,7 +723,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateSequenceStmt:
- if pg.IsSystemSchema(stmt.SequenceDef.SequenceName.Schema) {
+ if pgparser.IsSystemSchema(stmt.SequenceDef.SequenceName.Schema) {
continue
}
oldSequence := oldSchemaMap.getSequence(stmt.SequenceDef.SequenceName.Schema, stmt.SequenceDef.SequenceName.Name)
@@ -741,7 +741,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
if !onlySetOwnedBy(stmt) {
return "", errors.Errorf("expect OwnedBy only, but found %v", stmt)
}
- if pg.IsSystemSchema(stmt.Name.Schema) {
+ if pgparser.IsSystemSchema(stmt.Name.Schema) {
continue
}
oldSequence := oldSchemaMap.getSequence(stmt.Name.Schema, stmt.Name.Name)
@@ -755,7 +755,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateExtensionStmt:
- if pg.IsSystemSchema(stmt.Schema) {
+ if pgparser.IsSystemSchema(stmt.Schema) {
continue
}
oldExtension := oldSchemaMap.getExtension(stmt.Schema, stmt.Name)
@@ -770,7 +770,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateFunctionStmt:
- if pg.IsSystemSchema(stmt.Function.Schema) {
+ if pgparser.IsSystemSchema(stmt.Function.Schema) {
continue
}
signature, err := functionSignature(stmt.Function)
@@ -789,7 +789,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateTriggerStmt:
- if pg.IsSystemSchema(stmt.Trigger.Table.Schema) {
+ if pgparser.IsSystemSchema(stmt.Trigger.Table.Schema) {
continue
}
oldTrigger := oldSchemaMap.getTrigger(stmt.Trigger.Table.Schema, stmt.Trigger.Table.Name, stmt.Trigger.Name)
@@ -804,7 +804,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateTypeStmt:
- if pg.IsSystemSchema(stmt.Type.TypeName().Schema) {
+ if pgparser.IsSystemSchema(stmt.Type.TypeName().Schema) {
continue
}
oldType := oldSchemaMap.getType(stmt.Type.TypeName().Schema, stmt.Type.TypeName().Name)
diff --git a/backend/runner/slowquerysync/syncer.go b/backend/runner/slowquerysync/syncer.go
index f6d3590ca9..51a403e2b0 100644
--- a/backend/runner/slowquerysync/syncer.go
+++ b/backend/runner/slowquerysync/syncer.go
@@ -16,7 +16,7 @@ import (
"github.com/bytebase/bytebase/backend/component/dbfactory"
"github.com/bytebase/bytebase/backend/component/state"
api "github.com/bytebase/bytebase/backend/legacyapi"
- "github.com/bytebase/bytebase/backend/plugin/db/pg"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
"github.com/bytebase/bytebase/backend/store"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
@@ -153,7 +153,7 @@ func (s *Syncer) syncPostgreSQLSlowQuery(ctx context.Context, instance *store.In
if database.SyncState != api.OK {
continue
}
- if pg.IsSystemDatabase(database.DatabaseName) {
+ if pgparser.IsSystemDatabase(database.DatabaseName) {
continue
}
if err := func() error {