diff --git a/storage/migrations/13_runtime_events_related_accounts.up.sql b/storage/migrations/13_runtime_events_related_accounts.up.sql index 2dfa2236a..ed6530e39 100644 --- a/storage/migrations/13_runtime_events_related_accounts.up.sql +++ b/storage/migrations/13_runtime_events_related_accounts.up.sql @@ -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 $$ @@ -30,6 +33,9 @@ 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; @@ -37,7 +43,7 @@ ALTER TABLE chain.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, @@ -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; diff --git a/storage/migrations/14_events_related_accounts.up.sql b/storage/migrations/14_events_related_accounts.up.sql index e8e3d3606..a1fd1fd78 100644 --- a/storage/migrations/14_events_related_accounts.up.sql +++ b/storage/migrations/14_events_related_accounts.up.sql @@ -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 @@ -28,6 +31,9 @@ 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; @@ -35,7 +41,6 @@ ALTER TABLE chain.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 ( @@ -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;