Skip to content

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

fix(span): allow users to access system views/tables (#9574)

h3n4lcf97572f3d3f
diff --git a/backend/plugin/parser/pg/system_objects.go b/backend/plugin/parser/pg/system_objects.go
index df56c50a6e..3e7727c281 100644
--- a/backend/plugin/parser/pg/system_objects.go
+++ b/backend/plugin/parser/pg/system_objects.go
@@ -23,6 +23,45 @@ var (
"template1": true,
}
+ // systemViews is the list of system views that we will exclude from the schema sync.
+ // https://www.postgresql.org/docs/16/views.html
+ systemViews = map[string]bool{
+ "pg_available_extensions": true,
+ "pg_available_extension_versions": true,
+ "pg_backend_memory_contexts": true,
+ "pg_config": true,
+ "pg_cursors": true,
+ "pg_file_settings": true,
+ "pg_group": true,
+ "pg_hba_file_rules": true,
+ "pg_ident_file_mappings": true,
+ "pg_indexes": true,
+ "pg_locks": true,
+ "pg_matview": true,
+ "pg_policies": true,
+ "pg_prepared_statement": true,
+ "pg_prepared_xacts": true,
+ "pg_publication_tables": true,
+ "pg_replication_origin_status": true,
+ "pg_replication_slots": true,
+ "pg_roles": true,
+ "pg_rules": true,
+ "pg_seclabels": true,
+ "pg_sequences": true,
+ "pg_settings": true,
+ "pg_shadow": true,
+ "pg_shmem_allocations": true,
+ "pg_stats": true,
+ "pg_stats_ext": true,
+ "pg_stats_ext_exprs": true,
+ "pg_tables": true,
+ "pg_timezone_abbrevs": true,
+ "pg_timezone_names": true,
+ "pg_user": true,
+ "pg_user_mappings": true,
+ "pg_views": true,
+ }
+
// systemSchemas is the list of system schemas that we will exclude from the schema sync.
systemSchemas = map[string]bool{
"information_schema": true,
@@ -38,6 +77,7 @@ var (
}
// systemTables is the list of system tables that we will exclude from the schema sync.
+ // https://www.postgresql.org/docs/16/catalogs.html
systemTables = map[string]bool{
"pg_aggregate": true,
"pg_am": true,
@@ -165,6 +205,9 @@ func IsSystemTable(table string) bool {
}
func IsSystemView(view string) bool {
+ if _, ok := systemViews[view]; ok {
+ return true
+ }
if strings.HasPrefix(view, "g_columnar_") {
return true
}
diff --git a/backend/plugin/parser/pg/v2/query_span_extractor.go b/backend/plugin/parser/pg/v2/query_span_extractor.go
index abef6eac08..910ab5d9d5 100644
--- a/backend/plugin/parser/pg/v2/query_span_extractor.go
+++ b/backend/plugin/parser/pg/v2/query_span_extractor.go
@@ -1031,9 +1031,12 @@ func (q *querySpanExtractor) getRangeVarsFromJSONRecursive(jsonData map[string]a
if jsonData["RangeVar"] != nil {
resource := base.ColumnResource{
Server: "",
- Database: currentDatabase,
- Schema: currentSchema,
+ Database: "",
+ Schema: "",
+ Table: "",
+ Column: "",
}
+
rangeVar, ok := jsonData["RangeVar"].(map[string]any)
if !ok {
return nil, errors.Errorf("failed to convert range var")
@@ -1052,23 +1055,32 @@ func (q *querySpanExtractor) getRangeVarsFromJSONRecursive(jsonData map[string]a
}
resource.Table = table
}
- databaseMetadata, err := q.getDatabaseMetadata(currentDatabase)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get database metadata for database: %s", currentDatabase)
- }
- // If the schema is system schema, we do not need to check the table.
- if !pg.IsSystemSchema(resource.Schema) {
+ // This is a false-positive behavior, the table we found may not be the table the query actually accesses.
+ // For example, the query is `WITH t1 AS (SELECT 1) SELECT * FROM t1` and we have a physical table `t1` in the database exactly,
+ // what we found is the physical table `t1`, but the query actually accesses the CTE `t1`.
+ // We do this because we do not have too much time to implement the real behavior.
+ // XXX(rebelice/zp): Can we pass more information here to make this function know the context and then
+ // figure out whether the table is the table the query actually accesses?
+
+ // Bytebase do not sync the system objects, so we skip finding for system objects in the metadata.
+ if msg := isSystemResource(resource); msg == "" {
+ // Backfill the default database/schema name.
+ if resource.Database == "" {
+ resource.Database = currentDatabase
+ }
+ if resource.Schema == "" {
+ resource.Schema = currentSchema
+ }
+ databaseMetadata, err := q.getDatabaseMetadata(currentDatabase)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get database metadata for database: %s", currentDatabase)
+ }
// Access pseudo table or table we do not sync, return directly.
if databaseMetadata == nil || databaseMetadata.GetSchema(resource.Schema) == nil || databaseMetadata.GetSchema(resource.Schema).GetTable(resource.Table) == nil {
return nil, nil
}
}
- // This is a false-positive behavior, the table we found may not be the table the query actually accesses.
- // For example, the query is `WITH t1 AS (SELECT 1) SELECT * FROM t1` and we have a physical table `t1` in the database exactly.
- // We do this because we do not have too much time to implement the real behavior.
- // XXX(rebelice/zp): Can we pass more information here to make this function know the context and then
- // figure out whether the table is the table the query actually accesses?
result = append(result, resource)
}
@@ -1098,17 +1110,39 @@ func (q *querySpanExtractor) getRangeVarsFromJSONRecursive(jsonData map[string]a
// isMixedQuery checks whether the query accesses the user table and system table at the same time.
func isMixedQuery(m base.SourceColumnSet) (allSystems bool, mixed error) {
- systemSchema := ""
+ userMsg, systemMsg := "", ""
for table := range m {
- if pg.IsSystemSchema(table.Schema) {
- systemSchema = table.Schema
+ if msg := isSystemResource(table); msg != "" {
+ systemMsg = msg
continue
}
-
- if systemSchema != "" {
- return false, errors.Errorf("cannot access user table %q.%q and system schema %q at the same time", table.Schema, table.Table, systemSchema)
+ userMsg = fmt.Sprintf("user table %q.%q", table.Schema, table.Table)
+ if systemMsg != "" {
+ return false, errors.Errorf("cannot access %s and %s at the same time", userMsg, systemMsg)
}
}
- return systemSchema != "", nil
+ if userMsg != "" && systemMsg != "" {
+ return false, errors.Errorf("cannot access %s and %s at the same time", userMsg, systemMsg)
+ }
+
+ return userMsg == "" && systemMsg != "", nil
+}
+
+func isSystemResource(resource base.ColumnResource) string {
+ // User can access the system table/view by name directly without database/schema name.
+ // For example: `SELECT * FROM pg_database`, which will access the system table `pg_database`.
+ // Additionally, user can create a table/view with the same name with system table/view and access them
+ // by specify the schema name, for example:
+ // `CREATE TABLE pg_database(id INT); SELECT * FROM public.pg_database;` which will access the user table `pg_database`.
+ if pg.IsSystemSchema(resource.Schema) {
+ return fmt.Sprintf("system schema %q", resource.Schema)
+ }
+ if resource.Database == "" && resource.Schema == "" && pg.IsSystemView(resource.Table) {
+ return fmt.Sprintf("system view %q", resource.Table)
+ }
+ if resource.Database == "" && resource.Schema == "" && pg.IsSystemTable(resource.Table) {
+ return fmt.Sprintf("system table %q", resource.Table)
+ }
+ return ""
}