mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
The credential store already does; it quotes values per libpq, so a password or certificate path with spaces or quotes no longer breaks the connection string. The OpenPGXDB alias has no callers left. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
122 lines
4.4 KiB
Go
122 lines
4.4 KiB
Go
// Package postgres2 provides PostgreSQL filer store implementation with bucket support
|
|
// Migrated from github.com/lib/pq to github.com/jackc/pgx for:
|
|
// - Active development and support
|
|
// - Better performance and PostgreSQL-specific features
|
|
// - Improved error handling (no more panics)
|
|
// - Built-in logging capabilities
|
|
// - Superior SSL certificate support
|
|
package postgres2
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/postgres"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/seaweedfs/seaweedfs/weed/util/pgxutil"
|
|
)
|
|
|
|
var _ filer.BucketAware = (*PostgresStore2)(nil)
|
|
|
|
func init() {
|
|
filer.Stores = append(filer.Stores, &PostgresStore2{})
|
|
}
|
|
|
|
type PostgresStore2 struct {
|
|
abstract_sql.AbstractSqlStore
|
|
}
|
|
|
|
func (store *PostgresStore2) GetName() string {
|
|
return "postgres2"
|
|
}
|
|
|
|
func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix string) (err error) {
|
|
// Fewer idle slots than concurrent operations means a fresh connection per
|
|
// operation, until the filer runs out of ephemeral ports. connection_max_open
|
|
// stays unset: a listing runs a second query from its own callback, so a
|
|
// bounded pool deadlocks once the concurrency reaches it.
|
|
configuration.SetDefault(prefix+"connection_max_idle", 50)
|
|
configuration.SetDefault(prefix+"connection_max_lifetime_seconds", 300)
|
|
// Default on so minimal configs are not exposed to duplicate-key tx
|
|
// poisoning on Postgres; an explicit false still disables it.
|
|
configuration.SetDefault(prefix+"enableUpsert", true)
|
|
return store.initialize(
|
|
configuration.GetString(prefix+"createTable"),
|
|
configuration.GetString(prefix+"upsertQuery"),
|
|
configuration.GetBool(prefix+"enableUpsert"),
|
|
configuration.GetString(prefix+"username"),
|
|
configuration.GetString(prefix+"password"),
|
|
configuration.GetString(prefix+"hostname"),
|
|
configuration.GetInt(prefix+"port"),
|
|
configuration.GetString(prefix+"database"),
|
|
configuration.GetString(prefix+"schema"),
|
|
configuration.GetString(prefix+"sslmode"),
|
|
configuration.GetString(prefix+"sslcert"),
|
|
configuration.GetString(prefix+"sslkey"),
|
|
configuration.GetString(prefix+"sslrootcert"),
|
|
configuration.GetString(prefix+"sslcrl"),
|
|
configuration.GetBool(prefix+"pgbouncer_compatible"),
|
|
configuration.GetInt(prefix+"connection_max_idle"),
|
|
configuration.GetInt(prefix+"connection_max_open"),
|
|
configuration.GetInt(prefix+"connection_max_lifetime_seconds"),
|
|
)
|
|
}
|
|
|
|
func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database, schema, sslmode, sslcert, sslkey, sslrootcert, sslcrl string, pgbouncerCompatible bool, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) {
|
|
|
|
store.SupportBucketTable = true
|
|
store.SkipDDL = createTable == "false"
|
|
createTable = postgres.ResolveCreateTableQuery(createTable)
|
|
if createTable == "" && !store.SkipDDL {
|
|
createTable = postgres.DefaultCreateTableQuery
|
|
}
|
|
if !enableUpsert {
|
|
upsertQuery = ""
|
|
} else if upsertQuery == "" {
|
|
upsertQuery = postgres.DefaultUpsertQuery
|
|
}
|
|
gen := &postgres.SqlGenPostgres{
|
|
CreateTableSqlTemplate: createTable,
|
|
DropTableSqlTemplate: `drop table if exists "%s"`,
|
|
UpsertQueryTemplate: upsertQuery,
|
|
}
|
|
store.SqlGenerator = gen
|
|
|
|
sqlUrl, adaptedSqlUrl := pgxutil.BuildDSN(pgxutil.DSNOptions{
|
|
Hostname: hostname,
|
|
Port: port,
|
|
User: user,
|
|
Password: password,
|
|
Database: database,
|
|
Schema: schema,
|
|
SSLMode: sslmode,
|
|
SSLCert: sslcert,
|
|
SSLKey: sslkey,
|
|
SSLRootCert: sslrootcert,
|
|
SSLCRL: sslcrl,
|
|
PgBouncerCompatible: pgbouncerCompatible,
|
|
})
|
|
db, openErr := pgxutil.OpenDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds)
|
|
if openErr != nil {
|
|
return openErr
|
|
}
|
|
if err = store.UseConnectionPools(db, func() (*sql.DB, error) {
|
|
return pgxutil.OpenDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds)
|
|
}, maxIdle, maxOpen, maxLifetimeSeconds); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !store.SkipDDL {
|
|
if err = store.CreateTable(context.Background(), abstract_sql.DEFAULT_TABLE); err != nil {
|
|
return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
|
|
}
|
|
}
|
|
|
|
postgres.ConfigureListOrdering(store.DB, gen)
|
|
|
|
return nil
|
|
}
|