Files
seaweedfs/weed/filer/postgres_flat/postgres_flat_store.go
T
Chris LuandClaude Opus 5.5 2fddcae20e filer/postgres_flat: name a missing filemeta table instead of a foreign layout
With createTable = false and no filemeta table, the layout check said the
table had no path column and blamed a postgres2 layout. Report the missing
table and the key-value table that also has to be created.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 16:58:17 -08:00

104 lines
3.5 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 missing filemeta table, which only a createTable
// of false leaves, and one of the postgres or postgres2 layout.
func checkFlatLayout(ctx context.Context, db *sql.DB) error {
var columns, pathColumns int
if err := db.QueryRowContext(ctx, `SELECT count(*), count(*) FILTER (WHERE column_name = 'path') FROM information_schema.columns
WHERE table_schema = current_schema() AND table_name = $1`, abstract_sql.DEFAULT_TABLE).Scan(&columns, &pathColumns); err != nil {
return fmt.Errorf("check table %s: %v", abstract_sql.DEFAULT_TABLE, err)
}
if columns == 0 {
return fmt.Errorf("table %s does not exist: with createTable = false, create it and %s beforehand", abstract_sql.DEFAULT_TABLE, kvTable)
}
if pathColumns == 0 {
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
}