Skip to content

Feature Request / Idea: PGlite integration? #1000

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

Open
cliffordfajardo opened this issue Dec 22, 2024 · 6 comments
Open

Feature Request / Idea: PGlite integration? #1000

cliffordfajardo opened this issue Dec 22, 2024 · 6 comments

Comments

@cliffordfajardo
Copy link

cliffordfajardo commented Dec 22, 2024

Description

Support PGLite (wasm postgres)

Motivation

Allows user to use this library (postgres) with local version of postgres without needing to install postgres directly on their computer or setup a container.

I love the API for this libray; its so flexible and well thought out 😭

@rmnoon
Copy link

rmnoon commented Feb 24, 2025

This would be so awesome since it'd make it trivial to write high performance unit tests in postgres.js apps!

@rmnoon
Copy link

rmnoon commented Feb 24, 2025

Unfortunately pglite is tightly coupled to a single-user mode for postgres to avoid having to fork processes per-connection (which postgres does and wasm doesn't support) and thus it doesn't actually spin up a server / doesn't speak the normal wire protocol. Integrating it with postgres.js thus seems pretty nontrivial I think.

@mike-marcacci
Copy link

I'm currently exploring a separate but related goal and came across pg-gateway, which implements the postgres wire protocol in TS.

It appears built to solve this problem, and they have an example of using it to "wrap" PGLite.

@rmnoon
Copy link

rmnoon commented Mar 23, 2025

Did you try it? Looks like a flurry of activity 8 mos ago. Great find!

@mike-marcacci
Copy link

mike-marcacci commented Mar 24, 2025

@rmnoon I just spent a bit of time today getting this working here. I want to note a few things:

Handling extra ReadyForQuery messages

There is an insidious bug between PGlite and postgres.js that was pretty hard to track down. Essentially, PGlite is sending extra ReadyForQuery at the end of each socket message. This appears to be a bug in PGlite itself, and while that scenario appears to be undefined behavior in this client, the effect I saw was indefinite hanging. It's not clear to me if this is a scenario is hypothetically possible for a "well behaving" postgres server, or if this is simply a violation of the postgres protocol that should never actually happen.

I currently use pg-gateway to filter out these extraneous messages.

Adding socket option in TS

Currently the socket option is not defined in the TS types for this library, even though it's documented, tested, and does work.

This should be an easy fix.

Consider accepting a WebSocketStream

While this is still not a finalized standard, a WebSocketStream is composed of modern web streams which have gained substantial traction and are by just about all accounts are successors to node's structures.

In my proof-of-concept I convert existing web streams into a node Socket for use by this library, potentially imperfectly.

@gersomvg
Copy link

gersomvg commented Apr 8, 2025

Related (I didn't want to flood the issues with feature requests at the time): #927

I feel like the easiest way to get this in the meantime is building a custom wrapper that wraps either postgres.js or pglite depending on whether you are running tests / dev environment. Drizzle could act as an intermediary as it supports both postgres.js / pglite and it has a sql template literal, but unfortunately its template literal isn't as powerful as postgres.js.

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