Repository navigation
Description
I'm trying to use pglite with a libpq-based application. I'm using pg-gateway as a proxy.
Following sample C code snippet (which calls PQprepare and PQexecPrepared) seems not work with pglite.
int main(int argc, char** argv) {
char conninfo[200];
const char *port = argv[1];
sprintf(conninfo, "host=localhost port=%s dbname=postgres user=psi password=abc", port);
PGconn *conn;
PGresult *res;
const char *paramValues[1];
paramValues[0] = argv[2];
conn = PQconnectdb(conninfo);
const char *stmtName = "my_statement";
const char *sql = "select $1";
res = PQprepare(conn, stmtName, sql, 1, NULL);
PQclear(res);
res = PQexecPrepared(conn, stmtName, 1, paramValues, NULL, NULL, 0);
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "Execution of statement failed: %s", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
exit(1);
}
printf("%s\n", PQgetvalue(res, 0, 0));
PQclear(res);
PQfinish(conn);
return 0;
}When I run this using a normal Postgres database I get the expected result:
willem@FH3T244:~/test$ ./pg 5432 hello
hello
willem@FH3T244:~/test$
When I use pglite, I get this:
willem@FH3T244:~/test$ ./pg 5433 hello
DEBUG: parse my_statement: select $1
DEBUG: bind <unnamed> to my_statement
Execution of statement failed:
Code snippet from the server, mostly borrowed from https://github.com/supabase-community/pg-gateway?tab=readme-ov-file#pglite:
async onMessage(data, { isAuthenticated }) {
if (!isAuthenticated) {
return false;
}
try {
const d = await db.execProtocolRaw(data);
socket.write(d);
} catch (err) {
console.log(err)
connection.sendError(err);
connection.sendReadyForQuery();
}
...Seems to happen on both Linux and Windows. When I don't use prepared statements (e.g. by using PQexec), it seems to work as expected.
Reacted by Nils and Jason Butler
Metadata
Metadata
Assignees
Labels
No labels
Activity
davebond commentedon Aug 28, 2024
I think we're having the same issue.
We're using
node-postgres[0] ->pg-gateway next branch[1] ->pgLiteWithout prepared statements this works fine, all prepared statements fail.
When using the native driver (libpq) queries fail and the logs are filled with these notices:
It also fails when using the javascript driver with the following:
[2]
According to [3]
activeQueryshould only benullonce areadyForQuerymessage has been received and this could be caused by the messages being received out of order. This is only an issue (so far) with prepared queries so I would assume there is a fault somewhere in handling the extended query protocol [4].I've not narrowed down whether this is caused by
pg-gatewayorpgLitebut hopefully this context is useful to someone.[0] https://node-postgres.com/
[1] https://github.com/supabase-community/pg-gateway/tree/next
[2] https://github.com/brianc/node-postgres/blob/54eb0fa216aaccd727765641e7d1cf5da2bc483d/packages/pg/lib/client.js#L359
[3] brianc/node-postgres#3174
[4] https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
gregnr commentedon Aug 29, 2024
I noticed that PGlite will return a
ReadyForQueryafter every message in the extended query series vs only at the end after aSync.Here is what I saw when connecting from
pg(JS) ->pg-gateway->PGlite:vs.
pg->pg-gateway->vanilla PG instance running in Docker:The extra
ReadyForQuerymessages from PGlite afterParseComplete,BindComplete, andRowDescriptionMessageseem to be what is tripping up clients. I tested manually removing these extra messages and it fixed the problem.The above were tested using this
pgJS snippet:joehan commentedon Sep 4, 2024
@gregnr you mentioned:
Any chance you could share the code you used to test this? I'd love to crib from it while this issue is being fixed.
should close #223
gregnr commentedon Sep 5, 2024
@joehan if you're willing to use an experimental version of pg-gateway, I've created a branch
feat/pglite-extended-query-patchwith the patch. You can see how it's used via this test:https://github.com/supabase-community/pg-gateway/blob/6834ab6db34b03fe51be6cb6296b587acea54ccd/packages/pg-gateway/test/node/tcp.test.ts#L28-L45
This is based off of the Web standard APIs PR which isn't merged yet, so unfortunately there's no release for this code yet (you would need to clone and build yourself).
I did try back porting the patch to work with the current v0.2.4 version of pg-gateway but kept hitting other upstream bugs that I'm guessing were inherently fixed in the Web standard APIs branch. Hoping to get that PR merged and released soon.
mattfysh commentedon Feb 6, 2025
Has this regressed?
When upgrading from v0.2.13 a ReadyForQuery response is added to the end of all
execProtocolRawreturn buffersAs mentioned by @gregnr #223 (comment)
I'm using psycopg which raises the error
psycopg.InternalError: got no result from the querywhen encountering this message unexpectedlypmp-p commentedon Feb 6, 2025
| Has this regressed?
@mattfysh : it would be really usefull to have a psycopg3-async test from yours, because indeed one change was made to accomodate psycopg pipelining mode when using pglite via a socket gateway.
mike-marcacci commentedon Mar 24, 2025
I spent some time today getting the (fantastic) postgres.js client to connect to PGlite via pg-gateway, and I ran into this issue.
It appears that the latest version continues to append
ReadyForQuerymessage to each response, causing undefined behavior and ultimately hanging in that client. @gregnr your filter really saved the day - thanks for that.I also want to confirm: is this something that clients should handle? I'm not an expert in the postgres wire protocol, but reading through the protocol flow this seems like clearly incorrect behavior on the part of PGlite:
My takeaway is that the client shouldn't need any special handling for this case?
Relevant links:
mattfysh commentedon Apr 10, 2025
Rather concerned this issue hasn't progressed. anything higher than v0.2.13 simply doesn't work with a range of clients due to the incompatibility with the official wire protocol.
samwillis commentedon Apr 10, 2025
Hey @mattfysh
This has reached the top of my list. I'm going to dig in over the next few days to understand the problem.
pmp-p commentedon May 13, 2025
please someone confirm latest fixes the problem for good.
joehan commentedon Jun 24, 2025
I think I am still running into this issue on 0.3.3 - when connecting with https://github.com/lib/pq, and running some migration statements generated by Atlas, it seems like extra Ready for Queries are still being sent. I added some logging to take a look at the full exchange:
fredzqm commentedon Dec 26, 2025
I can still reproduce this issue in the latest pglite version
0.3.14.Using it with golang's
*sql.DBdriver to callPreparea invalid SQL statement. We got this error from readParseResponseThe debug logs to (->) and from (<-) pglite:
It looks like the go driver expects an
[<- ReadyForQuery]after[<- ErrorMessage]but couldn't find it.