|
5 | 5 | "strings" |
6 | 6 | "time" |
7 | 7 |
|
| 8 | + "golang.org/x/exp/slog" |
| 9 | + |
8 | 10 | "github.com/jackc/pgx/v5" |
9 | 11 |
|
10 | 12 | "github.com/jackc/pgx/v5/pgxpool" |
@@ -57,6 +59,8 @@ func New(uri string, opts ...Option) (*Postgres, error) { |
57 | 59 |
|
58 | 60 | setDefaultQueryExecMode(writeConfig.ConnConfig) |
59 | 61 | setDefaultQueryExecMode(readConfig.ConnConfig) |
| 62 | + setPlanCacheMode(writeConfig.ConnConfig) |
| 63 | + setPlanCacheMode(readConfig.ConnConfig) |
60 | 64 |
|
61 | 65 | writeConfig.MinConns = int32(pg.maxIdleConnections) |
62 | 66 | readConfig.MinConns = int32(pg.maxIdleConnections) |
@@ -138,10 +142,35 @@ func setDefaultQueryExecMode(config *pgx.ConnConfig) { |
138 | 142 | for key := range queryExecModes { |
139 | 143 | if strings.Contains(config.ConnString(), "default_query_exec_mode="+key) { |
140 | 144 | config.DefaultQueryExecMode = queryExecModes[key] |
| 145 | + slog.Info("setDefaultQueryExecMode", slog.String("mode", key)) |
141 | 146 | return |
142 | 147 | } |
143 | 148 | } |
144 | 149 |
|
145 | 150 | // Set to default mode if no matching mode is found |
146 | 151 | config.DefaultQueryExecMode = queryExecModes[defaultMode] |
| 152 | + slog.Warn("setDefaultQueryExecMode", slog.String("mode", defaultMode)) |
| 153 | +} |
| 154 | + |
| 155 | +var planCacheModes = map[string]string{ |
| 156 | + "auto": "auto", |
| 157 | + "force_custom_plan": "force_custom_plan", |
| 158 | +} |
| 159 | + |
| 160 | +func setPlanCacheMode(config *pgx.ConnConfig) { |
| 161 | + // Default mode if no specific mode is found in the connection string |
| 162 | + defaultMode := "auto" |
| 163 | + |
| 164 | + // Check if a plan cache mode is mentioned in the connection string and set it |
| 165 | + for key := range planCacheModes { |
| 166 | + if strings.Contains(config.ConnString(), "plan_cache_mode="+key) { |
| 167 | + config.Config.RuntimeParams["plan_cache_mode"] = planCacheModes[key] |
| 168 | + slog.Info("setPlanCacheMode", slog.String("mode", key)) |
| 169 | + return |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // Set to default mode if no matching mode is found |
| 174 | + config.Config.RuntimeParams["plan_cache_mode"] = planCacheModes[defaultMode] |
| 175 | + slog.Warn("setPlanCacheMode", slog.String("mode", defaultMode)) |
147 | 176 | } |
0 commit comments