mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-16 12:58:00 +00:00
a84b259e8c
# Which Problems Are Solved Optimize the query that checks for terminated sessions in the access token verifier. The verifier is used in auth middleware, userinfo and introspection. # How the Problems Are Solved The previous implementation built a query for certain events and then appended a single `PositionAfter` clause. This caused the postgreSQL planner to use indexes only for the instance ID, aggregate IDs, aggregate types and event types. Followed by an expensive sequential scan for the position. This resulting in internal over-fetching of rows before the final filter was applied. ![Screenshot_20241007_105803](https://github.com/user-attachments/assets/f2d91976-be87-428b-b604-a211399b821c) Furthermore, the query was searching for events which are not always applicable. For example, there was always a session ID search and if there was a user ID, we would also search for a browser fingerprint in event payload (expensive). Even if those argument string would be empty. This PR changes: 1. Nest the position query, so that a full `instance_id, aggregate_id, aggregate_type, event_type, "position"` index can be matched. 2. Redefine the `es_wm` index to include the `position` column. 3. Only search for events for the IDs that actually have a value. Do not search (noop) if none of session ID, user ID or fingerpint ID are set. New query plan: ![Screenshot_20241007_110648](https://github.com/user-attachments/assets/c3234c33-1b76-4b33-a4a9-796f69f3d775) # Additional Changes - cleanup how we load multi-statement migrations and make that a bit more reusable. # Additional Context - Related to https://github.com/zitadel/zitadel/issues/7639
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package setup
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"embed"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"github.com/zitadel/logging"
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
)
|
|
|
|
var (
|
|
//go:embed 14/cockroach/*.sql
|
|
//go:embed 14/postgres/*.sql
|
|
newEventsTable embed.FS
|
|
)
|
|
|
|
type NewEventsTable struct {
|
|
dbClient *database.DB
|
|
}
|
|
|
|
func (mig *NewEventsTable) Execute(ctx context.Context, _ eventstore.Event) error {
|
|
// if events already exists events2 is created during a setup job
|
|
var count int
|
|
err := mig.dbClient.QueryRowContext(ctx,
|
|
func(row *sql.Row) error {
|
|
if err := row.Scan(&count); err != nil {
|
|
return err
|
|
}
|
|
return row.Err()
|
|
},
|
|
"SELECT count(*) FROM information_schema.tables WHERE table_schema = 'eventstore' AND table_name like 'events2'",
|
|
)
|
|
if err != nil || count == 1 {
|
|
return err
|
|
}
|
|
|
|
statements, err := readStatements(newEventsTable, "14", mig.dbClient.Type())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, stmt := range statements {
|
|
stmt.query = strings.ReplaceAll(stmt.query, "{{.username}}", mig.dbClient.Username())
|
|
logging.WithFields("file", stmt.file, "migration", mig.String()).Info("execute statement")
|
|
_, err = mig.dbClient.ExecContext(ctx, stmt.query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (mig *NewEventsTable) String() string {
|
|
return "14_events_push"
|
|
}
|
|
|
|
func (mig *NewEventsTable) ContinueOnErr(err error) bool {
|
|
pgErr := new(pgconn.PgError)
|
|
if errors.As(err, &pgErr) {
|
|
return pgErr.Code == "42P01"
|
|
}
|
|
return false
|
|
}
|