Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use batched inserts for models #73

Merged
merged 2 commits into from
Oct 8, 2020
Merged

perf: use batched inserts for models #73

merged 2 commits into from
Oct 8, 2020

Conversation

iand
Copy link
Contributor

@iand iand commented Oct 7, 2020

No description provided.

@iand
Copy link
Contributor Author

iand commented Oct 7, 2020

Coalesces multiple inserts into a single statement, e.g. going from

INSERT INTO "messages" AS "message" ("cid", "from", "to", "value", "gas_fee_cap", "gas_premium", "gas_limit", "size_bytes", "nonce", "method", "params") VALUES ('cid0', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT) ON CONFLICT do nothing RETURNING "params"
INSERT INTO "messages" AS "message" ("cid", "from", "to", "value", "gas_fee_cap", "gas_premium", "gas_limit", "size_bytes", "nonce", "method", "params") VALUES ('cid1', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT) ON CONFLICT do nothing RETURNING "params"
INSERT INTO "messages" AS "message" ("cid", "from", "to", "value", "gas_fee_cap", "gas_premium", "gas_limit", "size_bytes", "nonce", "method", "params") VALUES ('cid2', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT) ON CONFLICT do nothing RETURNING "params"
INSERT INTO "messages" AS "message" ("cid", "from", "to", "value", "gas_fee_cap", "gas_premium", "gas_limit", "size_bytes", "nonce", "method", "params") VALUES ('cid3', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT) ON CONFLICT do nothing RETURNING "params"
INSERT INTO "messages" AS "message" ("cid", "from", "to", "value", "gas_fee_cap", "gas_premium", "gas_limit", "size_bytes", "nonce", "method", "params") VALUES ('cid4', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT) ON CONFLICT do nothing RETURNING "params"
INSERT INTO "messages" AS "message" ("cid", "from", "to", "value", "gas_fee_cap", "gas_premium", "gas_limit", "size_bytes", "nonce", "method", "params") VALUES ('cid5', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT) ON CONFLICT do nothing RETURNING "params"
INSERT INTO "messages" AS "message" ("cid", "from", "to", "value", "gas_fee_cap", "gas_premium", "gas_limit", "size_bytes", "nonce", "method", "params") VALUES ('cid6', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT) ON CONFLICT do nothing RETURNING "params"

to

INSERT INTO "messages" AS "message" ("cid", "from", "to", "value", "gas_fee_cap", "gas_premium", "gas_limit", "size_bytes", "nonce", "method", "params") VALUES 
('cid0', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT), 
('cid1', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT), 
('cid2', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT), 
('cid3', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT), 
('cid4', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT), 
('cid5', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT), 
('cid6', 'from', 'to', 'val', 'gasfeecap', 'gaspremium', 0, 0, 0, 0, DEFAULT) 
ON CONFLICT do nothing RETURNING "params"

@iand iand requested review from placer14 and frrist and removed request for placer14 October 7, 2020 14:39
Copy link
Member

@frrist frrist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Contributor

@placer14 placer14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice trick! Will test this right now!

@placer14
Copy link
Contributor

placer14 commented Oct 7, 2020

I did hit this...

2020-10-07T22:59:21.725Z        ERROR   message message/message.go:91   failed to process tipset        {"error": "process tipset: persist: persisting receipts: pg: can't bulk-insert empty slice messages.Receipts", "height": 126838}
2020-10-07T22:59:21.816Z        ERROR   message message/message.go:91   failed to process tipset        {"error": "process tipset: persist: persisting messages: pg: can't bulk-insert empty slice messages.Messages", "height": 126837}

@placer14
Copy link
Contributor

placer14 commented Oct 8, 2020

More on this. I have reset these jobs a few times and they fail consistently. In the failure cases, they all have blocks with 0 messages in them. (https://filfox.info/en/tipset/126837) I think we just need to handle the empty case with an early return.

@iand iand force-pushed the iand/batch-inserts branch from 0590c2e to a7f7bbe Compare October 8, 2020 09:53
@placer14
Copy link
Contributor

placer14 commented Oct 8, 2020

Looks great, please merge when ready. I'll try to mention my approvals as being sticky next time when appropriate.

@iand
Copy link
Contributor Author

iand commented Oct 8, 2020

Don't worry, I was going to merge but hadn't got back to it after fixing up the commits.

@iand iand merged commit c34664d into master Oct 8, 2020
@iand iand deleted the iand/batch-inserts branch October 8, 2020 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants