Files
seaweedfs/weed/filer/postgres/postgres_store.go
T
Chris LuandClaude Opus 5.5 cb9f2095c9 filer/postgres: build the connection string with pgxutil.BuildDSN
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
2026-12-01 16:34:59 -08:00

115 lines
4.1 KiB
Go

// Package postgres provides PostgreSQL filer store implementation
// 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 postgres
import (
"context"
"database/sql"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/util/pgxutil"
)
func init() {
filer.Stores = append(filer.Stores, &PostgresStore{})
}
type PostgresStore struct {
abstract_sql.AbstractSqlStore
}
func (store *PostgresStore) GetName() string {
return "postgres"
}
func (store *PostgresStore) 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 *PostgresStore) 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 = false
createTable = ResolveCreateTableQuery(createTable)
if !enableUpsert {
upsertQuery = ""
} else if upsertQuery == "" {
upsertQuery = DefaultUpsertQuery
}
gen := &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 createTable != "" {
if _, err = store.DB.ExecContext(context.Background(), gen.GetSqlCreateTable(abstract_sql.DEFAULT_TABLE)); err != nil {
return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
}
}
ConfigureListOrdering(store.DB, gen)
return nil
}