pglite
pglite copied to clipboard
Documentation regarding Consistency
Can you please add some documentation about how to maintain consistency in terms of running it from browser?
- Can multiple
PGLiteeasily access single storage (IndexedDB)? - Will it corrupt the data? Do we need any external locking mechanism?
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.
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
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.