-
Notifications
You must be signed in to change notification settings - Fork 119
BE-596: Materialize entity aggregates into entity_edition_cache
#8854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TimDiekmann
wants to merge
6
commits into
main
Choose a base branch
from
t/be-596-materialize-the-aggregate-views-in-the-entity-subgraph-query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3a095b5
BE-596: Materialize entity aggregates into entity_edition_cache
TimDiekmann 60224bb
Refactor entity edition cache SQL
TimDiekmann 2868e6b
Update projections.rs
TimDiekmann 08d5d6e
Use DELETE instead of TRUNCATE in cache reindex
TimDiekmann 72a957f
Remove comment
TimDiekmann db769d6
Address review findings
TimDiekmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 1 addition & 9 deletions
10
libs/@local/graph/migrations/graph-migrations/v009__entities/down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,12 @@ | ||
| DROP VIEW IF EXISTS first_type_title_for_entity; | ||
| DROP VIEW IF EXISTS last_type_title_for_entity; | ||
| DROP VIEW IF EXISTS type_title_for_entity; | ||
|
|
||
| DROP VIEW IF EXISTS first_label_for_entity; | ||
| DROP VIEW IF EXISTS last_label_for_entity; | ||
| DROP VIEW IF EXISTS label_for_entity; | ||
|
|
||
| DROP TABLE entity_embeddings; | ||
| DROP VIEW entity_is_of_type_ids; | ||
| DROP TABLE entity_is_of_type; | ||
| DROP VIEW entity_has_left_entity; | ||
| DROP VIEW entity_has_right_entity; | ||
| DROP TABLE entity_edge; | ||
| DROP TYPE EDGE_DIRECTION; | ||
| DROP TYPE ENTITY_EDGE_KIND; | ||
| DROP TABLE entity_temporal_metadata; | ||
| DROP TABLE entity_edition_cache; | ||
| DROP TABLE entity_editions; | ||
| DROP TABLE entity_drafts; | ||
| DROP TABLE entity_ids; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
libs/@local/graph/postgres-store/postgres_migrations/V51__entity_edition_cache.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| -- Denormalized per-edition cache of an entity's type/label aggregates. Computing these | ||
| -- inline forces the planner to re-evaluate the aggregation per row in entity-subgraph | ||
| -- queries; reading the cache instead is a single 1:1 join. | ||
| -- | ||
| -- The cache is derived data: rows are inserted alongside `entity_is_of_type` writes and | ||
| -- can be fully rebuilt at any time (`reindex_entity_cache`), e.g. after in-place changes | ||
| -- to entity-type schemas. A row exists exactly for editions that have at least one | ||
| -- depth-0 entity type. | ||
| -- | ||
| -- The four type-derived arrays (`type_titles`, `base_urls`, `versions`, | ||
| -- `versioned_urls`) are | ||
| -- positionally aligned and cover ALL inheritance depths so that type predicates | ||
| -- (containment via `@>`) match supertypes. Order is (inheritance depth, title, | ||
| -- base URL, version DESC); the entity's direct types therefore form the array prefix | ||
| -- of length `direct_types` (used to project `entityTypeIds`), and `[1]` is the | ||
| -- canonically first direct type, providing the type-title sort key. Titles are taken | ||
| -- from `entity_types` without the `transaction_time @> now()` filter, so the cache | ||
| -- does not depend on type archival state. | ||
| -- | ||
| -- `labels` is resolved per DIRECT type (label inheritance already lives in each type's | ||
| -- `closed_schema.allOf`, nearest ancestor first), ordered by the canonical type order | ||
| -- (title, base URL, version DESC) with the `allOf` position as tie-breaker within one | ||
| -- type. `labels[1]` is the entity's display/sort label; NULL means the entity has no | ||
| -- label. Descending sorts use the same element β there is no min/max flip. | ||
| CREATE TABLE entity_edition_cache ( | ||
| entity_edition_id UUID PRIMARY KEY REFERENCES entity_editions ON DELETE CASCADE, | ||
| direct_types INT NOT NULL, | ||
| labels TEXT [], | ||
| type_titles TEXT [] NOT NULL, | ||
| base_urls TEXT [] NOT NULL, | ||
| versions BIGINT [] NOT NULL, | ||
| versioned_urls TEXT [] NOT NULL | ||
| ); | ||
|
|
||
| INSERT INTO entity_edition_cache ( | ||
| entity_edition_id, | ||
| direct_types, | ||
| labels, | ||
| type_titles, | ||
| base_urls, | ||
| versions, | ||
| versioned_urls | ||
| ) | ||
| SELECT | ||
| types.entity_edition_id, | ||
| types.direct_types, | ||
| labels.labels, | ||
| types.type_titles, | ||
| types.base_urls, | ||
| types.versions, | ||
| types.versioned_urls | ||
| FROM ( | ||
| SELECT | ||
| entity_is_of_type.entity_edition_id, | ||
| count(*) FILTER (WHERE entity_is_of_type.inheritance_depth = 0) AS direct_types, | ||
| array_agg(entity_types.schema ->> 'title' | ||
| ORDER BY entity_is_of_type.inheritance_depth, | ||
| entity_types.schema ->> 'title', ontology_ids.base_url, | ||
| ontology_ids.version DESC | ||
| ) AS type_titles, | ||
| array_agg(ontology_ids.base_url | ||
| ORDER BY entity_is_of_type.inheritance_depth, | ||
| entity_types.schema ->> 'title', ontology_ids.base_url, | ||
| ontology_ids.version DESC | ||
| ) AS base_urls, | ||
| array_agg(ontology_ids.version | ||
| ORDER BY entity_is_of_type.inheritance_depth, | ||
| entity_types.schema ->> 'title', ontology_ids.base_url, | ||
| ontology_ids.version DESC | ||
| ) AS versions, | ||
| array_agg(ontology_ids.base_url || 'v/' || ontology_ids.version | ||
| ORDER BY entity_is_of_type.inheritance_depth, | ||
| entity_types.schema ->> 'title', ontology_ids.base_url, | ||
| ontology_ids.version DESC | ||
| ) AS versioned_urls | ||
| FROM entity_is_of_type | ||
| INNER JOIN ontology_ids | ||
| ON entity_is_of_type.entity_type_ontology_id = ontology_ids.ontology_id | ||
| INNER JOIN entity_types | ||
| ON ontology_ids.ontology_id = entity_types.ontology_id | ||
| GROUP BY entity_is_of_type.entity_edition_id | ||
| ) AS types | ||
| LEFT JOIN ( | ||
| SELECT | ||
| entity_is_of_type.entity_edition_id, | ||
| array_agg(label_value.label | ||
| ORDER BY entity_types.schema ->> 'title', ontology_ids.base_url, | ||
| ontology_ids.version DESC, label_value.ordinality | ||
| ) FILTER (WHERE label_value.label IS NOT NULL) AS labels | ||
| FROM entity_is_of_type | ||
| INNER JOIN ontology_ids | ||
| ON entity_is_of_type.entity_type_ontology_id = ontology_ids.ontology_id | ||
| INNER JOIN entity_types | ||
| ON ontology_ids.ontology_id = entity_types.ontology_id | ||
| INNER JOIN entity_editions | ||
| ON entity_is_of_type.entity_edition_id = entity_editions.entity_edition_id | ||
| CROSS JOIN LATERAL ( | ||
| SELECT | ||
| jsonb_extract_path(entity_editions.properties, label_path.path) #>> '{}' AS label, | ||
| label_path.ordinality | ||
| FROM jsonb_array_elements_text(jsonb_path_query_array(entity_types.closed_schema, '$.allOf[*].labelProperty')) | ||
| WITH ORDINALITY AS label_path (path, ordinality) | ||
| ) AS label_value | ||
| WHERE entity_is_of_type.inheritance_depth = 0 | ||
| GROUP BY entity_is_of_type.entity_edition_id | ||
| ) AS labels | ||
| ON types.entity_edition_id = labels.entity_edition_id; | ||
|
|
||
| -- Type filters arrive as containment checks (`@>`); labels/titles are only sorted or | ||
| -- projected, never filtered, so they carry no index. | ||
| CREATE INDEX entity_edition_cache_base_urls ON entity_edition_cache USING gin (base_urls); | ||
| CREATE INDEX entity_edition_cache_versioned_urls ON entity_edition_cache USING gin (versioned_urls); | ||
|
|
||
| -- The cache replaces the per-row aggregate views for every consumer (query compiler and | ||
| -- HashQL), so they are dropped. | ||
| DROP VIEW first_label_for_entity; | ||
| DROP VIEW last_label_for_entity; | ||
| DROP VIEW label_for_entity; | ||
| DROP VIEW first_type_title_for_entity; | ||
| DROP VIEW last_type_title_for_entity; | ||
| DROP VIEW type_title_for_entity; | ||
| DROP VIEW entity_is_of_type_ids; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.