Skip to content

Commit

Permalink
migration optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Jan 31, 2025
1 parent 5ca8e62 commit 2f19393
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
33 changes: 23 additions & 10 deletions storage/migrations/13_runtime_events_related_accounts.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ BEGIN;
-- Update chain.runtime_events with event_index.
ALTER TABLE chain.runtime_events ADD COLUMN event_index UINT31;

-- Disable triggers for faster updates.
ALTER TABLE chain.runtime_events DISABLE TRIGGER ALL;

-- Populate the event_index column with sequential values for each (runtime, round)
-- to ensure uniqueness.
DO $$
Expand Down Expand Up @@ -30,14 +33,17 @@ BEGIN
END LOOP;
END $$;

-- Re-enable triggers.
ALTER TABLE chain.runtime_events ENABLE TRIGGER ALL;

ALTER TABLE chain.runtime_events
ALTER COLUMN event_index SET NOT NULL;

-- Primary key for runtime events.
ALTER TABLE chain.runtime_events
ADD CONSTRAINT pk_runtime_events PRIMARY KEY (runtime, round, event_index);

-- Create runtime events related accounts table.
-- Create and populate the runtime events related accounts table.
CREATE TABLE chain.runtime_events_related_accounts
(
runtime runtime NOT NULL,
Expand All @@ -50,20 +56,27 @@ CREATE TABLE chain.runtime_events_related_accounts
FOREIGN KEY (runtime, round, event_index) REFERENCES chain.runtime_events(runtime, round, event_index) DEFERRABLE INITIALLY DEFERRED
);

-- Disable logging for faster insertion.
ALTER TABLE chain.runtime_events_related_accounts SET UNLOGGED;

-- Populate the runtime_events_related_accounts table.
INSERT INTO
chain.runtime_events_related_accounts (runtime, round, event_index, tx_index, type, account_address)
SELECT
runtime, round, event_index, tx_index, type, unnest(related_accounts) AS account_address
FROM
chain.runtime_events
WHERE
related_accounts IS NOT NULL
AND array_length(related_accounts, 1) > 0;
COPY chain.runtime_events_related_accounts (runtime, round, event_index, tx_index, type, account_address)
FROM PROGRAM '
psql -XtA -c "
SELECT runtime, round, event_index, tx_index, type, unnest(related_accounts) AS account_address
FROM chain.runtime_events
WHERE related_accounts IS NOT NULL
AND array_length(related_accounts, 1) > 0
"'
WITH (FORMAT csv);

-- Restore logging.
ALTER TABLE chain.runtime_events_related_accounts SET LOGGED;

-- Used for fetching all events related to an account (sorted by round).
CREATE INDEX ix_runtime_events_related_accounts_account_address_round ON chain.runtime_events_related_accounts(runtime, account_address, round, tx_index);

-- Clean up.
DROP INDEX chain.ix_runtime_events_related_accounts;
ALTER TABLE chain.runtime_events DROP COLUMN related_accounts;

Expand Down
31 changes: 21 additions & 10 deletions storage/migrations/14_events_related_accounts.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ BEGIN;

ALTER TABLE chain.events ADD COLUMN event_index UINT31;

-- Disable triggers for faster updates.
ALTER TABLE chain.events DISABLE TRIGGER ALL;

-- Populate the event_index column with sequential values for each tx_block to ensure uniqueness.
DO $$
DECLARE
Expand All @@ -28,14 +31,16 @@ BEGIN
END LOOP;
END $$;

-- Re-enable triggers.
ALTER TABLE chain.events ENABLE TRIGGER ALL;

ALTER TABLE chain.events
ALTER COLUMN event_index SET NOT NULL;

-- Primary key for runtime events.
ALTER TABLE chain.events
ADD CONSTRAINT pk_events PRIMARY KEY (tx_block, type, event_index);


-- Create events related accounts table.
CREATE TABLE chain.events_related_accounts
(
Expand All @@ -49,21 +54,27 @@ CREATE TABLE chain.events_related_accounts
FOREIGN KEY (tx_block, type, event_index) REFERENCES chain.events(tx_block, type, event_index) DEFERRABLE INITIALLY DEFERRED
);

-- Disable logging for faster insertion.
ALTER TABLE chain.events_related_accounts SET UNLOGGED;

-- Populate the events_related_accounts table.
INSERT INTO
chain.events_related_accounts (tx_block, type, event_index, tx_index, account_address)
SELECT
tx_block, type, event_index, tx_index, unnest(related_accounts) AS account_address
FROM
chain.events
WHERE
related_accounts IS NOT NULL
AND array_length(related_accounts, 1) > 0;
COPY chain.events_related_accounts (tx_block, type, event_index, tx_index, account_address)
FROM PROGRAM '
psql -XtA -c "
SELECT tx_block, type, event_index, tx_index, unnest(related_accounts) AS account_address
FROM chain.events
WHERE related_accounts IS NOT NULL
AND array_length(related_accounts, 1) > 0
"'
WITH (FORMAT csv);

-- Restore logging.
ALTER TABLE chain.events_related_accounts SET LOGGED;

-- Used for fetching all events related to an account (sorted by round).
CREATE INDEX ix_events_related_accounts_account_address_block ON chain.events_related_accounts(account_address, tx_block DESC, tx_index); -- TODO: maybe also event index?

-- Clean up.
DROP INDEX chain.ix_events_related_accounts;
ALTER TABLE chain.events DROP COLUMN related_accounts;

Expand Down

0 comments on commit 2f19393

Please sign in to comment.