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

Postgresql database as Moneta backend ? #92

Closed
solyarisoftware opened this issue Nov 4, 2015 · 8 comments
Closed

Postgresql database as Moneta backend ? #92

solyarisoftware opened this issue Nov 4, 2015 · 8 comments

Comments

@solyarisoftware
Copy link

Hi Daniel, all

Moneta seem a superb level of abstraction!
Here minor points, not issues! just feedbacks:

1. Postgresql as relational db supported backend

I read you mention Sqlite3 (with multiprocess limitations you point out in the comparison table)
I suppose I'd use Postgres instead, maybe through the beloved Sequel ORM adapter,

Probably a dedicated Postgres adapter could benefit by the HSTORE (http://www.postgresql.org/docs/9.4/static/hstore.html) or JSONB (http://www.postgresql.org/docs/9.4/static/datatype-json.html) in last release database features, just to store hashes/unstructured data.

I didn't yet read the Sequel code of Moneta Sequel backend adapter but I presume that's generic (not using any specific feature of Posgres); that's correct ?

2. Oj as JSON Moneta::Transformer

I see in the list mentioned Ox (I presume that's Peter Ohler optimizer for XMLs adata)
Do you also foresee superfast Oj (https://github.com/ohler55/oj) as JSON optimizer ?

3. Performances/Benchmarks

Do you pheraps did some performance benchmarks comparing different backends (with disk persistence) ?
BTW, I found interesting this: https://gist.github.com/stephan-nordnes-eriksen/6c9c56f63f36d5d100b2

Sorry for many topics and to not yet browsed the project code
respect
giorgio

@solyarisoftware solyarisoftware changed the title Moneta using Postgres ? Postgres database as Moneta backend ? Nov 4, 2015
@solyarisoftware solyarisoftware changed the title Postgres database as Moneta backend ? Postgresql database as Moneta backend ? Nov 4, 2015
@minad
Copy link
Member

minad commented Nov 4, 2015

@solyarisoftware
Copy link
Author

  • Thanks for benchmark link/script! :)
    LevelDB backend seems to me the fastest with disk persistence (If I well read your report)
  • I'll study more in deep your great project and possibly propose a postgres-hstore adapter.
  • Any occasion is good for me to say thank you also to Peter Ohler :)

@asppsa
Copy link
Collaborator

asppsa commented May 6, 2018

I've added some optimisations to the Sequel adapter in master to use the INSERT ... ON CONFLICT and RETURNING features of Postgres, and I've also added some travis testing using postgres. No hstore support as-yet though.

@asppsa
Copy link
Collaborator

asppsa commented May 7, 2018

I've now added an hstore implementation too. If you are interested in trying it out, you can do something like:

store = Moneta::Adapters::Sequel.new(
  db: 'posgres://...',
  table: :some_table,
  hstore: 'key value')

store['x'] = 'y' 
# etc

On store creation, a table called :some_table gets added to your database with a string column k and an hstore column v. On each write to the store, a row gets added with k = 'key value' if it doesn't already exist, and the pair gets merged into the v column.

It all seems to work fine, but its definitely slower in some cases than using a row for every pair. In particular, concurrent writes are extremely slow. Still might be useful in some cases ...

@asppsa asppsa closed this as completed May 7, 2018
@bjeanes
Copy link

bjeanes commented Sep 18, 2018

I wat thinking about adding a dedicated Postgres adapter which could be a bit tighter than the existing ActiveRecord one by leaning on facilities like ON CONFLICT.

However, since you've already special-cased Sequel for this, would you consider special-casing ActiveRecord too?

@bjeanes
Copy link

bjeanes commented Sep 18, 2018

That being said, I was thinking about this in the context of adding native expires support, and I think that would be problematic. I'm not sure if you can use ON CONFLICT to handle this case so cleanly. You can use ON CONFLICT with an index conditions, but indices can't be dependent on the current time, so that feels like a non-starter...

EDIT: Never-mind, this should be possible, by adding a WHERE condition to a DO UPDATE:

INSERT INTO moneta (k, v, expire_at)
  VALUES ('key', 'v', now() + interval '1 minute')
  ON CONFLICT (k)
  DO UPDATE 
    SET v = 'v'
      , expire_at = now() + interval '1 minute'
    WHERE moneta.expire_at < now();

@asppsa
Copy link
Collaborator

asppsa commented Sep 19, 2018

@bjeanes if you were to subclass the ActiveRecord adapter in a similar way to the Sequel adapter, that contribution would very gratefully accepted!

@bjeanes
Copy link

bjeanes commented Sep 19, 2018

If I end up doing this work, I will absolutely contribute it back. As it stands, I want to stress test the existing one to make sure it is as resilient as it claims. Performance isn't my immediate concern, but resilience is. That being said, native expires (esp in the context of the mutex API changes discussed in #156) would be excellent.

EDIT: But my intention wasn't to use ActiveRecord necessarily, as it's mostly an unnecessary dependency. If special-casing to a specific DB, might as well use the DB driver directly to cut down on dependencies.

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

No branches or pull requests

4 participants