Skip to content

Commit 9c5f381

Browse files
committed
experiment: improve migration performance
1 parent 10fbf2b commit 9c5f381

File tree

2 files changed

+25
-35
lines changed
  • internal/storage/bucket/migrations
    • 17-moves-fill-transaction-id
    • 18-transactions-fill-inserted-at

2 files changed

+25
-35
lines changed

internal/storage/bucket/migrations/17-moves-fill-transaction-id/up.sql

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,38 @@ do $$
55
begin
66
set search_path = '{{.Schema}}';
77

8-
select count(seq)
8+
-- todo: debug
9+
--create temporary table transactions_ids as
10+
create table transactions_ids as
11+
select row_number() over (order by transactions.seq) as row_number,
12+
moves.seq as moves_seq, transactions.id, transactions.seq as transactions_seq
913
from moves
10-
where transactions_id is null
14+
join transactions on transactions.seq = moves.transactions_seq
15+
where transactions_id is null;
16+
17+
create index transactions_ids_rows on transactions_ids(row_number) include (moves_seq, transactions_seq, id);
18+
19+
select count(*)
20+
from transactions_ids
1121
into _max;
1222

1323
perform pg_notify('migrations-{{ .Schema }}', 'init: ' || _max);
14-
loop
1524

16-
with _outdated_moves as (
25+
for i in 0.._max-1 by _batch_size loop
26+
with _rows as (
1727
select *
18-
from moves
19-
where transactions_id is null
20-
limit _batch_size
28+
from transactions_ids
29+
where row_number >= i and row_number < i + _batch_size
2130
)
2231
update moves
23-
set transactions_id = (
24-
select id
25-
from transactions
26-
where seq = moves.transactions_seq
27-
)
28-
from _outdated_moves
29-
where moves.seq in (_outdated_moves.seq);
32+
set transactions_id = _rows.id
33+
from _rows
34+
where seq = _rows.moves_seq;
3035

31-
exit when not found;
36+
commit;
3237

3338
perform pg_notify('migrations-{{ .Schema }}', 'continue: ' || _batch_size);
34-
35-
commit ;
3639
end loop;
37-
38-
alter table moves
39-
add constraint transactions_id_not_null
40-
check (transactions_id is not null)
41-
not valid;
4240
end
4341
$$
4442
language plpgsql;

internal/storage/bucket/migrations/18-transactions-fill-inserted-at/up.sql

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ do $$
33
_ledger record;
44
_vsql text;
55
_batch_size integer := 1000;
6-
_date timestamp without time zone;
76
_count integer := 0;
87
begin
98
set search_path = '{{.Schema}}';
@@ -19,17 +18,12 @@ do $$
1918
execute _vsql;
2019
end loop;
2120

22-
-- select the date where the "11-make-stateless" migration has been applied
23-
select tstamp into _date
24-
from goose_db_version
25-
where version_id = 12;
26-
2721
create temporary table logs_transactions as
28-
select id, ledger, date, (data->'transaction'->>'id')::bigint as transaction_id
22+
select row_number() over (order by ledger, id) as row_number, ledger, date, (data->'transaction'->>'id')::bigint as transaction_id
2923
from logs
30-
where date <= _date;
24+
where type = 'NEW_TRANSACTION' or type = 'REVERTED_TRANSACTION';
3125

32-
create index on logs_transactions (ledger, transaction_id) include (id, date);
26+
create index on logs_transactions (row_number) include (ledger, date, transaction_id);
3327

3428
select count(*) into _count
3529
from logs_transactions;
@@ -40,14 +34,12 @@ do $$
4034
with _rows as (
4135
select *
4236
from logs_transactions
43-
order by ledger, transaction_id
44-
offset i
45-
limit _batch_size
37+
where row_number >= i and row_number < i + _batch_size
4638
)
4739
update transactions
4840
set inserted_at = _rows.date
4941
from _rows
50-
where transactions.ledger = _rows.ledger and transactions.id = _rows.transaction_id;
42+
where transactions.ledger = _rows.ledger and transactions.id = _rows.transaction_id and inserted_at is null;
5143

5244
commit;
5345

0 commit comments

Comments
 (0)