postgres_store.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Package postgres provides PostgreSQL filer store implementation
  2. // Migrated from github.com/lib/pq to github.com/jackc/pgx for:
  3. // - Active development and support
  4. // - Better performance and PostgreSQL-specific features
  5. // - Improved error handling (no more panics)
  6. // - Built-in logging capabilities
  7. // - Superior SSL certificate support
  8. package postgres
  9. import (
  10. "database/sql"
  11. "fmt"
  12. "strconv"
  13. "time"
  14. _ "github.com/jackc/pgx/v5/stdlib"
  15. "github.com/seaweedfs/seaweedfs/weed/filer"
  16. "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
  17. "github.com/seaweedfs/seaweedfs/weed/util"
  18. )
  19. func init() {
  20. filer.Stores = append(filer.Stores, &PostgresStore{})
  21. }
  22. type PostgresStore struct {
  23. abstract_sql.AbstractSqlStore
  24. }
  25. func (store *PostgresStore) GetName() string {
  26. return "postgres"
  27. }
  28. func (store *PostgresStore) Initialize(configuration util.Configuration, prefix string) (err error) {
  29. return store.initialize(
  30. configuration.GetString(prefix+"upsertQuery"),
  31. configuration.GetBool(prefix+"enableUpsert"),
  32. configuration.GetString(prefix+"username"),
  33. configuration.GetString(prefix+"password"),
  34. configuration.GetString(prefix+"hostname"),
  35. configuration.GetInt(prefix+"port"),
  36. configuration.GetString(prefix+"database"),
  37. configuration.GetString(prefix+"schema"),
  38. configuration.GetString(prefix+"sslmode"),
  39. configuration.GetString(prefix+"sslcert"),
  40. configuration.GetString(prefix+"sslkey"),
  41. configuration.GetString(prefix+"sslrootcert"),
  42. configuration.GetString(prefix+"sslcrl"),
  43. configuration.GetBool(prefix+"pgbouncer_compatible"),
  44. configuration.GetInt(prefix+"connection_max_idle"),
  45. configuration.GetInt(prefix+"connection_max_open"),
  46. configuration.GetInt(prefix+"connection_max_lifetime_seconds"),
  47. )
  48. }
  49. func (store *PostgresStore) initialize(upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database, schema, sslmode, sslcert, sslkey, sslrootcert, sslcrl string, pgbouncerCompatible bool, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) {
  50. store.SupportBucketTable = false
  51. if !enableUpsert {
  52. upsertQuery = ""
  53. }
  54. store.SqlGenerator = &SqlGenPostgres{
  55. CreateTableSqlTemplate: "",
  56. DropTableSqlTemplate: `drop table "%s"`,
  57. UpsertQueryTemplate: upsertQuery,
  58. }
  59. // pgx-optimized connection string with better timeouts and connection handling
  60. sqlUrl := "connect_timeout=30"
  61. // PgBouncer compatibility: add prefer_simple_protocol=true when needed
  62. // This avoids prepared statement issues with PgBouncer's transaction pooling mode
  63. if pgbouncerCompatible {
  64. sqlUrl += " prefer_simple_protocol=true"
  65. }
  66. if hostname != "" {
  67. sqlUrl += " host=" + hostname
  68. }
  69. if port != 0 {
  70. sqlUrl += " port=" + strconv.Itoa(port)
  71. }
  72. // SSL configuration - pgx provides better SSL support than lib/pq
  73. if sslmode != "" {
  74. sqlUrl += " sslmode=" + sslmode
  75. }
  76. if sslcert != "" {
  77. sqlUrl += " sslcert=" + sslcert
  78. }
  79. if sslkey != "" {
  80. sqlUrl += " sslkey=" + sslkey
  81. }
  82. if sslrootcert != "" {
  83. sqlUrl += " sslrootcert=" + sslrootcert
  84. }
  85. if sslcrl != "" {
  86. sqlUrl += " sslcrl=" + sslcrl
  87. }
  88. if user != "" {
  89. sqlUrl += " user=" + user
  90. }
  91. adaptedSqlUrl := sqlUrl
  92. if password != "" {
  93. sqlUrl += " password=" + password
  94. adaptedSqlUrl += " password=ADAPTED"
  95. }
  96. if database != "" {
  97. sqlUrl += " dbname=" + database
  98. adaptedSqlUrl += " dbname=" + database
  99. }
  100. if schema != "" && !pgbouncerCompatible {
  101. sqlUrl += " search_path=" + schema
  102. adaptedSqlUrl += " search_path=" + schema
  103. }
  104. var dbErr error
  105. store.DB, dbErr = sql.Open("pgx", sqlUrl)
  106. if dbErr != nil {
  107. if store.DB != nil {
  108. store.DB.Close()
  109. }
  110. store.DB = nil
  111. return fmt.Errorf("can not connect to %s error:%v", adaptedSqlUrl, dbErr)
  112. }
  113. store.DB.SetMaxIdleConns(maxIdle)
  114. store.DB.SetMaxOpenConns(maxOpen)
  115. store.DB.SetConnMaxLifetime(time.Duration(maxLifetimeSeconds) * time.Second)
  116. if err = store.DB.Ping(); err != nil {
  117. return fmt.Errorf("connect to %s error:%v", adaptedSqlUrl, err)
  118. }
  119. return nil
  120. }