postgres2_store.go 4.3 KB

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