supabase-community / pg-gateway

Postgres wire protocol for the server-side
MIT License
117 stars 3 forks source link

pg-gateway, pglite, and Atlas #12

Open joehan opened 1 week ago

joehan commented 1 week ago

Bug report

Describe the bug

I have a basic server set up (similar to https://github.com/supabase-community/pg-gateway?tab=readme-ov-file#pglite), and I'm trying to use Atlas to migrate the schema of my database. However, when I do run:

atlas schema inspect   --url "postgres://:5432/postgres?search_path=public&sslmode=disable"

I get

Error: postgres: querying schemas: pq: unexpected Describe statement response 'Z'

I spent some time under the hood on this, and it seems like pglite/pg-gateway is sending back an extra, unexpected Ready for Query message when replying to Parse or Bind messages. After adding a bunch of logging, this was the problematic message exchange:


Atlas:

Send Parse message

Send Describe message

Send Sync message
______________________________
Backend:

Send ParseComplete message:
1\u0000\u0000\u0000\u0004

Send Ready for Query message: // This one seems unexpected
Z\u0000\u0000\u0000\u0005I"

Send Parameter description message:
"t\u0000\u0000\u0000\n\u0000\u0001\u0000\u0000\u0000\u0013

Send Row description message:
T\u0000\u0000\u0000>O <omitted for length>

Send Ready for query message
Z\u0000\u0000\u0000\u0005I"
_____________________________

Atlas consume consumes ParseComplete.

Atlas tries to consume Parameter Description message, and errors when it instead sees a Ready for Query instead

I'm pretty stumped at this point, and was hoping someone with a better grasp of the wire protocol can explain why this isn't working as expected.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. From the pglite example, start up the server with npm run dev
  2. Download Atlas: curl -sSf https://atlasgo.sh | sh
  3. Run any Atlas command against the server: atlas schema inspect --url "postgres://:5432/postgres?search_path=public&sslmode=disable"

Expected behavior

Atlas successfully queries pglite and plans a migration.

System information

gregnr commented 1 week ago

Thanks for reporting @joehan! This sounds like the exact same issue as https://github.com/supabase-community/pg-gateway/issues/4. We identified this as a problem with PGlite during the extended query protocol (tracked here).

The ElectricSQL team is aware of the problem and currently working on a fix. In the meantime the only other solution could be to add custom logic that omits the incorrect ReadyForQuery messages, but only during an extended query flow.

joehan commented 1 week ago

Thanks so much for the quick reply, glad to see I'm not going crazy 😅 I'll keep an eye on https://github.com/electric-sql/pglite/issues/223 for the fix.