Skip to content

Commit fcf587d

Browse files
committed
refactor: split index migrations into isolated concurrent operations
Split the single migration 41 into 5 separate migrations: - Migration 41: Create idx_moves_pit_insertion index - Migration 42: Create idx_moves_pit_effective index - Migration 43: Create idx_accounts_metadata_pit index - Migration 44: Create idx_transactions_metadata_pit index - Migration 45: Drop all old indexes This ensures each concurrent index creation is isolated in its own migration, and all index drops are consolidated into a final migration. Also removed the optional idx_moves_account_balance index on Moves table.
1 parent 92be9f6 commit fcf587d

File tree

5 files changed

+54
-56
lines changed
  • internal/storage/bucket/migrations
    • 41-optimize-query-indexes
    • 42-optimize-query-indexes-pit-effective
    • 43-optimize-accounts-metadata-index
    • 44-optimize-transactions-metadata-index
    • 45-drop-old-indexes

5 files changed

+54
-56
lines changed
Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,11 @@
11
set search_path = '{{.Schema}}';
22

33
-- ========================================
4-
-- MOVES TABLE INDEXES OPTIMIZATION
4+
-- MOVES TABLE INDEXES OPTIMIZATION (1/4)
55
-- ========================================
66

7-
-- Step 1: Create new optimized indexes FIRST to avoid performance degradation
87
-- Critical: Index for Point-in-Time queries with insertion_date
98
-- Covers queries in resource_aggregated_balances.go and resource_accounts.go
109
-- Replaces: moves_post_commit_volumes
1110
create index {{ if not .Transactional }}concurrently{{end}} idx_moves_pit_insertion
1211
on "{{.Schema}}".moves (account_address, asset, insertion_date desc, seq desc);
13-
14-
-- Critical: Index for Point-in-Time queries with effective_date
15-
-- Covers queries in resource_aggregated_balances.go and resource_accounts.go
16-
-- Replaces: moves_effective_post_commit_volumes
17-
create index {{ if not .Transactional }}concurrently{{end}} idx_moves_pit_effective
18-
on "{{.Schema}}".moves (account_address, asset, effective_date desc, seq desc);
19-
20-
-- Optimal: Index for balance lookups by account with effective date
21-
-- Covers balance filtering queries in resource_accounts.go
22-
create index {{ if not .Transactional }}concurrently{{end}} idx_moves_account_balance
23-
on "{{.Schema}}".moves (account_address, effective_date desc, seq desc)
24-
include (asset, post_commit_effective_volumes);
25-
26-
-- Step 2: Drop old suboptimal indexes AFTER new ones are ready
27-
drop index if exists moves_post_commit_volumes;
28-
drop index if exists moves_effective_post_commit_volumes;
29-
30-
-- ========================================
31-
-- ACCOUNTS_METADATA TABLE OPTIMIZATION
32-
-- ========================================
33-
34-
-- Step 1: Create new optimized index FIRST
35-
-- Critical: Index for historical metadata queries
36-
-- Covers queries in resource_accounts.go for Point-in-Time metadata
37-
-- Replaces: accounts_metadata_revisions
38-
create index {{ if not .Transactional }}concurrently{{end}} idx_accounts_metadata_pit
39-
on "{{.Schema}}".accounts_metadata (accounts_address, revision desc)
40-
include (metadata, date);
41-
42-
-- Step 2: Drop old suboptimal index AFTER new one is ready
43-
drop index if exists accounts_metadata_revisions;
44-
45-
-- ========================================
46-
-- TRANSACTIONS_METADATA TABLE OPTIMIZATION
47-
-- ========================================
48-
49-
-- Step 1: Create new optimized index FIRST
50-
-- Critical: Index for historical transaction metadata queries
51-
-- Covers queries in resource_transactions.go for Point-in-Time metadata
52-
-- Replaces: transactions_metadata_revisions
53-
create index {{ if not .Transactional }}concurrently{{end}} idx_transactions_metadata_pit
54-
on "{{.Schema}}".transactions_metadata (transactions_id, revision desc)
55-
include (metadata, date);
56-
57-
-- Step 2: Drop old suboptimal index AFTER new one is ready
58-
drop index if exists transactions_metadata_revisions;
59-
60-
-- ========================================
61-
-- ACCOUNTS_VOLUMES TABLE CLEANUP
62-
-- ========================================
63-
64-
-- Drop redundant index - PRIMARY KEY already covers this pattern
65-
-- Safe to drop immediately as PRIMARY KEY provides same functionality
66-
drop index if exists accounts_volumes_idx;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
set search_path = '{{.Schema}}';
2+
3+
-- ========================================
4+
-- MOVES TABLE INDEXES OPTIMIZATION (2/4)
5+
-- ========================================
6+
7+
-- Critical: Index for Point-in-Time queries with effective_date
8+
-- Covers queries in resource_aggregated_balances.go and resource_accounts.go
9+
-- Replaces: moves_effective_post_commit_volumes
10+
create index {{ if not .Transactional }}concurrently{{end}} idx_moves_pit_effective
11+
on "{{.Schema}}".moves (account_address, asset, effective_date desc, seq desc);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set search_path = '{{.Schema}}';
2+
3+
-- ========================================
4+
-- ACCOUNTS_METADATA TABLE OPTIMIZATION
5+
-- ========================================
6+
7+
-- Critical: Index for historical metadata queries
8+
-- Covers queries in resource_accounts.go for Point-in-Time metadata
9+
-- Replaces: accounts_metadata_revisions
10+
create index {{ if not .Transactional }}concurrently{{end}} idx_accounts_metadata_pit
11+
on "{{.Schema}}".accounts_metadata (accounts_address, revision desc)
12+
include (metadata, date);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set search_path = '{{.Schema}}';
2+
3+
-- ========================================
4+
-- TRANSACTIONS_METADATA TABLE OPTIMIZATION
5+
-- ========================================
6+
7+
-- Critical: Index for historical transaction metadata queries
8+
-- Covers queries in resource_transactions.go for Point-in-Time metadata
9+
-- Replaces: transactions_metadata_revisions
10+
create index {{ if not .Transactional }}concurrently{{end}} idx_transactions_metadata_pit
11+
on "{{.Schema}}".transactions_metadata (transactions_id, revision desc)
12+
include (metadata, date);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
set search_path = '{{.Schema}}';
2+
3+
-- ========================================
4+
-- DROP OLD INDEXES
5+
-- ========================================
6+
7+
-- Drop old moves table indexes (replaced by migrations 41-42)
8+
drop index if exists moves_post_commit_volumes;
9+
drop index if exists moves_effective_post_commit_volumes;
10+
11+
-- Drop old accounts_metadata index (replaced by migration 43)
12+
drop index if exists accounts_metadata_revisions;
13+
14+
-- Drop old transactions_metadata index (replaced by migration 44)
15+
drop index if exists transactions_metadata_revisions;
16+
17+
-- Drop redundant accounts_volumes index - PRIMARY KEY already covers this pattern
18+
drop index if exists accounts_volumes_idx;

0 commit comments

Comments
 (0)