mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
* filer/store_test: conformance suite for filer stores Entries in and out of buckets, byte-ordered and prefixed listings, key-value records, transactions, bucket tables and the snapshot scan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer: run postgres2 and mysql2 through the conformance suite RUN_POSTGRES_TESTS and RUN_MYSQL_TESTS enable them, as RUN_REDIS_TESTS does the Redis stores; each test gets a schema or database of its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * ci: give the go test job Postgres and MySQL services Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: generators bind the arguments of row statements Insert, update, find and delete now take the row key and return their arguments with the query, so a generator decides how a key maps to its columns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: generators bind the arguments of directory statements Listing and deleting a directory's children take the directory and return their arguments with the query. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: batch lookup takes row keys and matches rows by name and directory A key-value key's directory column already determines its dirhash, so the batch read no longer selects dirhash to match its rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * 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 * filer/sql: scan pages take the row key of the last row as their cursor Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres: postgres and postgres2 share their connection setup 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 * filer/sql: key-value records may live in a table of their own A store whose entry tables have another layout keeps its key-value records in KvTable, written by KvSqlGenerator; its scans then read only entries and need not tell the records apart. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres: SQL generator keyed by the full path Rows have path as their primary key; a (directory, name) unique constraint serves the directory listings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * 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 * filer: register postgres_flat and add it to the filer.toml scaffold Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: test the arguments each generator binds Each statement's arguments must match its placeholders in count and column. Check them for every generator method, including the Postgres UPDATE whose parameters are now numbered key first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * 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> * filer/postgres: postgres_flat escapes the table name inside its quotes A double quote in it is doubled, as PostgreSQL reads a quoted identifier. Bucket names cannot hold one, but the statements no longer rely on that. Reported by CodeQL (go/unsafe-quoting). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres: escape the table name inside its quotes A double quote in it is doubled, as PostgreSQL reads a quoted identifier. Bucket names cannot hold one; the statements no longer rely on that. Reported by CodeQL (go/unsafe-quoting) on the lines this branch changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres: postgres_flat shares the generator's tableIdent Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres: keep the nameExpr doc comment on nameExpr Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres_flat: find filemeta along the search path when checking its layout The store's queries name filemeta unqualified, so with schema = "a,b" they reach a table in b; the layout check looked in current_schema() only and refused it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres_flat: force byte order when the path column is locale-ordered Directory listings order and bound rows by path, but only the name column's collation was checked. A createTable with a locale-ordered path column listed in locale order, and byte-order bounds missed entries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres: read the ordered columns' collation along the search path The collation check looked for filemeta in current_schema() only. With the tables in a later schema of the search path it found no column and took the database's order, so a locale-ordered path column next to a C database went unforced, and byte-ordered columns in a locale database got a needless sort. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres_flat: list a directory from its path range A directory's children are the rows of its path range whose directory is the listed one. The listing reads that range in growing batches and skips the rows of the children's subtrees in Go, so a versioned object's .versions rows cost no query of their own. Past 64 rows of one subtree it queries again from that subtree's end. A name prefix narrows the range. Listing no longer reads the (directory, name) index. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres_flat: find a folder's children in its path range Deleting a folder's children looked them up by directory alone, which only the (directory, name) index serves. Bound the delete by the folder's path range as well. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres_flat: key the table by path alone Listing and deleting a folder's children both read the path range now, so the (directory, name) unique index only cost writes and storage: flatPath already makes the path unique per (directory, name). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres: read the ICU or builtin database locale before datcollate An ICU-default database can report datcollate 'C' while its default collation orders by the ICU locale, so a name column on the database default looked byte-ordered and the listing kept locale order. Read datlocprovider and the provider locale (via to_jsonb, so servers before Postgres 15 still answer) and treat an ICU default as locale-ordered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres_flat: list a folder's children from its path range The bucket enumeration listed /buckets by directory and ordered by name, a sequential scan and sort now that path is the only index. Read the folder's path range in path order instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres_flat: only the path column needs byte order Listings now order and range by path alone, and directory and name are only compared for equality, which no deterministic collation changes. Drop their COLLATE "C" and check only the path column's order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
3.5 KiB
Go
105 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, "path")
|
|
|
|
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
|
|
// to_regclass resolves the name along the search path, as the store's queries do.
|
|
if err := db.QueryRowContext(ctx, `SELECT count(*), count(*) FILTER (WHERE attname = 'path') FROM pg_attribute
|
|
WHERE attrelid = to_regclass($1) AND attnum > 0 AND NOT attisdropped`, 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
|
|
}
|