2022-09-12 17:18:08 +01:00
|
|
|
package saml
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2024-04-15 12:17:36 +03:00
|
|
|
"github.com/go-jose/go-jose/v4"
|
2022-09-12 17:18:08 +01:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
"github.com/zitadel/saml/pkg/provider/key"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
2022-10-20 13:36:52 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-09-12 17:18:08 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/keypair"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-09-12 17:18:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
locksTable = "projections.locks"
|
|
|
|
signingKey = "signing_key"
|
|
|
|
samlUser = "SAML"
|
|
|
|
|
|
|
|
retryBackoff = 500 * time.Millisecond
|
|
|
|
retryCount = 3
|
|
|
|
lockDuration = retryCount * retryBackoff * 5
|
|
|
|
gracefulPeriod = 10 * time.Minute
|
|
|
|
)
|
|
|
|
|
|
|
|
type CertificateAndKey struct {
|
|
|
|
algorithm jose.SignatureAlgorithm
|
|
|
|
id string
|
|
|
|
key interface{}
|
|
|
|
certificate interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CertificateAndKey) SignatureAlgorithm() jose.SignatureAlgorithm {
|
|
|
|
return c.algorithm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CertificateAndKey) Key() interface{} {
|
|
|
|
return c.key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CertificateAndKey) Certificate() interface{} {
|
|
|
|
return c.certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CertificateAndKey) ID() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
2024-08-14 17:18:14 +03:00
|
|
|
func (p *Storage) GetCertificateAndKey(ctx context.Context, usage crypto.KeyUsage) (certAndKey *key.CertificateAndKey, err error) {
|
2022-09-12 17:18:08 +01:00
|
|
|
err = retry(func() error {
|
|
|
|
certAndKey, err = p.getCertificateAndKey(ctx, usage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if certAndKey == nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return zerrors.ThrowInternal(err, "SAML-8u01nks", "no certificate found")
|
2022-09-12 17:18:08 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return certAndKey, err
|
|
|
|
}
|
|
|
|
|
2024-08-14 17:18:14 +03:00
|
|
|
func (p *Storage) getCertificateAndKey(ctx context.Context, usage crypto.KeyUsage) (*key.CertificateAndKey, error) {
|
2022-09-12 17:18:08 +01:00
|
|
|
certs, err := p.query.ActiveCertificates(ctx, time.Now().Add(gracefulPeriod), usage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(certs.Certificates) > 0 {
|
|
|
|
return p.certificateToCertificateAndKey(selectCertificate(certs.Certificates))
|
|
|
|
}
|
|
|
|
|
2024-09-24 19:43:29 +03:00
|
|
|
var position float64
|
2023-10-19 12:19:10 +02:00
|
|
|
if certs.State != nil {
|
|
|
|
position = certs.State.Position
|
2022-09-12 17:18:08 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
return nil, p.refreshCertificate(ctx, usage, position)
|
2022-09-12 17:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Storage) refreshCertificate(
|
|
|
|
ctx context.Context,
|
2024-08-14 17:18:14 +03:00
|
|
|
usage crypto.KeyUsage,
|
2024-09-24 19:43:29 +03:00
|
|
|
position float64,
|
2022-09-12 17:18:08 +01:00
|
|
|
) error {
|
2023-10-19 12:19:10 +02:00
|
|
|
ok, err := p.ensureIsLatestCertificate(ctx, position)
|
2022-09-12 17:18:08 +01:00
|
|
|
if err != nil {
|
|
|
|
logging.WithError(err).Error("could not ensure latest key")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
logging.Warn("view not up to date, retrying later")
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
err = p.lockAndGenerateCertificateAndKey(ctx, usage)
|
2022-09-12 17:18:08 +01:00
|
|
|
logging.OnError(err).Warn("could not create signing key")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-24 19:43:29 +03:00
|
|
|
func (p *Storage) ensureIsLatestCertificate(ctx context.Context, position float64) (bool, error) {
|
2022-09-12 17:18:08 +01:00
|
|
|
maxSequence, err := p.getMaxKeySequence(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error retrieving new events: %w", err)
|
|
|
|
}
|
2024-09-24 19:43:29 +03:00
|
|
|
return position >= maxSequence, nil
|
2022-09-12 17:18:08 +01:00
|
|
|
}
|
|
|
|
|
2024-08-14 17:18:14 +03:00
|
|
|
func (p *Storage) lockAndGenerateCertificateAndKey(ctx context.Context, usage crypto.KeyUsage) error {
|
2022-09-12 17:18:08 +01:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
ctx = setSAMLCtx(ctx)
|
|
|
|
|
|
|
|
errs := p.locker.Lock(ctx, lockDuration, authz.GetInstance(ctx).InstanceID())
|
|
|
|
err, ok := <-errs
|
|
|
|
if err != nil || !ok {
|
2023-12-08 16:30:55 +02:00
|
|
|
if zerrors.IsErrorAlreadyExists(err) {
|
2022-09-12 17:18:08 +01:00
|
|
|
return nil
|
|
|
|
}
|
2023-04-25 19:20:59 +02:00
|
|
|
logging.OnError(err).Debug("initial lock failed")
|
2022-09-12 17:18:08 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch usage {
|
2024-08-14 17:18:14 +03:00
|
|
|
case crypto.KeyUsageSAMLMetadataSigning, crypto.KeyUsageSAMLResponseSinging:
|
|
|
|
certAndKey, err := p.GetCertificateAndKey(ctx, crypto.KeyUsageSAMLCA)
|
2022-09-12 17:18:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while reading ca certificate: %w", err)
|
|
|
|
}
|
|
|
|
if certAndKey.Key == nil || certAndKey.Certificate == nil {
|
|
|
|
return fmt.Errorf("has no ca certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch usage {
|
2024-08-14 17:18:14 +03:00
|
|
|
case crypto.KeyUsageSAMLMetadataSigning:
|
2022-09-12 17:18:08 +01:00
|
|
|
return p.command.GenerateSAMLMetadataCertificate(setSAMLCtx(ctx), p.certificateAlgorithm, certAndKey.Key, certAndKey.Certificate)
|
2024-08-14 17:18:14 +03:00
|
|
|
case crypto.KeyUsageSAMLResponseSinging:
|
2022-09-12 17:18:08 +01:00
|
|
|
return p.command.GenerateSAMLResponseCertificate(setSAMLCtx(ctx), p.certificateAlgorithm, certAndKey.Key, certAndKey.Certificate)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown usage")
|
|
|
|
}
|
2024-08-14 17:18:14 +03:00
|
|
|
case crypto.KeyUsageSAMLCA:
|
2022-09-12 17:18:08 +01:00
|
|
|
return p.command.GenerateSAMLCACertificate(setSAMLCtx(ctx), p.certificateAlgorithm)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown certificate usage")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-24 19:43:29 +03:00
|
|
|
func (p *Storage) getMaxKeySequence(ctx context.Context) (float64, error) {
|
|
|
|
return p.eventstore.LatestSequence(ctx,
|
|
|
|
eventstore.NewSearchQueryBuilder(eventstore.ColumnsMaxSequence).
|
2022-09-12 17:18:08 +01:00
|
|
|
ResourceOwner(authz.GetInstance(ctx).InstanceID()).
|
2023-10-19 12:19:10 +02:00
|
|
|
AwaitOpenTransactions().
|
2022-09-12 17:18:08 +01:00
|
|
|
AddQuery().
|
fix(perf): simplify eventstore queries by removing or in projection handlers (#9530)
# Which Problems Are Solved
[A recent performance
enhancement]((https://github.com/zitadel/zitadel/pull/9497)) aimed at
optimizing event store queries, specifically those involving multiple
aggregate type filters, has successfully improved index utilization.
While the query planner now correctly selects relevant indexes, it
employs [bitmap index
scans](https://www.postgresql.org/docs/current/indexes-bitmap-scans.html)
to retrieve data.
This approach, while beneficial in many scenarios, introduces a
potential I/O bottleneck. The bitmap index scan first identifies the
required database blocks and then utilizes a bitmap to access the
corresponding rows from the table's heap. This subsequent "bitmap heap
scan" can result in significant I/O overhead, particularly when queries
return a substantial number of rows across numerous data pages.
## Impact:
Under heavy load or with queries filtering for a wide range of events
across multiple aggregate types, this increased I/O activity may lead
to:
- Increased query latency.
- Elevated disk utilization.
- Potential performance degradation of the event store and dependent
systems.
# How the Problems Are Solved
To address this I/O bottleneck and further optimize query performance,
the projection handler has been modified. Instead of employing multiple
OR clauses for each aggregate type, the aggregate and event type filters
are now combined using IN ARRAY filters.
Technical Details:
This change allows the PostgreSQL query planner to leverage [index-only
scans](https://www.postgresql.org/docs/current/indexes-index-only-scans.html).
By utilizing IN ARRAY filters, the database can efficiently retrieve the
necessary data directly from the index, eliminating the need to access
the table's heap. This results in:
* Reduced I/O: Index-only scans significantly minimize disk I/O
operations, as the database avoids reading data pages from the main
table.
* Improved Query Performance: By reducing I/O, query execution times are
substantially improved, leading to lower latency.
# Additional Changes
- rollback of https://github.com/zitadel/zitadel/pull/9497
# Additional Information
## Query Plan of previous query
```sql
SELECT
created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision
FROM
eventstore.events2
WHERE
instance_id = '<INSTANCE_ID>'
AND (
(
instance_id = '<INSTANCE_ID>'
AND "position" > <POSITION>
AND aggregate_type = 'project'
AND event_type = ANY(ARRAY[
'project.application.added'
,'project.application.changed'
,'project.application.deactivated'
,'project.application.reactivated'
,'project.application.removed'
,'project.removed'
,'project.application.config.api.added'
,'project.application.config.api.changed'
,'project.application.config.api.secret.changed'
,'project.application.config.api.secret.updated'
,'project.application.config.oidc.added'
,'project.application.config.oidc.changed'
,'project.application.config.oidc.secret.changed'
,'project.application.config.oidc.secret.updated'
,'project.application.config.saml.added'
,'project.application.config.saml.changed'
])
) OR (
instance_id = '<INSTANCE_ID>'
AND "position" > <POSITION>
AND aggregate_type = 'org'
AND event_type = 'org.removed'
) OR (
instance_id = '<INSTANCE_ID>'
AND "position" > <POSITION>
AND aggregate_type = 'instance'
AND event_type = 'instance.removed'
)
)
AND "position" > 1741600905.3495
AND "position" < (
SELECT
COALESCE(EXTRACT(EPOCH FROM min(xact_start)), EXTRACT(EPOCH FROM now()))
FROM
pg_stat_activity
WHERE
datname = current_database()
AND application_name = ANY(ARRAY['zitadel_es_pusher_', 'zitadel_es_pusher', 'zitadel_es_pusher_<INSTANCE_ID>'])
AND state <> 'idle'
)
ORDER BY "position", in_tx_order LIMIT 200 OFFSET 1;
```
```
Limit (cost=120.08..120.09 rows=7 width=361) (actual time=2.167..2.172 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
InitPlan 1
-> Aggregate (cost=2.74..2.76 rows=1 width=32) (actual time=1.813..1.815 rows=1 loops=1)
Output: COALESCE(EXTRACT(epoch FROM min(s.xact_start)), EXTRACT(epoch FROM now()))
-> Nested Loop (cost=0.00..2.74 rows=1 width=8) (actual time=1.803..1.805 rows=0 loops=1)
Output: s.xact_start
Join Filter: (d.oid = s.datid)
-> Seq Scan on pg_catalog.pg_database d (cost=0.00..1.07 rows=1 width=4) (actual time=0.016..0.021 rows=1 loops=1)
Output: d.oid, d.datname, d.datdba, d.encoding, d.datlocprovider, d.datistemplate, d.datallowconn, d.dathasloginevt, d.datconnlimit, d.datfrozenxid, d.datminmxid, d.dattablespace, d.datcollate, d.datctype, d.datlocale, d.daticurules, d.datcollversion, d.datacl
Filter: (d.datname = current_database())
Rows Removed by Filter: 4
-> Function Scan on pg_catalog.pg_stat_get_activity s (cost=0.00..1.63 rows=3 width=16) (actual time=1.781..1.781 rows=0 loops=1)
Output: s.datid, s.pid, s.usesysid, s.application_name, s.state, s.query, s.wait_event_type, s.wait_event, s.xact_start, s.query_start, s.backend_start, s.state_change, s.client_addr, s.client_hostname, s.client_port, s.backend_xid, s.backend_xmin, s.backend_type, s.ssl, s.sslversion, s.sslcipher, s.sslbits, s.ssl_client_dn, s.ssl_client_serial, s.ssl_issuer_dn, s.gss_auth, s.gss_princ, s.gss_enc, s.gss_delegation, s.leader_pid, s.query_id
Function Call: pg_stat_get_activity(NULL::integer)
Filter: ((s.state <> 'idle'::text) AND (s.application_name = ANY ('{zitadel_es_pusher_,zitadel_es_pusher,zitadel_es_pusher_<INSTANCE_ID>}'::text[])))
Rows Removed by Filter: 49
-> Sort (cost=117.31..117.33 rows=8 width=361) (actual time=2.167..2.168 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
Sort Key: events2."position", events2.in_tx_order
Sort Method: quicksort Memory: 25kB
-> Bitmap Heap Scan on eventstore.events2 (cost=84.92..117.19 rows=8 width=361) (actual time=2.088..2.089 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
Recheck Cond: (((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'project'::text) AND (events2.event_type = ANY ('{project.application.added,project.application.changed,project.application.deactivated,project.application.reactivated,project.application.removed,project.removed,project.application.config.api.added,project.application.config.api.changed,project.application.config.api.secret.changed,project.application.config.api.secret.updated,project.application.config.oidc.added,project.application.config.oidc.changed,project.application.config.oidc.secret.changed,project.application.config.oidc.secret.updated,project.application.config.saml.added,project.application.config.saml.changed}'::text[])) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1)) OR ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'org'::text) AND (events2.event_type = 'org.removed'::text) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1)) OR ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'instance'::text) AND (events2.event_type = 'instance.removed'::text) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1)))
-> BitmapOr (cost=84.88..84.88 rows=8 width=0) (actual time=2.080..2.081 rows=0 loops=1)
-> Bitmap Index Scan on es_projection (cost=0.00..75.44 rows=8 width=0) (actual time=2.016..2.017 rows=0 loops=1)
Index Cond: ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'project'::text) AND (events2.event_type = ANY ('{project.application.added,project.application.changed,project.application.deactivated,project.application.reactivated,project.application.removed,project.removed,project.application.config.api.added,project.application.config.api.changed,project.application.config.api.secret.changed,project.application.config.api.secret.updated,project.application.config.oidc.added,project.application.config.oidc.changed,project.application.config.oidc.secret.changed,project.application.config.oidc.secret.updated,project.application.config.saml.added,project.application.config.saml.changed}'::text[])) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1))
-> Bitmap Index Scan on es_projection (cost=0.00..4.71 rows=1 width=0) (actual time=0.016..0.016 rows=0 loops=1)
Index Cond: ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'org'::text) AND (events2.event_type = 'org.removed'::text) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1))
-> Bitmap Index Scan on es_projection (cost=0.00..4.71 rows=1 width=0) (actual time=0.045..0.045 rows=0 loops=1)
Index Cond: ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'instance'::text) AND (events2.event_type = 'instance.removed'::text) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1))
Query Identifier: 3194938266011254479
Planning Time: 1.295 ms
Execution Time: 2.832 ms
```
## Query Plan of new query
```sql
SELECT
created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision
FROM
eventstore.events2
WHERE
instance_id = '<INSTANCE_ID>'
AND "position" > <POSITION>
AND aggregate_type = ANY(ARRAY['project', 'instance', 'org'])
AND event_type = ANY(ARRAY[
'project.application.added'
,'project.application.changed'
,'project.application.deactivated'
,'project.application.reactivated'
,'project.application.removed'
,'project.removed'
,'project.application.config.api.added'
,'project.application.config.api.changed'
,'project.application.config.api.secret.changed'
,'project.application.config.api.secret.updated'
,'project.application.config.oidc.added'
,'project.application.config.oidc.changed'
,'project.application.config.oidc.secret.changed'
,'project.application.config.oidc.secret.updated'
,'project.application.config.saml.added'
,'project.application.config.saml.changed'
,'org.removed'
,'instance.removed'
])
AND "position" < (
SELECT
COALESCE(EXTRACT(EPOCH FROM min(xact_start)), EXTRACT(EPOCH FROM now()))
FROM
pg_stat_activity
WHERE
datname = current_database()
AND application_name = ANY(ARRAY['zitadel_es_pusher_', 'zitadel_es_pusher', 'zitadel_es_pusher_<INSTANCE_ID>'])
AND state <> 'idle'
)
ORDER BY "position", in_tx_order LIMIT 200 OFFSET 1;
```
```
Limit (cost=293.34..293.36 rows=8 width=361) (actual time=4.686..4.689 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
InitPlan 1
-> Aggregate (cost=2.74..2.76 rows=1 width=32) (actual time=1.717..1.719 rows=1 loops=1)
Output: COALESCE(EXTRACT(epoch FROM min(s.xact_start)), EXTRACT(epoch FROM now()))
-> Nested Loop (cost=0.00..2.74 rows=1 width=8) (actual time=1.658..1.659 rows=0 loops=1)
Output: s.xact_start
Join Filter: (d.oid = s.datid)
-> Seq Scan on pg_catalog.pg_database d (cost=0.00..1.07 rows=1 width=4) (actual time=0.026..0.028 rows=1 loops=1)
Output: d.oid, d.datname, d.datdba, d.encoding, d.datlocprovider, d.datistemplate, d.datallowconn, d.dathasloginevt, d.datconnlimit, d.datfrozenxid, d.datminmxid, d.dattablespace, d.datcollate, d.datctype, d.datlocale, d.daticurules, d.datcollversion, d.datacl
Filter: (d.datname = current_database())
Rows Removed by Filter: 4
-> Function Scan on pg_catalog.pg_stat_get_activity s (cost=0.00..1.63 rows=3 width=16) (actual time=1.628..1.628 rows=0 loops=1)
Output: s.datid, s.pid, s.usesysid, s.application_name, s.state, s.query, s.wait_event_type, s.wait_event, s.xact_start, s.query_start, s.backend_start, s.state_change, s.client_addr, s.client_hostname, s.client_port, s.backend_xid, s.backend_xmin, s.backend_type, s.ssl, s.sslversion, s.sslcipher, s.sslbits, s.ssl_client_dn, s.ssl_client_serial, s.ssl_issuer_dn, s.gss_auth, s.gss_princ, s.gss_enc, s.gss_delegation, s.leader_pid, s.query_id
Function Call: pg_stat_get_activity(NULL::integer)
Filter: ((s.state <> 'idle'::text) AND (s.application_name = ANY ('{zitadel_es_pusher_,zitadel_es_pusher,zitadel_es_pusher_<INSTANCE_ID>}'::text[])))
Rows Removed by Filter: 42
-> Sort (cost=290.58..290.60 rows=9 width=361) (actual time=4.685..4.685 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
Sort Key: events2."position", events2.in_tx_order
Sort Method: quicksort Memory: 25kB
-> Index Scan using es_projection on eventstore.events2 (cost=0.70..290.43 rows=9 width=361) (actual time=4.616..4.617 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
Index Cond: ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = ANY ('{project,instance,org}'::text[])) AND (events2.event_type = ANY ('{project.application.added,project.application.changed,project.application.deactivated,project.application.reactivated,project.application.removed,project.removed,project.application.config.api.added,project.application.config.api.changed,project.application.config.api.secret.changed,project.application.config.api.secret.updated,project.application.config.oidc.added,project.application.config.oidc.changed,project.application.config.oidc.secret.changed,project.application.config.oidc.secret.updated,project.application.config.saml.added,project.application.config.saml.changed,org.removed,instance.removed}'::text[])) AND (events2."position" > <POSITION>) AND (events2."position" < (InitPlan 1).col1))
Query Identifier: -8254550537132386499
Planning Time: 2.864 ms
Execution Time: 5.414 ms
```
2025-03-13 16:50:23 +01:00
|
|
|
AggregateTypes(
|
|
|
|
keypair.AggregateType,
|
|
|
|
instance.AggregateType,
|
|
|
|
).
|
2023-10-19 12:19:10 +02:00
|
|
|
EventTypes(
|
|
|
|
keypair.AddedEventType,
|
|
|
|
keypair.AddedCertificateEventType,
|
fix(perf): simplify eventstore queries by removing or in projection handlers (#9530)
# Which Problems Are Solved
[A recent performance
enhancement]((https://github.com/zitadel/zitadel/pull/9497)) aimed at
optimizing event store queries, specifically those involving multiple
aggregate type filters, has successfully improved index utilization.
While the query planner now correctly selects relevant indexes, it
employs [bitmap index
scans](https://www.postgresql.org/docs/current/indexes-bitmap-scans.html)
to retrieve data.
This approach, while beneficial in many scenarios, introduces a
potential I/O bottleneck. The bitmap index scan first identifies the
required database blocks and then utilizes a bitmap to access the
corresponding rows from the table's heap. This subsequent "bitmap heap
scan" can result in significant I/O overhead, particularly when queries
return a substantial number of rows across numerous data pages.
## Impact:
Under heavy load or with queries filtering for a wide range of events
across multiple aggregate types, this increased I/O activity may lead
to:
- Increased query latency.
- Elevated disk utilization.
- Potential performance degradation of the event store and dependent
systems.
# How the Problems Are Solved
To address this I/O bottleneck and further optimize query performance,
the projection handler has been modified. Instead of employing multiple
OR clauses for each aggregate type, the aggregate and event type filters
are now combined using IN ARRAY filters.
Technical Details:
This change allows the PostgreSQL query planner to leverage [index-only
scans](https://www.postgresql.org/docs/current/indexes-index-only-scans.html).
By utilizing IN ARRAY filters, the database can efficiently retrieve the
necessary data directly from the index, eliminating the need to access
the table's heap. This results in:
* Reduced I/O: Index-only scans significantly minimize disk I/O
operations, as the database avoids reading data pages from the main
table.
* Improved Query Performance: By reducing I/O, query execution times are
substantially improved, leading to lower latency.
# Additional Changes
- rollback of https://github.com/zitadel/zitadel/pull/9497
# Additional Information
## Query Plan of previous query
```sql
SELECT
created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision
FROM
eventstore.events2
WHERE
instance_id = '<INSTANCE_ID>'
AND (
(
instance_id = '<INSTANCE_ID>'
AND "position" > <POSITION>
AND aggregate_type = 'project'
AND event_type = ANY(ARRAY[
'project.application.added'
,'project.application.changed'
,'project.application.deactivated'
,'project.application.reactivated'
,'project.application.removed'
,'project.removed'
,'project.application.config.api.added'
,'project.application.config.api.changed'
,'project.application.config.api.secret.changed'
,'project.application.config.api.secret.updated'
,'project.application.config.oidc.added'
,'project.application.config.oidc.changed'
,'project.application.config.oidc.secret.changed'
,'project.application.config.oidc.secret.updated'
,'project.application.config.saml.added'
,'project.application.config.saml.changed'
])
) OR (
instance_id = '<INSTANCE_ID>'
AND "position" > <POSITION>
AND aggregate_type = 'org'
AND event_type = 'org.removed'
) OR (
instance_id = '<INSTANCE_ID>'
AND "position" > <POSITION>
AND aggregate_type = 'instance'
AND event_type = 'instance.removed'
)
)
AND "position" > 1741600905.3495
AND "position" < (
SELECT
COALESCE(EXTRACT(EPOCH FROM min(xact_start)), EXTRACT(EPOCH FROM now()))
FROM
pg_stat_activity
WHERE
datname = current_database()
AND application_name = ANY(ARRAY['zitadel_es_pusher_', 'zitadel_es_pusher', 'zitadel_es_pusher_<INSTANCE_ID>'])
AND state <> 'idle'
)
ORDER BY "position", in_tx_order LIMIT 200 OFFSET 1;
```
```
Limit (cost=120.08..120.09 rows=7 width=361) (actual time=2.167..2.172 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
InitPlan 1
-> Aggregate (cost=2.74..2.76 rows=1 width=32) (actual time=1.813..1.815 rows=1 loops=1)
Output: COALESCE(EXTRACT(epoch FROM min(s.xact_start)), EXTRACT(epoch FROM now()))
-> Nested Loop (cost=0.00..2.74 rows=1 width=8) (actual time=1.803..1.805 rows=0 loops=1)
Output: s.xact_start
Join Filter: (d.oid = s.datid)
-> Seq Scan on pg_catalog.pg_database d (cost=0.00..1.07 rows=1 width=4) (actual time=0.016..0.021 rows=1 loops=1)
Output: d.oid, d.datname, d.datdba, d.encoding, d.datlocprovider, d.datistemplate, d.datallowconn, d.dathasloginevt, d.datconnlimit, d.datfrozenxid, d.datminmxid, d.dattablespace, d.datcollate, d.datctype, d.datlocale, d.daticurules, d.datcollversion, d.datacl
Filter: (d.datname = current_database())
Rows Removed by Filter: 4
-> Function Scan on pg_catalog.pg_stat_get_activity s (cost=0.00..1.63 rows=3 width=16) (actual time=1.781..1.781 rows=0 loops=1)
Output: s.datid, s.pid, s.usesysid, s.application_name, s.state, s.query, s.wait_event_type, s.wait_event, s.xact_start, s.query_start, s.backend_start, s.state_change, s.client_addr, s.client_hostname, s.client_port, s.backend_xid, s.backend_xmin, s.backend_type, s.ssl, s.sslversion, s.sslcipher, s.sslbits, s.ssl_client_dn, s.ssl_client_serial, s.ssl_issuer_dn, s.gss_auth, s.gss_princ, s.gss_enc, s.gss_delegation, s.leader_pid, s.query_id
Function Call: pg_stat_get_activity(NULL::integer)
Filter: ((s.state <> 'idle'::text) AND (s.application_name = ANY ('{zitadel_es_pusher_,zitadel_es_pusher,zitadel_es_pusher_<INSTANCE_ID>}'::text[])))
Rows Removed by Filter: 49
-> Sort (cost=117.31..117.33 rows=8 width=361) (actual time=2.167..2.168 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
Sort Key: events2."position", events2.in_tx_order
Sort Method: quicksort Memory: 25kB
-> Bitmap Heap Scan on eventstore.events2 (cost=84.92..117.19 rows=8 width=361) (actual time=2.088..2.089 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
Recheck Cond: (((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'project'::text) AND (events2.event_type = ANY ('{project.application.added,project.application.changed,project.application.deactivated,project.application.reactivated,project.application.removed,project.removed,project.application.config.api.added,project.application.config.api.changed,project.application.config.api.secret.changed,project.application.config.api.secret.updated,project.application.config.oidc.added,project.application.config.oidc.changed,project.application.config.oidc.secret.changed,project.application.config.oidc.secret.updated,project.application.config.saml.added,project.application.config.saml.changed}'::text[])) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1)) OR ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'org'::text) AND (events2.event_type = 'org.removed'::text) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1)) OR ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'instance'::text) AND (events2.event_type = 'instance.removed'::text) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1)))
-> BitmapOr (cost=84.88..84.88 rows=8 width=0) (actual time=2.080..2.081 rows=0 loops=1)
-> Bitmap Index Scan on es_projection (cost=0.00..75.44 rows=8 width=0) (actual time=2.016..2.017 rows=0 loops=1)
Index Cond: ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'project'::text) AND (events2.event_type = ANY ('{project.application.added,project.application.changed,project.application.deactivated,project.application.reactivated,project.application.removed,project.removed,project.application.config.api.added,project.application.config.api.changed,project.application.config.api.secret.changed,project.application.config.api.secret.updated,project.application.config.oidc.added,project.application.config.oidc.changed,project.application.config.oidc.secret.changed,project.application.config.oidc.secret.updated,project.application.config.saml.added,project.application.config.saml.changed}'::text[])) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1))
-> Bitmap Index Scan on es_projection (cost=0.00..4.71 rows=1 width=0) (actual time=0.016..0.016 rows=0 loops=1)
Index Cond: ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'org'::text) AND (events2.event_type = 'org.removed'::text) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1))
-> Bitmap Index Scan on es_projection (cost=0.00..4.71 rows=1 width=0) (actual time=0.045..0.045 rows=0 loops=1)
Index Cond: ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = 'instance'::text) AND (events2.event_type = 'instance.removed'::text) AND (events2."position" > <POSITION>) AND (events2."position" > 1741600905.3495) AND (events2."position" < (InitPlan 1).col1))
Query Identifier: 3194938266011254479
Planning Time: 1.295 ms
Execution Time: 2.832 ms
```
## Query Plan of new query
```sql
SELECT
created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision
FROM
eventstore.events2
WHERE
instance_id = '<INSTANCE_ID>'
AND "position" > <POSITION>
AND aggregate_type = ANY(ARRAY['project', 'instance', 'org'])
AND event_type = ANY(ARRAY[
'project.application.added'
,'project.application.changed'
,'project.application.deactivated'
,'project.application.reactivated'
,'project.application.removed'
,'project.removed'
,'project.application.config.api.added'
,'project.application.config.api.changed'
,'project.application.config.api.secret.changed'
,'project.application.config.api.secret.updated'
,'project.application.config.oidc.added'
,'project.application.config.oidc.changed'
,'project.application.config.oidc.secret.changed'
,'project.application.config.oidc.secret.updated'
,'project.application.config.saml.added'
,'project.application.config.saml.changed'
,'org.removed'
,'instance.removed'
])
AND "position" < (
SELECT
COALESCE(EXTRACT(EPOCH FROM min(xact_start)), EXTRACT(EPOCH FROM now()))
FROM
pg_stat_activity
WHERE
datname = current_database()
AND application_name = ANY(ARRAY['zitadel_es_pusher_', 'zitadel_es_pusher', 'zitadel_es_pusher_<INSTANCE_ID>'])
AND state <> 'idle'
)
ORDER BY "position", in_tx_order LIMIT 200 OFFSET 1;
```
```
Limit (cost=293.34..293.36 rows=8 width=361) (actual time=4.686..4.689 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
InitPlan 1
-> Aggregate (cost=2.74..2.76 rows=1 width=32) (actual time=1.717..1.719 rows=1 loops=1)
Output: COALESCE(EXTRACT(epoch FROM min(s.xact_start)), EXTRACT(epoch FROM now()))
-> Nested Loop (cost=0.00..2.74 rows=1 width=8) (actual time=1.658..1.659 rows=0 loops=1)
Output: s.xact_start
Join Filter: (d.oid = s.datid)
-> Seq Scan on pg_catalog.pg_database d (cost=0.00..1.07 rows=1 width=4) (actual time=0.026..0.028 rows=1 loops=1)
Output: d.oid, d.datname, d.datdba, d.encoding, d.datlocprovider, d.datistemplate, d.datallowconn, d.dathasloginevt, d.datconnlimit, d.datfrozenxid, d.datminmxid, d.dattablespace, d.datcollate, d.datctype, d.datlocale, d.daticurules, d.datcollversion, d.datacl
Filter: (d.datname = current_database())
Rows Removed by Filter: 4
-> Function Scan on pg_catalog.pg_stat_get_activity s (cost=0.00..1.63 rows=3 width=16) (actual time=1.628..1.628 rows=0 loops=1)
Output: s.datid, s.pid, s.usesysid, s.application_name, s.state, s.query, s.wait_event_type, s.wait_event, s.xact_start, s.query_start, s.backend_start, s.state_change, s.client_addr, s.client_hostname, s.client_port, s.backend_xid, s.backend_xmin, s.backend_type, s.ssl, s.sslversion, s.sslcipher, s.sslbits, s.ssl_client_dn, s.ssl_client_serial, s.ssl_issuer_dn, s.gss_auth, s.gss_princ, s.gss_enc, s.gss_delegation, s.leader_pid, s.query_id
Function Call: pg_stat_get_activity(NULL::integer)
Filter: ((s.state <> 'idle'::text) AND (s.application_name = ANY ('{zitadel_es_pusher_,zitadel_es_pusher,zitadel_es_pusher_<INSTANCE_ID>}'::text[])))
Rows Removed by Filter: 42
-> Sort (cost=290.58..290.60 rows=9 width=361) (actual time=4.685..4.685 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
Sort Key: events2."position", events2.in_tx_order
Sort Method: quicksort Memory: 25kB
-> Index Scan using es_projection on eventstore.events2 (cost=0.70..290.43 rows=9 width=361) (actual time=4.616..4.617 rows=0 loops=1)
Output: events2.created_at, events2.event_type, events2.sequence, events2."position", events2.payload, events2.creator, events2.owner, events2.instance_id, events2.aggregate_type, events2.aggregate_id, events2.revision, events2.in_tx_order
Index Cond: ((events2.instance_id = '<INSTANCE_ID>'::text) AND (events2.aggregate_type = ANY ('{project,instance,org}'::text[])) AND (events2.event_type = ANY ('{project.application.added,project.application.changed,project.application.deactivated,project.application.reactivated,project.application.removed,project.removed,project.application.config.api.added,project.application.config.api.changed,project.application.config.api.secret.changed,project.application.config.api.secret.updated,project.application.config.oidc.added,project.application.config.oidc.changed,project.application.config.oidc.secret.changed,project.application.config.oidc.secret.updated,project.application.config.saml.added,project.application.config.saml.changed,org.removed,instance.removed}'::text[])) AND (events2."position" > <POSITION>) AND (events2."position" < (InitPlan 1).col1))
Query Identifier: -8254550537132386499
Planning Time: 2.864 ms
Execution Time: 5.414 ms
```
2025-03-13 16:50:23 +01:00
|
|
|
instance.InstanceRemovedEventType,
|
2023-10-19 12:19:10 +02:00
|
|
|
).
|
2022-09-12 17:18:08 +01:00
|
|
|
Builder(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Storage) certificateToCertificateAndKey(certificate query.Certificate) (_ *key.CertificateAndKey, err error) {
|
|
|
|
keyData, err := crypto.Decrypt(certificate.Key(), p.encAlg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
privateKey, err := crypto.BytesToPrivateKey(keyData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := crypto.BytesToCertificate(certificate.Certificate())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &key.CertificateAndKey{
|
|
|
|
Key: privateKey,
|
|
|
|
Certificate: cert,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectCertificate(certs []query.Certificate) query.Certificate {
|
|
|
|
return certs[len(certs)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func setSAMLCtx(ctx context.Context) context.Context {
|
|
|
|
return authz.SetCtxData(ctx, authz.CtxData{UserID: samlUser, OrgID: authz.GetInstance(ctx).InstanceID()})
|
|
|
|
}
|
|
|
|
|
|
|
|
func retry(retryable func() error) (err error) {
|
|
|
|
for i := 0; i < retryCount; i++ {
|
|
|
|
err = retryable()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
time.Sleep(retryBackoff)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|