Skip to content

Commit

Permalink
Add a relaxed durability mode for use with the idb vfs (#79)
Browse files Browse the repository at this point in the history
* Add a relaxed durability for use with the idb vfs

* Update readme
  • Loading branch information
samwillis authored Apr 17, 2024
1 parent f09b269 commit 26bebe3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/pglite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Path to the directory to store the Postgres database. You can provide a url sche
##### `options`:

- `debug`: 1-5 - the Postgres debug level. Logs are sent to the console.
- `relaxedDurability`: boolean - under relaxed durability mode PGlite will not wait for flushes to storage to complete when using the indexedDB file system.

### Methods:

Expand Down
1 change: 1 addition & 0 deletions packages/pglite/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface ExecProtocolOptions {

export interface PGliteOptions {
debug?: DebugLevel;
relaxedDurability?: boolean;
}

export interface PGliteInterface {
Expand Down
27 changes: 24 additions & 3 deletions packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class PGlite implements PGliteInterface {
#closing = false;
#closed = false;
#inTransaction = false;
#relaxedDurability = false;

#resultAccumulator: Uint8Array[] = [];

Expand All @@ -47,6 +48,7 @@ export class PGlite implements PGliteInterface {
#queryMutex = new Mutex();
#transactionMutex = new Mutex();
#fsSyncMutex = new Mutex();
#fsSyncScheduled = false;

readonly debug: DebugLevel = 0;

Expand All @@ -69,6 +71,11 @@ export class PGlite implements PGliteInterface {
this.debug = options.debug;
}

// Enable relaxed durability if requested
if (options?.relaxedDurability !== undefined) {
this.#relaxedDurability = options.relaxedDurability;
}

// Create an event target to handle events from the emscripten module
this.#eventTarget = new EventTarget();

Expand Down Expand Up @@ -474,8 +481,22 @@ export class PGlite implements PGliteInterface {
* run after every query to ensure that the filesystem is synced.
*/
async #syncToFs() {
await this.#fsSyncMutex.runExclusive(async () => {
await this.fs!.syncToFs(this.emp.FS);
});
if (this.#fsSyncScheduled) {
return;
}
this.#fsSyncScheduled = true;

const doSync = async () => {
await this.#fsSyncMutex.runExclusive(async () => {
this.#fsSyncScheduled = false;
await this.fs!.syncToFs(this.emp.FS);
});
};

if (this.#relaxedDurability) {
doSync();
} else {
await doSync();
}
}
}
4 changes: 3 additions & 1 deletion packages/pglite/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ export class PGliteWorker implements PGliteInterface {
#closed = false;

#worker: WorkerInterface;
#options: PGliteOptions;

constructor(dataDir: string, options?: PGliteOptions) {
const { dataDir: dir, fsType } = parseDataDir(dataDir);
this.dataDir = dir;
this.fsType = fsType;
this.#options = options ?? {};
this.debug = options?.debug ?? 0;

this.#worker = Comlink.wrap(new Worker(WORKER_URL, { type: "module" }));
Expand All @@ -37,7 +39,7 @@ export class PGliteWorker implements PGliteInterface {
}

async #init(dataDir: string) {
await this.#worker.init(dataDir, { debug: this.debug });
await this.#worker.init(dataDir, this.#options);
this.#ready = true;
}

Expand Down

0 comments on commit 26bebe3

Please sign in to comment.