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.
Improve backfill performance by lifting the call to
pgroll.latest_version()
out of the up and down trigger functions. The result ofpgroll.latest_version()
is instead passed as a config parameter to the function template and used as a constant inside the trigger function.Trigger functions are invoked once per row in the affected table during migration start. Even though the execution of
pgroll.latest_version
is fast in isolation, lifting it out of the 'inner loop' like this has a significant effect on backfill durations.The trigger functions used to look like this:
Old trigger function
They now look like this:
New trigger function
The statement executed for each batch is like this:
Per batch SQL statement
Removing the call to
pgroll.latest_version()
from the up/down triggers has the following effect on the execution of the per-batch SQL statement used to perform backfills:Before:
explain analyze for old triggers
After:
explain analyze for new triggers
From a user-perspective, this change results in a backfill duration performance improvement of ~80%.