-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Add PrepareStmtAutoSave option to enable PostgreSQL autosave_prepared_statements #7641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,11 @@ | |
| // default maxsize=int64 Max value and ttl=1h | ||
| PrepareStmtMaxSize int | ||
| PrepareStmtTTL time.Duration | ||
| // PrepareStmtAutoSave sets autosave_prepared_statements for PostgreSQL | ||
| // Valid values: true or false | ||
| // This helps handle prepared statement cache invalidation when schema changes | ||
| // Only applicable for PostgreSQL | ||
| PrepareStmtAutoSave *bool | ||
|
|
||
| // DisableAutomaticPing | ||
| DisableAutomaticPing bool | ||
|
|
@@ -239,6 +244,10 @@ | |
| } | ||
| } | ||
|
|
||
| if err == nil && config.PrepareStmtAutoSave != nil { | ||
| err = db.configureAutoSave(context.Background()) | ||
| } | ||
|
|
||
| if err != nil { | ||
| config.Logger.Error(context.Background(), "failed to initialize database, got error %v", err) | ||
| } | ||
|
|
@@ -543,3 +552,28 @@ | |
|
|
||
| return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...) | ||
| } | ||
|
|
||
| // configureAutoSave sets the autosave_prepared_statements parameter for PostgreSQL | ||
| // This helps handle prepared statement cache invalidation when schema changes | ||
| // Only applicable for PostgreSQL databases | ||
| func (db *DB) configureAutoSave(ctx context.Context) error { | ||
propel-code-bot[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if db.PrepareStmtAutoSave == nil { | ||
| return nil | ||
| } | ||
|
|
||
| // Only PostgreSQL supports autosave_prepared_statements parameter | ||
| if db.Dialector.Name() != "postgres" { | ||
| db.Logger.Warn(ctx, "PrepareStmtAutoSave is only supported for PostgreSQL, skipping for %s", db.Dialector.Name()) | ||
| return nil | ||
| } | ||
|
|
||
| value := *db.PrepareStmtAutoSave | ||
|
|
||
| // Execute SET command on the connection | ||
| sql := fmt.Sprintf("SET autosave_prepared_statements = %v", value) | ||
| if err := db.Exec(sql).Error; err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [BestPractice] Connection-level vs session-level configuration: Using Consider setting this parameter in the PostgreSQL connection string instead, or document that this setting only applies to the current session. Context for Agents |
||
| return fmt.Errorf("failed to set autosave_prepared_statements on PostgreSQL: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[BestPractice]
The
configureAutoSavemethod is called only whenPrepareStmtAutoSaveis non-nil, but there's no validation thatPrepareStmtis enabled. Theautosave_prepared_statementsparameter only applies when prepared statements are being used. IfPrepareStmtis false butPrepareStmtAutoSaveis set, this configuration will have no effect and may be confusing.⚡ 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