Add PrepareStmtAutoSave option to enable PostgreSQL autosave_prepared_statements#7641
Add PrepareStmtAutoSave option to enable PostgreSQL autosave_prepared_statements#7641miladev95 wants to merge 2 commits intogo-gorm:masterfrom
Conversation
…d_statements - Add PrepareStmtAutoSave configuration field to Config struct (*bool type) - Support nil (disabled), true, and false values - PostgreSQL will automatically invalidate cached prepared statements on schema changes - Only applicable for PostgreSQL databases The autosave_prepared_statements parameter helps prevent errors when schema changes occur while prepared statements are cached. When enabled, PostgreSQL invalidates the cached statements automatically instead of causing statement execution errors.
|
Add Introduces a tri-state Key Changes• Added Affected Areas• This summary was automatically generated by @propel-code-bot |
| } | ||
| } | ||
|
|
||
| if err == nil && config.PrepareStmtAutoSave != nil { |
There was a problem hiding this comment.
[BestPractice]
The configureAutoSave method is called only when PrepareStmtAutoSave is non-nil, but there's no validation that PrepareStmt is enabled. The autosave_prepared_statements parameter only applies when prepared statements are being used. If PrepareStmt is false but PrepareStmtAutoSave is set, this configuration will have no effect and may be confusing.
| if err == nil && config.PrepareStmtAutoSave != nil { | |
| if err == nil && config.PrepareStmt && config.PrepareStmtAutoSave != nil { | |
| err = db.configureAutoSave(context.Background()) | |
| } |
⚡ Committable suggestion
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
Context for Agents
[**BestPractice**]
The `configureAutoSave` method is called only when `PrepareStmtAutoSave` is non-nil, but there's no validation that `PrepareStmt` is enabled. The `autosave_prepared_statements` parameter only applies when prepared statements are being used. If `PrepareStmt` is false but `PrepareStmtAutoSave` is set, this configuration will have no effect and may be confusing.
```suggestion
if err == nil && config.PrepareStmt && config.PrepareStmtAutoSave != nil {
err = db.configureAutoSave(context.Background())
}
```
⚡ **Committable suggestion**
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
File: gorm.go
Line: 247|
|
||
| // Execute SET command on the connection | ||
| sql := fmt.Sprintf("SET autosave_prepared_statements = %v", value) | ||
| if err := db.Exec(sql).Error; err != nil { |
There was a problem hiding this comment.
[BestPractice]
Connection-level vs session-level configuration: Using db.Exec() to set PostgreSQL parameters only affects the current connection/session. If GORM uses a connection pool, different connections may not have this setting applied, leading to inconsistent behavior.
Consider setting this parameter in the PostgreSQL connection string instead, or document that this setting only applies to the current session.
Context for Agents
[**BestPractice**]
Connection-level vs session-level configuration: Using `db.Exec()` to set PostgreSQL parameters only affects the current connection/session. If GORM uses a connection pool, different connections may not have this setting applied, leading to inconsistent behavior.
Consider setting this parameter in the PostgreSQL connection string instead, or document that this setting only applies to the current session.
File: gorm.go
Line: 574
This PR covers #7629 feature request.
What did this pull request do?
Add PrepareStmtAutoSave Support for PostgreSQL
Overview
Introduces new
PrepareStmtAutoSaveconfiguration option to enable PostgreSQL'sautosave_prepared_statementsparameter. This helps prevent prepared statement cache invalidation errors when schema changes occur during runtime.Problem Statement
When using prepared statements with GORM:
Solution
Enable PostgreSQL's
autosave_prepared_statementsparameter to automatically invalidate cached statements when schema changes, preventing execution errors.Changes
New Config Field:
PrepareStmtAutoSave *boolnil= disabled (default, no configuration)true= enable autosavefalse= explicitly disable autosaveNew Method:
configureAutoSave(ctx context.Context) errorSET autosave_prepared_statements = true/falseon PostgreSQL connectionPrepareStmtis enabledUpdated:
Open()function to callconfigureAutoSave()during initializationUsage Example