Skip to content

Conversation

@ramirogm
Copy link

Change-type: patch

Ereski and others added 3 commits February 13, 2023 10:52
After every write, pine has to rerun all rules from the model to ensure
consistency. These rules run over the entire database, sometimes causing
some queries to run for too long against what should be a simple write.

This commit adds a mechanism to help with this issue by narrowing the
set of rows that each rule should touch to those rows that were actually
changed.

Implementing this mechanism safely is doable and not necessarily complex
code-wise, but requires a deep modifications from the current
architecture. This commit adds a restricted form instead where we only
narrow the rows of the root table that were changed. If any other table
was changed then narrowing is a no op.

It can be proved that this is always safe as long as the root table is
selected from only once and the rule is positive ("It is necessary that
each ...").

The implementation here adds a single binding into the rule's SQL query
which can be bound by pine for each rule where an opportunity to use
this optimization arises.

The implementation itself is simple: count how many times the root table
is selected from and if it is selected from exactly one, then add a
narrowing constraint in the form of:

$1 = '{}' OR
<root table>.id = ANY(CAST($1 AS INTEGER[]))

Where $1 will be bound to either '{}', which disables narrowing, or to a
list of IDs that were affected by the write.

This initial implementation can be extended in the future.

Change-type: major
Signed-off-by: Carol Schulze <carol@balena.io>
Add binds for affected IDs in compiled rules
@ramirogm ramirogm marked this pull request as draft February 18, 2023 12:09
@ramirogm
Copy link
Author

@balena-ci re-test

ramirogm added 4 commits March 8, 2023 15:54
Change-type: patch
Signed-off-by: Ramiro Gonzalez <ramiro.gonzalez@balena.io>
Change-type: patch
Signed-off-by: Ramiro Gonzalez <ramiro.gonzalez@balena.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants