Skip to content

Commit

Permalink
perf: optimize block insertion (#330)
Browse files Browse the repository at this point in the history
* Add the previous value to the account_slots table

* add the previous nonce and balance to the accounts schema

* update sqls

* sqlx prepare

* revert column name change
  • Loading branch information
carneiro-cw authored Mar 8, 2024
1 parent efb3268 commit 46b403a
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 76 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/eth/storage/postgres/queries/insert_account.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
INSERT INTO accounts (address, latest_nonce, latest_balance, bytecode, creation_block)
VALUES ($1, $2, $3, $4, $5)
INSERT INTO accounts (address, latest_nonce, latest_balance, bytecode, creation_block, previous_balance, previous_nonce)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (address) DO
UPDATE
SET latest_nonce = EXCLUDED.latest_nonce,
latest_balance = EXCLUDED.latest_balance
WHERE accounts.latest_nonce = $6 AND accounts.latest_balance = $7
WHERE accounts.latest_nonce = excluded.previous_balance AND accounts.latest_balance = excluded.previous_balance
49 changes: 39 additions & 10 deletions src/eth/storage/postgres/queries/insert_account_batch.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
WITH account_updates
AS (SELECT *
FROM UNNEST($1::bytea[], $2::bytea[], $3::numeric[], $4::numeric[], $5::numeric[], $6::numeric[], $7::numeric[])
AS t(address, bytecode, new_balance, new_nonce, creation_block, original_balance, original_nonce)
WITH account_updates AS (
SELECT *
FROM
unnest(
$50::bytea [],
$51::bytea [],
$52::numeric [],
$53::numeric [],
$54::numeric [],
$55::numeric [],
$56::numeric []
)
AS t (
address,
bytecode,
new_balance,
new_nonce,
creation_block,
previous_balance,
previous_nonce
)
)
INSERT INTO accounts (address, bytecode, latest_balance, latest_nonce, creation_block)
SELECT address, bytecode, new_balance, new_nonce, creation_block

INSERT INTO accounts (
address, bytecode, latest_balance, latest_nonce, creation_block, previous_balance, previous_nonce
)
SELECT
address,
bytecode,
new_balance,
new_nonce,
creation_block,
previous_balance,
previous_nonce
FROM account_updates
ON CONFLICT (address) DO
UPDATE
SET latest_nonce = EXCLUDED.latest_nonce,
latest_balance = EXCLUDED.latest_balance
WHERE accounts.latest_nonce = (SELECT original_nonce FROM account_updates WHERE account_updates.address=accounts.address)
AND accounts.latest_balance = (SELECT original_balance FROM account_updates WHERE account_updates.address=accounts.address)
SET latest_nonce = excluded.latest_nonce,
latest_balance = excluded.latest_balance,
previous_balance = excluded.previous_balance,
previous_nonce = excluded.previous_nonce
WHERE accounts.latest_nonce = excluded.previous_nonce
AND accounts.latest_balance = excluded.previous_balance
5 changes: 3 additions & 2 deletions src/eth/storage/postgres/queries/insert_account_slot.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
INSERT INTO account_slots VALUES ($1, $2, $3, $4)
INSERT INTO account_slots(idx, value, previous_value, account_address, creation_block)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (idx, account_address)
DO UPDATE SET value = EXCLUDED.value
WHERE account_slots.value = $5
WHERE account_slots.value = excluded.previous_value
42 changes: 16 additions & 26 deletions src/eth/storage/postgres/queries/insert_entire_block.sql
Original file line number Diff line number Diff line change
Expand Up @@ -135,37 +135,31 @@ account_insert AS (
new_balance,
new_nonce,
creation_block,
original_balance,
original_nonce
previous_balance,
previous_nonce
)
)

INSERT INTO accounts (
address, bytecode, latest_balance, latest_nonce, creation_block
address, bytecode, latest_balance, latest_nonce, creation_block, previous_balance, previous_nonce
)
SELECT
address,
bytecode,
new_balance,
new_nonce,
creation_block
creation_block,
previous_balance,
previous_nonce
FROM account_updates
ON CONFLICT (address) DO
UPDATE
SET latest_nonce = excluded.latest_nonce,
latest_balance = excluded.latest_balance
WHERE accounts.latest_nonce
= (
SELECT original_nonce
FROM account_updates
WHERE account_updates.address = excluded.address
)
AND accounts.latest_balance
= (
SELECT original_balance
FROM account_updates
WHERE account_updates.address = excluded.address
)
latest_balance = excluded.latest_balance,
previous_balance = excluded.previous_balance,
previous_nonce = excluded.previous_nonce
WHERE accounts.latest_nonce = excluded.previous_nonce
AND accounts.latest_balance = excluded.previous_balance
RETURNING 1 as res
),

