Skip to content

Commit 0911627

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

File tree

1 file changed

+5
-13
lines changed
  • internal/storage/bucket/migrations/18-transactions-fill-inserted-at

1 file changed

+5
-13
lines changed

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)