Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/benchmark-comparison.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:
count:
description: "Number of times to run each benchmark "
required: false
default: 1
default: 5
pull_request:
types: [assigned, opened, synchronize, reopened, labeled]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Optimize Query Indexes - PIT Insertion (1/5)
description: |
Create optimized index for Point-in-Time queries with insertion_date on moves table.
This is part of a multi-step migration where each index is created in a separate migration.

changes:
moves_table:
- action: "CREATE"
new_index: "idx_moves_pit_insertion (account_address, asset, insertion_date desc, seq desc)"
reason: |
Creates index for PIT queries filtering on insertion_date.
Replaces moves_post_commit_volumes which used accounts_seq.
This index aligns with actual query patterns for better performance.

affected_queries:
- "resource_aggregated_balances.go: PIT queries with DISTINCT ON (accounts_address, asset)"
- "resource_accounts.go: Account expansion volumes queries"

performance_impact:
- "MAJOR improvement for Point-in-Time queries using insertion_date"

migration_safety:
- "Uses CONCURRENTLY flag to avoid locking tables during index creation"
- "Each index creation is isolated in its own migration"
- "Old indexes will be dropped in migration 45 after all new indexes are created"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- ========================================
-- MOVES TABLE INDEXES OPTIMIZATION (1/4)
-- ========================================

-- Critical: Index for Point-in-Time queries with insertion_date
-- Covers queries in resource_aggregated_balances.go and resource_accounts.go
-- Replaces: moves_post_commit_volumes
create index {{ if not .Transactional }}concurrently{{end}} idx_moves_pit_insertion
on "{{.Schema}}".moves (accounts_address, asset, insertion_date desc, seq desc);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Optimize Query Indexes - PIT Effective (2/5)
description: |
Create optimized index for Point-in-Time queries with effective_date on moves table.
This is part of a multi-step migration where each index is created in a separate migration.

changes:
moves_table:
- action: "CREATE"
new_index: "idx_moves_pit_effective (account_address, asset, effective_date desc, seq desc)"
reason: |
Creates index for PIT queries filtering on effective_date.
Replaces moves_effective_post_commit_volumes which used accounts_seq.
This index aligns with actual query patterns for better performance.

affected_queries:
- "resource_aggregated_balances.go: PIT queries with effective_date filtering"
- "resource_accounts.go: Balance queries with effective_date"

performance_impact:
- "MAJOR improvement for Point-in-Time queries using effective_date"

migration_safety:
- "Uses CONCURRENTLY flag to avoid locking tables during index creation"
- "Each index creation is isolated in its own migration"
- "Old indexes will be dropped in migration 45 after all new indexes are created"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- ========================================
-- MOVES TABLE INDEXES OPTIMIZATION (2/4)
-- ========================================

-- Critical: Index for Point-in-Time queries with effective_date
-- Covers queries in resource_aggregated_balances.go and resource_accounts.go
-- Replaces: moves_effective_post_commit_volumes
create index {{ if not .Transactional }}concurrently{{end}} idx_moves_pit_effective
on "{{.Schema}}".moves (accounts_address, asset, effective_date desc, seq desc);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Optimize Accounts Metadata Index (3/5)
description: |
Create optimized index for historical metadata queries on accounts_metadata table.
This is part of a multi-step migration where each index is created in a separate migration.

changes:
accounts_metadata_table:
- action: "CREATE"
new_index: "idx_accounts_metadata_pit (accounts_address, revision desc) INCLUDE (metadata, date)"
reason: |
Creates index for historical account metadata lookups.
Replaces accounts_metadata_revisions which used accounts_seq.
New index aligns with actual query patterns using accounts_address.

affected_queries:
- "resource_accounts.go: Historical metadata lookups for Point-in-Time queries"

performance_impact:
- "MAJOR improvement for historical account metadata queries"

migration_safety:
- "Uses CONCURRENTLY flag to avoid locking tables during index creation"
- "Each index creation is isolated in its own migration"
- "Old index will be dropped in migration 45 after all new indexes are created"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- ========================================
-- ACCOUNTS_METADATA TABLE OPTIMIZATION
-- ========================================

-- Critical: Index for historical metadata queries
-- Covers queries in resource_accounts.go for Point-in-Time metadata
-- Replaces: accounts_metadata_revisions
create index {{ if not .Transactional }}concurrently{{end}} idx_accounts_metadata_pit
on "{{.Schema}}".accounts_metadata (accounts_address, revision desc)
include (metadata, date);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Optimize Transactions Metadata Index (4/5)
description: |
Create optimized index for historical transaction metadata queries on transactions_metadata table.
This is part of a multi-step migration where each index is created in a separate migration.

changes:
transactions_metadata_table:
- action: "CREATE"
new_index: "idx_transactions_metadata_pit (transactions_id, revision desc) INCLUDE (metadata, date)"
reason: |
Creates index for historical transaction metadata lookups.
Replaces transactions_metadata_revisions which used transactions_seq.
New index aligns with actual query patterns using transactions_id.

affected_queries:
- "resource_transactions.go: Historical transaction metadata lookups for Point-in-Time queries"

performance_impact:
- "MAJOR improvement for historical transaction metadata queries"

