mirror of
https://github.com/zitadel/zitadel.git
synced 2025-01-11 11:53:40 +00:00
df2c6f1d4c
# Which Problems Are Solved We were seeing high query costs in a the lateral join executed in the commands_to_events procedural function in the database. The high cost resulted in incremental CPU usage as a load test continued and less req/sec handled, sarting at 836 and ending at 130 req/sec. # How the Problems Are Solved 1. Set `PARALLEL SAFE`. I noticed that this option defaults to `UNSAFE`. But it's actually safe if the function doesn't `INSERT` 2. Set the returned `ROWS 10` parameter. 3. Function is re-written in Pl/PgSQL so that we eliminate expensive joins. 4. Introduced an intermediate state that does `SELECT DISTINCT` for the aggregate so that we don't have to do an expensive lateral join. # Additional Changes Use a `COALESCE` to get the owner from the last event, instead of a `CASE` switch. # Additional Context - Function was introduced in https://github.com/zitadel/zitadel/pull/8816 - Closes https://github.com/zitadel/zitadel/issues/8352 --------- Co-authored-by: Silvan <27845747+adlerhurst@users.noreply.github.com>
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package setup
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"fmt"
|
|
|
|
"github.com/zitadel/logging"
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
)
|
|
|
|
var (
|
|
//go:embed 40/cockroach/*.sql
|
|
//go:embed 40/postgres/*.sql
|
|
initPushFunc embed.FS
|
|
)
|
|
|
|
type InitPushFunc struct {
|
|
dbClient *database.DB
|
|
}
|
|
|
|
func (mig *InitPushFunc) Execute(ctx context.Context, _ eventstore.Event) (err error) {
|
|
statements, err := readStatements(initPushFunc, "40", mig.dbClient.Type())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
conn, err := mig.dbClient.Conn(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
closeErr := conn.Close()
|
|
logging.OnError(closeErr).Debug("failed to release connection")
|
|
// Force the pool to reopen connections to apply the new types
|
|
mig.dbClient.Pool.Reset()
|
|
}()
|
|
|
|
for _, stmt := range statements {
|
|
logging.WithFields("file", stmt.file, "migration", mig.String()).Info("execute statement")
|
|
if _, err := conn.ExecContext(ctx, stmt.query); err != nil {
|
|
return fmt.Errorf("%s %s: %w", mig.String(), stmt.file, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (mig *InitPushFunc) String() string {
|
|
return "40_init_push_func_v2"
|
|
}
|