Surus - PostgreSQL Acceleration for ActiveRecord

Posted on 02/16/2012

Surus accelerates ActiveRecord with PostgreSQL specific types and functionality. It enables indexed searching of serialized arrays and hashes. It also can control PostgreSQL synchronous commit behavior. By relaxing PostgreSQL’s durability guarantee, transaction commit rate can be increased by 50% or more.


gem install surus

Or add to your Gemfile.

gem 'surus'


Hashes can be serialized to an hstore column. hstore is a PostgreSQL key/value type that can be indexed for fast searching.

class User < ActiveRecord::Base
  serialize :properties,

User.create :properties => { :favorite_color => "green", :results_per_page => 20 }
User.create :properties => { :favorite_colors => ["green", "blue", "red"] }

Even though the underlying hstore can only use strings for keys and values (and NULL for values) Surus can successfully maintain type for integers, floats, bigdecimals, dates, and any value that YAML can serialize. It does this by storing an extra key value pair (or two) to maintain type information.

Because it falls back to YAML serialization for complex types, this means that nested data structures can be serialized to an hstore. In other words, any hash that can be serialized with the normal Rails YAML serialization can be serialized with Surus. But you can get the benefits of PostgreSQL indexing on the top level keys and values for free.

Hstores can be searched with helper scopes.

User.hstore_has_pairs(:properties, "favorite_color" => "green")
User.hstore_has_key(:properties, "favorite_color")
User.hstore_has_all_keys(:properties, "favorite_color", "gender")
User.hstore_has_any_keys(:properties, "favorite_color", "favorite_artist")

Read more in the PostgreSQL hstore documentation.


Ruby arrays can be serialized to PostgreSQL arrays. Surus includes support for text, integer, float, and decimal arrays.

class User < ActiveRecord::Base
  serialize :permissions,
  serialize :favorite_integers,
  serialize :favorite_floats,
  serialize :favorite_decimals,

User.create :permissions => %w{ read_notes write_notes, manage_topics },
  :favorite_integers => [1, 2, 3],
  :favorite_floats => [1.3, 2.2, 3.1],
  :favorite_decimals => [BigDecimal("3.14"), BigDecimal("4.23"]

Arrays can be searched with helper scopes.

User.array_has(:permissions, "admin")
User.array_has(:permissions, "manage_accounts", "manage_users")
User.array_has_any(:favorite_integers, 7, 11, 42)

Synchronous Commit

PostgreSQL can trade durability for speed. By disabling synchronous commit, transactions will return before the data is actually stored on the disk. This can be substantially faster, but it entails a short window where a crash could cause data loss (but not data corruption). This can be enabled for an entire session or per transaction.

User.synchronous_commit # -> true

User.transaction do
  User.synchronous_commit false
end # This transaction can return before the data is written to the drive

# synchronous_commit returns to its former value outside of the transaction
User.synchronous_commit # -> true

# synchronous_commit can be turned off permanently
User.synchronous_commit false

Read more in the PostgreSQL asynchronous commit documentation.

Head over to Github for more details.

blog comments powered by Disqus