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

storage/consensus: Add index for transaction type #708

Merged
merged 2 commits into from
Jun 20, 2024

Conversation

ptrus
Copy link
Member

@ptrus ptrus commented Jun 14, 2024

Adds an index on consensus transactions method.

Additionally removes minFee, maxFee and code query filters from /consensus/transactions. These filters don't seem all that useful (I have confirmed with frontend that these are not needed) and don't have any indexes set, so these aren't usable in practice anyway.

@ptrus ptrus force-pushed the ptrus/feature/consensus-method-idx branch from 98c5f7c to 1c78e00 Compare June 14, 2024 07:40
@@ -0,0 +1,5 @@
BEGIN;

CREATE INDEX ix_transactions_method ON chain.transactions (method);
Copy link
Collaborator

Choose a reason for hiding this comment

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

this kind of index usually ends up needing height at the end, otherwise the api can't get "recent transactions of x method" without walking this index for all transactions of a method and sorting them or ignoring this index and scanning chronologically and filtering by method

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, will update 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you think It might make sense to update the ix_transactions_sender index in the same way?

Most of the accounts probably have a relatively low number of transactions which are fine, but there are likely some accounts with huge amount of transactions which could cause problems for the "recent transactions of x sender" query, since it would need to sort the filtered list of transactions which could be large?

Likely way less problematic, I should probably find the accounts with most transactions and test the queries on a live nexus deployment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Adding the ix_transactions_sender_block index fixes the above examples to be quick.

@pro-wh
Copy link
Collaborator

pro-wh commented Jun 14, 2024

ok on removing fee and code filters 🤷 seems like they would have been an easy way to DoS

@ptrus ptrus force-pushed the ptrus/feature/consensus-method-idx branch 2 times, most recently from 88aa18c to c671266 Compare June 16, 2024 11:56
@ptrus ptrus requested a review from pro-wh June 18, 2024 10:39
@ptrus ptrus force-pushed the ptrus/feature/consensus-method-idx branch 2 times, most recently from 77add2a to 736cd5f Compare June 18, 2024 10:41
@@ -0,0 +1,8 @@
BEGIN;
Copy link
Member Author

Choose a reason for hiding this comment

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

Added these indexes might take non-trivial time. Do we have any special requirements or handling of such migrations that aren't super quick?

The specific changes in this migration are not breaking and could actually be made whenever (don't need to happen at deployment), so these can be handled that way, but i'm asking more for any general guidelines.

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't have a great general solution.

Specifically for indexes, we sometimes run "CREATE INDEX CONCURRENTLY ..." manually in psql, in advance.
If you do this, you'll want CREATE INDEX IF NOT EXISTS in the migratino.

I'm not sure what happens if you use CONCURRENTLY in the migration, i.e. if the migration will conclude super fast and then the index will be created later, while nexus is running.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the info. For now I updated just update the migration to do CREATE INDEX IF NOT EXISTS so that we are more flexible on how to handle this deployment.

@@ -0,0 +1,8 @@
BEGIN;
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't have a great general solution.

Specifically for indexes, we sometimes run "CREATE INDEX CONCURRENTLY ..." manually in psql, in advance.
If you do this, you'll want CREATE INDEX IF NOT EXISTS in the migratino.

I'm not sure what happens if you use CONCURRENTLY in the migration, i.e. if the migration will conclude super fast and then the index will be created later, while nexus is running.

@@ -0,0 +1,8 @@
BEGIN;

CREATE INDEX ix_transactions_method_height ON chain.transactions (method, block);
Copy link
Contributor

Choose a reason for hiding this comment

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

I often find it hard to understand why we have certain indexes in place. Makes it hard and/or scary to modify or remove them. Let's try to document every index with its intended purpose. E.g. here:

-- `method` is a possible external API parameter; `block` lets us efficiently retrieve the most recent N txs with a given method.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 good idea

Copy link
Contributor

Choose a reason for hiding this comment

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

I forget, when did we say we were going to call a change "breaking"? Is it whenever it introduces a backwards-incompatible DB change? I think this is not breaking because:

  • We're breaking the API, but it's the consensus API which is not officially released yet, so we're OK to muck with it. Even if this were an "official" breaking API change, I guess it would mean that we need to introduce api/spec/v2.yaml (and do some thinking about how we'll manage multiple versions), but nexus itself would stay at the same major version? This philosophy is untested :)
  • We're not breaking nexus: A current nexus operator can deploy this PR over an existing DB, and it will keep working thanks to migrations. An example of a breaking change here is consolidate migrations #574 which requires an operator to be very careful and hands-on when deploying.

Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed that #685 was marked as a breaking change and it is only an API change.

But i agree that the state (consensus part of) nexus is currently in, then most changes would be breaking. I'll make it non breaking, imo it doesn't really matter that much as there's not many external uses of nexus i think.

@ptrus ptrus force-pushed the ptrus/feature/consensus-method-idx branch from 736cd5f to 7d8c75d Compare June 20, 2024 07:00
@ptrus ptrus enabled auto-merge June 20, 2024 07:03
@ptrus ptrus merged commit 403dae6 into main Jun 20, 2024
16 checks passed
@ptrus ptrus deleted the ptrus/feature/consensus-method-idx branch June 20, 2024 07:04
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