Expand All @@ -183,23 +177,19 @@ slot_insert AS (
AS t (idx, value, account_address, creation_block, original_value)
)

INSERT INTO account_slots (idx, value, account_address, creation_block)
INSERT INTO account_slots (idx, value, previous_value, account_address, creation_block)
SELECT
idx,
value,
original_value,
account_address,
creation_block
FROM slot_updates
ON CONFLICT (idx, account_address) DO
UPDATE
SET value = excluded.value
WHERE account_slots.value = (
SELECT original_value
FROM slot_updates
WHERE
slot_updates.idx = excluded.idx
AND slot_updates.account_address = excluded.account_address
)
SET value = excluded.value,
previous_value = excluded.previous_value
WHERE account_slots.value = excluded.previous_value
RETURNING 1 as res
),

Expand Down
34 changes: 22 additions & 12 deletions src/eth/storage/postgres/queries/insert_slot_batch.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
WITH slot_updates
AS (SELECT *
FROM UNNEST($1::bytea[], $2::bytea[], $3::bytea[], $4::numeric[], $5::bytea[])
AS t(idx, value, account_address, creation_block, original_value)
WITH slot_updates AS (
SELECT *
FROM
unnest(
$57::bytea [],
$58::bytea [],
$59::bytea [],
$60::numeric [],
$61::bytea []
)
AS t (idx, value, account_address, creation_block, original_value)
)
INSERT INTO account_slots (idx, value, account_address, creation_block)
SELECT idx, value, account_address, creation_block

INSERT INTO account_slots (idx, value, previous_value, account_address, creation_block)
SELECT
idx,
value,
original_value,
account_address,
creation_block
FROM slot_updates
ON CONFLICT (idx, account_address) DO
UPDATE
SET value = EXCLUDED.value
WHERE account_slots.value = (
SELECT original_value
FROM slot_updates
WHERE slot_updates.idx = EXCLUDED.idx
AND slot_updates.account_address = EXCLUDED.account_address)
SET value = excluded.value,
previous_value = excluded.previous_value
WHERE account_slots.value = excluded.previous_value
5 changes: 4 additions & 1 deletion static/schema/001-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ CREATE TABLE IF NOT EXISTS accounts (
bytecode BYTEA,
latest_balance NUMERIC NOT NULL CHECK (latest_balance >= 0),
latest_nonce NUMERIC NOT NULL CHECK (latest_nonce >= 0),
previous_balance NUMERIC CHECK (latest_balance >= 0),
previous_nonce NUMERIC CHECK (latest_balance >= 0),
creation_block NUMERIC NOT NULL REFERENCES blocks (number) ON DELETE CASCADE,
PRIMARY KEY (address)
);
Expand All @@ -49,7 +51,8 @@ CREATE TABLE IF NOT EXISTS account_slots (
idx BYTEA NOT NULL CHECK (LENGTH(idx) = 32),
value BYTEA NOT NULL CHECK (LENGTH(value) = 32),
account_address BYTEA NOT NULL REFERENCES accounts (address) ON DELETE CASCADE,
creation_block NUMERIC NOT NULL REFERENCES blocks (number) ON DELETE CASCADE,
previous_value BYTEA CHECK (LENGTH(value) = 32),
creation_block NUMERIC NOT NULL REFERENCES blocks (number) ON DELETE CASCADE,
PRIMARY KEY (idx, account_address)
);

Expand Down

0 comments on commit 46b403a

Please sign in to comment.