package postgres import ( "database/sql" "strings" "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql" "github.com/seaweedfs/seaweedfs/weed/glog" ) // ConfigureListOrdering switches the list queries to COLLATE "C" when the live // collation of a filemeta column they order by (name, unless columns says // otherwise) is locale-aware, so S3 ListObjectsV2 stays lexicographic. The // fallback costs a sort, so warn the operator to fix it. func ConfigureListOrdering(db *sql.DB, gen *SqlGenPostgres, columns ...string) { if isCockroachDB(db) { // CockroachDB string comparison is already byte-ordered, so the // fallback is redundant; older versions also reject COLLATE "C". return } if len(columns) == 0 { columns = []string{"name"} } for _, column := range columns { collation, isBinary, err := columnCollation(db, column) if err != nil { glog.V(1).Infof("postgres: skip filemeta.%s collation check: %v", column, err) continue } if isBinary { continue } gen.ForceBinaryCollation = true glog.Warningf(`postgres: filemeta.%s collation %q is not byte-ordered, so S3 list order is not byte-lexicographic and clients that merge sorted listings may report spurious diffs. Falling back to a slower COLLATE "C" sort; declare the %s column COLLATE "C" (or use a C/C.UTF-8 database collation) for correct, indexed ordering.`, column, collation, column) return } } // isCockroachDB reports whether db is a CockroachDB backend, whose version() // string carries "CockroachDB". On any error we assume real Postgres. func isCockroachDB(db *sql.DB) bool { var version string if err := db.QueryRow("SELECT version()").Scan(&version); err != nil { glog.V(1).Infof("postgres: skip CockroachDB detection: %v", err) return false } return strings.Contains(version, "CockroachDB") } func columnCollation(db *sql.DB, column string) (collation string, isBinary bool, err error) { // to_regclass resolves the table along the search path, as the store's // queries do. A column on the database default has the "default" // collation, so fall back to the database locale. An ICU or builtin // default (Postgres 15+) orders by its own locale, not datcollate; to_jsonb // reads those columns without failing on older servers that lack them. row := db.QueryRow(`SELECT (SELECT NULLIF(c.collname, 'default') FROM pg_attribute a JOIN pg_collation c ON c.oid = a.attcollation WHERE a.attrelid = to_regclass($1) AND a.attname = $2 AND NOT a.attisdropped), d.datcollate, to_jsonb(d)->>'datlocprovider', COALESCE(to_jsonb(d)->>'datlocale', to_jsonb(d)->>'daticulocale') FROM pg_database d WHERE d.datname = current_database()`, abstract_sql.DEFAULT_TABLE, column) var declared, dbCollate, dbProvider, dbLocale sql.NullString if err = row.Scan(&declared, &dbCollate, &dbProvider, &dbLocale); err != nil { return "", false, err } effective := declared.String if effective == "" { switch dbProvider.String { case "i": return "ICU " + dbLocale.String, false, nil case "b": effective = dbLocale.String default: effective = dbCollate.String } } return effective, isByteOrderedCollation(effective), nil } // isByteOrderedCollation reports whether a Postgres locale sorts by byte value // (C, POSIX, C.UTF-8) rather than locale order (en_US.UTF-8, ICU, ...). func isByteOrderedCollation(name string) bool { switch strings.ToLower(strings.TrimSpace(name)) { case "c", "posix", "c.utf-8", "c.utf8": return true } return false }