2020-10-05 19:09:26 +02:00
|
|
|
package sql
|
|
|
|
|
|
|
|
|
|
import (
|
2020-10-21 19:00:41 +02:00
|
|
|
"context"
|
2020-10-05 19:09:26 +02:00
|
|
|
"database/sql"
|
2020-11-23 19:31:12 +01:00
|
|
|
"encoding/json"
|
2020-10-05 19:09:26 +02:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2023-10-19 12:19:10 +02:00
|
|
|
"strconv"
|
2020-10-05 19:09:26 +02:00
|
|
|
"strings"
|
|
|
|
|
|
2025-05-27 17:13:17 +02:00
|
|
|
"github.com/shopspring/decimal"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/logging"
|
2022-03-15 07:19:02 +01:00
|
|
|
|
fix(projections): overhaul the event projection system (#10560)
This PR overhauls our event projection system to make it more robust and
prevent skipped events under high load. The core change replaces our
custom, transaction-based locking with standard PostgreSQL advisory
locks. We also introduce a worker pool to manage concurrency and prevent
database connection exhaustion.
### Key Changes
* **Advisory Locks for Projections:** Replaces exclusive row locks and
inspection of `pg_stat_activity` with PostgreSQL advisory locks for
managing projection state. This is a more reliable and standard approach
to distributed locking.
* **Simplified Await Logic:** Removes the complex logic for awaiting
open transactions, simplifying it to a more straightforward time-based
filtering of events.
* **Projection Worker Pool:** Implements a worker pool to limit
concurrent projection triggers, preventing connection exhaustion and
improving stability under load. A new `MaxParallelTriggers`
configuration option is introduced.
### Problem Solved
Under high throughput, a race condition could cause projections to miss
events from the eventstore. This led to inconsistent data in projection
tables (e.g., a user grant might be missing). This PR fixes the
underlying locking and concurrency issues to ensure all events are
processed reliably.
### How it Works
1. **Event Writing:** When writing events, a *shared* advisory lock is
taken. This signals that a write is in progress.
2. **Event Handling (Projections):**
* A projection worker attempts to acquire an *exclusive* advisory lock
for that specific projection. If the lock is already held, it means
another worker is on the job, so the current one backs off.
* Once the lock is acquired, the worker briefly acquires and releases
the same *shared* lock used by event writers. This acts as a barrier,
ensuring it waits for any in-flight writes to complete.
* Finally, it processes all events that occurred before its transaction
began.
### Additional Information
* ZITADEL no longer modifies the `application_name` PostgreSQL variable
during event writes.
* The lock on the `current_states` table is now `FOR NO KEY UPDATE`.
* Fixes https://github.com/zitadel/zitadel/issues/8509
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
(cherry picked from commit 0575f67e942c3192b36e39fd6ae06b1502bc0f5f)
2025-09-03 17:29:00 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-08-22 14:49:02 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2023-02-27 22:36:43 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/database/dialect"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-10-05 19:09:26 +02:00
|
|
|
)
|
|
|
|
|
|
2020-10-06 21:28:09 +02:00
|
|
|
type querier interface {
|
2023-10-19 12:19:10 +02:00
|
|
|
columnName(field repository.Field, useV1 bool) string
|
2020-10-05 19:09:26 +02:00
|
|
|
operation(repository.Operation) string
|
|
|
|
|
conditionFormat(repository.Operation) string
|
|
|
|
|
placeholder(query string) string
|
2023-10-19 12:19:10 +02:00
|
|
|
eventQuery(useV1 bool) string
|
2025-05-27 17:13:17 +02:00
|
|
|
maxPositionQuery(useV1 bool) string
|
2023-10-19 12:19:10 +02:00
|
|
|
instanceIDsQuery(useV1 bool) string
|
2024-10-28 09:29:34 +01:00
|
|
|
Client() *database.DB
|
2024-05-28 08:49:30 +02:00
|
|
|
orderByEventSequence(desc, shouldOrderBySequence, useV1 bool) string
|
2023-02-27 22:36:43 +01:00
|
|
|
dialect.Database
|
2020-10-05 19:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
type scan func(dest ...interface{}) error
|
2020-10-05 19:09:26 +02:00
|
|
|
|
2023-08-22 14:49:02 +02:00
|
|
|
type tx struct {
|
|
|
|
|
*sql.Tx
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *tx) QueryContext(ctx context.Context, scan func(rows *sql.Rows) error, query string, args ...any) error {
|
|
|
|
|
rows, err := t.Tx.QueryContext(ctx, query, args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
closeErr := rows.Close()
|
|
|
|
|
logging.OnError(closeErr).Info("rows.Close failed")
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
if err = scan(rows); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return rows.Err()
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
func query(ctx context.Context, criteria querier, searchQuery *eventstore.SearchQueryBuilder, dest interface{}, useV1 bool) error {
|
2023-10-19 12:19:10 +02:00
|
|
|
q, err := repository.QueryFromBuilder(searchQuery)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-05-28 08:49:30 +02:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
query, rowScanner := prepareColumns(criteria, q.Columns, useV1)
|
|
|
|
|
where, values := prepareConditions(criteria, q, useV1)
|
2020-10-05 19:09:26 +02:00
|
|
|
if where == "" || query == "" {
|
2023-12-08 16:30:55 +02:00
|
|
|
return zerrors.ThrowInvalidArgument(nil, "SQL-rWeBw", "invalid query factory")
|
2020-10-05 19:09:26 +02:00
|
|
|
}
|
fix(projections): overhaul the event projection system (#10560)
This PR overhauls our event projection system to make it more robust and
prevent skipped events under high load. The core change replaces our
custom, transaction-based locking with standard PostgreSQL advisory
locks. We also introduce a worker pool to manage concurrency and prevent
database connection exhaustion.
### Key Changes
* **Advisory Locks for Projections:** Replaces exclusive row locks and
inspection of `pg_stat_activity` with PostgreSQL advisory locks for
managing projection state. This is a more reliable and standard approach
to distributed locking.
* **Simplified Await Logic:** Removes the complex logic for awaiting
open transactions, simplifying it to a more straightforward time-based
filtering of events.
* **Projection Worker Pool:** Implements a worker pool to limit
concurrent projection triggers, preventing connection exhaustion and
improving stability under load. A new `MaxParallelTriggers`
configuration option is introduced.
### Problem Solved
Under high throughput, a race condition could cause projections to miss
events from the eventstore. This led to inconsistent data in projection
tables (e.g., a user grant might be missing). This PR fixes the
underlying locking and concurrency issues to ensure all events are
processed reliably.
### How it Works
1. **Event Writing:** When writing events, a *shared* advisory lock is
taken. This signals that a write is in progress.
2. **Event Handling (Projections):**
* A projection worker attempts to acquire an *exclusive* advisory lock
for that specific projection. If the lock is already held, it means
another worker is on the job, so the current one backs off.
* Once the lock is acquired, the worker briefly acquires and releases
the same *shared* lock used by event writers. This acts as a barrier,
ensuring it waits for any in-flight writes to complete.
* Finally, it processes all events that occurred before its transaction
began.
### Additional Information
* ZITADEL no longer modifies the `application_name` PostgreSQL variable
during event writes.
* The lock on the `current_states` table is now `FOR NO KEY UPDATE`.
* Fixes https://github.com/zitadel/zitadel/issues/8509
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
(cherry picked from commit 0575f67e942c3192b36e39fd6ae06b1502bc0f5f)
2025-09-03 17:29:00 +02:00
|
|
|
|
|
|
|
|
var contextQuerier interface {
|
|
|
|
|
QueryContext(context.Context, func(rows *sql.Rows) error, string, ...interface{}) error
|
|
|
|
|
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
|
|
|
|
}
|
|
|
|
|
contextQuerier = criteria.Client()
|
|
|
|
|
if q.Tx != nil {
|
|
|
|
|
contextQuerier = &tx{Tx: q.Tx}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if q.AwaitOpenTransactions && q.Columns == eventstore.ColumnsEvent {
|
|
|
|
|
instanceID := authz.GetInstance(ctx).InstanceID()
|
|
|
|
|
if q.InstanceID != nil {
|
|
|
|
|
instanceID = q.InstanceID.Value.(string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = contextQuerier.ExecContext(ctx,
|
|
|
|
|
"select pg_advisory_lock('eventstore.events2'::REGCLASS::OID::INTEGER, hashtext($1)), pg_advisory_unlock('eventstore.events2'::REGCLASS::OID::INTEGER, hashtext($1))",
|
|
|
|
|
instanceID,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
where += awaitOpenTransactions(useV1)
|
|
|
|
|
}
|
2020-10-05 19:09:26 +02:00
|
|
|
query += where
|
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
// instead of using the max function of the database (which doesn't work for postgres)
|
|
|
|
|
// we select the most recent row
|
2025-05-27 17:13:17 +02:00
|
|
|
if q.Columns == eventstore.ColumnsMaxPosition {
|
2023-10-19 12:19:10 +02:00
|
|
|
q.Limit = 1
|
|
|
|
|
q.Desc = true
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 08:49:30 +02:00
|
|
|
// if there is only one subquery we can optimize the query ordering by ordering by sequence
|
|
|
|
|
var shouldOrderBySequence bool
|
|
|
|
|
if len(q.SubQueries) == 1 {
|
|
|
|
|
for _, filter := range q.SubQueries[0] {
|
|
|
|
|
if filter.Field == repository.FieldAggregateID {
|
|
|
|
|
shouldOrderBySequence = filter.Operation == repository.OperationEquals
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
switch q.Columns {
|
|
|
|
|
case eventstore.ColumnsEvent,
|
2025-05-27 17:13:17 +02:00
|
|
|
eventstore.ColumnsMaxPosition:
|
2024-05-28 08:49:30 +02:00
|
|
|
query += criteria.orderByEventSequence(q.Desc, shouldOrderBySequence, useV1)
|
2020-10-05 19:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
if q.Limit > 0 {
|
|
|
|
|
values = append(values, q.Limit)
|
2020-10-05 19:09:26 +02:00
|
|
|
query += " LIMIT ?"
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 13:25:41 +01:00
|
|
|
if q.Offset > 0 {
|
|
|
|
|
values = append(values, q.Offset)
|
|
|
|
|
query += " OFFSET ?"
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 19:09:26 +02:00
|
|
|
query = criteria.placeholder(query)
|
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err = contextQuerier.QueryContext(ctx,
|
2023-08-22 14:49:02 +02:00
|
|
|
func(rows *sql.Rows) error {
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
err := rowScanner(rows.Scan, dest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}, query, values...)
|
2020-10-21 19:00:41 +02:00
|
|
|
if err != nil {
|
2022-03-15 07:19:02 +01:00
|
|
|
logging.New().WithError(err).Info("query failed")
|
2023-12-08 16:30:55 +02:00
|
|
|
return zerrors.ThrowInternal(err, "SQL-KyeAx", "unable to filter events")
|
2020-10-21 19:00:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
2020-10-05 19:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
func prepareColumns(criteria querier, columns eventstore.Columns, useV1 bool) (string, func(s scan, dest interface{}) error) {
|
2020-10-05 19:09:26 +02:00
|
|
|
switch columns {
|
2025-05-27 17:13:17 +02:00
|
|
|
case eventstore.ColumnsMaxPosition:
|
|
|
|
|
return criteria.maxPositionQuery(useV1), maxPositionScanner
|
2023-10-19 12:19:10 +02:00
|
|
|
case eventstore.ColumnsInstanceIDs:
|
|
|
|
|
return criteria.instanceIDsQuery(useV1), instanceIDsScanner
|
|
|
|
|
case eventstore.ColumnsEvent:
|
|
|
|
|
return criteria.eventQuery(useV1), eventsScanner(useV1)
|
2020-10-05 19:09:26 +02:00
|
|
|
default:
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-27 22:36:43 +01:00
|
|
|
|
2025-05-27 17:13:17 +02:00
|
|
|
func maxPositionScanner(row scan, dest interface{}) (err error) {
|
|
|
|
|
position, ok := dest.(*decimal.Decimal)
|
2020-10-05 19:09:26 +02:00
|
|
|
if !ok {
|
2025-05-27 17:13:17 +02:00
|
|
|
return zerrors.ThrowInvalidArgumentf(nil, "SQL-NBjA9", "type must be pointer to decimal.Decimal got: %T", dest)
|
2020-10-05 19:09:26 +02:00
|
|
|
}
|
2025-05-27 17:13:17 +02:00
|
|
|
var res decimal.NullDecimal
|
|
|
|
|
err = row(&res)
|
2020-10-05 19:09:26 +02:00
|
|
|
if err == nil || errors.Is(err, sql.ErrNoRows) {
|
2025-05-27 17:13:17 +02:00
|
|
|
*position = res.Decimal
|
2020-10-05 19:09:26 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
2023-12-08 16:30:55 +02:00
|
|
|
return zerrors.ThrowInternal(err, "SQL-bN5xg", "something went wrong")
|
2020-10-05 19:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
func instanceIDsScanner(scanner scan, dest interface{}) (err error) {
|
2022-07-22 12:08:39 +02:00
|
|
|
ids, ok := dest.(*[]string)
|
|
|
|
|
if !ok {
|
2023-12-08 16:30:55 +02:00
|
|
|
return zerrors.ThrowInvalidArgument(nil, "SQL-Begh2", "type must be an array of string")
|
2022-07-22 12:08:39 +02:00
|
|
|
}
|
|
|
|
|
var id string
|
|
|
|
|
err = scanner(&id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logging.WithError(err).Warn("unable to scan row")
|
2023-12-08 16:30:55 +02:00
|
|
|
return zerrors.ThrowInternal(err, "SQL-DEFGe", "unable to scan row")
|
2022-07-22 12:08:39 +02:00
|
|
|
}
|
|
|
|
|
*ids = append(*ids, id)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
func eventsScanner(useV1 bool) func(scanner scan, dest interface{}) (err error) {
|
|
|
|
|
return func(scanner scan, dest interface{}) (err error) {
|
2023-10-19 18:21:31 +03:00
|
|
|
reduce, ok := dest.(eventstore.Reducer)
|
2023-10-19 12:19:10 +02:00
|
|
|
if !ok {
|
2023-12-08 16:30:55 +02:00
|
|
|
return zerrors.ThrowInvalidArgumentf(nil, "SQL-4GP6F", "events scanner: invalid type %T", dest)
|
2023-10-19 12:19:10 +02:00
|
|
|
}
|
|
|
|
|
event := new(repository.Event)
|
2025-05-27 17:13:17 +02:00
|
|
|
position := new(decimal.NullDecimal)
|
2023-10-19 12:19:10 +02:00
|
|
|
|
|
|
|
|
if useV1 {
|
|
|
|
|
err = scanner(
|
|
|
|
|
&event.CreationDate,
|
|
|
|
|
&event.Typ,
|
|
|
|
|
&event.Seq,
|
2023-10-19 18:21:31 +03:00
|
|
|
&event.Data,
|
2023-10-19 12:19:10 +02:00
|
|
|
&event.EditorUser,
|
|
|
|
|
&event.ResourceOwner,
|
|
|
|
|
&event.InstanceID,
|
|
|
|
|
&event.AggregateType,
|
|
|
|
|
&event.AggregateID,
|
|
|
|
|
&event.Version,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
var revision uint8
|
|
|
|
|
err = scanner(
|
|
|
|
|
&event.CreationDate,
|
|
|
|
|
&event.Typ,
|
|
|
|
|
&event.Seq,
|
|
|
|
|
position,
|
2023-10-19 18:21:31 +03:00
|
|
|
&event.Data,
|
2023-10-19 12:19:10 +02:00
|
|
|
&event.EditorUser,
|
|
|
|
|
&event.ResourceOwner,
|
|
|
|
|
&event.InstanceID,
|
|
|
|
|
&event.AggregateType,
|
|
|
|
|
&event.AggregateID,
|
|
|
|
|
&revision,
|
|
|
|
|
)
|
|
|
|
|
event.Version = eventstore.Version("v" + strconv.Itoa(int(revision)))
|
|
|
|
|
}
|
2020-10-05 19:09:26 +02:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
if err != nil {
|
|
|
|
|
logging.New().WithError(err).Warn("unable to scan row")
|
2023-12-08 16:30:55 +02:00
|
|
|
return zerrors.ThrowInternal(err, "SQL-M0dsf", "unable to scan row")
|
2023-10-19 12:19:10 +02:00
|
|
|
}
|
2025-05-27 17:13:17 +02:00
|
|
|
event.Pos = position.Decimal
|
2023-10-19 18:21:31 +03:00
|
|
|
return reduce(event)
|
2023-10-19 12:19:10 +02:00
|
|
|
}
|
2020-10-05 19:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
func prepareConditions(criteria querier, query *repository.SearchQuery, useV1 bool) (_ string, args []any) {
|
|
|
|
|
clauses, args := prepareQuery(criteria, useV1, query.InstanceID, query.InstanceIDs, query.ExcludedInstances)
|
|
|
|
|
if clauses != "" && len(query.SubQueries) > 0 {
|
|
|
|
|
clauses += " AND "
|
2023-10-19 12:19:10 +02:00
|
|
|
}
|
|
|
|
|
subClauses := make([]string, len(query.SubQueries))
|
|
|
|
|
for i, filters := range query.SubQueries {
|
|
|
|
|
var subArgs []any
|
|
|
|
|
subClauses[i], subArgs = prepareQuery(criteria, useV1, filters...)
|
|
|
|
|
// an error is thrown in [query]
|
|
|
|
|
if subClauses[i] == "" {
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
if len(query.SubQueries) > 1 && len(subArgs) > 1 {
|
|
|
|
|
subClauses[i] = "(" + subClauses[i] + ")"
|
|
|
|
|
}
|
|
|
|
|
args = append(args, subArgs...)
|
|
|
|
|
}
|
|
|
|
|
if len(subClauses) == 1 {
|
|
|
|
|
clauses += subClauses[0]
|
|
|
|
|
} else if len(subClauses) > 1 {
|
|
|
|
|
clauses += "(" + strings.Join(subClauses, " OR ") + ")"
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
additionalClauses, additionalArgs := prepareQuery(criteria, useV1,
|
|
|
|
|
query.Position,
|
2023-11-03 15:52:48 +01:00
|
|
|
query.Owner,
|
|
|
|
|
query.Sequence,
|
|
|
|
|
query.CreatedAfter,
|
|
|
|
|
query.CreatedBefore,
|
|
|
|
|
query.Creator,
|
|
|
|
|
)
|
2023-10-19 12:19:10 +02:00
|
|
|
if additionalClauses != "" {
|
|
|
|
|
if clauses != "" {
|
|
|
|
|
clauses += " AND "
|
|
|
|
|
}
|
|
|
|
|
clauses += additionalClauses
|
|
|
|
|
args = append(args, additionalArgs...)
|
|
|
|
|
}
|
2020-10-05 19:09:26 +02:00
|
|
|
|
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
|
|
|
excludeAggregateIDs := query.ExcludeAggregateIDs
|
|
|
|
|
if len(excludeAggregateIDs) > 0 {
|
|
|
|
|
excludeAggregateIDs = append(excludeAggregateIDs, query.InstanceID, query.InstanceIDs, query.Position, query.CreatedAfter, query.CreatedBefore)
|
|
|
|
|
}
|
|
|
|
|
excludeAggregateIDsClauses, excludeAggregateIDsArgs := prepareQuery(criteria, useV1, excludeAggregateIDs...)
|
|
|
|
|
if excludeAggregateIDsClauses != "" {
|
feat(eventstore): exclude aggregate IDs when event_type occurred (#8940)
# Which Problems Are Solved
For truly event-based notification handler, we need to be able to filter
out events of aggregates which are already handled. For example when an
event like `notify.success` or `notify.failed` was created on an
aggregate, we no longer require events from that aggregate ID.
# How the Problems Are Solved
Extend the query builder to use a `NOT IN` clause which excludes
aggregate IDs when they have certain events for a certain aggregate
type. For optimization and proper index usages, certain filters are
inherited from the parent query, such as:
- Instance ID
- Instance IDs
- Position offset
This is a prettified query as used by the unit tests:
```sql
SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision
FROM eventstore.events2
WHERE instance_id = $1
AND aggregate_type = $2
AND event_type = $3
AND "position" > $4
AND aggregate_id NOT IN (
SELECT aggregate_id
FROM eventstore.events2
WHERE aggregate_type = $5
AND event_type = ANY($6)
AND instance_id = $7
AND "position" > $8
)
ORDER BY "position" DESC, in_tx_order DESC
LIMIT $9
```
I used this query to run it against the `oidc_session` aggregate looking
for added events, excluding aggregates where a token was revoked,
against a recent position. It fully used index scans:
<details>
```json
[
{
"Plan": {
"Node Type": "Index Scan",
"Parallel Aware": false,
"Async Capable": false,
"Scan Direction": "Forward",
"Index Name": "es_projection",
"Relation Name": "events2",
"Alias": "events2",
"Actual Rows": 2,
"Actual Loops": 1,
"Index Cond": "((instance_id = '286399006995644420'::text) AND (aggregate_type = 'oidc_session'::text) AND (event_type = 'oidc_session.added'::text) AND (\"position\" > 1731582100.784168))",
"Rows Removed by Index Recheck": 0,
"Filter": "(NOT (hashed SubPlan 1))",
"Rows Removed by Filter": 1,
"Plans": [
{
"Node Type": "Index Scan",
"Parent Relationship": "SubPlan",
"Subplan Name": "SubPlan 1",
"Parallel Aware": false,
"Async Capable": false,
"Scan Direction": "Forward",
"Index Name": "es_projection",
"Relation Name": "events2",
"Alias": "events2_1",
"Actual Rows": 1,
"Actual Loops": 1,
"Index Cond": "((instance_id = '286399006995644420'::text) AND (aggregate_type = 'oidc_session'::text) AND (event_type = 'oidc_session.access_token.revoked'::text) AND (\"position\" > 1731582100.784168))",
"Rows Removed by Index Recheck": 0
}
]
},
"Triggers": [
]
}
]
```
</details>
# Additional Changes
- None
# Additional Context
- Related to https://github.com/zitadel/zitadel/issues/8931
---------
Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
2024-11-25 17:25:11 +02:00
|
|
|
if clauses != "" {
|
|
|
|
|
clauses += " AND "
|
|
|
|
|
}
|
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
|
|
|
if useV1 {
|
|
|
|
|
clauses += "aggregate_id NOT IN (SELECT aggregate_id FROM eventstore.events WHERE " + excludeAggregateIDsClauses + ")"
|
|
|
|
|
} else {
|
|
|
|
|
clauses += "aggregate_id NOT IN (SELECT aggregate_id FROM eventstore.events2 WHERE " + excludeAggregateIDsClauses + ")"
|
|
|
|
|
}
|
feat(eventstore): exclude aggregate IDs when event_type occurred (#8940)
# Which Problems Are Solved
For truly event-based notification handler, we need to be able to filter
out events of aggregates which are already handled. For example when an
event like `notify.success` or `notify.failed` was created on an
aggregate, we no longer require events from that aggregate ID.
# How the Problems Are Solved
Extend the query builder to use a `NOT IN` clause which excludes
aggregate IDs when they have certain events for a certain aggregate
type. For optimization and proper index usages, certain filters are
inherited from the parent query, such as:
- Instance ID
- Instance IDs
- Position offset
This is a prettified query as used by the unit tests:
```sql
SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision
FROM eventstore.events2
WHERE instance_id = $1
AND aggregate_type = $2
AND event_type = $3
AND "position" > $4
AND aggregate_id NOT IN (
SELECT aggregate_id
FROM eventstore.events2
WHERE aggregate_type = $5
AND event_type = ANY($6)
AND instance_id = $7
AND "position" > $8
)
ORDER BY "position" DESC, in_tx_order DESC
LIMIT $9
```
I used this query to run it against the `oidc_session` aggregate looking
for added events, excluding aggregates where a token was revoked,
against a recent position. It fully used index scans:
<details>
```json
[
{
"Plan": {
"Node Type": "Index Scan",
"Parallel Aware": false,
"Async Capable": false,
"Scan Direction": "Forward",
"Index Name": "es_projection",
"Relation Name": "events2",
"Alias": "events2",
"Actual Rows": 2,
"Actual Loops": 1,
"Index Cond": "((instance_id = '286399006995644420'::text) AND (aggregate_type = 'oidc_session'::text) AND (event_type = 'oidc_session.added'::text) AND (\"position\" > 1731582100.784168))",
"Rows Removed by Index Recheck": 0,
"Filter": "(NOT (hashed SubPlan 1))",
"Rows Removed by Filter": 1,
"Plans": [
{
"Node Type": "Index Scan",
"Parent Relationship": "SubPlan",
"Subplan Name": "SubPlan 1",
"Parallel Aware": false,
"Async Capable": false,
"Scan Direction": "Forward",
"Index Name": "es_projection",
"Relation Name": "events2",
"Alias": "events2_1",
"Actual Rows": 1,
"Actual Loops": 1,
"Index Cond": "((instance_id = '286399006995644420'::text) AND (aggregate_type = 'oidc_session'::text) AND (event_type = 'oidc_session.access_token.revoked'::text) AND (\"position\" > 1731582100.784168))",
"Rows Removed by Index Recheck": 0
}
]
},
"Triggers": [
]
}
]
```
</details>
# Additional Changes
- None
# Additional Context
- Related to https://github.com/zitadel/zitadel/issues/8931
---------
Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
2024-11-25 17:25:11 +02:00
|
|
|
args = append(args, excludeAggregateIDsArgs...)
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
if clauses == "" {
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return " WHERE " + clauses, args
|
|
|
|
|
}
|
2021-07-06 13:55:57 +02:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func prepareQuery(criteria querier, useV1 bool, filters ...*repository.Filter) (_ string, args []any) {
|
|
|
|
|
clauses := make([]string, 0, len(filters))
|
|
|
|
|
args = make([]any, 0, len(filters))
|
|
|
|
|
for _, filter := range filters {
|
|
|
|
|
if filter == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
arg := filter.Value
|
|
|
|
|
|
|
|
|
|
// marshal if payload filter
|
|
|
|
|
if filter.Field == repository.FieldEventData {
|
|
|
|
|
var err error
|
|
|
|
|
arg, err = json.Marshal(arg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logging.WithError(err).Warn("unable to marshal search value")
|
|
|
|
|
continue
|
2021-07-06 13:55:57 +02:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clauses = append(clauses, getCondition(criteria, filter, useV1))
|
|
|
|
|
// if mapping failed an error is thrown in [query]
|
|
|
|
|
if clauses[len(clauses)-1] == "" {
|
|
|
|
|
return "", nil
|
2020-10-05 19:09:26 +02:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
args = append(args, arg)
|
2020-10-05 19:09:26 +02:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
|
|
|
|
|
return strings.Join(clauses, " AND "), args
|
2020-10-05 19:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func getCondition(cond querier, filter *repository.Filter, useV1 bool) (condition string) {
|
|
|
|
|
field := cond.columnName(filter.Field, useV1)
|
2020-10-05 20:39:36 +02:00
|
|
|
operation := cond.operation(filter.Operation)
|
2020-10-05 19:09:26 +02:00
|
|
|
if field == "" || operation == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2020-10-05 20:39:36 +02:00
|
|
|
format := cond.conditionFormat(filter.Operation)
|
2020-10-05 19:09:26 +02:00
|
|
|
|
|
|
|
|
return fmt.Sprintf(format, field, operation)
|
|
|
|
|
}
|