migration_safety:
- "Uses CONCURRENTLY flag to avoid locking tables during index creation"
- "Each index creation is isolated in its own migration"
- "Old index will be dropped in migration 45 after all new indexes are created"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- ========================================
-- TRANSACTIONS_METADATA TABLE OPTIMIZATION
-- ========================================

-- Critical: Index for historical transaction metadata queries
-- Covers queries in resource_transactions.go for Point-in-Time metadata
-- Replaces: transactions_metadata_revisions
create index {{ if not .Transactional }}concurrently{{end}} idx_transactions_metadata_pit
on "{{.Schema}}".transactions_metadata (transactions_id, revision desc)
include (metadata, date);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Add Index for Trigger Optimization (3/5)
description: |
Create optimized index for the update_effective_volumes trigger that runs on every
INSERT when MOVES_HISTORY feature is enabled (ON by default).

This trigger updates all moves where effective_date > new.effective_date, which
requires an efficient index on (accounts_address, asset, effective_date) to avoid
full table scans on large datasets.

changes:
moves_table:
- action: "CREATE"
new_index: "idx_moves_update_effective_volumes (accounts_address, asset, effective_date)"
reason: |
Optimizes the UPDATE query in update_effective_volumes trigger.
Without this index, every INSERT causes a slow sequential scan when
MOVES_HISTORY is ON, causing severe performance degradation.

The existing moves_range_dates index has the same columns but in a
different order (account_address, asset, effective_date) and was created
before the column rename. This new index ensures optimal performance.

affected_queries:
- "update_effective_volumes trigger: UPDATE moves WHERE effective_date > ?"
- "Runs on EVERY INSERT when MOVES_HISTORY=ON (default configuration)"

performance_impact:
- "CRITICAL improvement for write performance with MOVES_HISTORY enabled"
- "Reduces INSERT latency by eliminating sequential scans on moves table"
- "Essential for maintaining performance at scale with history tracking"

migration_safety:
- "Index creation is non-blocking"
- "No data modification"
- "moves_range_dates will be dropped in next migration (46) as redundant"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- ========================================
-- MOVES TABLE INDEX FOR TRIGGER OPTIMIZATION (3/5)
-- ========================================

-- Critical: Index to optimize update_effective_volumes trigger
-- This trigger runs on EVERY INSERT when MOVES_HISTORY feature is ON
-- The trigger updates all moves with effective_date > new.effective_date
-- Replaces/optimizes: moves_range_dates which has suboptimal column order
create index {{ if not .Transactional }}concurrently{{end}} idx_moves_update_effective_volumes
on "{{.Schema}}".moves (accounts_address, asset, effective_date);
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Drop Old Indexes (6/6)
description: |
Drop all old suboptimal indexes that have been replaced by new optimized indexes
created in migrations 41-45. This final step completes the index optimization process.

changes:
moves_table:
- action: "DROP"
old_indexes:
- "moves_post_commit_volumes"
- "moves_effective_post_commit_volumes"
- "moves_range_dates"
reason: |
- moves_post_commit_volumes and moves_effective_post_commit_volumes
replaced by idx_moves_pit_insertion and idx_moves_pit_effective in migrations 41-42
- moves_range_dates replaced by idx_moves_update_effective_volumes in migration 45

accounts_metadata_table:
- action: "DROP"
old_index: "accounts_metadata_revisions"
reason: "Replaced by idx_accounts_metadata_pit in migration 43"

transactions_metadata_table:
- action: "DROP"
old_index: "transactions_metadata_revisions"
reason: "Replaced by idx_transactions_metadata_pit in migration 44"

accounts_volumes_table:
- action: "DROP"
old_index: "accounts_volumes_idx"
reason: "Redundant - PRIMARY KEY already covers this pattern"

migration_safety:
- "Old indexes are dropped only AFTER all new indexes are created"
- "Ensures no performance degradation during migration"
- "No data modification - only removes redundant index structures"
17 changes: 17 additions & 0 deletions internal/storage/bucket/migrations/46-drop-old-indexes/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- ========================================
-- DROP OLD INDEXES
-- ========================================

-- Drop old moves table indexes (replaced by migrations 41-42-45)
drop index if exists moves_post_commit_volumes;
drop index if exists moves_effective_post_commit_volumes;
drop index if exists moves_range_dates;

-- Drop old accounts_metadata index (replaced by migration 43)
drop index if exists accounts_metadata_revisions;

-- Drop old transactions_metadata index (replaced by migration 44)
drop index if exists transactions_metadata_revisions;

-- Drop redundant accounts_volumes index - PRIMARY KEY already covers this pattern
drop index if exists accounts_volumes_idx;
8 changes: 6 additions & 2 deletions test/e2e/app_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ var _ = Context("Ledger application lifecycle tests", func() {
})
When("restarting the service", func() {
BeforeEach(func(ctx context.Context) {
testServer, err := testServer.Wait(ctx)
srv, err := testServer.Wait(ctx)
Expect(err).To(BeNil())

Expect(testServer.Restart(ctx)).To(BeNil())
Expect(srv.Restart(ctx)).To(BeNil())

DeferCleanup(func(ctx context.Context) {
Expect(srv.Stop(ctx)).To(BeNil())
})
})
It("should be ok", func() {})
})
Expand Down
Loading