new - Creates new postgresql db instance
(writerUri, readerUri string, opts ...Option)
| 63 | |
| 64 | // new - Creates new postgresql db instance |
| 65 | func newDB(writerUri, readerUri string, opts ...Option) (*Postgres, error) { |
| 66 | pg := &Postgres{ |
| 67 | maxConnections: _defaultMaxConnections, |
| 68 | maxIdleConnections: _defaultMaxIdleConnections, |
| 69 | minConnections: _defaultMinConnections, |
| 70 | minIdleConnections: _defaultMinIdleConnections, |
| 71 | maxDataPerWrite: _defaultMaxDataPerWrite, |
| 72 | maxRetries: _defaultMaxRetries, |
| 73 | watchBufferSize: _defaultWatchBufferSize, |
| 74 | healthCheckPeriod: _defaultHealthCheckPeriod, |
| 75 | maxConnectionLifetimeJitter: _defaultMaxConnectionLifetimeJitter, |
| 76 | connectTimeout: _defaultConnectTimeout, |
| 77 | } |
| 78 | |
| 79 | // Custom options |
| 80 | for _, opt := range opts { |
| 81 | opt(pg) |
| 82 | } |
| 83 | |
| 84 | pg.Builder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar) |
| 85 | |
| 86 | writeConfig, err := pgxpool.ParseConfig(writerUri) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | |
| 91 | readConfig, err := pgxpool.ParseConfig(readerUri) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | // Set the default execution mode for queries using the write and read configurations. |
| 97 | setDefaultQueryExecMode(writeConfig.ConnConfig) |
| 98 | setDefaultQueryExecMode(readConfig.ConnConfig) |
| 99 | |
| 100 | // Set the plan cache mode for both write and read configurations to optimize query planning. |
| 101 | setPlanCacheMode(writeConfig.ConnConfig) |
| 102 | setPlanCacheMode(readConfig.ConnConfig) |
| 103 | |
| 104 | // Set the minimum number of connections in the pool for both write and read configurations. |
| 105 | // For backward compatibility: if MinConnections is not set (0) but MaxIdleConnections is set, use MaxIdleConnections (old behavior). |
| 106 | minConns := pg.minConnections |
| 107 | if minConns == 0 && pg.maxIdleConnections > 0 { |
| 108 | minConns = pg.maxIdleConnections |
| 109 | } |
| 110 | if minConns > 0 { |
| 111 | writeConfig.MinConns = int32(minConns) |
| 112 | readConfig.MinConns = int32(minConns) |
| 113 | } |
| 114 | |
| 115 | // Set the minimum number of idle connections in the pool. |
| 116 | // Note: MinIdleConnections was not set in the old code, so we only set it if explicitly configured. |
| 117 | if pg.minIdleConnections > 0 { |
| 118 | writeConfig.MinIdleConns = int32(pg.minIdleConnections) |
| 119 | readConfig.MinIdleConns = int32(pg.minIdleConnections) |
| 120 | } |
| 121 | |
| 122 | // Set the maximum number of connections in the pool for both write and read configurations. |
no test coverage detected