searchcraft icon indicating copy to clipboard operation
searchcraft copied to clipboard

Document refreshing concurrently

Open drnic opened this issue 8 months ago • 0 comments

From @andyatkinson in #2

Also, re: indexes, passing on something else useful in case you didn't already know: refreshing a materialized view ideally is done "concurrently", so that other queries using it can continue to run while the results are replaced. Concurrent refreshes do require one unique index exists on the materialized view:

For here: https://github.com/drnic/searchcraft/blob/develop/lib/searchcraft/model.rb#L44

Docs on the concurrently keyword and unique index requirement: https://www.postgresql.org/docs/current/sql-refreshmaterializedview.html

SearchCraft does support concurrent refreshes; and there is a technique to add a new unique ID column; but these are not yet documented.

In brief:

  • Each MV currently also gets a bonus sequence, even if it doesn't use it. See Builder#create_sequence! and Builder#view_id_sequence_name
  • To add an id column to your MV using Arel, try something like Arel::Nodes::SqlLiteral.new("nextval('#{view_id_sequence_name}') AS id") - which could of course become a nice Builder helper in future.

drnic avatar Oct 25 '23 21:10 drnic