diff --git a/weed/filer/postgres_flat/postgres_flat_store.go b/weed/filer/postgres_flat/postgres_flat_store.go new file mode 100644 index 0000000000..5d12070fb8 --- /dev/null +++ b/weed/filer/postgres_flat/postgres_flat_store.go @@ -0,0 +1,99 @@ +// 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 +} diff --git a/weed/filer/postgres_flat/postgres_flat_store_test.go b/weed/filer/postgres_flat/postgres_flat_store_test.go new file mode 100644 index 0000000000..6308f8693d --- /dev/null +++ b/weed/filer/postgres_flat/postgres_flat_store_test.go @@ -0,0 +1,74 @@ +package postgres_flat + +import ( + "context" + "strings" + "testing" + + "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/filer/store_test" + "github.com/seaweedfs/seaweedfs/weed/util" + "github.com/seaweedfs/seaweedfs/weed/util/pgxutil" +) + +func newTestStore(t *testing.T, db pgxutil.DSNOptions) *PostgresFlatStore { + store := &PostgresFlatStore{} + if err := store.initialize("", "", true, postgres.Connection{DSNOptions: db, MaxIdle: 10, MaxLifetimeSeconds: 60}); err != nil { + t.Fatal(err) + } + t.Cleanup(store.Shutdown) + return store +} + +func TestStoreConformance(t *testing.T) { + store := newTestStore(t, store_test.NewPostgresTestDatabase(t)) + defer abstract_sql.SetScanPageSizeForTest(abstract_sql.SetScanPageSizeForTest(3)) + store_test.TestFilerStoreConformance(t, store) +} + +func TestRowsAreKeyedByObjectKey(t *testing.T) { + store := newTestStore(t, store_test.NewPostgresTestDatabase(t)) + ctx := context.Background() + store.OnBucketCreation("photos") + for _, p := range []string{"/buckets/photos/2026/01/a.jpg", "/topics/t.log"} { + if err := store.InsertEntry(ctx, &filer.Entry{FullPath: util.FullPath(p)}); err != nil { + t.Fatal(err) + } + } + if err := store.KvPut(ctx, []byte("hardlink-id"), []byte("record")); err != nil { + t.Fatal(err) + } + for table, want := range map[string]string{"photos": "/2026/01/a.jpg", abstract_sql.DEFAULT_TABLE: "/topics/t.log"} { + var path, directory, name string + if err := store.DB.QueryRow(`SELECT path, directory, name FROM "`+table+`"`).Scan(&path, &directory, &name); err != nil { + t.Fatalf("%s: %v", table, err) + } + if path != want || directory+"/"+name != want { + t.Errorf("%s row is %q (%q, %q), want %q", table, path, directory, name, want) + } + } + var kvRows int + if err := store.DB.QueryRow(`SELECT count(*) FROM "` + kvTable + `"`).Scan(&kvRows); err != nil || kvRows != 1 { + t.Fatalf("%s holds %d rows, %v", kvTable, kvRows, err) + } +} + +func TestRefusesAnotherLayout(t *testing.T) { + db := store_test.NewPostgresTestDatabase(t) + old := &abstract_sql.AbstractSqlStore{} + if err := (postgres.Connection{DSNOptions: db}).Open(old); err != nil { + t.Fatal(err) + } + defer old.Shutdown() + if _, err := old.DB.Exec(strings.ReplaceAll(postgres.DefaultCreateTableQuery, "%s", abstract_sql.DEFAULT_TABLE)); err != nil { + t.Fatal(err) + } + store := &PostgresFlatStore{} + err := store.initialize("", "", true, postgres.Connection{DSNOptions: db}) + if err == nil || !strings.Contains(err.Error(), "no path column") { + t.Fatalf("initialize over a postgres2 table: %v", err) + } + store.Shutdown() +}