pglite icon indicating copy to clipboard operation
pglite copied to clipboard

Documentation regarding Consistency

Open ackava opened this issue 1 year ago • 3 comments

Can you please add some documentation about how to maintain consistency in terms of running it from browser?

  1. Can multiple PGLite easily access single storage (IndexedDB)?
  2. Will it corrupt the data? Do we need any external locking mechanism?

ackava avatar Sep 14 '24 09:09 ackava

Good idea, we should document this better.

For quick context, PGlite is Postgres in "single user mode". There is no support for concurrent connections and you are like to corrupt the database if you open it multiple times at once.

The PGliteWorker helps you to open only a single instance and share it across browser tabs.

samwillis avatar Sep 14 '24 09:09 samwillis

Related question - is there any way to check if a target directory is currently in use by a PGlite instance?

It would be awesome if the PGlite constructor threw (or the waitReady promise rejected) with a descriptive error, e.g. "this dataDir is currently in use by another PGlite instance" rather than allow undefined behavior / data corruption. I'm using the Node.js filesystem, but I imagine this applies to the others as well.

I think this validation would be reasonable in either the init or initialSyncFs methods on the Filesystem interface.

https://github.com/electric-sql/pglite/blob/5e390365b6ed5b5c122047a248a4117c50390fa4/packages/pglite/src/pglite.ts#L345-L346

typedarray avatar Oct 14 '24 20:10 typedarray

Hey @0xOlias

Yep, we should absolutely do this.

In the browser I think the best way is to implement it with WebLocks, for Node we should open and lock a file in the FS.

samwillis avatar Oct 14 '24 21:10 samwillis