Skip to content

Convert materialized views to tables with triggers #5

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

Closed

Conversation

mentatbot[bot]
Copy link
Contributor

@mentatbot mentatbot bot commented Mar 5, 2025

This PR converts the following materialized views to tables that are updated using triggers:

  1. pool_states_materialized -> pool_states_table

    • Added triggers on swaps, position_updates, and pool_initializations
    • Each trigger updates the table when the underlying data changes
  2. oracle_pool_states_materialized -> oracle_pool_states_table

    • Added a trigger on oracle_snapshots
    • The trigger updates the table when new oracle snapshots are added
  3. last_24h_pool_stats_materialized -> last_24h_pool_stats_table

    • Added triggers on hourly_volume_by_token and hourly_tvl_delta_by_token
    • The triggers update the table when the hourly data changes

This change eliminates the need to periodically refresh the materialized views, as the tables are now automatically updated when the underlying data changes. The refreshOperationalMaterializedView and refreshAnalyticalTables methods have been updated to remove the refresh operations.

Closes #4


🤖 See my steps and track the cost of the PR here

  • Wake on any new activity.

This PR converts the following materialized views to tables that are updated using triggers:

1. `pool_states_materialized` -> `pool_states_table`
   - Added triggers on `swaps`, `position_updates`, and `pool_initializations`
   - Each trigger updates the table when the underlying data changes

2. `oracle_pool_states_materialized` -> `oracle_pool_states_table`
   - Added a trigger on `oracle_snapshots`
   - The trigger updates the table when new oracle snapshots are added

3. `last_24h_pool_stats_materialized` -> `last_24h_pool_stats_table`
   - Added triggers on `hourly_volume_by_token` and `hourly_tvl_delta_by_token`
   - The triggers update the table when the hourly data changes

This change eliminates the need to periodically refresh the materialized views, as the tables are now automatically updated when the underlying data changes. The `refreshOperationalMaterializedView` and `refreshAnalyticalTables` methods have been updated to remove the refresh operations.

Closes # 4
Copy link
Contributor Author

mentatbot bot commented Mar 5, 2025

I've converted all three materialized views to tables with triggers as requested:

  1. pool_states_materialized → pool_states_table

    • Created triggers that update the table when changes occur in swaps, position_updates, or pool_initializations
    • Each trigger recalculates the state using the same logic as the original view
  2. oracle_pool_states_materialized → oracle_pool_states_table

    • Created a trigger that updates the table when new oracle snapshots are added
    • The trigger only updates the timestamp if the new one is more recent
  3. last_24h_pool_stats_materialized → last_24h_pool_stats_table

    • Created triggers that update the table when changes occur in hourly_volume_by_token or hourly_tvl_delta_by_token
    • The volume and TVL triggers update their respective parts of the data

The refreshOperationalMaterializedView and refreshAnalyticalTables methods have been updated to remove the refresh operations, as they're no longer needed.

This approach should be more efficient as it updates the tables incrementally when data changes, rather than periodically refreshing the entire materialized view.

@moodysalem moodysalem closed this May 31, 2025
@moodysalem moodysalem deleted the mentat-4-1-convert-materialized-views-to-tables branch May 31, 2025 07:28
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.

make all materialized views into tables that are indexed using triggers
1 participant