Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -239,6 +244,10 @@
}
}

if err == nil && config.PrepareStmtAutoSave != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Suggested change
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

err = db.configureAutoSave(context.Background())
}

if err != nil {
config.Logger.Error(context.Background(), "failed to initialize database, got error %v", err)
}
Expand Down Expand Up @@ -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 {
if db.PrepareStmtAutoSave == nil {
return nil
}

// Only PostgreSQL supports autosave_prepared_statements parameter
if db.Dialector.Name() != "postgres" {

Check failure on line 565 in gorm.go

View workflow job for this annotation

GitHub Actions / lint

QF1008: could remove embedded field "Dialector" from selector (staticcheck)
db.Logger.Warn(ctx, "PrepareStmtAutoSave is only supported for PostgreSQL, skipping for %s", db.Dialector.Name())

Check failure on line 566 in gorm.go

View workflow job for this annotation

GitHub Actions / lint

QF1008: could remove embedded field "Dialector" from selector (staticcheck)
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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

return fmt.Errorf("failed to set autosave_prepared_statements on PostgreSQL: %w", err)
}

return nil
}
Loading