Skip to content
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

Add binds for affected IDs in compiled rules #63

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ereski
Copy link
Contributor

@Ereski Ereski commented Feb 1, 2023

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 [email protected]

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:

```sql
$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 <[email protected]>
@Ereski Ereski requested a review from Page- February 1, 2023 19:23
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.

1 participant