-
Notifications
You must be signed in to change notification settings - Fork 136
feat: improve migration performances #1175
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
63 changes: 1 addition & 62 deletions
63
internal/storage/bucket/migrations/27-fix-invalid-pcv/up.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,67 +1,6 @@ | ||
| do $$ | ||
| declare | ||
| _offset integer := 0; | ||
| _batch_size integer := 1000; | ||
| begin | ||
| set search_path = '{{ .Schema }}'; | ||
|
|
||
| drop table if exists moves_view; | ||
|
|
||
| create table moves_view as | ||
| select transactions_seq, public.aggregate_objects(jsonb_build_object(accounts_address, volumes)) as volumes | ||
| from ( | ||
| select transactions_seq, accounts_address, public.aggregate_objects(json_build_object(asset, json_build_object('input', (post_commit_volumes).inputs, 'output', (post_commit_volumes).outputs))::jsonb) as volumes | ||
| from ( | ||
| SELECT DISTINCT ON (moves.transactions_seq, accounts_address, asset) moves.transactions_seq, accounts_address, asset, | ||
| first_value(post_commit_volumes) OVER ( | ||
| PARTITION BY moves.transactions_seq, accounts_address, asset | ||
| ORDER BY seq DESC | ||
| ) AS post_commit_volumes | ||
| FROM moves | ||
| where insertion_date < ( | ||
| select tstamp from goose_db_version where version_id = 12 | ||
| ) | ||
| ) moves | ||
| group by transactions_seq, accounts_address | ||
| ) data | ||
| group by transactions_seq; | ||
|
|
||
| create index moves_view_idx on moves_view(transactions_seq); | ||
| -- speed up hash join when updating rows later | ||
| alter table moves_view add foreign key(transactions_seq) references transactions(seq); | ||
|
|
||
| if (select count(*) from moves_view) = 0 then | ||
| drop table moves_view; | ||
| return; | ||
| end if; | ||
|
|
||
| perform pg_notify('migrations-{{ .Schema }}', 'init: ' || (select count(*) from moves_view)); | ||
|
|
||
| loop | ||
| with data as ( | ||
| select transactions_seq, volumes | ||
| from moves_view | ||
| -- play better than offset/limit | ||
| where transactions_seq >= _offset and transactions_seq < _offset + _batch_size | ||
| ) | ||
| update transactions | ||
| set post_commit_volumes = data.volumes | ||
| from data | ||
| where transactions.seq = data.transactions_seq; | ||
|
|
||
| if not found then | ||
| drop table moves_view; | ||
| exit; | ||
| end if; | ||
|
|
||
| _offset = _offset + _batch_size; | ||
|
|
||
| perform pg_notify('migrations-{{ .Schema }}', 'continue: ' || _batch_size); | ||
|
|
||
| commit; | ||
| end loop; | ||
|
|
||
| drop table if exists moves_view; | ||
| raise notice 'Migration superseded by next migration'; | ||
| end | ||
| $$; | ||
|
|
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
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
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
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
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
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.
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.