mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
ReadConnection reads the settings and their defaults, Connection.Open opens the store's pools. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
73 lines
2.1 KiB
Go
73 lines
2.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"
|
|
"fmt"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
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) {
|
|
// 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"),
|
|
ReadConnection(configuration, prefix),
|
|
)
|
|
}
|
|
|
|
func (store *PostgresStore) initialize(createTable, upsertQuery string, enableUpsert bool, conn Connection) (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
|
|
|
|
if err = conn.Open(&store.AbstractSqlStore); 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
|
|
}
|