Skip to content

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

chore: sync search path (#16214)

rebelice90200e3fbffd
diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.go
index 33ecfd60fb..9ecd0f2ea1 100644
--- a/backend/plugin/db/pg/pg.go
+++ b/backend/plugin/db/pg/pg.go
@@ -312,6 +312,23 @@ func (d *Driver) getDatabases(ctx context.Context) ([]*storepb.DatabaseSchemaMet
return databases, nil
}
+// GetSearchPath gets the current search path of the database.
+// It returns the search path as a raw comma-separated string of schema names.
+func (d *Driver) GetSearchPath(ctx context.Context) (string, error) {
+ // SHOW search_path returns the current search path.
+ // PostgreSQL supports it since 8.2.
+ // https://www.postgresql.org/docs/current/sql-show.html
+ query := "SHOW search_path"
+ var searchPath string
+ if err := d.db.QueryRowContext(ctx, query).Scan(&searchPath); err != nil {
+ if err == sql.ErrNoRows {
+ return "", common.FormatDBErrorEmptyRowWithQuery(query)
+ }
+ return "", util.FormatErrorWithQuery(err, query)
+ }
+ return strings.TrimSpace(searchPath), nil
+}
+
// getVersion gets the version of Postgres server.
func (d *Driver) getVersion(ctx context.Context) (string, error) {
// SHOW server_version_num returns an integer such as 100005, which means 10.0.5.
diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.go
index f393b2d06d..f252bc64e3 100644
--- a/backend/plugin/db/pg/sync.go
+++ b/backend/plugin/db/pg/sync.go
@@ -77,6 +77,11 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
return nil, common.Errorf(common.NotFound, "database %q not found", d.databaseName)
}
isAtLeastPG10 := isAtLeastPG10(d.connectionCtx.EngineVersion)
+ searchPath, err := d.GetSearchPath(ctx)
+ if err != nil {
+ return nil, common.Errorf(common.Internal, "failed to get search path for database %q: %v", d.databaseName, err)
+ }
+ databaseMetadata.SearchPath = searchPath
txn, err := d.db.BeginTx(ctx, nil)
if err != nil {