Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,8 @@ select aggregate_objects(volumes_to_jsonb(volumes_with_asset))
from get_all_account_volumes(_ledger, _account_address, _before := _before) volumes_with_asset
$$ set search_path from current;

create temporary table tmp_volumes as
drop table if exists tmp_volumes;
create temporary table tmp_volumes on commit drop as
select
ledger,
accounts_address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ do $$
begin
set search_path = '{{.Schema}}';

drop table if exists transactions_ids;
create temporary table transactions_ids as
select row_number() over (order by transactions.seq) as row_number,
moves.seq as moves_seq, transactions.id, transactions.seq as transactions_seq
Expand Down Expand Up @@ -37,6 +38,8 @@ do $$

perform pg_notify('migrations-{{ .Schema }}', 'continue: ' || _batch_size);
end loop;

drop table transactions_ids;
end
$$
language plpgsql;
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ do $$
execute _vsql;
end loop;

drop table if exists logs_transactions;
create temporary table logs_transactions as
select row_number() over (order by ledger, id) as row_number, ledger, date, (data->'transaction'->>'id')::bigint as transaction_id
from logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ do $$
begin
set search_path = '{{.Schema}}';

create temporary table tmp_volumes as
drop table if exists tmp_volumes;
create temporary table tmp_volumes on commit drop as
Comment on lines +8 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

ON COMMIT DROP will destroy the temp table after the first batched COMMIT on line 47, breaking all subsequent iterations.

The loop on lines 35–51 calls COMMIT after each batch. With ON COMMIT DROP, tmp_volumes is automatically dropped at the first COMMIT, so the second batch's SELECT * FROM tmp_volumes (line 38) will fail with a "relation does not exist" error. The explicit DROP TABLE tmp_volumes on line 53 will also error out.

This only manifests when there are more than 1000 rows to migrate (_batch_size), making it an easy-to-miss regression in small test datasets.

To fix: keep the DROP TABLE IF EXISTS (line 8) for orphan cleanup, but remove ON COMMIT DROP from the CREATE statement.

Proposed fix
 		drop table if exists tmp_volumes;
-		create temporary table tmp_volumes on commit drop as
+		create temporary table tmp_volumes as
🤖 Prompt for AI Agents
In `@internal/storage/bucket/migrations/20-accounts-volumes-fill-history/up.sql`
around lines 8 - 9, The temp table creation uses "CREATE TEMPORARY TABLE
tmp_volumes ON COMMIT DROP", which causes tmp_volumes to be automatically
removed at the first COMMIT inside the batch loop and breaks subsequent
iterations; modify the CREATE statement for tmp_volumes to remove "ON COMMIT
DROP" so the table persists across commits, while keeping the initial "DROP
TABLE IF EXISTS tmp_volumes" cleanup and the explicit "DROP TABLE tmp_volumes"
at the end of the script.

select distinct on (ledger, accounts_address, asset)
ledger,
accounts_address,
Expand Down