Files
seaweedfs/weed/filer/postgres_flat/postgres_flat_store.go
T
Chris LuandClaude Opus 5.5 cb1356f7f1 filer: postgres_flat store
Entries are keyed by their full path, the object key inside a bucket's
table; buckets get tables of their own as in postgres2. Key-value
records keep the postgres2 layout in filemeta_kv. The store refuses a
filemeta table of another layout, so it needs a schema of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
2026-12-01 16:38:39 -08:00

100 lines
3.2 KiB
Go

// Package postgres_flat is a PostgreSQL filer store whose rows are keyed by
// their full path, one table per bucket as in postgres2.
package postgres_flat
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"
)
const kvTable = "filemeta_kv"
var _ filer.BucketAware = (*PostgresFlatStore)(nil)
func init() {
filer.Stores = append(filer.Stores, &PostgresFlatStore{})
}
type PostgresFlatStore struct {
abstract_sql.AbstractSqlStore
}
func (store *PostgresFlatStore) GetName() string {
return "postgres_flat"
}
func (store *PostgresFlatStore) Initialize(configuration util.Configuration, prefix string) (err error) {
configuration.SetDefault(prefix+"enableUpsert", true)
return store.initialize(
configuration.GetString(prefix+"createTable"),
configuration.GetString(prefix+"upsertQuery"),
configuration.GetBool(prefix+"enableUpsert"),
postgres.ReadConnection(configuration, prefix),
)
}
func (store *PostgresFlatStore) initialize(createTable, upsertQuery string, enableUpsert bool, conn postgres.Connection) (err error) {
store.SupportBucketTable = true
store.SkipDDL = createTable == "false"
if createTable == "" || createTable == "true" || store.SkipDDL {
createTable = postgres.DefaultFlatCreateTableQuery
}
if !enableUpsert {
upsertQuery = ""
} else if upsertQuery == "" {
upsertQuery = postgres.DefaultFlatUpsertQuery
}
gen := &postgres.SqlGenPostgresFlat{SqlGenPostgres: postgres.SqlGenPostgres{
CreateTableSqlTemplate: createTable,
DropTableSqlTemplate: `drop table if exists "%s"`,
UpsertQueryTemplate: upsertQuery,
}}
store.SqlGenerator = gen
store.KvSqlGenerator = &postgres.SqlGenPostgres{
CreateTableSqlTemplate: postgres.DefaultCreateTableQuery,
UpsertQueryTemplate: postgres.DefaultUpsertQuery,
}
store.KvTable = kvTable
if err = conn.Open(&store.AbstractSqlStore); err != nil {
return err
}
ctx := context.Background()
if !store.SkipDDL {
if err = store.CreateTable(ctx, abstract_sql.DEFAULT_TABLE); err != nil {
return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
}
if _, err = store.DB.ExecContext(ctx, store.KvSqlGenerator.GetSqlCreateTable(kvTable)); err != nil {
return fmt.Errorf("init table %s: %v", kvTable, err)
}
}
if err = checkFlatLayout(ctx, store.DB); err != nil {
return err
}
postgres.ConfigureListOrdering(store.DB, &gen.SqlGenPostgres)
return nil
}
// checkFlatLayout refuses a filemeta table of the postgres or postgres2 layout.
func checkFlatLayout(ctx context.Context, db *sql.DB) error {
var found bool
if err := db.QueryRowContext(ctx, `SELECT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_schema = current_schema() AND table_name = $1 AND column_name = 'path')`, abstract_sql.DEFAULT_TABLE).Scan(&found); err != nil {
return fmt.Errorf("check table %s: %v", abstract_sql.DEFAULT_TABLE, err)
}
if !found {
return fmt.Errorf("table %s has no path column: postgres_flat needs a database or schema of its own, apart from the postgres and postgres2 stores", abstract_sql.DEFAULT_TABLE)
}
return nil
}