Skip to content

Commit

Permalink
only one schema is supported in search_path.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijian-pro committed Sep 2, 2022
1 parent dff9653 commit df146e7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/en/guide/how_to_set_up_metadata_engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ juicefs format \
pics
```

:::note
1. juicefs uses public [schema](https://www.postgresql.org/docs/current/ddl-schemas.html) by default, if you want to use a `non-public schema`, you need to specify `search_path` in the connection string parameter. e.g `postgres://user:[email protected]:5432/juicefs?search_path=pguser1`
2. If the `public schema` is not the first hit in the `search_path` configured on the PostgreSQL server, the `search_path` parameter must be explicitly set in the connection string.
3. The `search_path` connection parameter can be set to multiple schemas natively, but currently juicefs only supports setting one. `postgres://user:[email protected]:5432/juicefs?search_path=pguser1,public` will be considered illegal.
:::


### Mount a file system

```shell
Expand Down
6 changes: 6 additions & 0 deletions docs/zh_cn/guide/how_to_set_up_metadata_engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ juicefs format \
pics
```

:::note 说明
1. juicefs 默认使用的 public [schema](https://www.postgresql.org/docs/current/ddl-schemas.html) ,如果要使用非 `public schema`,需要在连接字符串中指定 `search_path` 参数,例如 `postgres://user:[email protected]:5432/juicefs?search_path=pguser1`
2. 如果 `public schema` 并非是 PostgreSQL 服务端配置的 `search_path` 中第一个命中的,则必须在连接字符串中明确设置 `search_path` 参数
3. `search_path` 连接参数原生可以设置为多个 schema ,但是目前 juicefs 仅支持设置一个。`postgres://user:[email protected]:5432/juicefs?search_path=pguser1,public` 将被认为不合法
:::

### 挂载文件系统

```shell
Expand Down
15 changes: 14 additions & 1 deletion pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"errors"
"fmt"
"io"
"net/url"
"runtime"
"sort"
"strings"
Expand Down Expand Up @@ -175,8 +176,20 @@ type dbSnap struct {
}

func newSQLMeta(driver, addr string, conf *Config) (Meta, error) {
var searchPath string
if driver == "postgres" {
addr = driver + "://" + addr

parse, err := url.Parse(addr)
if err != nil {
return nil, fmt.Errorf("parse url %s failed: %s", addr, err)
}
searchPath = parse.Query().Get("search_path")
if searchPath != "" {
if len(strings.Split(searchPath, ",")) > 1 {
return nil, fmt.Errorf("currently, only one schema is supported in search_path")
}
}
}
engine, err := xorm.NewEngine(driver, addr)
if err != nil {
Expand All @@ -202,7 +215,7 @@ func newSQLMeta(driver, addr string, conf *Config) (Meta, error) {
if time.Since(start) > time.Millisecond*5 {
logger.Warnf("The latency to database is too high: %s", time.Since(start))
}

engine.SetSchema(searchPath)
engine.DB().SetMaxIdleConns(runtime.NumCPU() * 2)
engine.DB().SetConnMaxIdleTime(time.Minute * 5)
engine.SetTableMapper(names.NewPrefixMapper(engine.GetTableMapper(), "jfs_"))
Expand Down
10 changes: 10 additions & 0 deletions pkg/meta/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
package meta

import (
"fmt"
"path"
"testing"

"github.com/pkg/errors"
)

func TestSQLiteClient(t *testing.T) {
Expand All @@ -45,3 +48,10 @@ func TestPostgreSQLClient(t *testing.T) {
}
testMeta(t, m)
}

func TestPostgreSQLClientWithSearchPath(t *testing.T) {
_, err := newSQLMeta("postgres", "localhost:5432/test?sslmode=disable&search_path=juicefs,public", &Config{})
if errors.Is(err, fmt.Errorf("currently, only one schema is supported in search_path")) {
t.Fatalf("create meta: %s", err)
}
}
6 changes: 6 additions & 0 deletions pkg/object/object_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,12 @@ func TestPG(t *testing.T) {
testStorage(t, s)

}
func TestPGWithSearchPath(t *testing.T) {
_, err := newSQLStore("postgres", "localhost:5432/test?sslmode=disable&search_path=juicefs,public", "", "")
if errors.Is(err, fmt.Errorf("currently, only one schema is supported in search_path")) {
t.Fatalf("newSQLStore error: %s", err)
}
}

func TestMySQL(t *testing.T) {
if os.Getenv("MYSQL_ADDR") == "" {
Expand Down
14 changes: 14 additions & 0 deletions pkg/object/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"strings"
"time"
Expand Down Expand Up @@ -157,8 +158,20 @@ func newSQLStore(driver, addr, user, password string) (ObjectStorage, error) {
if user != "" {
uri = user + ":" + password + "@" + addr
}
var searchPath string
if driver == "postgres" {
uri = "postgres://" + uri

parse, err := url.Parse(addr)
if err != nil {
return nil, fmt.Errorf("parse url %s failed: %s", addr, err)
}
searchPath = parse.Query().Get("search_path")
if searchPath != "" {
if len(strings.Split(searchPath, ",")) > 1 {
return nil, fmt.Errorf("currently, only one schema is supported in search_path")
}
}
}
engine, err := xorm.NewEngine(driver, uri)
if err != nil {
Expand All @@ -176,6 +189,7 @@ func newSQLStore(driver, addr, user, password string) (ObjectStorage, error) {
default:
engine.SetLogLevel(log.LOG_OFF)
}
engine.SetSchema(searchPath)
engine.SetTableMapper(names.NewPrefixMapper(engine.GetTableMapper(), "jfs_"))
if err := engine.Sync2(new(blob)); err != nil {
return nil, fmt.Errorf("create table blob: %s", err)
Expand Down

0 comments on commit df146e7

Please sign in to comment.